我正在使用MySQL(Percona 5.5.34-32.0
),我有這張表;AUTO_INCREMENT在插入時需要一個值,但有時只有
mysql> SHOW CREATE TABLE privmsgs\G
*************************** 1. row ***************************
Table: privmsgs
Create Table: CREATE TABLE `privmsgs` (
`msg_id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`root_level` mediumint(8) unsigned NOT NULL DEFAULT '0',
`author_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`icon_id` mediumint(8) unsigned NOT NULL DEFAULT '0',
`author_ip` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '',
`message_time` int(11) unsigned NOT NULL DEFAULT '0',
`enable_bbcode` tinyint(1) unsigned NOT NULL DEFAULT '1',
`enable_smilies` tinyint(1) unsigned NOT NULL DEFAULT '1',
`enable_magic_url` tinyint(1) unsigned NOT NULL DEFAULT '1',
`enable_sig` tinyint(1) unsigned NOT NULL DEFAULT '1',
`message_subject` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`message_text` mediumtext COLLATE utf8_bin NOT NULL,
`message_edit_reason` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`message_edit_user` mediumint(8) unsigned NOT NULL DEFAULT '0',
`message_attachment` tinyint(1) unsigned NOT NULL DEFAULT '0',
`bbcode_bitfield` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
`bbcode_uid` varchar(8) COLLATE utf8_bin NOT NULL DEFAULT '',
`message_edit_time` int(11) unsigned NOT NULL DEFAULT '0',
`message_edit_count` smallint(4) unsigned NOT NULL DEFAULT '0',
`to_address` text COLLATE utf8_bin NOT NULL,
`bcc_address` text COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`msg_id`),
KEY `author_ip` (`author_ip`),
KEY `message_time` (`message_time`),
KEY `author_id` (`author_id`),
KEY `root_level` (`root_level`)
) ENGINE=InnoDB AUTO_INCREMENT=5238652 DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=COMPACT
我有這樣的模式對整個會話啓用:
mysql> SELECT @@session.sql_mode\G
*************************** 1. row ***************************
@@session.sql_mode: ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,
ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
當我嘗試插入一些東西,出現這種情況:
mysql> INSERT INTO privmsgs
-> (root_level, author_id, icon_id, author_ip, message_time,
-> enable_bbcode, enable_smilies, enable_magic_url, enable_sig,
-> message_subject, message_text, message_attachment, bbcode_bitfield,
-> bbcode_uid, to_address, bcc_address)
-> VALUES
-> (0, 12345, 0, '127.0.0.0', 123456789, 1, 1, 0, 0, 'test',
-> 'testing message', 0, '', 'xxx', 'u_12345', '');
ERROR 1364 (HY000): Field 'msg_id' doesn't have a default value
最奇怪的是,如果我創建另一個運行與我之前粘貼的SHOW CREATE TABLE privmsgs
完全相同的輸出(但顯然將其名稱更改爲其他內容,例如privmsgs2
),我可以運行沒有任何錯誤或警告完全相同的查詢:
mysql> INSERT INTO privmsgs2
-> (root_level, author_id, icon_id, author_ip, message_time,
-> enable_bbcode, enable_smilies, enable_magic_url, enable_sig,
-> message_subject, message_text, message_attachment, bbcode_bitfield,
-> bbcode_uid, to_address, bcc_address)
-> VALUES
-> (0, 12345, 0, '127.0.0.0', 123456789, 1, 1, 0, 0, 'test',
-> 'testing message', 0, '', 'xxx', 'u_12345', '');
Query OK, 1 row affected (0.00 sec)
這是怎麼回事?
請注意,禁用STRICT_*
模式是不是我要找的解決方案:它不乾淨,而且我可以在同一會話表明運行在另一個相同表完全相同的查詢的事實(?)問題可能不在那裏。
這是因爲你處於STRICT模式。只需在您的INSERT中添加msg_id和NULL即可。因此,這是一個...[SQL語句中使用INSERT和AUTO-INCREMENT列的錯誤]的可能重複(http://stackoverflow.com/questions/7899676/error-in-sql-statement-using-insert-and-auto-increment-column) – Strawberry
請閱讀完整的問題:我知道禁用STRICT模式可以解決這個問題。但爲什麼它在桌子上而不是在其他桌子上? –
我認爲@strawberry給了你答案,INSERT不能猜測你的msg_id是未定義的,你需要直到它。這是AUTO_INCREMENT的工作原理,您提供NULL作爲自動增量字段的值。在某些情況下,MySQL足夠聰明地猜測它。 – nrathaus