DBMS Exp-03 Sem-Iii Mumbai University

You might also like

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

DEPARTMENT OF INFORMATION TECHNOLOGY

ITL302 SQL Lab


Third Semester, 2021-2022 (Odd Semester)

Name of Student : ROHAN YADAV

Roll No. : 63

Division :D

Batch : D3

Day / Session :

Venue : Online

Experiment No. : 03

Title of Experiment : Creating a Database using DDL and apply Integrity Constraints

Date of Conduction :

Date of Submission : 28/11/2021

Max. Marks
Particulars
Marks Obtained
Preparedness and Efforts(PE) 3
Knowledge of tools(KT) 3
Debugging and results(DR) 3
Documentation(DN) 3
Punctuality & Lab Ethics(PL) 3
Total 15
Grades – Meet Expectations (3 Marks), Moderate Expectations (2 Marks), Below Expectations (1 Mark)

Checked and Verified by

Name of Faculty : Prof. Saurabh Suman


Signature :
Date :
EXPERIMENT NO. 04
AIM : Create a Database using DDL and apply Integrity Constraints .
THEORY:

DDL (Data Definition Language) :


DDL or Data Definition Language actually consists of the SQL commands that can be used to
define the database schema. It simply deals with descriptions of the database schema and is
used to create and modify the structure of database objects in the database.DDL is a set of
SQL commands used to create, modify, and delete database structures .
1) CREATE:
Types of DDL commands
This command is used to create the database or its objects (like table, index,
function, views, store procedure, and triggers).

There are two CREATE statements available in SQL

I. CREATE DATABASE:

SYNTAX:

CREATE DATABASE database_name;

INPUT/ EXAMPLE

OUTPUT:
'"l ' , • • (- , t"� \j •'-- ..-� ...
J - ,_, •
,) (.IJ •- c1 -
• - - IL •
� '::.>.t--' ; •..., , .::j ''
J •
I
�) J •• • ' 1 J Ii� ' 1 r I I f (j .. t •• , ♦ •' � �� 0 ') �1 t' \

II. CREATE TABLE:

SYNTAX:

CREATE TABLE table_name

columnl data_type(size),
column2 data_type(size),
column3 data_type(size),
INPUT/EXAMPLE:
Create Table Books

Id int Primary Key


Name varchar(20),
Price int

2) DROP:

This command is used to delete objects from the database.

SYNTAX:

DROP object object_name


Examples:
DROP TABLE table_name;
DROP DATABASE database_name;

INPUT/EXAMPLE:

Drop Table Books

3) ALTER
This is used to alter the structure of the database.

SYNTAX:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnnamen datatype)

INPUT/EXAMPLE:
Alter table books
add Author varchar (20) ;
OUTPUT
d Name Pnice Autnor

Disney 100

Harry Potter ul

Arabian Nights 200 ull

Donald 400

Constraints in DBMS
Relational constraints are the restrictions imposed on the database contents and
operations. They ensure the correctness of data in the database.

Iypes of Constraintsin DBMS:


In DBMS, there are following 4 different types of relational constraints
1) Domain constraint
2) Key constraint
3) Entity Integrity constraint
4) Referential Integrity constraint

Domain Constraint:
Domain constraint defines the domain or set of values for an attribute.
It specifies that the value taken by the attribute must be the atomic value from
its domain.

EX: Consider the following Student table

STUID Name Age

SO01 Akshay 0

SO02 Abhishek
SO03 Shashank 20

SO04 Rahul

Here, value 'A' is not allowed since only integer values can be taken by the age
attribute.
INPUT/EXAMPLE:

Insert into Books (Td, Name, Price)


Values(4,'Donald',A)

QUTPUT:

Eror 1: could not prepare statement (1 no such column: A)

OK

2) Key Constraint:

Key constraint specifies that in any relation-


All the values of primary key must be unique.
The value of primary key must not be nul.

EX: Consider the following Student table

STU_ID Name Age

S001 Akshay 20

SO02 Abhishek

SO05 Shashank 20

S004 Rahul 20

This relation does not satisfy the key constraint as here all the values of primary
key are not unique

INPUT/EXAMPLE:

Insert into Books (Id, Name, Price)


Values(1, 'Arabian', 300);
OUTPUT:
Eror 1: could not execute statement due to a constraint failure (19
UNIQUE Constraint failed: Books.ld)

3) EntityIntegrityConstraint:
Entity integrity constraint specifies that no attribute of primary key must contain a
null value in any relation.

This is because the presence of null value in the primary key violates the uniqueness
property.

EX: Consider the following Student


table
STU_ID Name
Age
S001 Akshay 20

sO02 Abhishek
SO03 Shashank

Rahul 20

This relation does not satisfy the entity integrity constraint as here the primary key
contains a NULL value.

INPUT/EXAMPLE:

Insert into Books (Name , Price)


Values ("Arabi an', 300);

OUTPUT:
Error 1: could not execute statement due to a constraint failure (19 NOT
NULL constraint failed: Books.ld)

OK
4) ReferentialIntegrity Constraint
This constraint is enforced when a foreign key references the primary key of a relation.

It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.

cONCLUSION
We have learn different DDL Commands and their uses and different types of Integrity
Constraints in SQL.

You might also like