0% found this document useful (0 votes)
18 views15 pages

Constraints

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Constraints

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Date:30-12-2024 Page No:

WEEK-2
Implementation of Constraints PRIMARY KEY, FOREIGN KEY,
CHECK, NOT NULL, UNIQUE
Constraints in SQL
Constraints in SQL means we are applying certain conditions or restrictions on
the database. This further means that before inserting data into the database, we
are checking for some conditions. If the condition we have applied to the
database holds true for the data which is to be inserted, then only the data will
be inserted into the database tables.
Constraints in SQL can be categorized into two types:
1. Column Level Constraint:
Column-level constraint is used to apply a constraint on a single column.
2. Table Level Constraint:
Table Level Constraint is used to apply a constraint on multiple columns.
Constraints available in SQL are:
1. NOT NULL
2. UNIQUE
3. PRIMARY KEY
4. FOREIGN KEY
5. CHECK

1. NOT NULL
o NULL means empty, i.e., the value is not available.
o Whenever a table's column is declared as NOT NULL, then the value for
that column cannot be empty for any of the table's records.
o There must exist a value in the column to which the NOT NULL
constraint is applied.
NOTE: NULL does not mean zero. NULL means empty column, not even zero.

NOT NULL:
Syntax to apply the NOT NULL constraint during table creation:
Query 1:
CREATE TABLE student
(StudentID INT NOT NULL,
Student_FirstName VARCHAR (20),
Student_LastName VARCHAR (20),
Student_PhoneNumber VARCHAR (20),
Student_Email_ID VARCHAR (40)
);
Page no:

Output:
Table Created

Query 2:
To verify that the not null constraint is applied to the table's column and the
student table is created successfully, we will execute the following query:

DESC student;
Output:

Query 3:
Syntax to apply the NOT NULL constraint on an existing table's column:
Syntax:
ALTER TABLE TableName CHANGE Old_ColumnName New_ColumnName
Datatype NOT NULL;

Example:
Consider we have an existing table student, without any constraints applied to it.
Later, we decided to apply a NOT NULL constraint to one of the table's
column. Then we will execute the following query:
Page no:

ALTER TABLE student CHANGE StudentID StudentID INT NOT NULL;

2. UNIQUE
o Duplicate values are not allowed in the columns to which the UNIQUE
constraint is applied.
o The column with the unique constraint will always contain a unique
value.
o This constraint can be applied to one or more than one column of a table,
which means more than one unique constraint can exist on a single table.
o Using the UNIQUE constraint, you can also modify the already created
tables.

Syntax to apply the UNIQUE constraint on a single column:

CREATE TABLE TableName


(ColumnName1 datatype UNIQUE,
ColumnName2 datatype….,
ColumnNameN datatype
);

Example:

Create a student table and apply a UNIQUE constraint on one of the table's
column while creating a table.

Case 1:

DROP TABLE STUDENT;

SHOW TABLES;

CREATE TABLE student

(StudentID INT UNIQUE,

Student_FirstName VARCHAR(20),

Student_LastName VARCHAR (20),

Student_PhoneNumber VARCHAR (20),

Student_Email_ID VARCHAR (40)


Page no:

);

Case 2:

ALTER TABLE student CHANGE StudentID StudentID INT UNIQUE;

DESC student;

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

CREATE TABLE TableName

(ColumnName1 datatype,

ColumnName2 datatype,….,

ColumnNameN datatype,

UNIQUE (ColumnName1, ColumnName 2, ColumnNameN));

Example:

Create a student table and apply a UNIQUE constraint on more than one table's
column while creating a table.

CREATE TABLE student


(StudentID INT,
Student_FirstName VARCHAR(20),
Student_LastName VARCHAR(20),
Student_PhoneNumber VARCHAR(20),
Student_Email_ID VARCHAR(40),
UNIQUE(StudentID, Student_PhoneNumber));
Page no:

To verify that the unique constraint is applied to more than one table's column
and the student table is created successfully, we will execute the following
query:
DESC STUDENT;

Output:

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

Syntax:

ALTER TABLE TableName ADD UNIQUE (ColumnName);

Example:

Consider we have an existing table student, without any constraints applied to it.
Later, we decided to apply a UNIQUE constraint to one of the table's column.
Then we will execute the following query:

ALTER TABLE student ADD UNIQUE (StudentID);

DESC STUDENT;
Page no:

3. PRIMARY KEY
o PRIMARY KEY Constraint is a combination of NOT NULL and Unique
constraints.
o NOT NULL constraint and a UNIQUE constraint together forms a
PRIMARY constraint.
o The column to which we have applied the primary constraint will always
contain a unique value and will not allow null values.

Syntax of primary key constraint during table creation:

CREATE TABLE TableName

(ColumnName1 datatype PRIMARY KEY,

ColumnName2 datatype,….,

ColumnNameN datatype

);

Example:

Create a student table and apply the PRIMARY KEY constraint while creating a
table.

CREATE TABLE student


(StudentID INT PRIMARY KEY,
Student_FirstName VARCHAR (20),
Student_LastName VARCHAR (20),
Student_PhoneNumber VARCHAR (20),
Student_Email_ID VARCHAR (40)
);
Page no:

To verify that the primary key constraint is applied to the table's column and the
student table is created successfully, we will execute the following query

DESC STUDENT;

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

Syntax:

ALTER TABLE TableName ADD PRIMARY KEY (ColumnName);


Example:
Consider we have an existing table student, without any constraints applied to it.
Later, we decided to apply the PRIMARY KEY constraint to the table's column.
Then we will execute the following query:
ALTER TABLE STUDENT DROP COLUMN STUDENTID;
ALTER TABLE STUDENT ADD COLUMN STUDENTID INT;

