MySQL SQL 列计数与值计数不匹配错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5746155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL column count does not match value count error
提问by Tower
I have this table:
我有这张桌子:
CREATE TABLE `message` (
`m_id` INT(11) NOT NULL AUTO_INCREMENT,
`p_id` VARCHAR(30) NOT NULL,
PRIMARY KEY (`m_id`)
)
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
and this insert
这个插入
INSERT INTO `message` VALUES ('7');
and I am receiving a "column count does not match a row value count" error. It should work fine since the primary key is auto_increment
.
并且我收到“列数与行值数不匹配”错误。它应该可以正常工作,因为主键是auto_increment
.
回答by Conrad Frix
Typically when you have an Auto Incrementing field you don't want to specify the value for that column. Which means you remove that value from the VALUES
通常,当您有一个 Auto Incrementing 字段时,您不想为该列指定值。这意味着您从VALUES
However as Johan points out, whenever the number of columns in the table doesn't match the number of columns you must specify the column list on the target table.
但是,正如 Johan 指出的那样,只要表中的列数与列数不匹配,您就必须在目标表上指定列列表。
Its good practice to do anyway in case the number of columns or column order changes
无论如何,如果列数或列顺序发生变化,这是一种很好的做法
INSERT INTO
message
(p_id)
Values ('7')
回答by David Tang
You must specify the column names when the number of values is not equal to the number of columns:
当值数不等于列数时,您必须指定列名:
INSERT INTO message (p_id) VALUES ('7');
回答by John Hartsock
You need to specify the columns you are inserting into. If you do not the Database assumes the values you are providing are values for each column and in the exact order of the columns according to the schema. Try the statement below.
您需要指定要插入的列。如果您不这样做,则数据库假定您提供的值是每列的值,并且根据架构按照列的确切顺序排列。试试下面的语句。
INSERT INTO `message` (p_id) VALUES ('7');
In addition it is just good practice to always specify the columns in an insert statement. It helps for readability.
此外,始终在插入语句中指定列是一种很好的做法。它有助于提高可读性。
回答by yaswanth
You can do this
你可以这样做
INSERT INTO `message` VALUES (NULL, '7');
This will match the number of columns and ignore the null insert into the auto increment id and insert the auto increment
value;
这将匹配列数并忽略空插入到自动增量 id 并插入auto increment
值;
回答by gmadd
Try this:
尝试这个:
INSERT INTO `message` (p_id) VALUES ('7');
You're not specifying the columns, which there are 2 of, and trying to insert 1 value.
您没有指定有 2 个的列,并尝试插入 1 个值。
回答by Eric Frick
maybe if you do this
也许如果你这样做
INSERT INTO message (p_id) VALUES ("7");