go-mysql-server icon indicating copy to clipboard operation
go-mysql-server copied to clipboard

Feature: `last_insert_uuid()` function

Open fulghum opened this issue 1 year ago • 0 comments

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) or CHAR(36) with a default value expression of UUID()
  • VARBINARY(16) or BINARY(16) with a default value expression of UUID_to_bin(UUID()) (optionally, the swap_flag for UUID_to_bin may 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

fulghum avatar Mar 01 '24 23:03 fulghum