0

Let's say I have a create table script like that:

CREATE TABLE Person (
    id INT PRIMARY KEY,
    age INT DEFAULT 18,
    iq INT DEFAULT 100
);

So I have two DEFAULT constraints in a row. Now I make an insertion like that:

INSERT INTO Person VALUES(1,85);

How do we know if we skip the attribute age or the attribute iq?

Thank you for your help.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38
JacopoStanchi
  • 421
  • 5
  • 16

1 Answers1

5

The answer to your question: it depends. ;-)

MySQL

Running your query on MySQL will get you the following error:

Error: ER_WRONG_VALUE_COUNT_ON_ROW: Column count doesn't match value count at row 1

For MySQL you need to tell the DBMS which columns you want to have (and which columns the DB should fill with default data). Like this:

INSERT INTO Person(id, age) VALUES(1,85);

Or you need to tell the DB, where to use default values. You can pass the keyword DEFAULT for these columns:

INSERT INTO Person VALUES(1,85, DEFAULT);

See: MySQL INSERT Syntax

SQLite

SQLite acts like MySQL, the error message here is:

SQLITE_ERROR: table Person has 3 columns but 2 values were supplied

Which means, you have to specify the columns.

PostgreSQL

Running the query on a PostgreSQL table will yield

id  age iq
1   85  100

Here PostgreSQL will use the columns from the table definition in the defined order and fill all remaining columns with the default value.

MS SQL Server

See: How to insert default values in SQL table?

Community
  • 1
  • 1
rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38