如何强制MySQL将0作为有效的自增值

发布于 2024-07-27 04:45:54 字数 604 浏览 4 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

べ繥欢鉨o。 2024-08-03 04:45:54

从我得到的答案此处

您可以使用:

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO'

此处,将阻止 MySQL 将 INSERT/UPDATE ID 0 解释为下一个序列 ID。 此类行为将被限制为 NULL。

不过,我认为应用程序的行为非常糟糕。 您必须非常小心,确保它的使用一致,特别是如果您选择稍后实施复制。

From the answer I got here:

You can use:

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO'

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.

尘曦 2024-08-03 04:45:54

检查您的 sql DB 模式:

SELECT @@[GLOBAL|SESSION].sql_mode;

如果它为空或未设置,请使用:

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO'

小心! 如果您使用GLOBAL,则不会立即更改,您需要重新启动连接才能应用设置。

因此,例如,如果您要将数据从一个数据库恢复到另一个数据库,并且您不确定是否应用此设置,请使用 SESSION 立即进行更改(关闭连接时它会重置)。 完成后,插入0值,即使改变sql_mode,它也不会改变。

要重置此模式(和其他模式),

SET [GLOBAL|SESSION] sql_mode=''

请不要使用零自动增量值,因为它们未在 MySQL 数据库中设置为默认值。

有关详细信息,请查看 mysql 开发页面主题

更新

对于 MariaDB,请使用 此评论

SET sql_mode='NO_AUTO_VALUE_ON_ZERO'

Check your sql DB mode with:

SELECT @@[GLOBAL|SESSION].sql_mode;

If it's empty or not set, use:

SET [GLOBAL|SESSION] sql_mode='NO_AUTO_VALUE_ON_ZERO'

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 the sql_mode is changed.

To reset this mode (and others) use

SET [GLOBAL|SESSION] sql_mode=''

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

SET sql_mode='NO_AUTO_VALUE_ON_ZERO'
最近可好 2024-08-03 04:45:54

如果您不想处理 mysql 变量,这里有一个有用的 hack:

INSERT INTO `{{TABLE_PREFIX}}users` VALUES (-1, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL);

显然

UPDATE `{{TABLE_PREFIX}}users` SET id = 0 where id = -1;

,这假设您使用的是有符号整数,并且表 id 不使用负值。

If you do not want to deal with mysql variables, here's a useful hack:

INSERT INTO `{{TABLE_PREFIX}}users` VALUES (-1, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL);

And then

UPDATE `{{TABLE_PREFIX}}users` SET id = 0 where id = -1;

Obviously, this assumes that you're using a signed integer and not using negative values for your table ids.

來不及說愛妳 2024-08-03 04:45:54

故障安全方法:

SET @@session.sql_mode = 
    CASE WHEN @@session.sql_mode NOT LIKE '%NO_AUTO_VALUE_ON_ZERO%' 
        THEN CASE WHEN LENGTH(@@session.sql_mode)>0
            THEN CONCAT_WS(',',@@session.sql_mode,'NO_AUTO_VALUE_ON_ZERO')  -- added, wasn't empty
            ELSE 'NO_AUTO_VALUE_ON_ZERO'                                    -- replaced, was empty
        END
        ELSE @@session.sql_mode                                             -- unchanged, already had NO_AUTO_VALUE_ON_ZERO set
    END
  • 检查 sql_mode 中是否已设置 NO_AUTO_VALUE_ON_ZERO
  • 如果不为空,则添加到 sql_mode
  • 仅设置会话,我建议仅在需要插入 0 的会话上使用它

Fail safe way:

SET @@session.sql_mode = 
    CASE WHEN @@session.sql_mode NOT LIKE '%NO_AUTO_VALUE_ON_ZERO%' 
        THEN CASE WHEN LENGTH(@@session.sql_mode)>0
            THEN CONCAT_WS(',',@@session.sql_mode,'NO_AUTO_VALUE_ON_ZERO')  -- added, wasn't empty
            ELSE 'NO_AUTO_VALUE_ON_ZERO'                                    -- replaced, was empty
        END
        ELSE @@session.sql_mode                                             -- unchanged, already had NO_AUTO_VALUE_ON_ZERO set
    END
  • Checks if NO_AUTO_VALUE_ON_ZERO already set in sql_mode
  • Adds to sql_mode if not empty
  • Sets only the session, I recommend using it only on the sessions where you need to insert a 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文