SQL Default Constraint With Example

aimtocode

Constraints Definition:

SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the accuracy and integrity of the data inside table.

Constraints provide a standard mechanism to maintain the accuracy and integrity of the data inside a database table.


4. DEFAULT constraints:

This constraint specifies a default value for the column when no value is specified by the user.

SQL DEFAULT constraint specified only at column level.

A column default is some value that will be inserted in the column by the database engine when an INSERT statement doesn't explicitly assign a particular value.

Syntax:

CREATE TABLE table_name(
    column_name datatype[(size)] [ NULL | NOT NULL ] DEFAULT default_value,
    column_name datatype[(size)] [ NULL | NOT NULL ] DEFAULT default_value,
    ....
);

Example: defined only at Column level

SQL> CREATE TABLE std_info(
    no NUMBER(3,0) PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    std VARCHAR(18) DEFAULT 'M.Sc.(CS)',
    fees_pay NUMBER(5) DEFAULT 2000
);
-------------------------------------
Table created.

Read Also:


aimtocode