MySQL 如何插入空行,但正确更新自动编号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6156942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I insert an empty row, but have the autonumber update correctly?
提问by Shane
I was hoping to be able to do something like
我希望能够做类似的事情
INSERT INTO `table`;
or maybe even a SET clause like
或者甚至是一个 SET 子句,比如
SET `primary_key` = null
where all the columns of the table aren't set (and are left to their default), with the exception of the autonumbered primary key which should be set.
其中表的所有列都没有设置(并保留为默认值),除了应该设置的自动编号主键。
Any suggestions?
有什么建议?
回答by Dan Simon
INSERT INTO `table` (`primary_key`) VALUES (NULL);
回答by Joe Stefanelli
INSERT INTO `table` () VALUES();
回答by Linus Unneb?ck
I believe that the more correct answer would be this:
我相信更正确的答案是这样的:
INSERT INTO my_table DEFAULT VALUES
As suggested here: https://stackoverflow.com/a/13605273/148072
正如这里所建议的:https: //stackoverflow.com/a/13605273/148072
回答by Alex
A little late.. But maybe handy for anyone looking for this answer.
有点晚了......但对于寻找这个答案的人来说可能很方便。
INSERT INTO `table` () VALUES ()
You do not need any specification of the primary key.
您不需要任何主键规范。
回答by stefgosselin
Depends on your table. If it allows null values on every field, just do an insert with all values as null. If it does not allow null values for every field, you will need to insert a row with those fields having some value.
取决于你的桌子。如果它允许每个字段上的空值,只需将所有值都作为空值进行插入。如果它不允许每个字段的空值,您将需要插入一行,这些字段具有某些值。