DBMSLab
DBMSLab
Roadway Travels
"Roadway Travels" is in business since 1997 with several buses connecting different places in
india. Its main office is located in Hydearabd.
Reservations are directly handled by booking office. Reservations can be made 30 days in
advance and tickets issued to passenger. One Passenger/person can book many tickets (to his/her
family).
In the process of computerization of Roadway Travels you have to design and develop a
Database which consists the data of Buses, Passengers, Tickets, and Reservation and cancellation
details. You should also develop query's usinf SQL to retrieve the data from database.
2. E-R Model
3. Relational Model
4. Normalization
6. Querying.
Students are suppossed to work on these steps week wise and finally create a complete "Database
System" to Roadway Travels. Examples are given at every experiment for guidance to students.
AIM :To Study And identify the entity in road way travels database
system
Analyze the problem carefully and come up with the entities in it. Identify what data has
to be persisted in the database. This contains the entities, attributes etc.
Entity:
An entity may be defined as a thing which is recognized as being capable of an
independent existence and which can be uniquely identified. An entity is an abstraction from the
complexities of some domain. When we speak of an entity we normally speak of some aspect of
the real world which can be distinguished from other aspects of the real world. Entities can be
thought of as nouns.
Examples:
A computer, an employee, a song, and a mathematical theorem.
Relationship:
A relationship captures how two or more entities are related to one another. Relationships
can be thought of as verbs, linking two or more nouns.
Examples:
An “owns” relationship between a company and a computer, a supervises relationship
between an employee and a department, a performs relationship between an artist and a song, a
proved relationship between a mathematician and a theorem.
Attributes:
Examples:
An employee entity might have a Social Security Number (SSN) attribute; the proved
relationship may have a date attribute.
1. Bus:
a. BUS NUMBER,
b. SOURCE,
c. DESTINATION.
d. NO OF DAYS AVAILBLE
2. Passenger:
a. PASSEBGER _ID
b. NAME,
c. AGE,
d. GENDER
e. ADDRESS
3. Ticket:
a. TICKET_ID
b. JOUTNEY_DATE
c. PNR NO
d. SOURCE
e. DESTINATION
f. BUS NO
1. Reservation:
a. PNRNO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS
2. Cancellation:
a. PNR _NO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS
d.
Primary keys:
E-R Diagrams:
Example:
A particular song is an entity. The collection of all songs in a database is an entity set.
The eaten relationship between a child and her lunch is a single relationship. The set of all such
child-lunch relationships in a database is a relationship set. In other words, a relationship set
corresponds to a relation in mathematics, while a relationship corresponds to a member of the
relation.
Entity sets are drawn as rectangles, relationship sets as diamonds. If an entity set participates in a
relationship set, they are connected with a line. Attributes are drawn as ovals and are connected
with a line to exactly one entity or relationship set.
An underlined name of an attribute indicates that it is a key: two different entities or relationships
with this attribute always have different values for this attribute.
Result: All the entities and relationships of database system are identified.
Pasgr id
Address
Ticket_id
Ticket_id
Passenger_ID
PNR_No Source
Bus_no
Experiment 3
Relational Model "Roadway Travels" database
Aim : To perform the all the entities and relationships in tabular fashion
Description :There are different ways of representing relationships as tables based on the
cardinality. Represent attributes as columns in table or as tables based on the requirement.
Different types of attributes based on your E-R model.
The relational models for the entities and relations are as follows:
Bus:
Bus_No Source Destination No of days available
Passenger:
P_ID NAME Age Gender Address Ticket_ID
Ticket:
Ticket_ID Journey_Date PNR_NO BUS_NO Source Destination
Reservation:
PNR_No Journey_Date No_of_seats
Cancellation:
PNR_No Journey_Date No_of_seats
Experiment 4
Normalization for database tables
AIM: To perform the normalization in a database system
4. BCNF
Passenger:
P_ID NAME Age Gender Address Ticket_ID
Passenger:
P_ID NAME Age Gender Address
Pastic
P_ID Ticket_ID
Experiment 5
Installation of MySQL
Creation & Alteration of tables using DDL commands
Aim:perform the installation of MYSQL and execute and manipulate all the DDL
commands in a database system.
Installation of MySQL. In this week you will learn creating databases, creating tables,
altering the databases, dropping tables and databases if not required. You will also try truncate,
rename commands etc.
Installation of MYSQL.
The process for starting the wizard depends on the contents of the installation package you
download. If there is a setup.exe file present, double-click it to start the installation process.
The Typical installation type installs the MySQL server, the mysql command-line client, and the
command-line utilities. The command-
line clients and utilities include mysqldump, myisamchk, and several other tools to help you
manage the MySQL server.
The Complete installation type installs all components included in the installation package. The
full installation package includes components
such as the embedded server library, the benchmark suite, support scripts, and documentation.
The Custom installation type gives you complete control over which packages you wish to
install and the installation path that is used.
If you choose the Typical or Complete installation types and click the NEXT button,
If you wish to change the installation path or the specific components that are installed by the
MySQL Installation Wizard, choose the Custom installation type.
A tree view on the left side of the custom install dialog lists all available components.
Components that are not installed have a red X icon; components that are installed have a gray
icon. To change whether a component is installed, click that component's icon and choose a new
option from the drop-down list that appears.
Once you choose an installation type and optionally choose your installation components, you
advance to the confirmation dialog. Your installation type and installation path are displayed for
you to review.
To install MySQL if you are satisfied with your settings, click the INSTALL button. To change
your settings, click the BACK button. To exit the MySQL Installation Wizard without installing
MySQL, click the CANCEL button.
Definitions:
3. a) alter add: ‘alter add’ command adds the attributes to an existing relation.
4. truncate: ‘truncate’ command truncates the table i.e. all the rows will be
deleted in the table.
Syntax: truncate table table-name;
5. drop: the ‘drop’ command deletes all information about the relation
from the database i.e. it deletes the schema of the relation.
Syntax: drop table table-name;
Creating tables:
1. Bus:
i. BUS NUMBER,
ii. SOURCE,
iii. DESTINATION.
iv. NO OF DAYS AVAILBLE
2. Passenger:
i. PASSEBGER _ID
ii. NAME,
iii. AGE,
iv. GENDER
v. ADDRESS
3. Ticket:
i. TICKET_ID
ii. JOUTNEY_DATE
iii. PNR NO
iv. SOURCE
v. DESTINATION
vi. BUS NO
4. Pastic
i. PID,
ii. TID
5. Reservation:
i. PNRNO
ii. JOUTNEY_DATE
iii. NUMBER_OF_SEATS
6. Cancellation:
a. PNR _NO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS
Primary keys:
PASSENGER:Passenger_id
TICKET :Ticket_id
BUS Table:
SQL>desc BUS;
Passenger table:
TABLE CREATED.
Reservation Table:
SQL>DESC RESERVATION;
Cancellation Table:
SQL>DESC CANCELLATION;
Ticket Table:
SQL>desc TICKET;
To truncate a table:
To DROP a table:
DML COMMANDS
AIM: To study and execute all the DML commands in a database system.
INSERT Command:
Table created.
1 row created.
SQL> /
Enter value for busno: 5678
Enter value for source: guntur
Enter value for des: vizaq
old 1: insert into bus540 values('&busno','&source','&des')
new 1: insert into bus540 values('5678','guntur','vizaq')
1 row created.
SQL> /
Enter value for busno: 1345
Enter value for source: vinukonda
Enter value for des: guntur
old 1: insert into bus540 values('&busno','&source','&des')
new 1: insert into bus540 values('1345','vinukonda','guntur')
1 row created.
1 row updated.
SQL> create table passenger1540(pid varchar(10) primary key,name varchar(20) not null,
age integer,gender varchar(5),address varchar(20));
Table created.
1 row created.
SQL> /
Enter value for pid: 222
Enter value for name: sunay
Enter value for age: 11
Enter value for gender: m
Enter value for address: kcp
old 1: insert into passenger540 values('&pid','&name',&age,'&gender','&address')
new 1: insert into passenger540 values('222','sunay',11,'m','kcp')
1 row created.
SQL> /
Enter value for pid: 333
Enter value for name: nari
Enter value for age: 27
Enter value for gender: m
Enter value for address: guntur
old 1: insert into passenger540 values('&pid','&name',&age,'&gender','&address')
new 1: insert into passenger540 values('333','nari',27,'m','guntur')
1 row created.
Table created.
1 row created.
SQL> /
Enter value for tid: 223344
Enter value for journdate: 28-jul-13 9:30:33pm
Enter value for source: tirupathi
Enter value for destination: guntur
Enter value for busno: 1345
Enter value for pnrno: 114
old 1: insert into ticket1540 values(&tid,'&journdate','&source','&destination','&busno',&pnrno
)
new 1: insert into ticket1540 values(223344,'28-jul-13 9:30:33pm','tirupathi','guntur','1345',1
14)
1 row created.
SQL> /
Enter value for tid: 334455
Enter value for journdate: 29-mar-13 9:20:22pm
Enter value for source: manglore
Enter value for destination: hyd
Enter value for busno: 1234
Enter value for pnrno: 115
old 1: insert into ticket1540 values(&tid,'&journdate','&source','&destination','&busno',&pnrno
)
new 1: insert into ticket1540 values(334455,'29-mar-13 9:20:22pm','manglore','hyd','1234',115)
1 row created.
Table created.
1 row created.
1 row created.
1 row created.
SQL> select * from pastic;
TID PID
---------- ----------
112233 111
1223344 222
223344 333
Table created.
1 row created.
SQL> /
Enter value for pnrno: 124
1 row created.
SQL> /
Enter value for pnrno: 111
Enter value for date: 29-mar-2013
Enter value for noofseats: 3
old 1: insert into reservation540 values(&pnrno,'&date',&noofseats)
new 1: insert into reservation540 values(111,'29-mar-2013',3)
1 row created.
Table created.
1 row created.
SQL> /
Enter value for pnrno: 111
Enter value for date: 21-dec-3013
Enter value for noofseats: 4
old 1: insert into cance540 values(&pnrno,'&date',&noofseats)
new 1: insert into cance540 values(111,'21-dec-3013',4)
1 row created.
SELECT Command:
SQL> SELECT * FROM BUS; (Selects all the attributes and display)
UPDATE Command:
Delete Command:
SQL> DELETE FROM BUS; (To delete entire rows from a table)
SQL> DELETE * FROM BUS WHERE bus_no=’boo1’; (To delete an individual row)
Result: Inserting of values into roadway travels tables are performed using with
DML commands
Description: If the answer to a query is a multiset of rows then we use the set
operations. Set operations include:
1. Union
2. Intersect
3. Except
Definitions:
3. Except: R-S returns a relation instance containing all tuples that occur in R
but not in S.
EXISTS Operator:
The EXISTS sub query is used when we want to display all rows where we have a
matching column in both tables.
SQL> select name from passenger where exists (select * from ticket540 where tid='112233');
NAME
--------------------------------------------------
tarun
manu
deepak
shona
SQL> select name from passenger540 where exists (select * from ticket540 where tid='112233');
NAME
--------------------
ruchin
sunay
nari
SQL> select name from passenger540 where exists (select name from ticket540
where tid='112233');
NAME
--------------------
ruchin
sunay
nari
The NOT EXISTS subquery is used to display cases where a selected column does not
appear in another table.
SQL> SELECT pnr_no,age FROM PASSENGER WHERE NOT EXISTS(SELECT age FROM
TICKET WHERE age>22);
SQL> (select name from passenger540 where address='hyderabad') minus (select name from
passenger
where address='guntur');
NAME
--------------------
ruchin
SQL> (select name from passenger540 where address='kcp')minus (select name from
passenger540 whe
ddress='guntur');
NAME
--------------------
UNION Operator:
Combine the results of two SELECT statements into one result set, and then eliminates
and duplicate rows from that result set.
SQL> SELECT pnr_no FROM PASSENGER WHERE age>20 UNION SELECT pnr_no
FROM RESERVATION WHERE no_of_seats > 2;
6 rows selected.
SQL> (select pid,name from passenger540) union (select pid,name from passenger540);
PID NAME
---------- --------------------
111 ruchin
222 sunay
333 nari
PID NAME
-------------------- --------------------
111 ruchin
222 sunay
333 nari
nari 333
ruchin 111
sunay 222
6 rows selected.
INTERSECT Operator:
Combine the results of two SELECT statements into one result set, and returns only those
rows that are returned by each of two statements.
SQL> >SELECT pnr_no FROM PASSENGER WHERE age>20 INTERSECT SELECT pnr_no
FROM RESERVATION WHERE no_of_seats > 2;
Queries using set comparison operators using ANY, ALL, IN and along with nested
queries
AIM: To execute all the set comparison operations in a SQL language.
Description: If the answer to a query is a multiset of rows then we use the set
operations. Set comparison operations include:
i. Any
ii. ALL
iii. In
Definitions:
The ANY Operator:the any operator allows you to specify multiple values in a where clause
and select any value from that list
The All Operator:the all operator allows you to specify multiple values in a where clause and
select all value from that list
The IN Operator:the in operator allows you to specify multiple values in a where clause.
The BETWEEN Operator:The BETWEEN operator selects values within a range. The values
can be numbers, text, or dates.
ANY Operator:
ALL Operator:
IN Operator:
SQL> SELECT pnr_no,age FROM PASSENGER WHERE age IN(SELECT age FROM
TICKET WHERE age>22);
BETWEEN Operator
SQL> SELECT name FROM PASSENGER WHERE age BETWEEN 30 AND 45;
5. Find the ticket numbers of the passengers whose name starts with ‘A’ and ends with
‘H’.
6. Find the names of all the passengers whose age is between 30 and 45.
SQL> SELECT name FROM PASSENGER WHERE age BETWEEN 30 AND 45;
Queries using Aggregate functions (COUNT, SUM, AVG, MAX,and MIN) and Queries
using GROUP BY, HAVING and Creation and droping of VIEWS.
AIM: To execute all the group functions and To execute all the group by Having
clause and view in a SQL language.
1.Avg
2.Min
3.Max
4.Sum
5.Count
Definitions:
1. Sum: The input to sum must be a collection of numbers and it gives sum of
those numbers.
Description: To write queries using clauses such as GROUP BY, HAVING and creation of
VIEW, etc. and retrieving
information by joining tables.
Definitions:
GROUP BY CLAUSE: The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
HAVING CLAUSE: The HAVING clause was added to SQL because the WHERE keyword
could not be used with aggregate functions.
SQL> SQL> SELECT name, AVG(age) FROM PASSENGER GROUP BY name HAVING
AVG(age)>30;
Q1)WAQ to display infor pressent in reservation and cancellation tables using union operator
SQL> select pnrno from reservation540 union select pnrno from cance540;
PNRNO
----------
111
123
124
Q)WAQ to find no of seats booked fro each pnrno using group by clause
PNRNO NOOFSEATS
---------- --------------
123 3
124 2
111 3
Q)WAQ to find no of pnrno's in a tableon which 123 pnr no is available using distinct
COUNT(DISTINCTSOURCE)
---------------------
1
PNRNO
----------
123
124
111
SUM(NOOFSEATS)
--------------
6
Creating VIEWS:
Updating a View
Dropping a View
Result: All the Aggregate functions and GROUP BY ,HAVING AND VIEW queries are
performed
Definition: a trigger is a SQL procedure that initiates an action (i.e., fires an action)
when an event (INSERT, DELETE or UPDATE) occurs. Sincetriggers are event-driven
specialized procedures, they are stored in and managed by the DBMS.
OUT PUT:
OUT PUT:
Declare a curser that defines a result set. Open the curser to establish a the result set. Fetch
the data into local variables as needed from the cursor, one row at a time. Close the cursor
when done.
Definition: A cursor is a temporary work area created in the system memory when a SQL
statement is executed. A cursor contains information on a select statement and the rows of data
accessed by it.
OUTPUT: