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
13. SERIAL with DEFAULT

Instruction

Nice.

SERIAL can also be used as the DEFAULT value of a column. Look at the query below:

INSERT INTO author VALUES
(DEFAULT, 'Mark', 'Barker', 'imgs/barker1.jpg', DEFAULT, DEFAULT);

Which returned the result:

id first_name last_name photo create_timestamp is_active
... ... ... ... ... ...
11 Mark Barker imgs/barker1.jpg {current timestamp} false

As you see, you can omit column names and set values in all columns. Whenever there's a serial column, we simply write DEFAULT. This will generate the next integer value for your serial column.

Exercise

Insert a post for the author with id = 1 using DEFAULT for the serial primary key value. The post has title

'The summer trip'
and the text
'It was a very long trip. We were tired but wanted to reach our goal – to visit our friend from Alaska'