DBMS LAB MANUAL
DBMS LAB MANUAL
Course Overview:
The purpose of this course is to provide a clear understanding of fundamentals with emphasis on their
applications to create and manage large data sets. It highlights on technical overview of database software to retrieve
data from n database. The course includes database design principles, normalization, concurrent transaction
processing,, security, recovery and file organization techniques.
CourseObjectives:
• IntroduceERdatamodel,database designandnormalization
• LearnSQL basicsfordata definition and datamanipulation
CourseOutcomes:
• Designdatabaseschemafor agivenapplicationandapply normalization
• Acquireskillsinusing SQLcommandsfordata definitionand datamanipulation.
• Developsolutionsfordatabaseapplicationsusingprocedures,cursorsandtriggers
ListofExperiments:
1. ConceptdesignwithE-RModel
2. Relational Model
3. Normalization
4. PracticingDDLcommands
5. PracticingDMLcommands
6. Querying(usingANY,ALL,IN,Exists,NOTEXISTS,UNION,INTERSECT,Constraintsetc.)
7. QueriesusingAggregatefunctions,GROUPBY,HAVINGandCreationanddroppingofViews.
8. Triggers(Creationof inserttrigger,deletetrigger,updatetrigger)
9. Procedures
10. UsageofCursors
TEXTBOOKS:
1. DatabaseManagementSystems,RaghuramaKrishnan,JohannesGehrke,TataMcGrawHill,3rd Edition
2. DatabaseSystemConcepts,Silberschatz,Korth,McGrawHill,Vedition.
REFERENCESBOOKS:
1. DatabaseSystemsdesign,Implementation,andManagement,PeterRob&CarlosCoronel7thEdition.
2. FundamentalsofDatabaseSystems,ElmasriNavrate,PearsonEducation
3. IntroductiontoDatabaseSystems, C.J.Date,PearsonEducation
4. OracleforProfessionals,The XTeam,S.Shah andV.Shah,SPD.
5. DatabaseSystemsUsingOracle:ASimplifiedguide toSQLandPL/SQL,Shah,PHI.
6. FundamentalsofDatabaseManagementSystems, M.L. Gillenson,WileyStudentEdition.
EXPERIMENT- 1
CONCEPT DESIGN WITH E-R MODEL
AIM: To Relate the entities appropriately. Apply cardinalities for each relationship. Identify
strong and weak entities. Indicate the type of relationships (total/partial). Incorporate
generalization, aggregation and specialization etc wherever required.
E-R Model
Student (Entity)
Last_
First Nam
_Na e Ci St
me Stud
Na ent t at
m id Ady ePin
e cod
dre e
Stu ss
den
t
Pho
ne D A
no O g
B e
Faculty : (Entity)
Last_N
First_N ame
ame
Facul
Na ty id
me
Depart
ment
F
a
c
Phon
e no u
Sal
l
ary
t
y
Course_Nam Course_id
e
Department
Course
Department : (Entity)
Department_id
Department_Name
Department
Subjects : (Entity)
Subject_id
Subject_Name
Subjects
Exams : (Entity)
Exam_code
Room_no
Time
Date
Exams
NRCM-CSE Mohd Nawazuddin-Asst Prof
Hostel : (Entity)
Hostel_name
Hostel_id
No_of_seats
Hostel
AIM: To Represent all the entities (Strong, Weak) in tabular fashion. Represent relationships in
a tabular fashion.
SCHEMA:
Mysql>create table student(Student_Id integer primary
key,First_Name Varchar(20) not null,Last_Name varchar(20) not
null,DOB date,Age int,phone_number int,city varchar(20),state
varchar(20),pincode int);
Mysql>desc student;
SCHEMA:
Mysql>desc faculty;
SCHEMA:
mysql>desc course;
DEPARTMENT:
SCHEMA:
SUBJECT:
SCHEMA:
EXAMS:
HOSTEL:
SCHEMA:
NORMALIZATION
AIM: Apply the database Normalization techniques for designing relational database tables to minimize
duplication of information like 1NF, 2NF, 3NF, BCNF.
Normalization is a process of converting a relation to be standard form by decomposition a larger relation into
smaller efficient relation that depicts a good database design.
1NF: A Relation scheme is said to be in 1NF if the attribute values in the relation are atomic .i.e., Mutlivalued
attributes are not permitted.
2NF: A Relation scheme is said to be in 2NF,iff and every Non-key attribute is fully functionally dependent on
primary Key.
3NF: A Relation scheme is said to be in 3NF,iff and does not have transitivity dependencies. A Relation is said
to be 3NF if every determinant is a key for each & every functional dependency.
BCNF: A Relation scheme is said to be BCNF if the following statements are true for eacg FD P->Q in set F of
FDs that holds for each FD. P->Q in set F of FD’s that holds over R. Here P is the subset of attributes of R & Q
is a single attribute of R.
The given FD is a trival
P is a super key.
Consider the Faculty table, with the primary key underlined, and the following data:
Faculty:
Faculty_Id Faculty_Name Skills
1 Sri C,C++
2 Ram Java
3 Abhi C,Java
A. No it is not in 1NF,As skills attribute in above table has multi value attributes. In 1NF only atomic i.e.
single value are allowed.
A. So solution to bring table to 1NF, here we need to decompose the table as following.
Consider the Students table, with the primary key underlined, and the following data:
Students:
Student_Id Student_Name Student_Address Course_Name Date of completion
A. The above given table is in 1NF but not in 2NF. Because STUDENT_NAME, STUDENT_ADDRESS
depends on STUDENT_ID but not on COURSE_NAME that makes a partial dependency and partial
dependency are not allowed in 2NF .
Consider the Employee table, with the primary key underlined, and the following data:
Employee:
Emp_no Emp_Name Address Salary Company_Name Location
b) If the Employee table is not in 3NF, redesign or decompose the tables such that all the information
currently in the Employee table is found in the resulting tables, and the resulting tables are in 3 NF. For
each of the resulting tables, give the table name, column names, primary keys, and foreign keys.
A. To covert relation to 3NF,we should decompose as following.
The determinant attribute(here Teacher) has to be a super key.But here Teacher is not a superkey.
STUDENTSTUDENT_ID TEACHER
DDL Commands:
DDL means Data Definition Language. It is used to create an object, alter the structure of an
object and also drop already created object.
The Data Definition Languages used for table definition can be classified into following:
• Rename
SQL - CREATE TABLE: Table is a primary object of database, used to store data in form of
rows and columns.
Syntax:
CREATE TABLE tablename (column_name data_ type constraints, …)
Example:
MYSQL > CREATE TABLE STUDENTS ((SID INT PRIMARY KEY, SNAME VARCHAR
(10), BRANCH VARCHAR (10), AGE INT );
Table Created.
Desc command
The DESCRIBE command is used to view the structure of a table as follows.
MYSQL>DESC STUDENTS;
EXERCISE:
2. ALTER TABLE :
To ADD a column:
Sol:
SYNTAX:
MYSQL> RENAME table oldtablename TO newtablename;
EXERCISE:
1. PRACTICE THE COMMAND BY RENAMING THE ALREADY CREATED TABLES
4. TRUNCATE A TABLE
SYNTAX:
MYSQL> TRUNCATE TABLE tablename;
5. DROP A TABLE
SYNTAX:
MYSQL> DROP TABLE tablename;
EXERCISE:
1. PRACTICE THE COMMAND BY DROPING THE ALREADY CREATED TABLES
DML: Data Manipulation Language (DML) statements are used for managing data within
schema objects and to manipulate data of a database objects.
DELETE - deletes all records from a table, the space for the records remain
OR
INSERT INTO TABLE_NAME VALUES(value1, value2, value3,...valueN);
Exercise:
Insert any five records into the tables STUDENTS, FACULTY, EMPLOYEE.
STUDENTS
+ + + + +
| SID | SNAME | BRANCH | AGE |
+ + + + +
| 101 | RAHUL | CSE | 19 |
| 102 | AJAY | CSE | 20 |
| 103 | VINAY | ECE | 18 |
| 104 | VICKY | ME | 20 |
| 105 | VIJAY | EEE | 20 |
+ + + + +
Exercise:
1. Update the job = trainee of employee with empno=12005.
Exercise:
practice the delete command on previous created table.
A. Data Manipulation Language which deals with data manipulation and it is used to store,
modify, retrieve, delete and update data in a database.
A. DDL is Data Definition Language which is used to define data structures. For example:
create table, alter table are instructions in MYSQL.
DML is Data Manipulation Language which is used to manipulate data itself. For example:
insert, update, delete are instructions in MYSQL.
AUPDATE table_name
SET column1 = value1, column2 = value2. .. , columnN = valueN
WHERE [condition];
A. It deletes the record from given table name with help of where condition.
A. Update command are used along with SET and WHERE conditions.
A.SELECT.
A. DELETE is a Data Manipulation Language command, DML command and is used to remove
tuple /records from a relation/table. Whereas DROP is a Data Definition Language, DDL
command and is used to remove named elements of schema like relations/table, constraints or
entire schema
F.K P.K
EID PID PNAME LOCATION
E01 P1 IOT BANGALORE
E03 P3 BIG DATA DELHI
E04 P4 RETAIL MUMBAI
E05 P2 ANDROID HYDERABAD
1.Find the detail of the employees who is working on at least one project.
SELECT EID, FIRSTNAME, LASTNAME FROM EMPLOYEE WHERE EID = ANY
(SELECT EID FROM PROJECT);
5. Find the detail of the employees who is working on at least one project.
SELECT EID, FIRSTNAME, LASTNAME FROM EMPLOYEE WHERE EXISTS
(SELECT EID FROM PROJECT WHERE EMPLOYEE.EID = PROJECT.EID );
TABLE: STUDENT2
1.UNION:
SELECT SNAME FROM STUDENT1 UNION SELECT SNAME FROM STUDENT2;
SELECT SNAME FROM STUDENT1 UNION ALL SELECT SNAME FROM STUDENT2;
By giving union we won’t get any duplicate values in result. while with union all it gives duplicate values also in
results.
Now try to insert age less than 18 i.e.,17 in student1 table. It won’t allow user to enter age of student less than 18
EXPERIMENT – 7
Queries using Aggregate functions, GROUP BY, HAVING and Creation and dropping of
Views.
Aim: To Practice Queries using Aggregate functions for the following
TABLE: EMPINFO
EID EMPLOYEENAME JOB MGR HIREDATE SALARY COMMISSION DEPTNO
ID
1001 ANIL MANAGER NULL 03/03/2010 35000 NULL 10
1002 AKHIL CLERK 1001 02/04/2015 25000 NULL 10
NRCM-CSE Mohd Nawazuddin-Asst Prof
1003 VINOD SALES 1001 05/06/2016 18000 1800 10
1004 VIKAS SALES 1001 06/07/2016 16000 1600 10
1005 SUNIL MANAGER NULL 03/04/2011 30000 NULL 20
1006 KIRAN CLERK 1005 05/06/2016 20000 NULL 20
1007 AREEB SALES 1005 10/05/2016 15000 1500 20
AGGREGRATE FUNCTIONS:
1.Write a query to get total number of employees working in organization.
4.Write a query to get details of employee whose salary is maximum amongst all employees.
5.Write a query to get details of employee whose salary is minimum amongst all employees.
3.WAQ to get salary in ascending order for the employees belonging to department number 10.
4.WAQ to create duplicate table and store the records based on salary wise.
GROUPBY, HAVING:
1.WAQ to get count of employees from each department.
5.WAQ to get count of employees from each department only when the count is greater than 3.
GOODSTUDENTS whose grade is ‘A’ and view as average AVGSTUDENTS whose grade is
‘B’.
EXPERIMENT – 8
TRIGGERS
Aim: Creation of insert trigger, delete trigger and update trigger.
A trigger is a procedure that is automatically invoked by the DBMS in response to specified changes to the database.
Six types of actions or events in the form of triggers:
• Before Insert: It is activated before the insertion of data into the table.
• After Insert: It is activated after the insertion of data into the table.
• Before Update: It is activated before the update of data in the table.
• After Update: It is activated after the update of the data in the table.
• Before Delete: It is activated before the data is removed from the table.
TABLE: EMPINFO
PK
EID EMPLOYEENAME JOB MGR HIREDATE SALARY COMMISSION DEPTNO
ID
1001 ANIL MANAGER NULL 03/03/2010 35000 NULL 10
1002 AKHIL CLERK 1001 02/04/2015 25000 NULL 10
1003 VINOD SALES 1001 05/06/2016 18000 1800 10
1004 VIKAS SALES 1001 06/07/2016 16000 1600 10
1005 SUNIL MANAGER NULL 03/04/2011 30000 NULL 20
1006 KIRAN CLERK 1005 05/06/2016 20000 NULL 20
1007 AREEB SALES 1005 10/05/2016 15000 1500 20
BEFORE INSERT: Creating a trigger not to allow any insertion in EMPINFO table:
Now check whether the trigger is invoked or not by inserting values in EMPINFO table
----------------------------------------------------------------------------------------------------------------------------- ------------------
-------------------------------------------------------------------------------------------
AFTER INSERT: creating a trigger FOR any insertion in EMPINFO table copies the data to another table
EMPINFOAUDIT.
CREATE A TABLE EMPINFOAUDIT:
Now check whether the trigger is invoked or not by inserting values in EMPINFO table
----------------------------------------------------------------------------------------------------------------------------- ------------------
-------------------------------------------------------------------------------------------
BEFORE UPDATE: creating a trigger that not allows to modify manager or clerk details:
Now check whether the trigger is invoked or not by updating values in EMPINFO table
Now check whether the trigger is invoked or not by updating values in EMPINFO table
-----------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
BEFORE DELETE: creating trigger which not allows to delete more than one row from table.
Now check whether the trigger is invoked or not by deleting multiple rows in EMPINFO table
Now check by deleting single row.it should delete a single row as we written trigger for multiple rows i.e., more than 1
row.
Now check whether the trigger is invoked or not by deleting rows in EMPINFO table and also check the
EMPINFOAUDIT table to check whether it copied the deleted row.
EXPERIMENT – 9
PROCEDURES
Aim: Creation of stored Procedures and Execution of Procedures and Modification of
Procedures.
NRCM-CSE Mohd Nawazuddin-Asst Prof
1.Create a procedure to print sum of two values.
2.Create a procedure to accept EID and display info of Employee from EMPINFO table.
5.Create a procedure to accept EID and delete the record from EMPINFO Table.
6.Create a procedure to check duplicate EID while ADDING Record in EMPINFO Table.
NRCM-CSE Mohd Nawazuddin-Asst Prof
NRCM-CSE Mohd Nawazuddin-Asst Prof
EXPERIMENT – 10
CURSORS
Aim: Declare a cursor that defines a result set. Open the cursor to establish the result set. Fetch the data into local
variables as needed from the cursor, one row at a time. Close the cursor when done.
MySQL Cursor
Declare Cursor. A cursor is a select statement, defined in the declaration section in MySQL.
Open Cursor. After declaring the cursor, the next step is to open the cursor using open statement.
Fetch Cursor. After declaring and opening the cursor, the next step is to fetch the cursor. ...
Close Cursor. The final step is to close the cursor.
Declare Cursor
Syntax
DECLARE cursor_name CURSOR FOR Select statement;
Open Cursor
Syntax
Open cursor_name;
Fetch Cursor
Syntax
FETCH <cursor_name> INTO <variable_list>;
Close Cursor
Syntax
Close cursor_name;
1.Create a CURSOR to display records i.e., Empname and salary from EMPINFO table.
First create an empty table as EMPBACKUP in which you want to copy data.