在 mysql 数据库中,我有这个表:
CREATE TABLE `actualities` (
`id` int(11) NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`body` text COLLATE utf8_unicode_ci,
`project_id` int(11) DEFAULT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `project_id` (`project_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
当我尝试在此表中创建一个新条目时,ID 设置为 0,如下所示:
mysql> insert into actualities (title) VALUES ('title');
Query OK, 1 row affected, 1 warning (0.13 sec)
mysql> select * from actualities order by created_at desc limit 1;
+----+-------+------+------------+---------------------+---------------------+
| id | title | body | project_id | created_at | updated_at |
+----+-------+------+------------+---------------------+---------------------+
| 0 | title | NULL | NULL | 2012-12-15 20:14:20 | 0000-00-00 00:00:00 |
+----+-------+------+------------+---------------------+---------------------+
我不明白为什么。你有解决方案吗?
解决方案
ALTER TABLE actualities MODIFY id INT AUTO_INCREMENT;