我正在使用MySQL(Percona 5.5.34-32.0
),我有这个表格;
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
), I可以运行完全相同的查询,没有任何错误或警告:
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_*
模式不是我正在寻找的解决方案:它不干净,而且我可以在同一会话中的另一个相同的表上运行完全相同的查询建议(?)问题可能不存在。
答案 0 :(得分:0)
好的,这是事实:
STRICT_ALL_TABLES
和STRICT_TRANS_TABLES
模式,并且已经这样做了很长时间。SHOW CREATE TABLE privmsgs
输出的表时,新表未显示此行为。使用CREATE TABLE privmsgs2 FROM privmsgs
时,确实。我尝试使用ALTER TABLE privmsgs ENGINE=InnoDB
,according official documentation重建表格,但这没有帮助。
所以我使用mysqldump databasename privmsgs > table.sql
转储了表格,将其名称更改为privmsgs2
文件中的sed
,然后用mysql databasename < table.sql
重新导入。然后我删除了privmsgs
并将privmsgs2
重命名为privmsgs
。
然后它再次开始完美运作。我不明白为什么,也找不到任何其他对此行为的引用。如果有人能够以比我更精确的答案解释它,请这样做,我会接受你的回答。