SQL Basic
SQL HOMESQL Intro
SQL Syntax
SQL Select
SQL Distinct
SQL Where
SQL And & Or
SQL Order By
SQL Insert
SQL Update
SQL Delete
SQL Demo
SQL Try ItSQL Advanced
SQL TopSQL Like
SQL Wildcards
SQL In
SQL Between
SQL Alias
SQL Joins
SQL Inner Join
SQL Left Join
SQL Right Join
SQL Full Join
SQL Union
SQL Select Into
SQL Create DB
SQL Create Table
SQL Constraints
SQL Not Null
SQL Unique
SQL Primary Key
SQL Foreign Key
SQL Check
SQL Default
SQL Create Index
SQL Drop
SQL Alter
SQL Increment
SQL Views
SQL Dates
SQL Nulls
SQL isnull()
SQL Data Types
SQL Functions
SQL FunctionsSQL avg()
SQL count()
SQL first()
SQL last()
SQL max()
SQL min()
SQL sum()
SQL Group By
SQL Having
SQL ucase()
SQL lcase()
SQL mid()
SQL len()
SQL round()
SQL now()
SQL format()
SQL Quick Ref
SQL Hosting
SQL Summary
SQL Quiz
SQL QuizSQL DEFAULT Constraint
| « Previous | Next Chapter » |
SQL DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.
SQL DEFAULT Constraint on CREATE TABLE
The following SQL creates a DEFAULT constraint on the "City" column when the "Persons" table is created:
My SQL / SQL Server / Oracle / MS Access:
| CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' ) |
The DEFAULT constraint can also be used to insert system values, by using functions like GETDATE():
| CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, OrderDate date DEFAULT GETDATE() ) |
SQL DEFAULT Constraint on ALTER TABLE
To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL:
MySQL:
| ALTER TABLE Persons ALTER City SET DEFAULT 'SANDNES' |
SQL Server / Oracle / MS Access:
| ALTER TABLE Persons ALTER COLUMN City SET DEFAULT 'SANDNES' |
To DROP a DEFAULT Constraint
To drop a DEFAULT constraint, use the following SQL:
MySQL:
| ALTER TABLE Persons ALTER City DROP DEFAULT |
SQL Server / Oracle / MS Access:
| ALTER TABLE Persons ALTER COLUMN City DROP DEFAULT |
| « Previous | Next Chapter » |
