在sql server中有类似
的东西IDENTITY_INSERT设置为OFF,我们可以在
之后设置为ON我想知道的是mysql中的类似内容,我需要临时启动并设置为ON,即
create procedure ()
begin
IDENTITY_INSERT is set to OFF
Some insert statement
Some insert statement
IDENTITY_INSERT is set to ON
end
答案 0 :(得分:5)
我无法想象为什么你需要关闭它。
如果您需要使用自定义id
插入任何记录,AUTO_INCREMENT
不是障碍。
http://sqlfiddle.com/#!9/4d413/1
CREATE TABLE t1 (id int NOT NULL AUTO_INCREMENT, PRIMARY KEY (id));
INSERT INTO t1 VALUES (23);
INSERT INTO t1 VALUES (null);
那你为什么要把它关掉呢?
答案 1 :(得分:1)
答案是您无法在某些暂停模式下将其关闭。你能做的最好的事情是将数据楔入空白。这样做是为了你自己的危险。
设置auto_increment可能有效,也可能无效。请参阅下文,其中部分内容已在评论中的图片中显示给另一位回答者。有时它只是忽略重置auto_increment数字的命令(在他的回答中)。特别是,它可以保护您免受错误的呼叫。
create table table1
( id int auto_increment primary key,
type int not null,
salary int not null
)engine=innodb;
insert table1(type,salary) values (1,99000),(1,40000),(2,49000);
select * from table1;
-- we now have 3 rows, id's 1 to 3
show create table table1; -- shows AI=4 for next one
ALTER TABLE `table1` AUTO_INCREMENT = 2;
show create table table1; -- above does nothing, AI still equal to 4
insert table1(type,salary) values (1,50000); -- inserts it at id 4
show create table table1; -- shows AI=5 for next one
-- ok move it forward (create a gap) ... note, we currently have 4 rows
ALTER TABLE `table1` AUTO_INCREMENT = 8;
show create table table1; -- confirm it. Yes, it accepted your change
insert table1(type,salary) values (1,33000); -- inserts it at id 8
select * from table1; -- we now have 1 2 3 4 8
ALTER TABLE `table1` AUTO_INCREMENT = 5; -- move it back as you say
show create table table1; -- confirm it (it fails to move it back) AI=9
-- it fails to let you move it back as you say (AI=9)
-- why you would want to move it back, who knows.
insert table1(id,type,salary) values (5,1,31123);
-- inserts it at id 5 (sure, no problem, in the gap)
select * from table1;
show create table table1; -- AI for next one is still 9
insert table1(id,type,salary) values (5,1,39999); -- fails of course
简而言之,不要使用明智的代码搞乱auto_increments。通常它会导致错误,浪费时间,并且是OCD思维模式的标志。
答案 2 :(得分:1)
谢谢!。来自你所有人的赞赏:)
所以根据对话,我认为插入id(max + 1)会更好地摆脱我的问题。