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

Constraints

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SQL Constraints

SQL constraints are used to specify rules for data in a table. Constraints can be specified
when the table is created with the CREATE TABLE statement, or after the table is created
with the ALTER TABLE statement.
SQL constraints are used to specify rules for the data in a table.
Constraints 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 table. If there is any violation between the
constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely
identifies each row in a table
• FOREIGN KEY - Prevents actions that would destroy links between tables
• CHECK - Ensures that the values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column if no value is specified
• CREATE INDEX - Used to create and retrieve data from the database very
quickly

1. NOT NULL
Syntax to apply the NOT NULL constraint during table creation:

CREATE TABLE TableName (ColumnName1 datatype NOT NULL, Colum


nName2 datatype,…., ColumnNameN datatype);
Syntax to apply the NOT NULL constraint on an existing table's column:
ALTER TABLE TableName CHANGE Old_ColumnName New_ColumnName
Datatype NOT NULL;

2. UNIQUE
Syntax to apply the UNIQUE constraint on a single column:

CREATE TABLE TableName (ColumnName1 datatype UNIQUE, ColumnN


ame2 datatype,…., ColumnNameN datatype);

Syntax to apply the UNIQUE constraint on more than one column:


CREATE TABLE TableName (ColumnName1 datatype, ColumnName2 dat
atype,…., ColumnNameN datatype, UNIQUE (ColumnName1, ColumnNa
me 2));

Syntax to apply the UNIQUE constraint on an existing table's column:

ALTER TABLE TableName ADD UNIQUE (ColumnName);


3. PRIMARY KEY
Syntax of primary key constraint during table creation:
CREATE TABLE TableName (ColumnName1 datatype PRIMARY KEY, Col
umnName2 datatype,…., ColumnNameN datatype);

Syntax to apply the primary key constraint on an existing table's column:

ALTER TABLE TableName ADD PRIMARY KEY (ColumnName);

4. FOREIGN KEY
Syntax to apply a foreign key constraint during table creation:

CREATE TABLE tablename(ColumnName1 Datatype(SIZE) PRIMARY KE


Y, ColumnNameN Datatype(SIZE), FOREIGN KEY( ColumnName ) REFER
ENCES PARENT_TABLE_NAME(Primary_Key_ColumnName));
Syntax to apply the foreign key constraint with constraint name:
CREATE TABLE tablename(ColumnName1 Datatype PRIMARY KEY, Colu
mnNameN Datatype(SIZE), CONSTRAINT ConstraintName FOREIGN KE
Y( ColumnName ) REFERENCES PARENT_TABLE_NAME(Primary_Key_Colu
mnName));

Syntax to apply the foreign key constraint on an existing table's column:


ALTER TABLE Parent_TableName ADD FOREIGN KEY (ColumnName) RE
FERENCES Child_TableName (ColumnName);

5. CHECK
Syntax to apply check constraint on a single column:
CREATE TABLE TableName (ColumnName1 datatype CHECK (ColumnNa
me1 Condition), ColumnName2 datatype,…., ColumnNameN datatype);
Create a student table and apply CHECK constraint to check for the age less than or equal
to 15 while creating a table.
Syntax to apply check constraint on multiple columns:
CREATE TABLE TableName (ColumnName1 datatype, ColumnName2 dat
atype CHECK (ColumnName1 Condition AND ColumnName2 Condition),
…., ColumnNameN datatype);

Syntax to apply check constraint on an existing table's column:


ALTER TABLE TableName ADD CHECK (ColumnName Condition);

6. DEFAULT
Syntax to apply default constraint during table creation:
CREATE TABLE TableName (ColumnName1 datatype DEFAULT Value, C
olumnName2 datatype,…., ColumnNameN datatype);

Syntax to apply default constraint on an existing table's column:


ALTER TABLE TableName ALTER ColumnName SET DEFAULT Value;

7. CREATE INDEX
Syntax to create an index on single column:
CREATE INDEX IndexName ON TableName (ColumnName 1);

Indexes are not visible to the user, but they help the user to speed up the searching speed
or retrieval of data from the database.

You might also like