ALTER TABLE student ADD PRIMARY KEY (StudentID);


To verify that the primary key constraint is applied to the student table's
column, we will execute the following query:
DESC STUDENT;
Page no:

4. FOREIGN KEY
o A foreign key is used for referential integrity.

o When we have two tables, and one table takes reference from another
table, i.e., the same column is present in both the tables and that column
acts as a primary key in one table. That particular column will act as a
foreign key in another table.

Syntax to apply a foreign key constraint during table creation:

CREATE TABLE tablename

(ColumnName1 Datatype(SIZE) PRIMARY KEY,

ColumnNameN Datatype(SIZE),

FOREIGN KEY( ColumnName ) REFERENCES PARENT_TABLE_NAME


(Primary_Key_ColumnName));

Example:

Create an employee table and apply the FOREIGN KEY constraint while
creating a table.

To create a foreign key on any table, first, we need to create a primary key on a
table.

Example:

Create an employee table and apply the FOREIGN KEY constraint while
creating a table.

To create a foreign key on any table, first, we need to create a primary key on a
table.

CREATE TABLE employee


Page no:

(Emp_ID INT NOT NULL PRIMARY KEY,

Emp_Name VARCHAR (40),

Emp_Salary VARCHAR (40));

To verify that the primary key constraint is applied to the employee table's
column, we will execute the following query:

DESC STUDENT;

Now, we will write a query to apply a foreign key on the department table
referring to the primary key of the employee table, i.e., Emp_ID.

CREATE TABLE department

(Dept_ID INT NOT NULL PRIMARY KEY,

Dept_Name VARCHAR (40),

Emp_ID INT NOT NULL,

CONSTRAINT emp_id_fk FOREIGN KEY(Emp_ID) REFERENCES


employee (Emp_ID));

To verify that the foreign key constraint is applied to the department table's
column, we will execute the following query:

DESC department;
Page no:

Syntax to apply the foreign key constraint with constraint name:

CREATE TABLE tablename

(ColumnName1 Datatype PRIMARY KEY,

ColumnNameN Datatype (SIZE),

CONSTRAINT ConstraintName FOREIGN KEY( ColumnName ) REFERE


NCES PARENT_TABLE_NAME(Primary_Key_ColumnName));

Example:

Create an employee table and apply the FOREIGN KEY constraint with a
constraint name while creating a table.

To create a foreign key on any table, first, we need to create a primary key on a
table.

NOTE:

DROP DEPARTMENT;

DROP EMPLOYEE;

CREATE TABLE employee (Emp_ID INT NOT NULL PRIMARY KEY,


Emp_Name VARCHAR (40), Emp_Salary VARCHAR (40));
Page no:

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

ALTER TABLE Parent_TableName ADD FOREIGN KEY (ColumnName)


REFERENCES Child_TableName (ColumnName));

Example:

Consider we have an existing table employee and department. Later, we decided


to apply a FOREIGN KEY constraint to the department table's column. Then
we will execute the following query:

ALTER TABLE department ADD FOREIGN KEY (Emp_ID) REFERENCES


employee (Emp_ID);

5. CHECK
o Whenever a check constraint is applied to the table's column, and the user
wants to insert the value in it, then the value will first be checked for
certain conditions before inserting the value into that column.
o For example: if we have an age column in a table, then the user will
insert any value of his choice. The user will also enter even a negative
value or any other invalid value. But, if the user has applied check
constraint on the age column with the condition age greater than 18. Then
in such cases, even if a user tries to insert an invalid value such as zero or
any other value less than 18, then the age column will not accept that
Page no:

value and will not allow the user to insert it due to the application of
check constraint on the age column.

Syntax to apply check constraint on a single column:

CREATE TABLE TableName

(ColumnName1 datatype CHECK (ColumnName1 Condition),

ColumnName2 datatype,….,

ColumnNameN datatype)

);

Example:

Create a student table and apply CHECK constraint to check for the age less
than or equal to 15 while creating a table.

DROP TABLE STUDENT;

CREATE TABLE student

(StudentID INT,

Student_FirstName VARCHAR(20),

Student_LastName VARCHAR(20),

Student_PhoneNumber VARCHAR(20),

Student_Email_ID VARCHAR(40),

Age INT CHECK( Age <= 15)

);

DESC STUDENT;

Output:
Page no:

Syntax to apply check constraint on multiple columns:

CREATE TABLE TableName

(ColumnName1 datatype,

ColumnName2 datatype CHECK (ColumnName1 Condition AND ColumnNa


me2 Condition),….,

ColumnNameN datatype)

);

Example:

Create a student table and apply CHECK constraint to check for the age less
than or equal to 15 and a percentage greater than 85 while creating a table.

DROP TABLE STUDENT;

CREATE TABLE student

(StudentID INT,

Student_FirstName VARCHAR(20),

Student_LastName VARCHAR(20),

Student_PhoneNumber VARCHAR(20),

Student_Email_ID VARCHAR(40),

Age INT, Percentage INT,


Page no:

CHECK( Age <= 15 AND Percentage > 85)

);

DESC STUDENT;

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

ALTER TABLE TableName ADD CHECK (ColumnName Condition);

Example:

Consider we have an existing table student. Later, we decided to apply the


CHECK constraint on the student table's column. Then we will execute the
following query:

ALTER TABLE student ADD CHECK ( Age <=15 );


To verify that the check constraint is applied to the student table's column, we
will execute the following query:
DESC STUDENT;
Page no:

You might also like