时间:2021-07-01 10:21:17 帮助过:37人阅读
主要有以下几种方式:
1、自增字段作为主键。【推荐方案】
MySql虽然比Oracle少了Sequence,但是多了字段的自增长特性。
插入完了以后可以通过执行【SELECT @@IDENTITY】获取上一条插入语句中生成的自增长字段的值。
这个语句很特别,没有关联到特定的SQL语句,会 让人感觉迷糊,他到底是怎么获取值的。在并发情况下会不会获取其他线程执行后的值。
答案是有可能的,但是不用怕、是可控的。只有不当的编码才会导致取到其他线程的值。先来说一下原理:
SUMMARY The Jet OLE DB version 4.0 provider supports the SELECT @@Identity query that allows you to retrieve the value of the auto-increment field generated on your connection. Auto-increment values used on other connections to your database do not affect the results of this specialized query. This feature works with Jet 4.0 databases but not with older formats.
2、在MySql中模拟Sequence
第一步:创建--Sequence 管理表
DROP TABLE IF EXISTS sequence; CREATE TABLE WFO_SEQ( name VARCHAR(50) NOT NULL, current_value INT NOT NULL, increment INT NOT NULL DEFAULT 1, PRIMARY KEY (name) ) ENGINE=InnoDB;第二步:创建--取当前值的函数
DROP FUNCTION IF EXISTS currval; DELIMITER $ CREATE FUNCTION currval (seq_name VARCHAR(50)) RETURNS INTEGER LANGUAGE SQL DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN DECLARE value INTEGER; SET value = 0; SELECT current_value INTO value FROM WFO_SEQ WHERE name = seq_name; RETURN value; END $ DELIMITER ;第三步:创建--取下一个值的函数
DROP FUNCTION IF EXISTS nextval; DELIMITER $ CREATE FUNCTION nextval (seq_name VARCHAR(50)) RETURNS INTEGER LANGUAGE SQL DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN DECLARE C_V INTEGER; UPDATE WFO_SEQ SET CURRENT_VALUE = CURRENT_VALUE + INCREMENT WHERE NAME = SEQ_NAME; SET C_V = CURRVAL(SEQ_NAME); IF C_V = -1 THEN INSERT INTO WFO_SEQ(NAME, CURRENT_VALUE, INCREMENT) VALUES(SEQ_NAME, 1, 1); RETURN 1; END IF; RETURN C_V; END $ DELIMITER ;
DROP FUNCTION IF EXISTS setval; DELIMITER $ CREATE FUNCTION setval (seq_name VARCHAR(50), value INTEGER) RETURNS INTEGER LANGUAGE SQL DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER COMMENT '' BEGIN UPDATE WFO_SEQ SET current_value = value WHERE name = seq_name; RETURN currval(seq_name); END $ DELIMITER ;
SELECT SETVAL('TestSeq',
10);---设置指定sequence的初始值
SELECT CURRVAL('TestSeq');--查询指定sequence的当前值
SELECT NEXTVAL('TestSeq');--查询指定sequence的下一个值