如何强制MySQL将0作为有效的自增值
长话短说,我有一个 SQL 文件,我想将其作为 skel
样式文件导入,因此这将以编程方式重复完成。 我可以根据需要编辑 SQL 文件,但我不想碰应用程序本身。
此应用程序使用 userid = 0
来表示匿名用户。 它在数据库中还有一个相关(空白)条目来表示该“用户”。 因此,我的 skel.sql 中的行看起来像这样:
INSERT INTO `{{TABLE_PREFIX}}users` VALUES (0, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL);
问题是 uid 是一个 auto_increment 字段,对于该字段,从技术上讲,0
是无效值。 或者至少,如果你将其设置为 0,你基本上是在告诉 MySQL,“请将下一个 id 插入到该字段中。”
现在,我想我可以将一个INSERT
然后一个UPDATE
查询放入我的SQL文件中,但是有没有办法告诉MySQL一般来说,是的,我实际上想插入0
进入此字段?
Long story short, I have a SQL file that I want to import as a skel
style file, so this will be done repeatedly, programmatically. I can edit the SQL file however I want, but I'd rather not touch the application itself.
This application uses userid = 0
to represent the anonymous user. It also has a relevant (blank) entry in the database to represent this 'user'. Hence, the line in my skel.sql
looks something like this:
INSERT INTO `{{TABLE_PREFIX}}users` VALUES (0, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL);
The problem with this is that uid
is a auto_increment
field, for which, technically, 0
is an invalid value. Or atleast, if you set it to 0, you're basically telling MySQL, "Please insert the next id into this field."
Now, I suppose I could put an INSERT
then an UPDATE
query into my SQL file, but is there a way of telling MySQL in general that yes, I actually want to insert 0
into this field?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从我得到的答案此处:
您可以使用:
如此处,将阻止 MySQL 将 INSERT/UPDATE ID 0 解释为下一个序列 ID。 此类行为将被限制为 NULL。
不过,我认为应用程序的行为非常糟糕。 您必须非常小心,确保它的使用一致,特别是如果您选择稍后实施复制。
From the answer I got here:
You can use:
Which as described here, will prevent MySQL from interpreting an INSERT/UPDATE ID of 0 as being the next sequence ID. Such behaviour will be limited to NULL.
It is what I'd consider pretty bad behaviour from the application though. You'll have to be real careful that it's used consistently, especially if you choose to implement replication at a later date.
检查您的 sql DB 模式:
如果它为空或未设置,请使用:
小心! 如果您使用
GLOBAL
,则不会立即更改,您需要重新启动连接才能应用设置。因此,例如,如果您要将数据从一个数据库恢复到另一个数据库,并且您不确定是否应用此设置,请使用
SESSION
立即进行更改(关闭连接时它会重置)。 完成后,插入0值,即使改变sql_mode
,它也不会改变。要重置此模式(和其他模式),
请不要使用零自动增量值,因为它们未在 MySQL 数据库中设置为默认值。
有关详细信息,请查看 mysql 开发页面主题此
更新
对于 MariaDB,请使用 此评论
Check your sql DB mode with:
If it's empty or not set, use:
BE CAREFUL! If you use
GLOBAL
, it's not an immediate change, you need to restart your connection to apply the setting.So, if you're restoring data from one DB to another, for example, and you're not sure if this setting is applied, use
SESSION
for an immediate change (it resets when closing the connection). When done, insert 0 value and it won't change even if thesql_mode
is changed.To reset this mode (and others) use
Zero auto-increment values are not recommended because they're not set as default in MySQL databases.
For more info check mysql dev page topic on this
Update
For MariaDB use the command pointed out in this comment
如果您不想处理 mysql 变量,这里有一个有用的 hack:
显然
,这假设您使用的是有符号整数,并且表 id 不使用负值。
If you do not want to deal with mysql variables, here's a useful hack:
And then
Obviously, this assumes that you're using a signed integer and not using negative values for your table ids.
故障安全方法:
Fail safe way: