Question #9

Author: admin
tags: PostgreSQL  
A table was created in Postgresql and several values were added to it.
CREATE TABLE games(
  id serial PRIMARY KEY,
  name text NOT NULL UNIQUE
);

INSERT INTO games (id, name) VALUES
  (1, 'Dune 2'),
  (2, 'Supaplex'),
  (3, 'SimCity');
Then we add a 4th entry without specifying an id:
INSERT INTO games (name) VALUES ('Worms');
What will happen after that?
Unlike MySQL, there will be no problem. An entry will be created:
 id | name
----+-------
  4 | Worms
An error will occur with message about duplicate key value
The first entry will be rewritten:
 id | name
----+-------
  1 | Worms
The serial type is not a true type. When a serial column is created, a sequence is automatically created.
When manually adding a number value to the id column, the value of the sequence does not change!
Therefore the query:
INSERT INTO games (name) VALUES ('Worms');
tried to add an id with a value of 1, not 4.
Rate the difficulty of the question:
easyhard