×
>
<

DBMS

DBMS Default Constraints | CrackEase

Default Constraints

Default

When a column usually has the same value for most rows, you can define a DEFAULT for that column so you don't have to supply the value every time you insert a row.

For example, if most employees of a company live in Hyderabad, you can set the column City to default to 'Hyderabad'. When inserting a new row you can omit the city column and the default will be used.

CREATE
DEFAULT clause

  • The DEFAULT clause in SQL assigns a default value to a column.
  • If a value for that column is not provided in an INSERT, the default is used automatically.
  • The default can be overridden by explicitly providing a different value in the INSERT.

Example for DEFAULT clause

The following SQL creates an emp table where the City column defaults to 'Hyderabad':

MySQL / SQL Server / Oracle / MS Access (example)

  
CREATE TABLE emp (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    City varchar(255) DEFAULT 'Hyderabad'
);
  

After inserting rows without specifying City, the default value appears in query results:

ID LastName FirstName Age City
66 Dunphy Alex 21 Hyderabad
67 Tucker Lily 22 Hyderabad
68 Dunphy Luke 22 Hyderabad
69 Pritchett Alex 23 Hyderabad

  • All shown rows have City = 'Hyderabad' because the column has that default value.
  • When inserting new rows you can omit the City column; the default will be applied automatically.
  • If you provide a value for City in the INSERT, that provided value overrides the default.

Footer Content | CrackEase