8.lab manual dbms
8.lab manual dbms
(AUTONOMOUS)
Special Batch - Emerging Technologies
II B. Tech II Semester
HOD-IT
Dr.
Thariq Hussan
S.No. Contents
1 Lab Objectives
2 Lab Outcomes
3 List of Experiments
INDEX
LAB OBJECTIVES
Introduce ER data model, database design and normalization
Learn SQL basics for data definition and data manipulation
LAB OUTCOMES
CO1: Design database schema for a given application and apply normalization.
CO2: Acquire skills in using SQL commands for data definition and data manipulation.
CO3: Develop solutions for database applications using procedures, cursors and triggers.
LIST OF EXPERIMENTS
1. Concept design with E-R Model
2. Relational Model
3. Normalization
4. Practicing DDL commands
5. Practicing DML commands
6. A. Querying (using ANY, ALL, UNION, INTERSECT, JOIN, Constraints etc.)
B. Nested, Correlated subqueries
7. Queries using Aggregate functions, GROUP BY, HAVING and Creation and dropping
of Views.
8. Triggers (Creation of insert trigger, delete trigger, update trigger)
9. Procedures
10. Usage of Cursors
LIST OF ADDITIONAL EXPERIMENTS
1. Student Information System
2. Library Management System
TEXT BOOKS
1. Database Management Systems, Raghurama Krishnan, Johannes Gehrke, Tata Mc Graw
Hill, 3rd Edition
2. Database System Concepts, Silberschatz, Korth, McGraw Hill, V edition.
REFERENCE BOOKS
1. Database Systems design, Implementation, and Management, Peter Rob & Carlos
Coronel 7th Edition.
2. Fundamentals of Database Systems, Elmasri Navrate, Pearson Education
3. Introduction to Database Systems, C.J. Date, Pearson Education
4. Oracle for Professionals, The X Team, S. Shah and V. Shah, SPD.
5. Database Systems Using Oracle: A Simplified guide to SQL and PL/SQL, Shah, PHI.
6. Fundamentals of Database Management Systems, M. L. Gillenson, Wiley Student
Edition.
Aim: Analyze the problem and come with the entities in it. Identify what data has to be persisted in
the databases.
Prerequisites: Student must know the entities and attributes and representation of diagrams.
Ticket
- TicketNo
- DOJ
- Age
- Sex
- Source
- Destination
- BusNo
Reservation
- PNRNo
- DOJ
- ContactNo
- No_of_seats
- BusNo
- Address
Cancellation
- PNRNo
- DOJ
- No_of_seats
- Status
The schemes for the following entities are:
1. Bus (BusNo String, Source String, Destination String, Coach_Type String);
2. Passenger (PassportID String, Name String, Age integer, Sex character, Address
String);
3. Ticket (TicketNo Integer, DOJ date, Age integer, Dept_time Timestamp, Source
String, Destination String, BusNo String);
4. Reservation (PNRNo String, DOJ Date, ContactNo integer, No_of_Seats integer,
BusNo String, Address String);
5. Cancellation (PNRNo String, DOJ Date, SeatNo integer, ContactNo integer, Status
String);
Conclusion: The student is able to identify the Entities and Attributes of the Roadway
Travels.
Week 2: 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.
Prerequisites: Student must know the entities and attributes and relationship among entities.
Conceptual data models provide concepts that are close to the way many users
perceive data.
Before implementation, a rough model of database is created.
This model is never implemented but is used for designing purpose.
This model use concepts such as entities, attributes and relationships.
o Example: E-R Model
Entity: It is an object with a physical existence. For example, each person in an enterprise,
car, house, a company, student.
Draw an E-R Diagram for an employee works in one department. The department contains
phone, the employee also has phone. Assume that an employee works in maximum 2
departments or minimum one department. Each department must have maximum 3 phones or
minimum zero phone. Design an E-R diagram for the above.
Construct an E-R diagram for a car-insurance company whose customers own one or more
cars each. Each car has associated with it zero to any number of recorded accidents.
Construct an E-R diagram for a hospital with a set of patients and a set of medical doctors.
Associate with each patient a log of the various tests and examinations conducted.
Week 3: Relational Model
Aim: To represent all the entities (Strong, Weak) in tabular fashion. Represent relationships
in a tabular fashion.
Bus:
Ticket:
Reservation:
Cancellation:
Viva-Voce
2. What are the different types of attribute can be there in any relation?
Aim: Apply the database normalization techniques for designing relational database tables to
minimize duplication of information like 1NF, 2NF, 3NF, BCNF.
1NF: A relation scheme is said to be in 1NF if the attribute values in the relation are atomic
i.e., Multivalve attributes are not permitted.
2NF: A relation scheme is said to be in 2NF, if and every non-key attribute is fully
functionally dependent on primary key.
3NF: A relation scheme is said to be in 3NF, and does not have transitivity dependencies. A
relation is said to be 3NF if every determinant is a key for each and every functional
dependency.
BCNF: A Relation scheme is said to be BCNF if the following statements are true for each
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.
i) The given FD is a trival
ii) P is a super key.
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
Step1: Make sure you already downloaded the MySQL essential 5.0.45 win32.msi file. Double click
on the .msi file.
Step2: This is MySQL Server 5.0 setup wizard. The setup wizard will install MySQL Server 5.0
release 5.0.45 on your computer. To continue, click next.
Step3: 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.
Step4: 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.
Step8: Wizard Completed. Setup has finished installing MySQL 5.0. Checks configure the MySQL
server now to continue. Click Finish to exit the wizard
Step9: The configuration wizard will allow configuring the MySQL Server 5.0 server instance. To
continue, click next.
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.
Step11: 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.
Step15: Configuration file created. Windows service MySQL5 installed. Press finish to close the
wizard.
Creating Tables and altering the Tables
Table Created
Table Created
Table Created
Table Created
Table Created
6. Mysql>Create table Cancellation(PNR_No integer(10), DOJ Date, No_of_Seats
integer(2), Address varchar(20), ContactNo integer(12), Status varchar(10),
FOREIGN KEY(PNR_No) REFERENCES Reservation(PNR_No));
Table Created
Field Datatype
BusNo varchar(10),
Source varchar(20),
Destination varchar(20),
CoachType varchar(10),
Capacity integer(2)
Conclusion: The student is able to install the MySQL and create database, create tables and
alter tables.
Viva Voce
1. What is DDL?
2. Which command is used to create a table or a database?
3. What are the various commands used for DDL Languages?
Week 6: Practicing DML Commands
DML Commands
Mysql>Insert into Bus values(‘AP123’,’Hyderabad’,’Banglore’,’Volvo’);
PassportID TicketNo
8738939 453
5443243 332
Conclusion: The student is able to perform DML Commands like Insert, Update, Delete and
Select.
Viva-Voce
1. What are DML commands?
2. Write the syntax for insert command.
3. What is the syntax for update command?
4. Write the syntax for delete command.
5. Write the syntax for select command.
6. Differentiate DCL and TCL.
Week 7: Queries using ANY, All, IN, Exists, Not Exists, Union, Intersect, Constraints
PNR_No
783-32923
928-38983
Name
Smith
John
3. Display the ticket numbers and names of all the passengers.
MySQL>Select TicketNo,Name from Ticket, Passenger;
name ticketno
Sai 1500
Raju 1502
Ramu 1503
4. Find the ticket numbers of the passengers whose name start with ‘r’ and ends with ‘h’.
MySQL> Select Name from Passenger where name LIKE ‘R%’ AND name LIKE ‘%H’;
Name
Rajesh
Ramesh
5. Find the names of Passengers whose age is between 30 and 45.
MySQL> Select Name from Passenger where age BETWEEN 30 AND 45;
Name
Neha
Ramu
Name
Akash
Avinash
Arivind
7. Display the sorted list of Passengers names
MySQL> Select Name from Passenger order by NAME;
Name
Akash
Aravind
Avinash
Neha
Ramu
smith
Conclusion: The student is able to execute the Queries from above database.
Viva-Voce:
4. What is Concatenation?
PNR_NO SUM_OF_SEATS
78393445
78393930
SUM(NO_OF_SEATS)
----------------
6
Viva-Voce
1. What is the difference between SUM and COUNT?
2. What is the purpose of MIN?
3. What is the use of MAX?
4. Define VIEW. What is the use of VIEW?
5. What is difference between DROP table and DELETE?
6. What is the use of AVG?
Week 9: Querying
CANCELLED_SEATS
03
Conclusion: The student is able to execute the Queries using SQL Clauses.
Week 10:Triggers
UPDATE
MySQL>UPDATE BUS SET SOURCE='KERALA' WHERE BUSNO='AP123'$$
Viva-Voce
1. What is TRIGGER?
CALL SAMPLE2()$$
BusNo Source Destination CoachType
AP123 Hyderabad Bangalore Volvo
AP234 Mumbai Hyderabad Semi-Sleeper
CREATE PROCEDURE SIMPLE_PROC(OUT PARAM1 INT)
BEGIN
SELECT COUNT(*) INTO PARAM1 FROM BUS;
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.
Syntax: 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 records 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 cursors attributes?