Mysql 4 vs Mysql 5 插入时自动递增字段

发布于 2024-10-09 14:34:18 字数 467 浏览 1 评论 0原文

我一路上学到了这一点,但不知道在哪里读到或听到它,因为我在网上找不到任何支持它的东西,但我记得当从 mysql4.x 升级到 mysql5.x 时,其中之一所需的更改是插入的自动增量字段必须从 '' 更改为 NULL(如果包含)。

我知道无论如何都不需要在插入中包含它,但只是为了兴趣点......

Mysql 4.x 将允许: INSERT INTO TABLE (table_id, name, location) VALUES ('', 'john', 'NY');

但 mysql 5.x 必须有: INSERT INTO TABLE (table_id, name, location) VALUES (NULL, 'john', 'NY');

我在 mysql 的网站上找不到任何信息来支持这一点,但我知道它在 mysql 5.x 中抛出一个错误,并且知道它在 4.x 中与 '' 一起工作,但这在哪里记录?

I've learned this along the way but can't figure out where I read it or heard it, as there is nothing I have found online supporting it, but I remember that when upgrading from mysql4.x to mysql5.x, one of the required changes was that the auto-increment field for inserts had to change from '' to NULL if it was included.

I know its not required to have in the insert anyway, but just for point of interest...

Mysql 4.x would allow:
INSERT INTO TABLE (table_id, name, location) VALUES ('', 'john', 'NY');

But mysql 5.x had to have:
INSERT INTO TABLE (table_id, name, location) VALUES (NULL, 'john', 'NY');

I can't find any information on mysql's site to support this, but I know for a fact it throws an error in mysql 5.x and know it worked with '' in 4.x, but where is this documented?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

青芜 2024-10-16 14:34:18

4.15.0 文档指出需要 0 或 NULL:

未指定任何值
AUTO_INCRMENT列,所以MySQL
分配的序列号
自动地。您还可以明确地
将 NULL 或 0 分配给该列
生成序列号。

Both the 4.1 and 5.0 docs state that 0 or NULL is required:

No value was specified for the
AUTO_INCREMENT column, so MySQL
assigned sequence numbers
automatically. You can also explicitly
assign NULL or 0 to the column to
generate sequence numbers.

孤单情人 2024-10-16 14:34:18

没关系,mysql内部仍然转换为整数

mysql> CREATE TABLE some_test ( id int(10) unsigned NOT NULL auto_increment, primary key(id));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into some_test values ('');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------------------+
| Level   | Code | Message                                              |
+---------+------+------------------------------------------------------+
| Warning | 1264 | Out of range value adjusted for column 'id' at row 1 |
+---------+------+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from some_test;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)

但是,我建议使用0来避免这个警告

It does not matter, mysql internally still convert to integer

mysql> CREATE TABLE some_test ( id int(10) unsigned NOT NULL auto_increment, primary key(id));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into some_test values ('');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+------------------------------------------------------+
| Level   | Code | Message                                              |
+---------+------+------------------------------------------------------+
| Warning | 1264 | Out of range value adjusted for column 'id' at row 1 |
+---------+------+------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from some_test;
+----+
| id |
+----+
|  1 |
+----+
1 row in set (0.00 sec)

However, I will suggest use 0 to avoid this warning

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文