Professional Documents
Culture Documents
Y SUBBA RAYUDU
M. Tech
INDEX
SNO
1
NAME OF PROGRAM
Syllabus
Page No
4-14
2 Required Work
3 Program Education Objectives
15
17
18
Y SUBBA RAYUDU
16
18
Page 1
E-R Model
19-21
22-24
Relational Model
25-26
10 Normalization
11 Installation of Mysql and practicing DDL commands
27-33
36-37
34-35
38-40
14 Querying (Continued)
15 Querying (Continued)
41
16 Triggers
17 Procedures
43
18 Cursers
19 Additional programs
45
47
49
50-51
24 Viva Questions
52-53
42
44
46
48
L T/P/D C
-
Y SUBBA RAYUDU
Page 2
-/3/-
This lab enables the students to practice the concept learnt in the subject DBMS by
developing a database for an example company named "Roadway Travels" whose
description is as follows. The student is expected to practice the designing, developing
and querying a database in the context of example database "Roadway travels".
Students are expected to use "Mysql" database.
Roadway Travels
"Roadway Travels" is in business since 1997 with several buses connecting different places in
india. Its main office is located in Hydearabd.
The company wants to computerize its operations in the following areas:
Cancellations
Page 3
Example: Entities:
1. BUS
2. Ticket
3. Passenger
Relationships:
1. Reservation
2. Cancellation
PRIMARY KEY ATTRIBUTES:
1. Ticket ID (Ticket Entity)
2. Passport ID (Passenger Entity)
3. Bus_No (Bus Entity)
A part from the above mentioned entities you can identify more. The above mentioned are
few.
Note: The student is required to submit a document by writing the Entities and keys to the lab
teacher.
Relate the entities appropriately. Apply cardnalities for each relationship. Identify strong
entities and weak entities (if any). Indicate the type of relationships (total / partial). Try to
incorporate generalization, aggregation, specialization etc wherever required.
Example: E-R diagram for bus
Note: The student is required to submit a document by drawing the E-R Diagram to the lab
teacher.
Y SUBBA RAYUDU
Page 4
Passenger
Name
Age
Sex
Address
Ticket_id
Passport ID
Experiment 4: Normalization
Database normalization is a technique for designing realtional database tables to minimize
duplication of information and, in so doing, to safegaurd the database against certain types of
logical or structural problems, namely data anormalies. For example, when multiple instances
of a given piece of information occur in a table, the possibility exists that these instances will
not be kept consistent when the data within tha table is updated, leading to a loass of data
integrity. A table that is sufficiently normalized is less vulnerable to problems of this kind,
because its structure reflects the basic assumptions for when multiple instances of the same
information should be represented by a single instance only.
For the above table in the First normalization we can remove the multiple valued attribute
Ticket_id and place it in another table along with the primary key of passenger.
First Normal Form: The above table can divided into two tables as shown below.
Y SUBBA RAYUDU
Page 5
Passenger
Name
Age
Passport ID
Sex
Address
Passport ID
Ticket_id
You can do the second and third normal forms if required. Anyhow Normalized tables are
given at the end.
Page 6
DELETE - deletes all records from a table, the space for the records remain
Y SUBBA RAYUDU
Page 7
Experiment 7: Querying
In this week you are going to practice queries(along with sub queries) using ANY, ALL, IN,
EXISTS, NOT EXIST, UNION, INTERSECT, Constraints etc.
Practice the following Queries:
1. Dispaly unique PNR_No of all passengers.
2. Display all the names of male passengers.
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'.
5. Find the names of passengers whose age is between 30 and 45.
6. Display all the passengers names beginning with 'A'
7. Display the sorted list of passengers names.
Experiment 8 and Experiment 9: Querying (continued...)
You are going to practice queries using Aggregate functions (COUNT, SUM, AVG, MAX,and
MIN), GROUP BY, HAVING and Creation and droping of VIEWS.
1. Write a Query to display the information present in the Passenger and cancellation
tables. Hint: Use UNION Operator.
2. Display the number of days in a week on which the 9W01 bus is available.
3. Find number of tickets booked for each PNR_No using GROUP BY CLAUSE. Hint:
Use GROUP BY on PNR_No.
4. Find the distinct PNR numbers that are present.
5. Find the number of tickets booked by a passenger where the number of seats is greater
than 1. Hint: Use GROUP BY, WHERE and HAVING CLAUSES.
6. Find the total number of cancelled seats.
Experiment 10: Triggers
In this week you are going to work on Triggers. Creation of insert trigger, delete trigger,
update trigger. Practice triggers using the above database.
Y SUBBA RAYUDU
Page 8
E.g:
CREATE TRIGGER updatecheck BEFORE UPDATE ON passenger FOR EACH ROW
BEGIN
IF NEW.TickentNO > 60 THEN
SET New.TickentNO = TicketNo;
ELSE
SET New.TicketNo = 0;
END IF;
END
COUNT(Tickets)
Ticket
age >= 40;
END;
Page 9
OPEN c1;
FETCH c1 INTO v_id, v_name;
CLOSE c1;
END;
Tables:
BUS
Bus No: VARCAHR : PK(primary key)
Source: VARCHAR
Destination: VARCHAR
Passenger
PPNO: VARCHAR(15) : PK
Name: VARCHAR(15)
Age: INT(4)
Sex: CHAR(10) : Male/Female
Address: VARCHAR(20)
Passenger_Tickets
PPNO: VARCHAR(15) : PK
Ticket_No: NUMERIC(9)
Reservation
PNR_No: NUMERIC(9) : FK
Journey_date: DATETIME(8)
No_of_seats: INT(8)
Y SUBBA RAYUDU
Page 10
Address: VARCHRA(50)
Contact_No: NUMERIC(9) --> Should not less than 9 and Should not accept any other
character other than interger
STATUS: CHAR(2) : Yes/No
Cancellation
PNR_No: NUMERIC(9) : FK
Journey_date: DATETIME(8)
No_of_seats: INT(8)
Address: VARCHRA(50)
Contact_No: NUMERIC(9) --> Should not less than 9 and Should not accept any other
character other than interger STATUS: CHAR(2) : Yes/No
Ticket
Ticket_No: NUMERIC(9) : FK
Journey_date: DATETIME(8)
Age: INT(4)
Y SUBBA RAYUDU
Page 11
2. Required Work
There will be homework assignments of about 12 programmatic/project scripts/memos, most
of which will involve programming. Assignments are due at the beginning of its theoretic
class on the date specified. Late assignments will receive 75% of full credit if they are handed
in within one week of the specified due date. After one week, no credit will be given or else
losing of credits is made in practice. There is no specific attendance policy for the course,
although it is expected that absences will leave the student unprepared for tests and
assignments. Tests will not be rescheduled except in extreme circumstances. However, the
lowest quiz grade will be dropped. Grades will be determined as follows:
Day to- Day evaluation Marks
15 marks
10 marks
50 marks
At the minimum, traditional grading cutoffs will apply. That is, 90% is guaranteed an A, 87%
is guaranteed a B+, etc. Depending on class performance, some shifting of grades (in an
upward direction only) may occur as final letter grades are assigned.
Y SUBBA RAYUDU
Page 12
Y SUBBA RAYUDU
Page 13
Y SUBBA RAYUDU
Page 14
Experiment numbers
1
Y SUBBA RAYUDU
Page 15
10
11
12
1.
2.
3.
4.
5.
The entities for the above company named Roadway Travels are as follows
Bus
Ticket
Passenger
Reservation
Cancellation
Now we will identify the attributes for each entity and construct the E-R Model.
1. BUS Entity:
The attributes are:
i. Bus_No
ii. Source
iii. Destination
iv. Dep_Time
The E-R Model is as follows:
Dep_Time
Destination
Source
Bus_No
BUS
Y SUBBA RAYUDU
Page 16
i.
i.
i.
v.
v.
i.
i.
i.
i.
v.
v.
i.
Age
Sex
Source
Destination
Journey_Date
Ticket_No
Dep_Time
TICKET
Age
Sex
Ticket_No
PNR_No
PPNo
PASSENGER
In the above figure PNR_No is the Primary Key and Ticket_No is Foreign Key
4. RESERVATION Entity:
The attributes are:
PNR_NO
Journey_Date
No_Of_Seats
Address
Contact_No
Status
The E-R Model is as follow:
i.
i.
i.
v.
v.
i.
No_Of_Seats
Address
Contact_No
Journey_Date
PNR_No
Status
RESERVATION
Address
Contact_No
Journey_Date
PNR_No
Status
CANCELLATION
Source
Destination
Bus_No
Dep_Time
BUS
Travels
in
PNR_No
PPNO
PASSENGER
Ticket_No
Sex
Name
Age
The relationship between BUS Entity and PASSENGER Entity is strong relationship. Because the two entities
consists of primary keys. Here the two entities are strong entities. So the relationship is strong relationship.
Relationship between PASSENGER Entity and RESERVATION Entity:
The following diagram depicts the relationship exists between PASSENGER Entity and
RESERVATION Entity.
Name
Age
Sex
Ticket_No
PNR_No
PPNo
PASSENGER
Takes
PNR_No
Status
RESERVATION
Journey_Date
Contact_No
No_Of_Seats
Address
The relationship between PASSENGER Entity and RESERVATION Entity is weakrelationship. Because the one
entity consists of primary key and another entity consists offoreign key. Here the one entity is strong entity
(single border rectangle) and another entity is weak entity (double border rectangle). The relationship between
strong entity and weak entity is weak relationship.
Name
Age
Sex
Ticket_No
PNR_No
PPNo
PASSENGER
Cancels
Status
PNR_No
CANCELLATION
Journey_Date
Contact_No
No_Of_Seats
Address
The relationship between PASSENGER Entity and CANCELLATION Entity is weakrelationship. Because the
one entity consists of primary key and another entity consists offoreign key. Here the one entity is strong entity
(single border rectangle) and another entity is weak entity (double border rectangle). The relationship between
strong entity and weak entity is weak relationship.
Source
Destination
Dep_Time
1234
Anantapur
Kurnool
10:30am
4567
Kadapa
Hyderabad
10:30pm
1245
Hyderabad
Chennai
07:00pm
3467
Bangalore
Hyderabad
12:30pm
Age
Sex
Source
Destination
Dep_Time
2345
20-08-2010
25
Anantapur
Kurnool
10:30am
5678
25-08-2010
20
Hyderabad
Chennai
07:00pm
2457
30-08-2010
32
Bangalore
Hyderabad
12:30pm
4679
05-09-2010
28
Kadapa
Hyderabad
10:30pm
Ticket_No
Name
Age
Sex
PPNo
2345
Varuni
25
9003345678
5678
Rahul
20
9247212345
2457
Ajay
32
9989892731
4679
Suni
28
9885252069
Journey_Date
No_Of_Seats
Address
Contact_No
Status
20-08-2010
Anantapur
9003345678
Yes
30-08-2010
Bangalore
9989892731
No
25-08-2010
Hyderabad
9885252069
Yes
05-09-2010
Kadapa
9247212345
No
Address
Contact_No
Status
Bangalore
9989892731
No
Journey_Date No_Of_Seats
30-08-2010
BUS
1234
Anantapur
Kurnool
10:30am
4567
Kadapa
Hyderabad
10:30pm
1245
Hyderabad
Chennai
07:00pm
3467
Bangalore
Hyderabad
12:30pm
Ticket
2345 20-08-2010
25
Anantapur
Kurnool
10:30am
5678 25-08-2010
20
Hyderabad
Chennai
07:00pm
2457 30-08-2010
32
Bangalore
Hyderabad
12:30pm
4679 05-09-2010
28
Kadapa
Hyderabad
10:30pm
Passenger
1
2345
Varuni
25
9003345678
5678
Rahul
20
9247212345
2457
Ajay
32
9989892731
4679
Suni
28
9885252069
Reservation`
1
20-08-2010
Anantapur
9003345678
yes
30-08-2010
Bangalore
9989892731
no
25-08-2010
Hyderabad
9885252069
yes
05-09-2010
Kadapa
9247212345
no
Cancellation
3
30-08-2010
Bangalore
9989892731
no
WEEK 4: NORMALIZATION
Database Normalization is a technique for designing relational database tables to minimize duplication of
information and, in doing, to safeguard the database against certain types of logical or structural problems,
namely data anomalies. For example, when multiple instances of a given piece of information occur in a
table, the possibility exists that these instances will not be kept consistent when the data within the table is
updated, leading to a loss of data integrity. A table that is sufficiently normalized is less vulnerable to
problems
of this kind. Because its structure reflects the basic assumptions for when multiple instances of the same
information should be represented by a single instance only.
Normalization: Database designed based on the E-R model may have some amount ofinconsistency, ambiguity
and redundancy. To resolve these issues some amount of refinement is required. This refinement process is called
normalization.
Let us now consider the transportation table
The above table shows the data file of the transportation in a table format. To reduce the anomalies normalization
is applied
1ST NORMAL FORM:
A relation R is said to be in the first normal form (1NF) if and only if all the attributes of the relation R are
atomic in nature.
Consider the Transportation table to reproduce the table. The following table shows the table in 1NF.
Bus_No
Source
Destination
Dep_Time
1234
Anantapur
Kurnool
10:30am
4567
Kadapa
Hyderabad
10:30pm
1245
Hyderabad
Chennai
07:00pm
3467
Bangalore
Hyderabad
12:30pm
Ticket_No
Journey_Date
Age
Sex
Source
Destination Dep_Time
2345
20-08-2010
25
Anantapur
Kurnool
10:30am
5678
25-08-2010
20
Hyderabad
Chennai
07:00pm
2457
30-08-2010
32
Bangalore
Hyderabad
12:30pm
4679
05-09-2010
28
Kadapa
Hyderabad
10:30pm
PNR_No
Ticket_No
Name
Age
Sex
PPNo
2345
Varuni
25
9003345678
5678
Rahul
20
9247212345
2457
Ajay
32
9989892731
4679
Suni
28
9885252069
PNR_No
Journey_Date
No_Of_Seats
Address
Contact_No
Status
20-08-2010
Anantapur
9003345678
Yes
30-08-2010
Bangalore
9989892731
No
25-08-2010
Hyderabad
9885252069
Yes
05-09-2010
Kadapa
9247212345
No
PNR_No
3
Journey_DateNo_Of_Seats
30-08-2010
AddressContact_No
Bangalore
9989892731
Status
No
In the new form, all the attributes are atomic, meaning that they are not further decomposable.
2ND NORMAL FORM:
The relation is said to be in second normal form if and only if:
1.
2.
Source
Destination
Dep_Time
1234
Anantapur
Kurnool
10:30am
4567
Kadapa
Hyderabad
10:30pm
1245
Hyderabad
Chennai
07:00pm
3467
Bangalore
Hyderabad
12:30pm
TICKET Table:
Ticket_No
Journey_Date
2345
20-08-2010
5678
25-08-2010
2457
30-08-2010
4679
05-09-2010
PASSENGER Table:
PNR_No
Name
Age
Sex
PPNo
Varuni
25
9003345678
Rahul
20
9247212345
Ajay
32
9989892731
Suni
28
9885252069
Journey_Date No_Of_Seats
Address
Contact_No
Status
20-08-2010
Anantapur
9003345678
Yes
30-08-2010
Bangalore
9989892731
No
25-08-2010
Hyderabad
9885252069
Yes
05-09-2010
Kadapa
9247212345
No
Address
Contact_No
Status
Bangalore
9989892731
No
Journey_Date No_Of_Seats
30-08-2010
It is in 2NF
2.
BUS Table:
Bus_No
Source
Destination
Dep_Time
1234
Anantapur
Kurnool
10:30am
4567
Kadapa
Hyderabad
10:30pm
1245
Hyderabad
Chennai
07:00pm
3467
Bangalore
Hyderabad
12:30pm
TICKET Table:
Ticket_No
Journey_Date
2345
20-08-2010
5678
25-08-2010
2457
30-08-2010
4679
05-09-2010
PASSENGER Table:
PNR_No
Name
Age
Sex
PPNo
Varuni
25
9003345678
Rahul
20
9247212345
Ajay
32
9989892731
Suni
28
9885252069
RESERVATION TABLE:
PNR_No
Journey_Date
No_Of_Seats
Address
Contact_No
Status
20-08-2010
Anantapur
9003345678
Yes
30-08-2010
Bangalore
9989892731
No
25-08-2010
Hyderabad
9885252069
Yes
05-09-2010
Kadapa
9247212345
No
Here the reservation table is split into two tables in order to remove transitive dependency. The following
tables shows the 3rd normal form.
Journey_Date
Status
20-08-2010
Yes
30-08-2010
No
25-08-2010
Yes
05-09-2010
No
No_Of_Seats
Address
Contact_No
Anantapur
9003345678
Bangalore
9989892731
Hyderabad
9885252069
Kadapa
9247212345
CANCELLATION TABLE
PNR_No
3
Journey_Date No_Of_Seats
30-08-2010
Address
Contact_No
Status
Bangalore
9989892731
No
Here the cancellation table is split into two tables in order to remove transitive dependency. The following
tables shows the 3rd normal form.
1. Cancellation status table
PNR_No
3
Journey_Date
30-08-2010
Status
No
No_Of_Seats
1
Address
Contact_No
Bangalore
9989892731
SQL> CREATE TABLE bus (Bus_Novarchar(10) primary key, Source varchar(20), Destination
varchar(20), Dep_Timevarchar(10));
Table Created
SQL> CREATE TABLE ticket (Ticket_No number primary key, Journey_Date number, Age
number,
Sex
varchar(2),
Source
varchar(20),
Destination
varchar(20), Dep_Timevarchar (10) );
Table Created
SQL> CREATE TABLE passenger (PNR_No number(10) primary key, Ticket_No
number,
Name
varchar(10),
Age
number(3),
Sex
varchar(2),
PPNo number(10));
Table Created
passenger,
varchar(10),
Dep_Time
10:30pm
1 row selected
2. INSERT Command:
The INSERT Command is used to insert data into a relation, we either specify a tuple to be inserted or write a
query whose result is a set of tuples to be inserted. The syntax is as follows.
To insert a single value into the table the following syntax is used.
SQL> INSERT INTO tablenameVALUES(value list);
To insert the multiple values into the table the following syntax is used.
SQL> INSERT INTO tablenameVALUES(&value1,&value2,,valueN);
Example:
SQL> INSERT INTO passenger VALUES(0012345678,1234,Rahul,25,M,9902345178);
1 row created.
WEEK 7: Querying
In this week you are going to practice queries (along with sub queries) using ANY, ALL,
ramesh
78
geeta
AGE
SEX
PPNO
78
geetha
36
abc124
45
ramesh
45
abc123
5. Find the ticket numbers of the passengers whose name starts with A and ends with
H.
8. Display the source and the destination having journey time more than than 10 hours.
SQL>select source,destination from bus where hours>=10;
Output:
Source
destination
----------------------------------Hyderabad
banglore
Kurnool
Chennai
Madras
manglore
9. Display the details of passengers who are travelling either in Ac or Non_Ac (using in
operator).
SQL> select * from passenger wherebusservice in(ac,non_ac);
Ouput;
PNR_NO TICKET_NO NAME AGE SEX PPNO BUSERVICE
--------------------------------------------------------------------------------------1
78
geetha
36
abc124
ac
45
ramesh
45
abc123
non_ac
1. Write a query to display the information present in the passenger and cancellation tables (using
UNION operator).
SQL>select pnr_no from passenger union select pnr_no from cancellation;
Output:
Pnr_no
---------1
2
2. Display the number of days ina week on which the 9wo1 bus is available.
SQL>select count(days) from bus where busno=9w01;
Output:
Days
-----4
3. Find the distincet numbers that are present.
SQL>select distinct pnr_no from passenger;
Output:
Pnr_no
---------1
2
4. Find the number of tickets booked for each pnr_no using GROUP B CLAUSE(use
GROUP BY on pnr_no).
SQL> select pnr_no,sum(no_of_tickets) nof_of_tickets from reservation group by pnr_no;
Output:
Pnr_nono_of_tickets
-----------------------------1
4
20
6
5. Write a query to count the number of tickets for the buses ,which travelled after the
date 14/13/2009(use HAVING CLAUSES).
SQL> select sum(no_of_tickets) no_of_tickets from passenger where journey_date between '12-mar-09'and
sysdate having count(pnr_no)>=0;
Output:
NO_OF_TICKETS
----------------------------------8
6. Find the total no of cancellation seats.
SQL>select sum(no_of_cancelseats) total_no_of_cancelseats from cancellation;
Output:
Total_no_of_canceltickets
---------------------------------5
7. Find the number of tickets booked in each class where the number of seats is greater
than1(use GROUP BY ,WHERE and HAVING VLAUSES).
SQL> select pnr_no,name,sum(no_of_tickets) nof_of_tickets ,busservice from passenger where busservice
in('ac','nonac') group by name,pnr_no,busservice having sum(no_of_seats)>1;
Output:
PNR_NO NAME
(NO_OF_TICKETS
BUSSERVICE
---------------------------------------------- ----------------------------------------1
ramesh
geetha
3
17
ac
nonac
:new.ticket_no:=:old.ticket_no;
aaa
Additional Programs
50
51
52
1.Write a function to accept the empno and return exp with minimum 3 decimal?
2.. Write a function to accept a grade and return the number of emps belongs to that grade?
3. Write a function to accept a character string and print it in case?
53
Viva Questions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
54