go-mysql-server
go-mysql-server copied to clipboard
Feature: `last_insert_uuid()` function
UUIDs are often used in place of auto_increment IDs, but MySQL doesn't provide an easy way to get the last generated UUID that was used in an insert. This change introduces a new function, last_insert_uuid() that operates similarly to last_insert_id(). For a column identified as a UUID column, callers can use last_insert_uuid() to retrieve the last generated UUID value that was inserted into that column. In order to be considered a UUID column, a column must be part of the primary key and it must meet one of the following type signatures:
VARCHAR(36)orCHAR(36)with a default value expression ofUUID()VARBINARY(16)orBINARY(16)with a default value expression ofUUID_to_bin(UUID())(optionally, theswap_flagforUUID_to_binmay also be specified)
Example usage:
create table t (pk binary(16) primary key default (UUID_to_bin(UUID())), c1 varchar(100));
insert into t (c1) values ("one"), ("two");
select last_insert_uuid();
select c1 from t where pk = uuid_to_bin(last_insert_id());
Related to https://github.com/dolthub/dolt/issues/7547