Dbms Lab Manual Mysql
Dbms Lab Manual Mysql
II B. Tech II Semester
Verified &
Authorized by
HOD (CSE)
INDEX
S.No Contents
1 Lab Objective
2 Lab outcomes
3 Introduction About Lab
4 Guidelines to students
5 List of experiments as per the university curriculum
6 List of Additional Experiments
8 Text Books / Reference Books
1. LAB OBJECTIVE
This DBMS Lab enables the students to practice the concepts learnt in the DBMS
subject by developing a database for an example company named “Roadway Travels”
2. LAB OUTCOME
Upon successful completion of this Lab the student will be able to:
ER Model: The Objective of the lab is to analyze the problem carefully and come up
with entities and attributes etc in the Roadway Travels and later buildup the conceptual
design with the E-R Model.
Relational Model: The Objective of the lab exercise is to represent all the entities in
tabular fashion, representing relationships as tables based on Cardinality, different types
of attributes(composite, Multi-valued and Derived)
Installation of MySQL and Practicing DDL & DML commands: The Objective of the
lab Exercise is to install the MySQL software and Practice DDL & DML Commands and
Practicing the Queries.
Triggers, Cursors and Procedures: The Objective of the lab exercise is to create
triggers and cursors and writing Procedures on Roadway Travels database
STRUCTURE OF A DBMS
E-R Model
Aim: Analyze the problem and come with the entities in it. Identify what Data has to be
Persisted in the databases.
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
Prerequisites: Student must know the entities and attributes and representation of diagrams
The Following are the entities:
Identify the primary keys for all the entities. Identify the other keys like candidate keys,
partial keys, if any.
Example:
Entities: 1. BUS
2. Ticket
3. Passenger
Relationships: 1. Reservation
2. Cancellation
E-R Model
Bus
- BusNo
- Source
- Destination
- CoachType
SCHEMA
Ticket
- TicketNo
- DOJ
- Address
- ContactNo
- BusNo
- SeatNo
- Source
- Destination
SCHEMA
Passenger
- PassportID
- TicketNo
- Name
- ContactNo
- Age
- Sex
- Address
SCHEMA
Reservation
- PNRNo
- DOJ
- No_of_seats
- Address
- ContactNo
- BusNo
- SeatNo
SCHEMA
Reservation(PNRNo: String, DOJ: Date, NoofSeats: integer , Address: String ,ContactNo:
String, , BusNo: String,SeatNo:Integer)
Cancellation
- PNRNo
- DOJ
- SeatNo
- ContactNo
- Status
SCHEMA
Conclusion: The Student is able to Identify the Entities and Attributes of the
Roadway Travels
Viva-Voce:
1. What is SQL?
2. what is entity and relationship?
3. What is DBMS?
4. What is a Database system?
5. Advantages of DBMS?
6.How many types of database languages are available?
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.
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
Prerequisites: Student must know the entities and attributes and relationship among entities
Conclusion: The Student is able design the concept design of Road-way Travels
Viva-Vice:
3. RELATIONAL MODEL
Aim: To Represent all the entities (Strong, Weak) in tabular fashion. Represent
relationships in a tabular fashion.
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
Mysql>desc Bus;
Ticket:
Mysql>desc Ticket;
Passenger:
Type of
ColumnName Datatype Constraints
Attributes
PassportID Varchar(15) Primary Key Single-valued
TicketNo Varchar(20) Foreign Key Single-valued
Name Varchar(20) Composite
ContactNo Varchar(20) Multi-valued
Age Integer Single-valued
Sex character Simple
Address Varchar(20) Composite
Reservation:
Cancellation:
Conclusion: The Student is able draw the tabular representation of the relations of
Roadway travels.
Viva-Voce:
2. What is VIEW ? and What will you get when you use VIEW
4.NORMALIZATION
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.
Viva-Voce:
1. Define Normalization?
4. What is 2NF?
5. What is 3NF?
Aim: Installation of MySQL. Creating database tables, altering the database, dropping
tables and truncate commands
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
Step 3
Choose the setup type that best suits your needs. For common program features select Typical
and it’s recommended for general use. To continue, click next.
Step 4
This wizard is ready to begin installation. Destination folder will be in C:\Program
Files\MySQL\MySQL Server 5.0\. To continue, click next
Step5
The program features you selected are being installed. Please wait while the setup wizard installs
MySQL 5.0. This may take several minutes.
Step6
To continue, click next.
Step7
To continue, click next
Step8
Wizard Completed. Setup has finished installing MySQL 5.0. Check the configure the MySQL
server now to continue. Click Finish to exit the wizard
Step9
The configuration wizard will allow you to configure the MySQL Server 5.0 server instance
Step10
Select a standard configuration and this will use a general purpose configuration for the
server that can be tuned manually. To continue, click next
Step 11
Check on the install as windows service and include bin directory in windows path. To
continue, click next.
Step12
Please set the security options by entering the root password and confirm retype the password.
To continue, click next .
Step13
Ready to execute? Clicks execute to continue.
Step14
Processing configuration in progress.
Step 15
Configuration file created. Windows service MySQL5 installed. Press finish to close the wizard.
Viva Voce:
Aim : Create a DML Commands are used to manage data within the scheme objects.
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
DML Commands:
Conclusion: The Student is able perform DML Commands like Insert, Update, Delete
and Select
Viva-Voce
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 64MB RAM and 100 MB free disk space.
MySQL 5.6.1
RESERVATION2
PNR_No
10201
10202
10203
10204
4. Find the ticket numbers of the passengers whose name start with ‘r’ and ends with ‘h’.
Name
Rajesh
Ramesh
Ramesh
Name
Akash
Arivind
Avinash
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?
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.
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
1. Write a Query to display the information present in the passenger and cancellation
tables
UNION
3. Find number of tickets booked for each PNR_No using GROUP BY CLAUSE
Viva-Voce:
5. Find the number of tickets booked by a passenger where the number of seats is
greater than 1.
6. Find the total number of cancelled seats.
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
5. Find the number of tickets booked by a passenger where the number of seats is
greater than 1.
mysql> create table students(sid int primary key,name varchar(15),login varchar(15), age int,gpa real);
mysql> create view Goodstudents(sid,gpa) AS select s.sid,s.gpa from students s where gpa > 3.0;
10.Triggers
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 64MB RAM and 100 MB free disk space.
MySQL 5.6.1
DELIMITER $$
BEGIN
SET action='update',
source=OLD.source,
changedon=NOW();
END$$
i)UPDATE :
ii) INSERT:
BEGIN
SET action='Insert',
source=NEW.source,
changedon=NOW();
END$$
iii)
BEGIN
SET action='Insert',
source=NEW.source,
changedon=NOW();
END$$
Examples
BEGIN
ELSE
SET New.TicketNo = 0;
END IF;
END;
Conclusion: The Student is able to work on Triggers and create active database.
Viva-Vice:
1. What is TRIGGER?
11.Procedures
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 64MB RAM and 100 MB free disk space.
MySQL 5.6.1
Ex1:
BEGIN
END$$
CALL BUS_PROC1()$$
Ex2:
BEGIN
DECLARE X INT(3);
SET X=10;
SELECT X;
END$$
BEGIN
END$$
Viva Voce:
12.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.
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster
processor with at least 64MB RAM and 100 MB free disk space.
MySQL 5.6.1
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.
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.
Delimiter $$
Viva Voce:
1. What is a cursor?
ADDITIONAL PROGRAMMS
EMPLOYEES TABLE
DEPARTMENT TABLE
Mysql> Create table Sailors(Sid integer PRIMARY KEY,sname varchar(15), rating int,age real);
mysql> select S.sname from sailors S, reserves R where S.sid=R.sid AND R.bid=103;
mysql> select sname from sailors s,Reserves R where S.sid=R.sid AND bid=103;
mysql> select R.sid from Boats B,Reserves R where B.bid=R.bid AND B.color='red';
mysql> select S.sname from sailors S,reserves R,Boats B where S.sid=R.sid AND R.bid=B.bid
AND B.color='red';
mysql> select B.color from Sailors S,Reserves R,Boats B where S.sid=R.sid AND R.bid=B.bid
AND S.sname='Lubber';
where 2*S1.rating=S2.rating-1;
1).Find the names of sailors who have reserved a red or a green boat.
OR
2). Find the names of sailors who have reserved both a red and a green boat.
SELECT S.SNAME
FROM SAILORS S,RESERVES R,BOATS B
WHERE S.SID=R.SID AND R.BID=B.BID AND B.COLOR='red'
INTERSECT
SELECT S2.SNAME
FROM SAILORS S2,RESERVES R2,BOATS B2
WHERE S2.SID=R2.SID AND R2.BID=B2.BID AND B2.COLOR='green';
NESTED QUERIES
Find the names of Sailors who have NOT reserved a red Boat
Find sailors whose rating is better than some sailor called Horatio
Sailors S2);
Find the age of the youngest sailor for each rating level.
Find the age of the youngest sailor who is eligible to vote for each rating level with at least two
such sailors
For each red boat , find the number of reservations for this boat
Find the average age of sailors for each rating level that has at least two sailors