如何解決 MySQL 中“列數與值數不匹配”的錯誤?


無論何時向目標表中插入記錄,如果列數與值數不匹配,就會發生這種型別的錯誤。為了進行演示,我們建立一個表格

mysql> create table errorDemo
   -> (
   -> User_Id int NOT NULL AUTO_INCREMENT,
   -> User_Name varchar(20),
   -> PRIMARY KEY(User_Id)
   -> );
Query OK, 0 rows affected (0.47 sec)

錯誤如下

mysql> insert into errorDemo values('John');
ERROR 1136 (21S01): Column count doesn't match value count at row 1

為了避免這種型別的錯誤,你需要使用以下語法

insert into yourTableName(yourColumnName1,yourColumnName2,...N)values(yourValue1,yourValue2,....N);

使用插入命令向表中插入一些記錄。

查詢如下

mysql> insert into errorDemo(User_Name) values('John');
Query OK, 1 row affected (0.12 sec)
mysql> insert into errorDemo(User_Name) values('Carol');
Query OK, 1 row affected (0.14 sec)
mysql> insert into errorDemo(User_Name) values('Sam');
Query OK, 1 row affected (0.15 sec)

使用選擇語句顯示錶中的所有記錄。

查詢如下

mysql> select *from errorDemo;

輸出如下

+---------+-----------+
| User_Id | User_Name |
+---------+-----------+
| 1       | John      |
| 2       | Carol     |
| 3       | Sam       |
+---------+-----------+
3 rows in set (0.00 sec)

更新於:30-Jul-2019

698 瀏覽量

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.