Domain Constraints, Relational Database Schemas
Domain Constraints, Relational Database Schemas
Domain Constraints, Relational Database Schemas
Subject : DBMS
Subject Code: BCA-S301T
Faculty: Saurabh Jha
Table / Relation
Relational Integrity Constraints
1. Domain Constraints
2. Key Constraints
3. Referential Integrity Constraints
Domain Constraints
Domain constraints can be violated if an attribute value is not appearing in
the corresponding domain or it is not of the appropriate data type.
Domain constraints specify that within each tuple, and the value of each
attribute must be unique. This is specified as data types which include
standard data types integers, real numbers, characters, Booleans, variable
length strings, etc.
Example:
Create DOMAIN CustomerName
CHECK (value not NULL)
Example:
In the given table, CustomerID is a key attribute of Customer Table.
It is most likely to have a single key for one customer, CustomerID
=1 is only for the CustomerName =" Google".
One important point to note about this constraint is that it cannot be defined at table
level.
The above query will declare that the s_id field of Student table will not take NULL
value.
UNIQUE Constraint
UNIQUE constraint ensures that a field or column will only have unique values. A UNIQUE constraint
field will not have duplicate data. This constraint can be applied at column level or table level.
The above query will declare that the s_id field of Student table will only have unique values and wont
take NULL value.
The above query specifies that s_id field of Student table will only have unique value.
Primary Key Constraint
Primary key constraint uniquely identifies each record in a database. A Primary Key must contain unique
value and it must not contain null value. Usually Primary Key is used to index the data inside the table.
To understand FOREIGN KEY, let's see its use, with help of the below tables:
Customer_Detail Table
c_id Customer_Name address
101 Adam Noida
102 Alex Delhi
103 Stuart Rohtak
Order_Detail Table
Order_id Order_Name c_id
10 Order1 101
11 Order2 103
12 Order3 102
Foreign Key Constraint
In Customer_Detail table, c_id is the primary key which is set
as foreign key in Order_Detail table.
If you try to insert any incorrect data, DBMS will return error
and will not allow you to insert the data.
Foreign Key Constraint
Using FOREIGN KEY constraint at Table Level
CREATE table Order_Detail
(
order_id int PRIMARY KEY,
order_name varchar(60) NOT NULL,
c_id int FOREIGN KEY REFERENCES Customer_Detail(c_id)
);
In this query, c_id in table Order_Detail is made as foriegn key, which is a
reference of c_id column in Customer_Detail table.
On Delete Cascade : This will remove the record from child table, if that value of
foreign key is deleted from the main table.
On Delete Null : This will set all the values in that record of child table as NULL, for
which the value of foreign key is deleted from the main table.
If we don't use any of the above, then we cannot delete data from the main table
for which data in child table exists.