The DEFAULT constraint assigns a default value to a column, which is applied to all new records if no other value is specified.
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() ); |
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’; |
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; |