时间:2021-07-01 10:21:17 帮助过:22人阅读
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.
当多线程编程时、强制把数据库连接传给各个线程同一时候运行时才会取到其它线程的【SELECT @@IDENTITY】。
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的下一个值
[DB][MySql]关于取得自增字段的值、及@@IDENTITY 与并发性问题
标签:return from height ted primary currval fine font provider