Sunteți pe pagina 1din 12

National College of Computer Studies

Paknajol,Kathmandu

Report on Database Presentation


‘Integrity Constraints Its Types’

Submitted By: Submitted To:


Aaditya Manandhar Sudarsan Nepal
Anish Krisnan (Internal)
Anuj Shrestha External Examiner
Bijay Maharjan
Acknowledgement

First and foremost we would like to extend our heartful thankness to


our subject teacher Surdsan Nepal who gave us this golden
oppurtunity

(Internal Examiner) (External Examiner)


Table of Contents
Introduction To Constraints

Simply we can understand constraints as the basic rules that must be


followed in database. We need to define constraints before inserting
the datas in our database. In the language of database we can define
constraints as rules to enforce on the datas to be inserted in database.
They are just the rules enforced on data columns on the table or
relations in database.
If data are inserted beyond the limit of the
constraints defined then such data are discarded from the database.
The main purpose of Constraints is to maintain the data integrity. The
data integrity means to ensure the accuracy and consistency of datas
over its life cycle. It helps to limit the type of datas that goes into a
table. It also helps to prevent the violation of the data and the defined
constraints.
For e.g. If we try to insert the integer data in character data type then
there will be violation in the data which will be prevented by
constraints.
Example and Syntax of Constraints
Syntax:
 CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);

Example:
CREATE TABLE Student
(
Sid INTEGER(10) PRIMARY KEY,
Firstname VARCHAR(30) NOT NULL,
Lastname VARCHAR (30) NOT NULL
);

Description of example
Here, in the example above the Sid which is Integer data type is
primary key that means it must be a unique one, it shouldn’t be
repeated values. If we keep repeated value then SQL won’t accept it.
Similarly incase of Firstname and Lastname too the first name and
lastname of person which must be in character must not be a null
value that means it can’t be empty.
Some of the Constraints used in SQL

Some of the basic constraints which are used in SQL are as follows:
a. NOT NULL
It specifies that the column cannot store the
NULL value. Some value must be provided in
particular column.

b. UNIQUE
Specifies that each row for a column must have
a unique value. It works just like PRIMARY
key

c. FOREIGN KEY
Referential integrity of data in one table
to match value in the next table.
d. DEFAULT
Specifies the default value in the column.
The column specified as default will
assign the default value when the value is
empty
e. PRIMARY KEY
Used for uniquely identifying the tuples
(rows) in the relation of the database.
Each key must be unique one.
Why to Use Constraints in Database?

After being familiar with the constraints one question may come in
our mind why are we using the constraints? Well the answer to the
question may be by showing some uses of the constraints :
a. Protect Integrity of database
As constraints are the rules employed in database, it
helps in protecting the integrity of database. It allows only the
valid and accurate data maintain the integrity of database.
 he values of individual columns are valid.
 in a table, rows have a valid primary key or unique key values.
 in a dependent table, rows have valid foreign key values that reference rows in a
parent table.
Integrity Constraints

Talking so much about the constraints now let us head towards


Integrity Constraints. So, what is Integrity constraint then?
The integrity constraints are also known as referential integrity
constraints. They ensure that the changes made to database do not
result loss of data consistency. It is imposed or used so as to restrict
on allowable data in database.

Types of Integrity Constraints

We can divide the integrity constraints into two types:


1. Referential Integrity Constraints
2. Entity Integrity Constraints

We have discussed about both types of cintegrity constraints


with suitable example and proper explanation

1. Referential Integrity Constraints

As the name suggests, the refrential Integrity constraints talks


about the reference that means referencing the table. Inorder
to refrence the table we need foreign key so this types talks
about the foreign key. According to this constraint there must
be every value of one column of relation to exists as value of
another column in different table i.e. relation. It defines the
relation between the tables. The foreign key must refrence to
a valid primary key in parent table.
Example of Referential Integrity Constraint

In order to clearly understand about Referential Integrity Constraint


the following example might come in handy

Primary Key Foreign Key


(Table 1)

Eno Name Age Dno


1 Ram 19 10
2 Kabir 18 11
3 Gita 22 14
4 Sanket 19 10

Primary Key
(Table 2)
Dno D.location
10 Block A
11 Block B
12 Block C

Here in the above example there are two tables table 1 and table 2
where Eno is primary key and Dno is Foreign key in Table 1. The
record of Eno 3 is not allowded as Dno 14 is not defined as a primary
key of table 2 and in table 1 Dno is a foreign key defined. This record
cannot be referencing the D.location from table 2.
2. Entity Integrity Constraints

This integrity constraint refers about the primary key which


uniquely identifies each tuples in a database. It states that no
attributes of primary key is null that means the primary key
cannot be a null value nor repeating value. Likewise the
relation in each database must contain a primary key. This
constraint will help to avoid any datas that violates the rules
of the primary key.

Example of Entity integrity constraints

(Table 1)

SID Name Semester Age


8001 Ankit 1st 21
8002 Smriti 2nd 18
8003 Saurav 4th 22
Ramseh 6th 25

Sid Name Semester Age


8001 Ankit 1st 21
8002 Smriti 2nd 20
8003 Saurav 3rd 25
8002 Tony 6th 26
(Table 2)
In this example two tables table 1 and 2 are given where primary
key is sid. In table 1 the primary key is empty which is not allowed
similarly in table 2 the primary key is repeating which is also not
allowed as it needs to be unique and non repeated. Such types of
constraints are handled by Entity Integrity Constraints.
Conclusion
References

In order to complete this report we have use the following references


which provided us great help

 www.tutorialspoint.com/sql/sql-constraints.htm
 beginnersbook.com/2015/04/constraints-in-dbms/
 www.w3schools.com/sql/sql_constraints.asp
 http://www.edugrabs.com/types-of-constraints-in-dbms-or-
relational-constraints/
 https://www.youtube.com/watch?v=zvYU9vlvDNY
 https://en.wikipedia.org/wiki/Entity_integrity

S-ar putea să vă placă și