Loading lesson path
Overview
constraint is used to automatically insert a default value for a column, if no value is specified. The default value will be added to all new records (if no other value is specified).
value for the "City" column upon creation of the "Persons" table:
( ID int PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int,
);constraint can also be used to insert system values, by using functions like CURRENT_DATE() to insert the current date:
( ID int PRIMARY KEY, OrderNumber int NOT NULL, OrderDate date DEFAULT CURRENT_DATE()
);To achieve the same result in SQL Server use the following SQL (to insert the current date):
( ID int PRIMARY KEY, OrderNumber int NOT NULL,
);constraint on the "City" column when the table is already created, use the following SQL:
ALTER City SET DEFAULT 'Sandnes';ADD CONSTRAINT df_City DEFAULT 'Sandnes' FOR City;ALTER COLUMN City SET DEFAULT 'Sandnes';MODIFY City DEFAULT 'Sandnes';constraint, use the following SQL: