DEFAULT Constraint SQL

The DEFAULT Constraint in SQL is used to designate what should be placed in a column cell if there is no input given for that cell at the time the row is inserted. This constraint is usually put on the column at the time of table creation. For example:

CREATE TABLE fict_chars
(
id INT NOT NULL,
name VARCHAR (255),
description TEXT,
book VARCHAR (255),
author VARCHAR (255) DEFAULT 'anonymous',
PRIMARY KEY(id)
)

Now if there is no author specified when a row is inserted the author will be 'anonymous'. At the same time the default can also be a function:

CREATE TABLE monopoly_prop
(
id INT NOT NULL,
street_name VARCHAR (255),
color VARCHAR(25),
date_acquired DATETIME (255) DEFAULT NOW(),
PRIMARY KEY(id)
)

DEFAULTs can be DROPped or SET for columns in the table by using the following:

ALTER TABLE monopoly_prop
ALTER date_acquired DROP DEFAULT

< Data Types in SQL | DELETE Statement SQL >


Send notes in disappearing ink!

Interesting Pages