The number of SQL columns does not match the count error

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 get the error "The number of columns does not match the number of rows." It should work fine since the primary key is auto_increment .

+7
source share
6 answers

Typically, when you have an Auto Incrementing field, you do not want to specify a value for this column. This means that you are removing this value from VALUES

However, as Johan points out, whenever the number of columns in a table does not match the number of columns, you must specify a list of columns in the target table.

Good practice anyway if the number of columns or reordering of columns

 INSERT INTO message (p_id) Values ('7') 
+12
source

You must specify column names if the number of values โ€‹โ€‹is not equal to the number of columns:

 INSERT INTO message (p_id) VALUES ('7'); 
+6
source

You need to specify the columns you are inserting into. If you are not using a database, it is assumed that the values โ€‹โ€‹you provide are values โ€‹โ€‹for each column and in the exact order of the columns according to the schema. Try the instructions below.

 INSERT INTO `message` (p_id) VALUES ('7'); 

Also, it's just good practice to always specify columns in an insert statement. It helps readability.

+4
source

Try the following:

 INSERT INTO `message` (p_id) VALUES ('7'); 

You do not specify columns that have 2, and try to insert 1 value.

0
source

maybe if you do

 INSERT INTO message (p_id) VALUES ("7"); 
0
source

You can do it

 INSERT INTO `message` VALUES (NULL, '7'); 

This will match the number of columns and ignore zero insertion into the auto increment identifier and insert the value of auto increment ;

0
source

All Articles