Sql ошибка 1136

One of the more common error message in MySQL goes like this: “ERROR 1136 (21S01): Column count doesn’t match value count at row 1“.

One of the more common error message in MySQL goes like this: “ERROR 1136 (21S01): Column count doesn’t match value count at row 1“.

This error typically occurs when you’re trying to insert data into a table, but the number of columns that you’re trying to insert don’t match the number of columns in the table.

In other words, you’re either trying to insert too many columns, or not enough columns.

To fix this issue, make sure you’re inserting the correct number of columns into the table.

Alternatively, you can name the columns in your INSERT statement so that MySQL knows which columns your data needs to be inserted into.

The error can also occur if you pass the wrong number of columns to a ROW() clause when using the VALUES statement.

Example of Error

Suppose we have the following table:

+----------+----------+----------+
| column_0 | column_1 | column_2 |
+----------+----------+----------+
|        1 |        2 |        3 |
|        4 |        5 |        6 |
+----------+----------+----------+

The following code will cause the error:

INSERT INTO t1 VALUES (7, 8, 9, 10);

Result:

ERROR 1136 (21S01): Column count doesn't match value count at row 1

In this case, I tried to insert data for four columns into a table that only has three columns.

We’ll get the same error if we try to insert too few columns:

INSERT INTO t1 VALUES (7, 8);

Result:

ERROR 1136 (21S01): Column count doesn't match value count at row 1

Solution 1

The obvious solution is to insert the correct number of rows. Therefore, we could rewrite our code as follows:

INSERT INTO t1 VALUES (7, 8, 9);

Result:

Query OK, 1 row affected (0.00 sec)

Solution 2

Another way of doing it is to explicitly name the columns for which we want to insert data. This technique can be used to insert less columns than are in the table.

Example:

INSERT INTO t1 (column_0, column_1) VALUES (7, 8);

Result:

Query OK, 1 row affected (0.00 sec)

This method may result in a different error if there are any constraints that require a value to be passed for that column (for example, if the table has a NOT NULL constraint on that column). Therefore, you’ll need to ensure you’re complying with any constraints on the column when doing this.

When trying to insert a new data row into a table, you might run into this error:

Column count doesn't match value count at row 1.

That error message typically means the number of values provided in the INSERT statement is bigger or smaller than the number of columns the table has, while at the same time, you did not specify the columns to be inserted. So MySQL doesn’t know which data to insert in which column and it throws back the error.

For example, you have this table employees:

CREATE TABLE employees (
  emp_no int(11) NOT NULL,
  birth_date date NOT NULL,
  first_name varchar(14) NOT NULL,
  last_name varchar(16) NOT NULL,
  gender enum('M','F') NOT NULL,
  hire_date date NOT NULL,
  email text,
  PRIMARY KEY (emp_no);

And you try to insert a new data rows into that table with this INSERT statement:

INSERT INTO employees
  VALUES('400000', '1990-09-09', 'Joe', 'Smith', 'M', '2009-09-11');

As you can see, there are 7 columns in the table employees but you are providing only 6 values in the INSERT statement. MySQL returns the error:

Column count doesn't match value count at row 1

To fix this

1. Provided the full required data

If you omit the column names when inserting data, make sure to provide a full row of data that matches the number of columns

INSERT INTO employees
  VALUES('400000', '1990-09-09', 'Joe', 'Smith', 'M', '2009-09-11', '[email protected]');

Or if the email field is empty:

INSERT INTO employees
  VALUES('800000', '1990-09-09', 'Joe', 'Smith', 'M', '2009-09-11', '');

2. Specify the columns to be inserted in case not all columns are going to have value.

INSERT INTO employees.employees (emp_no, birth_date, first_name, last_name, gender, hire_date)
  VALUES('400000', '1990-09-09', 'Joe', 'Smith', 'M', '2009-09-11');

Sometimes, all the values are provided but you still see this error, you likely forgot to use the delimiter between two particular values and make it appear as one value. So double-check the delimiter and make sure you did not miss any semicolon.


Need a good GUI tool for databases? TablePlus provides a native client that allows you to access and manage Oracle, MySQL, SQL Server, PostgreSQL, and many other databases simultaneously using an intuitive and powerful graphical interface.

Download TablePlus for Mac.

Not on Mac? Download TablePlus for Windows.

On Linux? Download TablePlus for Linux

Need a quick edit on the go? Download TablePlus for iOS

TablePlus in Dark mode

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!

Photo from Unsplash

When you run an INSERT statement, you might see MySQL responding with Column count doesn't match value count at row 1 error.

This error occurs when the number of columns that your table has and the number of values you try to insert into the table is not equal.

For example, suppose you have a pets table with 4 columns as follows:

+----+--------+---------+------+
| id | owner  | species | age  |
+----+--------+---------+------+
|  1 | Jessie | bird    |    2 |
|  2 | Ann    | duck    |    3 |
|  3 | Joe    | horse   |    4 |
|  4 | Mark   | dog     |    4 |
|  5 | Ronald | cat     |    1 |
+----+--------+---------+------+

When you want to insert data into the pets table above, you need to provide values for all 4 columns, or you will trigger the column count doesn't match value count error:

The following INSERT statement only provides one value for the table:

INSERT INTO pets 
  VALUES("Jack");

But since MySQL requires 4 values for each column, the error gets triggered:

ERROR 1136 (21S01): Column count doesn't match value count at row 1

To resolve this error, you can either:

  • Provide values equal to the number of columns you have in your table
  • Or specify the column table you wish to insert values

Let’s learn how to do both.

First, you can specify the values for each column as in the following statement:

INSERT INTO pets 
  VALUES(NULL, "Jack", "duck", 2);

The above statement will work because the pets table has four columns and four values are provided in the statement.

But if you only have values for specific columns in your table, then you can specify the column name(s) that you want to insert into.

Here’s an example of inserting value only to the species column. Note that the column name is added inside parentheses after the table name:

INSERT INTO pets(`species`) 
  VALUES("panda");

You can put as many column names as you need inside the parentheses.

Here’s another example of inserting data into three columns:

INSERT INTO pets(`owner`, `species`, `age`) 
  VALUES("Bruce", "tiger", 5);

The id column is omitted in the example above.

Keep in mind that the same error will happen if you have more values to insert than the table columns.

Both statements below will return the error:

-- 👇 trying to insert five values
INSERT INTO pets 
  VALUES(NULL, "Jack", "duck", 2, TRUE);

-- 👇 trying to insert two values, but specify three columns
INSERT INTO pets(`owner`, `species`, `age`) 
  VALUES("Jane", "cat");

By default, MySQL will perform an INSERT statement expecting each column to have a new value.

When you specify the column names that you want to insert new values into, MySQL will follow your instructions and remove the default restrictions of inserting to all columns.

And those are the two ways you can fix the Column count doesn't match value count at row 1 error in MySQL database server.

I’ve also written several articles on fixing other MySQL errors. I’m leaving the links here in case you need help in the future:

  • Fix MySQL Failed to open file error 2
  • Fix MySQL skip grant tables error
  • Solve MySQL unknown column in field list error

And that’s it for column count doesn’t match value count error.

Thanks for reading! 👍

Понравилась статья? Поделить с друзьями:
  • Sql ошибка 1075
  • Sql ошибка 102
  • Sql отправка писем при ошибок создания резервной копии
  • Sql ожидание восстановления как исправить
  • Sql объект error