Mark As Completed Discussion

DEFAULT Constraint

When a record's designated column is left empty, the DEFAULT constraint is used to establish a default value that must be entered. If no alternative value is provided, the default value will be appended to all new records. The DEFAULT constraint can be set either when the table is created or at a later time via an ALTER statement. In the following example, we create a column City and whenever a city regarding a specific customer is not specified, we add the default value, which in our case would be Unknown City.

TEXT/X-SQL
1CREATE TABLE Customer (
2    Customer_Id int NOT NULL UNIQUE,
3    Age int,
4    City varchar(255) DEFAULTUnknown City’,
5    CHECK (Age>18)
6);

In case the table is already created, use the following query to create a DEFAULT constraint on the City column:

TEXT/X-SQL
1ALTER TABLE Customer (
2ALTER City SET DEFAULTUnknown City’;
3);