DBMS Lab Manual
DBMS Lab Manual
Lab Manual
B.Tech II Year - IISemester
(A.Y. 2021-22)
DBMS LAB MANUAL
2 Relational Model
3 Normalization
9 Procedures
10 Usage of Cursors
1. Concept design with E-R Model
Aim: Analyze the problem and come with the entities in it. Identify what Data has to be
Persisted in the databases. 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.
Prerequisites: Student must know the entities and attributes and representation of diagrams/
Student must know the entities and attributes and relationship among entities
Bus
- BusNo
- Source
- Destination
- CoachType
Passenger
- PassportID
- Name
- Age
- Sex
- Address
Ticket
- TicketNo
- DOJ
- Age
- Sex
- Destination
- BusNo
Reservation
- PNRNo
- DOJ
- ContactNo
- No_of_seats
- BusNo
- Address
Cancellation
- PNRNo
- DOJ
- No_of_seats
- Status
- The attributes in the Entities:
Bus:( Entity)
Destination
Source
Couch Type
Bus No
Bus
- Reservation (Entity)
Contact No
Bus No
No-of-Seats
Journey date
Address
PNR NO
Reservation
- Ticket :(Entity)
Dep- Time
Source
Age
Sex
Journey date
Destination
Ticket No
Bus No
Ticket
Passenger:
Contact NO
Age
Sex
PNR NO
Name
Ticket No
Passenger
Cancellation (Entity)
Seat No
Journey date
Contact No
PNR NO
Cancellation
The Schemes for the following entities are:
Viva-Voce:
1. What is SQL?
2. Is it Case Sensitive or non Case Snsitive?
3. What is DBMS?
4. What is a Database system?
5. Advantages of DBMS?
6.How many types of database languages are available?
Conclusion: The Student is able design the concept design of Road-way Travels
Viva-Vice:
Aim: To Represent all the entities (Strong, Weak) in tabular fashion. Represent
relationships in a tabular fashion.
Bus:
Ticket:
Passenger:
Cancellation:
Viva-Voce:
4. What is VIEW ? and What will you get when you use VIEW
Aim: Apply the database Normalization techniques for designing relational database
tables to minimize duplication of information like 1NF, 2NF, 3NF, BCNF.
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 1GB RAM and 500 MB free disk space.
MySQL 5.6.1
1NF: A Relation scheme is said to be in 1NF if the attribute values in the relation are
atomic.i.e., Mutli –valued 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.
Source varchar(20),
Destination varchar(20),
coachType varchar(10),
PRIMARY KEY(BusNo));
2. Create table Passenger(PassportID varchar(15) NOT NULL,
Name varchar(20),
Age integer,
Sex varchar,
Address varchar(20),
ContactNo Varchar(12),
PRIMARY KEY(PassportID));
Dept_Time varchar(10),
PRIMARY KEY(TicketNo));
PRIMARY KEY(PassportID,TicketNo),
DOJ date,
No_of_seats int(2),
Address varchar(20),
ContactNo Varchar(10),
Status varchar(10),
PRIMARY KEY(PNR_NO));
DOJ Date,
No_of_Seats integer(2),
Address varchar(20),
ContactNo integer(12),
Status varchar(10),
Viva-Voce:
1. Define Normalization?
4. What is 2NF?
5. What is 3NF?
Source varchar(20),
Destination varchar(20),
coachType varchar(10),
PRIMARY KEY(BusNo));
Name varchar(20),
Age integer,
Sex varchar,
Address varchar(20),
ContactNo Varchar(12),
PRIMARY KEY(PassportID));
Mysql>Create table Ticket(TicketNo integer(10), NOT NULL,
Dept_Time varchar(10),
PRIMARY KEY(TicketNo));
PRIMARY KEY(PassportID,TicketNo),
DOJ date,
No_of_seats int(2),
Address varchar(20),
ContactNo Varchar(10),
Status varchar(10),
PRIMARY KEY(PNR_NO));
DOJ Date,
No_of_Seats integer(2),
Address varchar(20),
ContactNo integer(12),
Status varchar(10),
FOREIGN KEY(PNR_No) REFERENCES Reservation(PNR_No));
ALTER Table
Field Datatype
BusNo varchar(10),
Source varchar(20),
Destination varchar(20),
CoachType varchar(10),
Capacity integer(2)
Mysql> TRUNCATE TABLE BUS;
Conclusion: The Student is able Install the MySQL and Create database, Create
tables and Alter tables.
Viva Voce:
Aim : Create a DML Commands are used to manage data within the scheme objects.
DML Commands:
PassportID TicketNo
8738939 453
5443243 332
‘98202030334’);
Conclusion: The Student is able perform DML Commands like Insert, Update, Delete
and Select
Viva-Voce
Querying
PNR_No
783-32923
928-38983
Name
Smith
John
3. Display the ticket numbers and names of all the passengers.
4. Find the ticket numbers of the passengers whose name start with ‘r’ and ends with ‘h’.
Name
Rajesh
Ramesh
5. Find the names of Passengers whose age is between 30 and 45.
Name
neha
Ramu
6. Display all the passengers names beginning with ‘A’.
Name
Akash
Avinash
Arivind
Name
Akash
Aravind
Avinash
Neha
Ramu
smith
Conclusion: The Student is able execute the Queries from above database.
Viva-Vice:
1. What is the result of String functions?
4. What is Concatenation?
Set Operators
Set operators combine the results of two queries into a single one. The following set
operators are availablein SQL.
Union
Union All
Intersect
Minus
7. Queries using Aggregate functions, Group By, Having Clause and Order
Clause creation and dropping of views.
1. Write a Query to display the information present in the passenger and cancellation
tables
2. Display the number of days in a week on which the AP123 bus is available
3. Find number of tickets booked for each PNR_No using GROUP BY CLAUSE
4. Find the distinct PNR Numbers that are present.
1. Write a Query to display the information present in the passenger and cancellation
tables
MINAGE
23
3. Find number of tickets booked for each PNR_No using GROUP BY CLAUSE
PNR_NO SUM_OF_SEATS
78393445 5
78393930 4
4. Find the distinct PNR Numbers that are present.
PNR_NO
78393445
78393930
SUM(NO_OF_SEATS)
----------------
6
To Practice Queries using Aggregate functions for following
Find the number of tickets booked by a passenger where the number of seats is
greater than 1.
Find the number of tickets booked by a passenger where the number of seats is
greater than 1.
CANCELLATION;
CANCELLED_SEATS
03
Find the No of Seats booked for each PNR_NO using GROUP BY Clause.
REASON: because all pnr_no are foreign keys, so all are unique.
a) CREATE VIEW
Create view pass_reserve as select * from Passenger full natural join Reservation;
c) INSERT
d) DROP VIEW
BEGIN
SET action='update',
source=OLD.source,
changedon=NOW();
END$$
i)UPDATE :
BEGIN
SET action='Insert',
source=NEW.source,
changedon=NOW();
END$$
BEGIN
SET action='Insert',
source=NEW.source,
changedon=NOW();
END$$
Conclusion: The Student is able to work on Triggers and create active database.
Viva-Vice:
1. What is TRIGGER?
Ex1:
BEGIN
END$$
CALL BUS_PROC1()$$
BEGIN
DECLARE X INT(3);
SET X=10;
SELECT X;
END$$
CALL SAMPLE2()$$
BEGIN
END$$
CALL SIMPLE_PROC(@a)$$
PARAM1
4
Viva Voce:
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.
Cursors
In MySQL, a cursor allows row-by-row processing of the result sets. A cursor is used for the
result set and returned from a query. By using a cursor, you can iterate, or by step through the
results of a query and perform certain operations on each row. The cursor allows you to iterate
through the result set and then perform the additional processing only on the rows that require it.
In a cursor contains the data in a loop. Cursors may be different from SQL commands that
operate on all the rows in the returned by a query at one time.
Declare a cursor
1 . Declaration of Cursor : To declare a cursor you must use the DECLARE statement. With
the help of the variables, conditions and handlers we need to declare a cursor before we can use
it. first of all we will give the cursor a name, this is how we will refer to it later in the procedure.
We can have more than one cursor in a single procedure so its necessary to give it a name that
will in some way tell us what its doing. We then need to specify the select statement we want to
associate with the cursor. The SQL statement can be any valid SQL statement and it is possible
to use a dynamic where clause using variable or parameters as we have seen previously.
2 . Open a cursor statement : For open a cursor we must use the open statement.If we want to
fetch rows from it you must open the cursor.
3 . Cursor fetch statement : When we have to retrieve the next row from the cursor and move
the cursor to next row then you need to fetch the cursor.
Synatx : FETCH cursor_name INTO var_name;
If any row exists, then the above statement fetches the next row and cursor pointer moves ahead
to the next row.
Syntax: CLOSE_name;
By this statement we can close the previously opened cursor. If it is not closed explicitly then a
cursor is closed at the end of compound statement in which that was declared.
Example :In the following example first of all we have to declare the Cursor and select the all
records from Company table. And after opened the cursor we fetch the record one by one from
cursor. And then insert these record in Products table
delimiter $$
CREATE PROCEDURE DemoCurs()
BEGIN
DECLARE d INT DEFAULT 0;
DECLARE c_id INT;
DECLARE c_name,c_address VARCHAR(20);
DECLARE cur CURSOR FOR SELECT * FROM company;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET d=1;
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET d=1;
OPEN cur;
lbl: LOOP
IF d=1 THEN
LEAVE lbl;
END IF;
IF NOT d=1 THEN
FETCH cur INTO c_id,c_name,c_address;
INSERT INTO products VALUES(c_id,c_name,c_address);
END IF;
END LOOP;
CLOSE cur;
END$$
mysql> delimiter ;
mysql> CALL DemoCurs();
Query OK, 1 row affected (0.12 sec)
Viva Voce:
1. What is a cursor?
2. What are the types of cursor?
3. What is the use of parameterized cursor?
4. What is the use of cursor variable?
5. What is a normal cursor?
6. What are Explicit cursor attributes?
Additional Experiments:
SQL>CREATE TABLE DEPT (DEPTNO NUMBER (2) PRIMARY KEY, DNAME VARCHAR2 (10) NOT
NULL, LOC VARCHAR2 (8));
Table created.
SQL>CREATE TABLE EMP(EMPNO NUMBER(4) PRIMARY KEY,ENAME VARCHAR2(9),JOB
VARCHAR2(9),MGR NUMBER(4),HIREDATE DATE,SAL NUMBER(7,2) CHECK(SAL<=10000),COMM
NUMBER(7,2),DEPTNO NUMBER(2) ,FOREIGN KEY(DEPTNO) REFERENCES DEPT);
Table created
SQL>CREATE TABLE SALGRADE (GRADE NUMBER (1), LOSAL NUMBER (4), HISAL NUMBER (4));
Table created.
To insert the following data into appropriate relations:
VALUES(&EMPNO,’&ENAME’,’&JOB’,&MGR,’&HIREDATE’,&SAL,&COMM,&DEPTNO);
Enter value for empno: 7499
Enter value for ename: ALLEN
Enter value for job: SALESMAN
Enter value for mgr: 7698
Enter value for hiredate: 20-FEB-81
Enter value for sal: 1600
Enter value for comm: 300
Enter value for deptno: 30
old 1: INSERT INTO EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
new 1: INSERT INTO EMP(7499,’ALLEN’,’SALESMAN’,7698,’20-FEB-
81’,1600,300,30) 1 row created.
3) SQL> INSERT INTO SALGRADE (GRADE,LOSAL,HISAL)
VALUES(&GRADE,&LOSAL,&HISAL);
Enter value for grade: 5
Enter value for losal: 3001
Enter value for hisal: 9999
old 1: INSERT INTO SALGRADE (GRADE,LOSAL,HISAL)
VALUES(&GRADE,&LOSAL,&HISAL)
new 1: INSERT INTO SALGRADE (GRADE,LOSAL,HISAL)
VALUES(5,3001,9999)
1 row created.
SINGLE ROW INSERTIONS
SQL> INSERT INTO DEPT(DEPTNO,DNAME,LOC) VALUES(20,'RESEARCH','DALLAS')
1 row created.
SQL> INSERT INTO EMP(EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
VALUES(7521,’WARD’,’SALESMAN’,7698,’22-FEB-81’,1250,500,30);
1 row created.
SQL> INSERT INTO SALGRADE (GRADE,LOSAL,HISAL) VALUES(4,2001,3000);
1 row created.
b) AIM: To Alter the tables using appropriate SQL statements:
1) Alter the size of dname of DEPT table
SQL> ALTER TABLE DEPT MODIFY (DNAME VARCHAR2 (20));
Table altered.
2) Alter the SALGRADE table by adding constraint unique to the field grade
SQL> ALTER TABLE SALGRADE ADD CONSTRAINT CONS_GRADE UNIQUE (GRADE);
Table altered.
3) Alter the SALGRADE table by dropping constraint unique to the field grade
SQL> ALTER TABLE SALGRADE DROP CONSTRAINT CONS_GRADE;
Table altered.
c)AIM: Dropping the tables using appropriate SQL statements
1) Drop the EMP table
SQL> DROP TABLE EMP;
Table dropped.
2) Drop the DEPT table
SQL> DROP TABLE DEPT;
Table dropped.
2)Execute a single line and group functions for a table.
AGGREGATE FUNCTIONS
STRING FUNCTIONS
NUMBER FUNCTIONS
DATE FUNCTIONS
CONVERSION FUNCTIONS
4) Left trim of character ‘s’ from employee names of department number 20.
SQL> SELECT LTRIM(ENAME,'S') FROM EMP WHERE DEPTNO=20;
LTRIM(ENAM
------------------
MITH
JONES
6) List employee names with all capital letters, with all small letters and with first letter
only as capital of department number 10.
SQL>SELECT ENAME,UPPER(ENAME),LOWER(ENAME),INITCAP(ENAME) FROM
EMP WHERE DEPTNO=10;
ENAME UPPER(ENAM) LOWER(ENAM INITCAP(EN
---------- ---------- ---------- ------------------ --------------------
CLARK CLARK clark Clark
KING KING king King
MILLER MILLER miller Miller
7) List employee names with length of the name sorted on length for department number
30.
SQL>SELECT ENAME, LENGTH (ENAME) FROM EMP WHERE DEPTNO=30 ORDER BY
LENGTH(ENAME);
ENAME LENGTH(ENAME)
---------- -------------------------
WARD 4
ALLEN 5
BLAKE 5
JAMES 5
MARTIN 6
TURNER 6
6 rows selected.
2)Find the first ‘SUN’day of employees after join in the organization of EMP table.
SQL>SELECT NEXT_DAY (HIREDATE,'SUN') AS HOLIDAY FROM EMP;
HIREDATE HOLIDAY
-------------- ---------------
17-DEC-80 21-DEC-80
20-FEB-81 22-FEB-81
22-FEB-81 01-MAR-81
02-APR-81 05-APR-81
4)Display last day of joining month of employees of deptno ‘10’ from EMP table.
SQL> SELECT HIREDATE,LAST_DAY(HIREDATE) AS LASTDAY FROM EMP WHERE DEPTNO=10;
HIREDATE LASTDAY
------------- -------------
09-JUN-81 30-JUN-81
17-NOV-81 30-NOV-81
23-JAN-82 31-JAN-82
2) Display empno, employee name, job, salary of the employees. Show the salary with
thousand separators.
SQL>SELECT EMPNO, ENAME,JOB,TO_CHAR(SAL,’$9,999’)AS SALARY FROM EMP;
EMPNO ENAME JOB SALARY
---------- ---------- --------- ----------------
7369 SMITH CLERK $800
7499 ALLEN SALESMAN $1,600
ENAME HIREDATE
---------- ----------------
JONES APR 81
MARTIN SEP 81
3. Create views, partitions and locks for a particular DB.
Creating views
Dropping views
1) Create a view that will store employee number ,names, job, salary and department
number of all employees.
2) Create a view that will store the information of all employees who are working Dallas.
3)Drop the view EMP1 that is created from the EMP table.
Table created.
INSERTING DATA:-
4 rows selected.
1 row selected.
TABLE ALTERED.
TABLE ALTERED