Académique Documents
Professionnel Documents
Culture Documents
INDEX:
Experiment 1: E-R Model: Identifying Entities and Keys........................................................................................3
Experiment 2: CONCEPT DESIGN WITH E-R MODEL.......................................................................................6
Experiment 3: RELATIONAL MODEL....................................................................................................................7
Experiment 4: Normalization......................................................................................................................................8
Experiment 5: Installation of MYSQL and practicing DDL commands...............................................................13
Experiment 6: Practicing DML commands..............................................................................................................34
Experiment 7: Querying.............................................................................................................................................37
Experiment 8 & 9: Querying.....................................................................................................................................45
Experiment 10: Triggers.............................................................................................................................................50
Experiment 11: Procedures........................................................................................................................................51
Experiment 12: Cursors:............................................................................................................................................52
Experiment 1: E-R Model: Identifying Entities and Keys
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.
Identify the primary keys for all the entities. Identify the other keys like candidate keys, partial keys, if any.
Example: Entities:
BUS
Ticket
Passenger
Apart from the above mentioned entities you can identify more. The above mentioned are few.
PROGRAM:The following are the entities and its attributes
Bus:
Field
type
null
key
default
Busno
varchar(8)
no
pri
null
Busmodel
varchar(10)
yes
null
null
Bustype
varchar(10)
yes
null
null
Source
varchar(20)
no
null
null
Destination
varchar(20)
no
null
null
Bus_depo
varchar(6)
yes
null
null
extra
null
null
null
null
null
null
Passenger:
Field
Pnr_no
Ticket_no
Name
Age
Sex
Ppno
extra
auto_incre
auto_incre
null
null
null
null
type
int(11)
int(11)
varchar(20)
int(3)
char(10)
int(10)
null
no
no
yes
yes
yes
yes
key
pri
pri
null
null
null
null
default
null
null
null
null
null
null
Bus details:
Field
Busno
Distance
Duration_ Of_journy
No_of_seats
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Reservation:
Field
Busno
Pnr_no
Date_of_journy
No_of_seats
Address
Status
Ticket:
Field
Ticket_no
Name
Age
Sex
Source
Destination
Dept_time
type
varchar(8)
int(11)
int(11)
int(3)
char(1)
char(1)
char(1)
char(1)
char(1)
char(1)
char(1)
type
varchar(8)
int(11)
date
int(3)
varchar(20)
char(1)
type
int(11)
varchar(20)
int(3)
char(10)
varchar(10)
varchar(10)
varchar(10)
null
yes
no
no
no
yes
yes
yes
yes
yes
yes
yes
key
null
null
null
null
null
null
null
null
null
null
null
default
null
null
null
null
null
null
null
null
null
null
null
extra
null
null
null
null
null
null
null
null
null
null
null
null
yes
yes
yes
yes
yes
yes
key
null
null
null
null
null
null
default
null
null
null
null
null
null
extra
null
null
null
null
null
null
null
no
no
yes
yes
yes
yes
yes
key
pri
null
null
null
null
null
null
default
null
null
null
null
null
null
null
extra
auto_incre
null
null
null
null
null
null
PROGRAM:-
Bus Table:
BUSNO
BUSMODEL
BUSTYPE
DURATION
_OF_
JOURNEY
Passenger Table:
PNR_NO
TICKET_NO
NAME
SOURCE
NO
_OF_
SEATS
SUN
DAY
AGE
DESTINATION
MON
DAY
TUES
DAY
SEX
Reservation Table:
BUSNO PNR_NO DATE_OF_JOURNEY NO_OF_SEATS ADDRESS STATUS
Ticket Table:
TICKET_NO
NAME
AGE
SEX
SOURCE
DESTINATION
BUS_DEPO
SATUR
DAY
PPNO
DEPT_TIME
Experiment 4: Normalization
Normalization is a method for organizing data elements in a database into tables.
Normalization Avoids
Duplication of Data The same data is listed in multiple lines of the database
Insert Anomaly A record about an entity cannot be inserted into the table without first inserting information about
another entity Cannot enter a customer without a sales order
Delete Anomaly A record cannot be deleted without deleting a record about a related entity.
Cannot delete a sales order without deleting all of the customers information.
Update Anomaly Cannot update information without changing information in many places. To update customer
information, it must be updated for each sales order the customer has placed
Normalization is a three stage process After the first stage, the data is said to be in first normal form, after the
second, it is in second normal form, after the third, it is in third normal form
Before Normalization
1.Begin with a list of all of the fields that must appear in the database. Think of this as one big table.
2.Do not include computed fields
3.One place to begin getting this information is from a printed document used by the system.
4.Additional attributes besides those for the entities described on the document can be added to the database.
Before Normalization Example
See Sales Order from below:
Fields in the original data table will be as follows:
SalesOrderNo, Date, CustomerNo, CustomerName, CustomerAdd, ClerkNo, ClerkName,
ItemNo, Description, Qty, UnitPrice
Sales Order
Fiction Company
202 N. Main
Mahattan, KS 66502
Sales Order
CustomerNumber: 1001
Number:
405
Sales Order
Customer Name: ABC Company
Date:
2/1/2000
Clerk
Customer Address: 100 Points
Number:
210
Manhattan, KS
Clerk
Martin
66502
Name:
Lawrence
Quantity Unit
Item Ordered Description
Price
Total
2,400.0
800
widgit small
40
60.00 0
801
tingimajigger
20
20.00 400.00
1,000.0
805
thingibob
10
100.00 0
Order Total
3,800.00
Normalization: First Normal Form
Separate Repeating Groups into New Tables.
Repeating Groups Fields that may be repeated several times for one document/entity
Create a new table containing the repeating data
The primary key of the new table (repeating group) is always a composite key; Usually document number and a
field uniquely describing the repeating line, like an item number.
First Normal Form Example
The new table is as follows:
SalesOrderNo, ItemNo, Description, Qty, UnitPrice
The repeating fields will be removed from the original data table, leaving the following.SalesOrderNo, Date,
CustomerNo, CustomerName, CustomerAdd, ClerkNo, ClerkName
These two tables are a database in first normal form
What if we did not Normalize the Database to First Normal Form?
Repetition of Data SO Header data repeated for every line in sales order.
Normalization: Second Normal Form
Remove Partial Dependencies.
Functional Dependency The value of one attribute in a table is determined entirely by the value of another.
Partial Dependency A type of functional dependency where an attribute is functionally dependent on only part of
the primary key (primary key must be a composite key).
Create separate table with the functionally dependent data and the part of the key on which it depends. Tables
created at this step will usually contain descriptions of resources.
Second Normal Form Example
The new table will contain the following fields:
ItemNo, Description
All of these fields except the primary key will be removed from the original table. The primary key will be left in the
original table to allow linking of data:
SalesOrderNo, ItemNo, Qty, UnitPrice
Never treat price as dependent on item. Price may be different for different sales orders (discounts, special
customers, etc.)
Along with the unchanged table below, these tables make up a database in second normal form:
SalesOrderNo, Date, CustomerNo, CustomerName, CustomerAdd, ClerkNo, ClerkName
What if we did not Normalize the Database to Second Normal Form?
Repetition of Data Description would appear every time we had an order for the item
Delete Anomalies All information about inventory items is stored in the SalesOrderDetail table. Delete a sales
order, delete the item.
Insert Anomalies To insert an inventory item, must insert sales order.
Update Anomalies To change the description, must change it on every SO.
Normalization: Third Normal Form
Remove transitive dependencies.
Transitive Dependency A type of functional dependency where an attribute is functionally dependent on an
attribute other than the primary key. Thus its value is only indirectly determined by the primary key.
Create a separate table containing the attribute and the fields that are functionally dependent on it. Tables created at
this step will usually contain descriptions of either resources or agents. Keep a copy of the key attribute in the
original file.
Third Normal Form Example
The new tables would be:
CustomerNo, CustomerName, CustomerAdd
ClerkNo, ClerkName
All of these fields except the primary key will be removed from the original table. The primary key will be left in the
original table to allow linking of data as follows:
SalesOrderNo, Date, CustomerNo, ClerkNo
Together with the unchanged tables below, these tables make up the database in third normal form.
ItemNo, Description
SalesOrderNo, ItemNo, Qty, UnitPrice
What if we did not Normalize the Database to Third Normal Form?
Repetition of Data Detail for Cust/Clerk would appear on every SO
Delete Anomalies Delete a sales order, delete the customer/clerk
Insert Anomalies To insert a customer/clerk, must insert sales order.
Update Anomalies To change the name/address, etc, must change it on every SO.
Completed Tables in Third Normal Form
Customers: CustomerNo, CustomerName, CustomerAdd
Clerks: ClerkNo, ClerkName
Inventory Items: ItemNo, Description
Sales Orders: SalesOrderNo, Date, CustomerNo, ClerkNo
SalesOrderDetail: SalesOrderNo, ItemNo, Qty, UnitPrice
Experiment 5: Installation of MYSQL and practicing DDL commands
click next.
Step3: click the checkbox and click next
Step4: Choose the installation path as c:\program files\ MySQL\ MySQL server 5.5. to continue click on execute.
10
11
2. Bus_reservation.busdetails:
create table bus_reservation.busdetails
(
busno varchar(8) referencesbus(busno) on delete cascade,
distance int not null, duration_of_journey int not null, no_of_seats int(3) not null,
sunday char check(sunday in('y','n')), monday char check(monday in('y','n')),
tuesday char check(tuesday in('y','n')),wednesday char check(wednesdayin('y','n')),
thursday char check(thursday in('y','n')),friday char check(friday in('y','n')),
-saturday char check(saturday in('y','n'))
);
select * from bus_reservation.busdetails;
12
3. Bus_reservation.passenger:
create table bus_reservation.passenger(pnr_no int primary key AUTO_INCREMENT, ticket_no int not null,
namevarchar(20) not null, age int(3),sex char(10) check(sex in('male','female')), ppno int(10));
select * from bus_reservation.passenger;
13
14
4. Bus_reservation.reservation:
create table bus_reservation.reservation
(
busno varchar(8) references bus(busno), pnr_no int references passenger(pnr_no),
date_of_journeydate, no_of_seats int(3),address varchar(20),status char(1) check(status in('y','n'))
);
select * from bus_reservation.reservation;
5. Bus_reservation.ticket:
create table bus_reservation .ticket
(
ticket_no int primary key AUTO_INCREMENT,namevarchar(20) not null,
age int(3),sex char(10) check(sex in('male','female')),source varchar(20),
destination varchar(20), dept_time varchar(10)
);
select * from bus_reservation .ticket;
15
6. Bus_reservation.cancellation:
create table bus_reservation.cancellation
(
busno varchar(8) references bus(busno), pnr_no int references passenger(pnr_no),date_of_journeydate,
no_of_seats int(3),address varchar(20),status char(1) check(status in('y','n'))
);
select * from bus_reservation.cancellation;
7. Bus_reservation.depo:
create table bus_reservation.depo (busno varchar(8) references bus(busno));
select * from bus_reservation.depo;
16
Alter table:
alter table bus_reservation.bus add bus_depo varchar(6);
select * from bus_reservation.bus;
Rename table:
alter table bus_reservation.depo rename bus_reservation.bus_depo_name;
select * from bus_reservation.bus_depo_name;
17
Drop table:
drop table bus_reservation.bus_depo_name;
select * from bus_reservation.bus_depo_name;
Truncate table:
truncate table bus_reservation.depo;
18
19
20
Experiment 7: Querying
queries (along with sub queries) using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT, Constaintsetc
1.Display unique PNR_NO of all passengers ?
select PNR_NO from bus_reservation.passenger;
2 . display all names of male passenger?
select name from bus_reservation.passenger where Sex='M';
21
22
5.Find the ticket number of the passengers whose name starts with S and ends with M?
select Ticket_NO from bus_reservation.passenger where name like 'S%M';
23
24
25
26
3. Display the number of days in a week on which the TG245 bus is available?
Select sunday='Y' and wednesday='y' from bus_reservation.busdetails wherebusno='TG245';
4. Find the number of tickets booked for each PNR_NO using GROUP BY CLAUSE?
Select PNR_NO, sum(TICKET_NO) FROM Bus_Reservation.Passenger GROUP BY
PNR_NO;
27
28
29
Creation of insert trigger, update trigger.practice triggers using the above database.
A trigger is executed implicitly whenever the triggering event happens and a trigger doesnt accept argument. the act
of executing a trigger is known as fixing the trigger.
<syntax>
CREATE [ OR REPLACE ] TRIGGER <trigger name>
{ BEFORE/AFTER/INSTEAD OF} ON REFERENCE
CREATE OR REPLACE TRIGGER updatePassenger
AFTER INSERT OR FELETE OR UPDATE ON passenger
FOR EACH ROW
BEGIN
IF NEW.TICKETNO>21 THEN
Update passenger Set new.ticketno= ticketno ;
ELSE
Set new.ticketno=0;
END IF;
END;
OUTPUT:
SET SERVER OUTPUT ON
TRIGGER IS SUCCESSFULLY CREATED
Experiment 11: Procedures
Creation of stored procedure, Execution of procedure and modification of procedure. Practice procedures using the
above database.
30
Fetch the data into local variable as needed from the courser, one row at a time.
Close the cursor when
a) To write a Cursor to display the list of Male and Female Passengers. Analyst.
DECLARE
cursor c(jb varchar2) is select Name from Passenger where Sex=m; prPassenger.Sex%type;
BEGIN
open c('m');
dbms_RESULT.put_line(' Name of Male Passenger are:'); loop
fetch c into pr;
exit when c%notfound; dbms_RESULT.put_line(pr); end loop;
close c;
open c('f');
dbms_RESULT.put_line(' Name of female Passengers are:'); loop
fetch c into em;
exit when c%notfound; dbms_RESULT.put_line(em); end loop;
close c;
END;
RESULT:
Name of Male Passenger are:
Santhosh
Santhosh_Ram
Jhan
Ashok
Srinivas
Venkatreddy
Name of female Passengers are:
Navya
Mallishwari
Jaya
Kalavathi
Suneetha
Lassini
Ramadevi
PL/SQL procedure successfully completed.
b) To write a Cursor to display List of Passengers from Passenger Table.
DECLARE
cursor c is select PNR_NO, Name, Age, Sex from Passenger ;
iPassenger.PNR_NO%type;
jPassenger.Name%type;
nPassenger.Age%type;
sPassenger.Sex%type;
BEGIN
open c;
dbms_RESULT.put_line('PNR_NO, Name, Age, Sex of Passengers are:= '); loop
fetch c into i, j, n, s; exit when c%notfound;
dbms_RESULT.put_line(i||' '||j||' '||n||' '||s); end loop;
close c; END;
RESULT:
SQL>@Passenger
PNR_NO
NAME
AGE
SEX
------------------------------------- ----------
31
1
2
3
4
5
6
7
8
9
10
11
12
13
Santhosh
Navya
Mallishwari
Santhosh_Ram
Jhan
Jaya
Kalavathi
suneetha 28
Ashok
Laasini
Srinivas
RamaDevi
Venkatreddy
22
23
24
23
36
56
55
F
32
06
38
32
60
M
F
F
M
M
F
F
M
F
M
F
M
32