Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Constraints: Using Create Command

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

SQL CONSTRAINTS

Constraints are the rules enforced on the data columns of a table. These are used to limit
the type of data that can go into a table. This ensures the accuracy and reliability of the
data in the database.

Constraints can be defined in two ways.

Using Create command: When a table is created with the CREATE TABLE
statement we can specify constraints. Constraints could be either on a 1. Column level or
2. Table level

Column level : Constraints can be specified at the time of column definition. Column-
level constraints refer to a single column in the table and do not specify a column name
(except check constraints). They refer to the column that they follow.

Syntax: create table table name(col1 datatype constraint constraint name constraint
type,

col2 data type,

col3 data type,.....coln data type);

Table level: Table-level constraints refer to one or more columns in the table. Table-
level constraints specify the names of the columns to which they apply. Table level
constraints are applied to the whole table

Syntax: create table table name(col1 datatype,

col2 data type,

col3 data type,.....coln data type,

constraint constraint name constraint type(column name));

Using Alter command: We can use the ALTER TABLE statement to add
constraints even after the table is created.

Syntax:

Alter table table name add constraint constraint name constraint type(column
name);
Following are some of the most commonly used constraints available in SQL.

NOT NULL Constraint − Ensures that a column cannot have a NULL value.

DEFAULT Constraint − Provides a default value for a column when none is specified.

UNIQUE Constraint − Ensures that all values in a column are different.

PRIMARY Key − Uniquely identifies each row/record in a database table.

FOREIGN Key − Uniquely identifies a row/record in any of the given database table.

CHECK Constraint − The CHECK constraint ensures that all the values in a column
satisfies certain conditions.

INDEX − Used to create and retrieve data from the database very quickly.

Column constraints include:

NOT NULL, PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK

Table constraints include:

PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK

Primary key constraints


A primary key is a field in a table which uniquely identifies each row/record in a
database table. Primary keys must contain unique values. A primary key column cannot
have NULL values.

A table can have only one primary key, which may consist of single or multiple fields.
When multiple fields are used as a primary key, they are called a composite key.

If a table has a primary key defined on any field(s), then you cannot have two records
having the same value of that field(s)

Syntax to define a Primary key at column level:


column name datatype [CONSTRAINT constraint_name] PRIMARY KEY

Syntax to define a Primary key at table level:


[constraint constraint name]primary key(column_name1, column_name2..)
column_name1, column_name2  are the names of the columns which define the
primary Key.
The syntax within the bracket i.e. [CONSTRAINT constraint_name] is optional

Not Null Constraint :


This constraint ensures all rows in the table contain a definite value for the column
which is specified as not null. Which means a null value is not allowed

Syntax to define a Not Null constraint:


[CONSTRAINT constraint name] NOT NULL

Unique Constraint:

The UNIQUE constraint ensures that all values in a column are different. Both the
UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE
constraint..We can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE

Syntax to define a Unique key at table level:


[CONSTRAINT constraint_name] UNIQUE(column_name)

FOREIGN Key Constraint

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.

The table containing the foreign key is called the child table, and the table containing the
candidate key is called the referenced or parent table.

Syntax to define a Foreign key at column level:

[constraint constraint name] references parent table name(column name)

Syntax to define a Foreign key at table level:

[constraint constraint_name] foreign key(column_name) references parent table


name(column name)
SQL Check Constraint :
This constraint defines a business rule on a column. All the rows must satisfy this rule.
The constraint can be applied for a single column or a group of columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)

SQL DEFAULT Constraint

The DEFAULT constraint is used to provide a default value for a column.

The default value will be added to all new records IF no other value is specified.

Syntax:

[Constraint constraint name] default default value

Dropping Constraints
Any constraint that you have defined can be dropped using the ALTER TABLE
command with the DROP CONSTRAINT option

Syntax: Alter table table name drop constraint constraint name;

You might also like