DEFAULT

SQL DEFAULT Constraint

DEFAULT sets a value for a column when an INSERT does not provide one. It can be a constant or a function such as the current date.

Example

Create a table with defaults, insert two rows, and see which values were filled in:

CREATE TABLE Tickets (
  TicketID  INTEGER PRIMARY KEY,
  Subject   VARCHAR(200) NOT NULL,
  Status    VARCHAR(20) DEFAULT 'open',
  Priority  INT DEFAULT 3,
  CreatedOn DATE DEFAULT CURRENT_DATE
);
INSERT INTO Tickets (TicketID, Subject) VALUES (1, 'Login page is slow');
INSERT INTO Tickets (TicketID, Subject, Priority) VALUES (2, 'Payment failed', 1);
SELECT TicketID, Subject, Status, Priority FROM Tickets;

Result: 2 rows

TicketIDSubjectStatusPriority
1Login page is slowopen3
2Payment failedopen1

DEFAULT on ALTER TABLE

-- MySQL
ALTER TABLE Customers ALTER Country SET DEFAULT 'India';
-- SQL Server
ALTER TABLE Customers ADD CONSTRAINT DF_Country DEFAULT 'India' FOR Country;
-- PostgreSQL
ALTER TABLE Customers ALTER COLUMN Country SET DEFAULT 'India';

Note

Note: An explicit NULL in the INSERT is stored as NULL. The default is only used when the column is left out (or the keyword DEFAULT is used).