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
Summary

Instruction

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 N'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, FirstName, LastName, Photo, CreateDateTime, IsActive) 
VALUES (1, N'Gregory', N'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 N'imgs/no-photo.jpg' will go into the Photo column. Here's what the result looks like:

Id FirstName LastName Photo CreateDateTime IsActive
1 Gregory Smith imgs/no-photo.jpg 2018-08-25 00:00:00.0 1

Exercise

Let's add the data for Cindy Barry into the Author table. She should have an ID of 2, a photo path set to N'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 this column is 0. It will be displayed as false.

Stuck? Here's a hint!

List columns using the DEFAULT value for the last column.