Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Default values
5. Inserting a DEFAULT value into one column
How to auto-generate values in a database

Instruction

Great! Let's see how DEFAULT works in practice.

In the author table, the photo column stores either a path to the author's photo (if one exists) or the string 'imgs/no-photo.jpg' (the default value). The default value will be put into the column if no other value is inserted into this field. In the following statement ...

INSERT INTO author (id, first_name, last_name, photo, create_timestamp, is_active) VALUES
(1, 'Gregory', 'Smith', DEFAULT, '2018-08-25', true);

... DEFAULT occupies the fourth position in the VALUES list, which corresponds to the photo column. When this record is inserted, the string 'imgs/no-photo.jpg' will go into the photo field. Here's what the result looks like:

id first_name last_name photo create_timestamp is_active
1 Gregory Smith imgs/no-photo.jpg 2018-08-25 00:00:00.0 true

Exercise

Let's add data for Cindy Barry to the author table. She should have an id of 2, a photo path set to 'imgs/cindy.jpg', and a registration date of '2018-08-27'.

We don't know if Cindy wants to have an active account yet, so we'll leave this as a DEFAULT value. Note: The default for the is_active column is false.

Stuck? Here's a hint!

Insert the values, using the DEFAULT value for the last column.