MySQL 报错合集
1.ERROR 1062 (23000): Duplicate entry ‘4’ for key '表名.PRIMARY’错误
mysql> select * from student;
+--------+------+------+----+
| name | sex | age | id |
+--------+------+------+----+
| 墨迹 | 男 | 18 | 1 |
| 李四 | 男 | 21 | 2 |
| nn | 男 | 20 | 3 |
| mm | 女 | 20 | 4 |
+--------+------+------+----+
4 rows in set (0.00 sec)
mysql> insert into `student` (`id`,`name`,`sex`) values(4,'张山','男');
ERROR 1062 (23000): Duplicate entry '4' for key 'student.PRIMARY'
这里报错是因为,插入的数据中,id
这一个字段已经设置为了主键,并且新插入的数据中又出现了4
,故报错;
mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| sex | varchar(15) | YES | | NULL | |
| age | int | YES | | 18 | |
| id | int | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
正确如下:
mysql> insert into `student` (`id`,`name`,`sex`) values(5,'张山','男');
Query OK, 1 row affected (0.01 sec)
mysql> select *from student;
+--------+------+------+----+
| name | sex | age | id |
+--------+------+------+----+
| 墨迹 | 男 | 18 | 1 |
| 李四 | 男 | 21 | 2 |
| nn | 男 | 20 | 3 |
| mm | 女 | 20 | 4 |
| 张山 | 男 | 18 | 5 |
+--------+------+------+----+
5 rows in set (0.00 sec)