【MySQL】AUTO_INCREMENT后INSERT的问题
本帖最后由 thepoy 于 2019-7-11 16:07 编辑有一个demo表,表里的id列是自增:
SELECT * FROM demo;
+----+------+---------+
| id | name | balance |
+----+------+---------+
|1 | Mike | 500 |
+----+------+---------+
1 row in set (0.001 sec)
DESC demo;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | NO | | NULL | |
| balance | int(11) | NO | | NULL | |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.030 sec)
现想要再插入一行数据,因id为自增主键,于是id的值设为NULL:
INSERT INTO demo VALUES(NULL,'Jone',1500);
报错:
ERROR 1048 (23000): Column 'id' cannot be null
请问是哪里出了问题呢?
打扰了,我没有把自增长设置成功。。。。。
怎么删贴 把null删掉。
INSERT INTO demo VALUES('Jone',1500); 不用管id insert into demo (name,balance) values('张三','1000');自增的ID不用值 设置自增,id的字段与值都不用写 declare @Num int
select @num=max(id) from demo
INSERT INTO demo VALUES(@num+1,'Jone',1500);
这样就保证插入最大ID按顺序插入了。亦或者INSERT INTO demo VALUES('Jone',1500);直接让系统安排ID wuxiaolei1 发表于 2019-7-11 16:18
把null删掉。
INSERT INTO demo VALUES('Jone',1500);
如果demo后不加列的话,也会报错
CREATE TABLE `admin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
INSERT INTO admin VALUES('aaa','0000');
ERROR 1136 (21S01): Column count doesn't match value count at row 1
页:
[1]