Questo è equivalente a Postgres
Specificare TABLE_SCHEMA
e MASKTABLE
sotto
Sto creando una MASKTABLE
con una prima voce fittizia
DELIMITER $$
CREATE OR REPLACE FUNCTION generate_next_id() RETURNS bigint NOT DETERMINISTIC
MAIN: BEGIN
DECLARE our_epoch bigint;
DECLARE seq_id bigint;
DECLARE now_millis bigint;
DECLARE shard_id int;
DECLARE param bigint ;
SET @our_epoch = 1568873367231;
SET @shard_id = 1;
SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = "SCHEMANAME" AND TABLE_NAME = "MASKTABLE" into @seq_id;
SELECT FLOOR(UNIX_TIMESTAMP()) * 1000 into @now_millis;
SELECT (@now_millis - @our_epoch) << 23 into @param;
SELECT @param | (@shard_id <<10) into @param;
select @param | (@seq_id) into @param;
RETURN @param;
END MAIN;$$
DELIMITER ;
Utilizzo
select generate_next_id()
Puoi usarlo in trigger come
CREATE TRIGGER trigger_name
BEFORE INSERT ON TableName
FOR EACH ROW
SET new.id = generate_next_id();