Curriculum
Course: SQL
Login

Curriculum

SQL

SQL References

0/80

MySQL Functions

0/139

SQL Server Functions

0/84

SQL Quick Ref

0/1
Text lesson

DEFAULT

DEFAULT

The DEFAULT constraint assigns a default value to a column, which is applied to all new records if no other value is specified.

SQL DEFAULT on CREATE TABLE

The provided SQL assigns a DEFAULT value for the “City” column during the creation of the “Persons” table.

My SQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons (
    City varchar(255) DEFAULT ‘Sandnes’
); 

The DEFAULT constraint can also incorporate system values, such as using functions like GETDATE(), for insertion.

CREATE TABLE Orders (
    OrderDate date DEFAULT GETDATE()
); 

SQL DEFAULT on ALTER TABLE

To apply a DEFAULT constraint on the “City” column after the table has been created, employ the following SQL:

MySQL:

ALTER TABLE Persons
ALTER City SET DEFAULT ‘Sandnes’

SQL Server:

ALTER TABLE Persons
ADD CONSTRAINT df_City
DEFAULT ‘Sandnes’ FOR City;

MS Access:

ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT ‘Sandnes’

Oracle:

ALTER TABLE Persons
MODIFY City DEFAULT ‘Sandnes’

DROP a DEFAULT Constraint

To remove a DEFAULT constraint, utilize the following SQL:

MySQL:

ALTER TABLE Persons
ALTER City DROP DEFAULT

SQL Server / Oracle / MS Access

ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT