用mysql生成唯一主键

CREATE PROCEDURE pkg_get_task_id_seq(IN seq_name VARCHAR(30), IN num INT(5), INOUT task_id VARCHAR(24))
BEGIN
DECLARE v_seq_day VARCHAR (24);
SET v_seq_day =DATE_FORMAT(NOW(), '%Y%m%d');

UPDATE ts_air_sequence
SET
VALUE = last_insert_id(IF(seq_day=v_seq_day,VALUE,0) + next + num),seq_day=v_seq_day
WHERE NAME = seq_name;

SET task_id =
(
SELECT
CONCAT(
v_seq_day,
LPAD(last_insert_id(), 5, '0')
)
);
COMMIT;
END;
-- auto-generated definition
CREATE TABLE ts_air_sequence
(
name VARCHAR(255) NOT NULL
COMMENT '自增序号获取表'
PRIMARY KEY,
value BIGINT(10) NULL,
next INT(3) NULL,
remark VARCHAR(255) NULL
COMMENT '备注',
seq_day VARCHAR(20) NULL
COMMENT '序号年月'
)
COMMENT '航空序列号表';

推荐阅读更多精彩内容