0% found this document useful (0 votes)
56 views5 pages

University of Information Technology & Sciences: Course Name:-Database Management System Lab

The document discusses different types of constraints that can be used in relational database management systems (RDBMS) to enforce data integrity. It describes NOT NULL, UNIQUE, DEFAULT, CHECK, PRIMARY KEY, FOREIGN KEY constraints and provides examples of each. It also covers domain constraints, mapping constraints such as one-to-one, one-to-many, many-to-one and many-to-many relationships, and how to implement them in tables through primary and foreign keys.

Uploaded by

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

University of Information Technology & Sciences: Course Name:-Database Management System Lab

The document discusses different types of constraints that can be used in relational database management systems (RDBMS) to enforce data integrity. It describes NOT NULL, UNIQUE, DEFAULT, CHECK, PRIMARY KEY, FOREIGN KEY constraints and provides examples of each. It also covers domain constraints, mapping constraints such as one-to-one, one-to-many, many-to-one and many-to-many relationships, and how to implement them in tables through primary and foreign keys.

Uploaded by

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

University of Information Technology &

Sciences

Course Name:- Database Management System Lab


Course Code:- CSE-302

Assignment 02

Assignment Topic:- Constraint Key

Submitted to:- Tania Akter Setu


Lecturer of Dept. of CSE, UITS

Submitted by:-
Shakilur Rahman
ID:-1814351038
Section:- B
Dept. Of CSE

Submitted Date:- 13/06/2020


Constraints keys
Constraints enforce limits to the data or type of data that can be
inserted/updated/deleted from a table. The whole purpose of constraints is to
maintain the data integrity during an update/delete/insert into a table. In this
tutorial we will learn several types of constraints that can be created in RDBMS.

Types of constraint
• NOT NULL
• UNIQUE
• DEFAULT
• CHECK
• Key Constraints – PRIMARY KEY, FOREIGN KEY
• Domain constraints
• Mapping constraints

NOT NULL:
NOT NULL constraint makes sure that a column does not hold NULL value.
When we don’t provide value for a particular column while inserting a record into
a table, it takes NULL value by default. By specifying NULL constraint, we can be
sure that a particular column(s) cannot have NULL values.

Example:

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (235),
PRIMARY KEY (ROLL_NO)
);

UNIQUE:
UNIQUE Constraint enforces a column or set of columns to have unique values.
If a column has a unique constraint, it means that particular column cannot have
duplicate values in a table.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);

DEFAULT:
The DEFAULT constraint provides a default value to a column when there is no
value provided while inserting a record into a table.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO)
);

CHECK:

This constraint is used for specifying range of values for a particular column of a
table. When this constraint is being set on a column, it ensures that the specified
column must have the value falling in the specified range.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL CHECK(ROLL_NO >1000) ,
STU_NAME VARCHAR (35) NOT NULL,
STU_AGE INT NOT NULL,
EXAM_FEE INT DEFAULT 10000,
STU_ADDRESS VARCHAR (35) ,
PRIMARY KEY (ROLL_NO)
);
In the above example we have set the check constraint on ROLL_NO column of
STUDENT table. Now, the ROLL_NO field must have the value greater than
1000.

Key constraints:
PRIMARY KEY:
Primary key uniquely identifies each record in a table. It must have unique values
and cannot contain nulls. In the below example the ROLL_NO field is marked as
primary key, that means the ROLL_NO field cannot have duplicate and null
values.

CREATE TABLE STUDENT(


ROLL_NO INT NOT NULL,
STU_NAME VARCHAR (35) NOT NULL UNIQUE,
STU_AGE INT NOT NULL,
STU_ADDRESS VARCHAR (35) UNIQUE,
PRIMARY KEY (ROLL_NO)
);
FOREIGN KEY:
Foreign keys are the columns of a table that points to the primary key of another
table. They act as a cross-reference between tables.

Domain constraints:
Each table has certain set of columns and each column allows a same type of
data, based on its data type. The column does not accept values of any other
data type.
Domain constraints are user defined data type and we can define them like this:

Domain Constraint = data type + Constraints (NOT NULL / UNIQUE / PRIMARY


KEY / FOREIGN KEY / CHECK / DEFAULT)

Mapping constraints:
Mapping Cardinality:
One to One: An entity of entity-set A can be associated with at most one entity of
entity-set B and an entity in entity-set B can be associated with at most one entity
of entity-set A.

One to Many: An entity of entity-set A can be associated with any number of


entities of entity-set B and an entity in entity-set B can be associated with at most
one entity of entity-set A.
Many to One: An entity of entity-set A can be associated with at most one entity
of entity-set B and an entity in entity-set B can be associated with any number of
entities of entity-set A.

Many to Many: An entity of entity-set A can be associated with any number of


entities of entity-set B and an entity in entity-set B can be associated with any
number of entities of entity-set A.

We can have these constraints in place while creating tables in database.

Example:

CREATE TABLE Customer (


customer_id int PRIMARY KEY NOT NULL,
first_name varchar(20),
last_name varchar(20)
);

CREATE TABLE Order (


order_id int PRIMARY KEY NOT NULL,
customer_id int,
order_details varchar(50),
constraint fk_Customers foreign key (customer_id)
references dbo.Customer
);
Assuming, that a customer orders more than once, the above relation
represents one to many relation. Similarly we can achieve other mapping
constraints based on the requirements.

You might also like