Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Default values
How to auto-generate values in a database
12. SERIAL – simple example

Instruction

Okay. Now let's use a query to insert data in a table with a SERIAL column.

In the author table, suppose that we've added SERIAL as the PRIMARY KEY to the id column. Let's insert a new record:

INSERT INTO author (first_name, last_name, photo) VALUES
('Evo', 'Black', 'imgs/black2.jpg');

This is the result of that INSERT operation:

id first_name last_name photo create_timestamp is_active
10 Evo Black imgs/black2.jpg {current timestamp} false

Notice that you have to omit the id column with SERIAL; its value will be auto-generated. Also, if we insert the first record into the table, the id will be 1. If there are other records in the table, SERIAL will be whatever the next value is. In this case, we had 9 other records before we inserted this one, so this row gets an id of 10.

Exercise

Add the next new author to the database. You only know her name, Sally Adams, and that her account is active.