SQL error · INSERT, UPDATE, DELETE and schema

INSERT has a different number of columns and values

Problem: Each VALUES row must have one value for every column listed in the INSERT.

Solution: Line up both lists, or drop from the target list the column that should get its DEFAULT.

Why it happens: INSERT has a different number of columns and values

Problem SQL
CREATE TABLE products (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  price NUMERIC DEFAULT 0
);

INSERT INTO products (id, name, price)
VALUES (1, 'Keyboard');

The target list contains id, name and price, but VALUES supplies two expressions. There is no one-to-one match.

Check the result with small data

A CTE with a column list follows the same rule as INSERT: three column names, three values in each row.

Self-contained query · SQLite
WITH new(id, name, price) AS (VALUES (2, 'Mouse', 24.90))
SELECT id, name, price FROM new;
Query result
idnameprice
2Mouse24.9

Each value takes the position of its column: 2 goes to id, Mouse to name and 24.90 to price. If you remove 24.90, SQLite rejects the query with "table new has 2 values for 3 columns", the same kind of error as in the INSERT. Do not fix it by removing a needed value at random.

Check your understanding: If you list three columns, how many values should you supply in that row?

Show answer

Three, one for each column and in the same order. If a column should take its DEFAULT, remove it from the list instead of leaving a gap.

“INSERT has a different number of columns and values” error messages by engine

The exact wording depends on the database. Compare these common messages with the one in your console.

EngineTypical message
PostgreSQLERROR: INSERT has more expressions than target columns (o INSERT has more target columns than expressions)
MySQLERROR 1136 (21S01): Column count doesn't match value count at row 1
SQL ServerMsg 109, Level 15: There are more columns in the INSERT statement than values specified in the VALUES clause. (Msg 110 si extra values)
OracleORA-00947: not enough values (o ORA-00913: too many values)
SQLiteError: 2 values for 3 columns

How to fix it in SQL

Fix 1: leave out the column that should use DEFAULT

Fixed SQL
INSERT INTO products (id, name)
VALUES (1, 'Keyboard');
Result: price gets its default value
idnameprice
1Keyboard0

Fix 2: supply every listed value

Complete INSERT
INSERT INTO products (id, name, price)
VALUES (2, 'Mouse', 24.90);

Both forms are valid because every expression in VALUES has an unambiguous target column.

How to check it

Count the expressions in each row and check the values and defaults that end up in the table.

How to avoid it next time

Adding NULL to make the count match may break NOT NULL and does not apply DEFAULT.

Related cases worth checking

A INSERT without a column list depends more on the table's physical structure and defined column order. Writing INSERT INTO products (id, name, price) makes the intent visible and lets you leave out columns that should get a default or NULL where the schema allows it.

Diagnose in layers: first check the number and order of the columns; then review types, NOT NULL, UNIQUE, FOREIGN KEY and the other constraints.