loading

SQL Default

SQL DEFAULT Constraint

A column’s default value can be set using the DEFAULT constraint.

If no alternative value is supplied, the default value will be appended to all newly created records.

SQL DEFAULT on CREATE TABLE

When the “Persons” table is created, the following SQL creates a DEFAULT value for the “City” column:

My SQL / SQL Server / Oracle / MS Access:

----- EXAMPLE MUKAVU -----

System values can also be inserted using the DEFAULT constraint and functions such as GETDATE():

				
					CREATE TABLE Orders (
    ID int NOT NULL,
    OrderNumber int NOT NULL,
    OrderDate date DEFAULT GETDATE()
);
				
			

SQL DEFAULT on ALTER TABLE

When the table has already been established, use the following SQL to create a DEFAULT constraint on the “City” column:

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

A DEFAULT constraint can be removed by using the following SQL:

MySQL:

				
					ALTER TABLE Persons
ALTER City DROP DEFAULT;
				
			

SQL Server / Oracle / MS Access:

				
					ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;
				
			
Share this Doc

SQL Default

Or copy link

Explore Topic