Snatch any course you want for just $15! Go to “Courses” and find a course ↓
Default values
5. Inserting a DEFAULT value into one column


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

In the author table, the photo column stores either a path to the image 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.

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

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 column. 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 1


Let's add the data for Cindy Barry into 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.

Stuck? Here's a hint!

List columns using the DEFAULT value for the last column.