假设我们的表A只有一列id(这是主键)
如何在不指定id的情况下向表中插入新行?
我试过这个
INSERT INTO A (`id`) VALUES (NULL)它不起作用
编辑:--我忘记提到id了,主键有auto_increment和NULL属性。
编辑2:运行上述查询时的确切错误是
Column 'id' cannot be null发布于 2011-05-11 09:39:37
只要' ID‘作为自动增量启用(假设ID是一个整数),您就可以这样做:
INSERT INTO A (id) values (null)“id”每次都会不断增加。
发布于 2011-05-11 09:34:18
只有在使用auto_increment主键( PK )时才有效,因为每个PK必须有一个唯一的非空值。
drop table if exists A;
create table A
(
id int unsigned not null auto_increment primary key
)
engine=innodb;
insert into A (id) values (null),(null);
mysql> select * from A order by id;
+----+
| id |
+----+
| 1 |
| 2 |
+----+
2 rows in set (0.00 sec)发布于 2012-07-20 04:46:18
我有一个类似的问题,然后我注意到,当我将id更改为主键+ not null +自动增量时,我没有应用这些更改。
https://stackoverflow.com/questions/5962026
复制相似问题