FB

Are you getting the error “Column count doesn’t match value count at row 1” in MySQL?

In this article, I’ll show you how to resolve this error, as well as what it means, and some examples.

Let’s take a look.

What Is the “Column count doesn’t match value count at row 1” Error?

When you try to run an INSERT statement to insert data into a table, you might get this error:

Column count doesn’t match value count at row 1

In short, it means the number of columns in your INSERT statement does not match the number of values in the INSERT statement. To resolve this, ensure the columns match.

But what does this mean?

 

Error Definition

The error “Column count doesn’t match value count at row 1” means that the number of columns does not match the number of rows in your INSERT statement.

To demonstrate this, we can create a simple table with a few columns:

CREATE TABLE product_test (
id INT(4),
product_name VARCHAR(20),
price INT(5)
);

So, let’s try to insert a record into this table:

INSERT INTO product_test VALUES (1, 'Office Chair');

When you run this query, you’ll get an error:

Error Code: 1136. Column count doesn't match value count at row 1

This error happened because the INSERT statement contained two values (1 and “Office Chair”), but the table has 3 columns. The third column, price, was not mentioned in the INSERT statement.

You’ll also get the same error if you specify the column names, but the number of columns does not match the number of values:

INSERT INTO product_test (id, product_name, price)
VALUES (2, 'Desk');

When you run this query, you’ll get an error:

Error Code: 1136. Column count doesn't match value count at row 1

In this example, you have specified three columns (id, product_name, price) but only two values (2 for id, and ‘Desk’ for product_name).

The “at row 1” part of the error just refers to the line of the query you’re running. It usually says “row 1” because the INSERT statement is run as a single statement.

So how do you fix this error?

 

How to Resolve the Error

To resolve this “Column count doesn’t match value count at row 1” error, you have to ensure that the columns in the table or your INSERT statement match the values you are inserting.

The best way to do this is to specify the columns you want in your INSERT statement and ensure the VALUES clause matches the column list.

In this example, you specify both the column names and the values:

INSERT INTO product_test (id, product_name)
VALUES (1, 'Office Chair');

This will insert the two values you want to insert and ensures that the right columns are used (id and product_name).

Or, you could specify all three columns and their values:

INSERT INTO product_test (id, product_name, price)
VALUES (1, 'Office Chair', 100);

Three columns are mentioned (id, product_name, price) and three values are mentioned (1, ‘Office Chair’, 100).

If you run this statement, it’s successful.

You could exclude the column names from the INSERT statement, and run something like this:

INSERT INTO product_test VALUES (1, 'Office Chair', 100);

This will work. However, it’s risky as the order of the columns in the table could change if you add more columns. If that happens, your statement would break.

 

Check For Triggers

If you’re sure that the columns and rows are matching in your INSERT statement, another area you can check are the database triggers.

Triggers are pieces of code that run on certain actions on your database. For example, a trigger may be set to run when you INSERT a record into a table, and the trigger inserts a record into another table.

If you have a trigger like this, you’ll get this “Column count doesn’t match value count at row 1” error, but the message doesn’t tell you the table name.

This might indicate it’s the table you’re inserting into, but it could be a table that the trigger is inserting into.

This is common when you’re storing audit records (keeping a history of changes to records in a different table).

So, check if you have any triggers on the table you’re inserting into, and if so, check the code to ensure the values match the columns.

 

Conclusion

Hopefully, this error doesn’t bother you anymore now you know what causes it and how to fix it!