Documente Academic
Documente Profesional
Documente Cultură
DATABASE
Week 1
Bus
Attribute: A single data item related to a database object. The database schema
associates one or more attributes with each database entity.
Also Known As: field, column
Name
Entity-Attribute Diagram:
Bus no
Bus
Bname
Types of attributes:
There are different
types of attributes
1. Domain Attribute: The set of possible values for an attribute is called the
domain of the attribute.
Divorced Single
Marital status
Married Widowed
2.Key Attribute: The attribute (or combination of attributes) that is unique for every
entityinstance.
Eg: The account number of account
The employee id of an employee
3. Simple Attribute: The attribute which can’t be broken-down into small components.
AGE
Day
Date
Month
Year
5. Single valued Attribute: The attribute can take on only a single value for each entity
instance.
Phno
6. Multi-valued Attribute: An attribute that may take more than one value for a given
entity. It is represented with double ellipse.
Addr
Pincod
Hno e
Street City
EMP
Joining Curren
Date t Date
1. Primary key: The value this key holds should be unique for each record in the
database. Every database table should have one or, more columns designated as the
primary key.
Eg: Bus no in the Bus record or table
PNR no in the passenger table
3. Foreign key: A foreign key (FK) is a column or combination of columns that is used
to establish and enforce a link between the data in two tables.
In a foreign key reference, a link is created between two tables when the column or
columns that hold the primary key value for one table are referenced by the column or
columns in another table. This column becomes a foreign key in the second table.
Eg: bus (bus no, bname, src, dest)
Ticket (tno, bus no, src, dest, age, gender)
Bus no is the foreign key in the ticket entity.
4. Composite key: Sometimes it requires more than one attribute to uniquely identify an
entity. A primary key that made up of more than one attribute is known as a composite
key.`
Eg: ticket (tno, bus no, age)
Tno & bus no are primary keys.
5. Super key: A super key is a combination of attributes that can be uniquely used to
identify a database record. A table might have many super keys.
Eg: bus (bus no, bname, src, dest)
<Bus no> <bus no, src> <bname, src> <bus no, dest>
6. Partial key: A weak entity type normally has a partial key, which is the set of attribute
that can uniquely identify weak entities that are related to the same owner entity.
Entities:
Bus (Bus no, No-of-seats, Type)
Ticket (Tno, Journey Date, Seat no, Src, Dest, Deptime, Gender, Bus no)
Passenger (PNR no, Pname, Age, Gender, Addr, Phno, Type)
Driver (Did, Dname, Addr, Age, Phno, Brno, Bus no)
MD (MDname, Phno, Addr, Gender, Age)
Branch (Brno, Brname, Addr, Phno, Email-Id)
PASSENGER Entity
PNR no: Int (10): Primary key
Pname: Varchar (20)
Age: Int (10)
Gender: Varchar (10): Male/Female
Addr: Varchar (30)
Phno: Varchar (20)
RESERVATION Entity
PNR no: Int (10): Foreign key
Journey Date: Date
No-of-seats: Int (10)
Addr: Varchar (30)
Phno: Varchar (20)
Status: Varchar (10): Yes/No
CANCELLATION Entity
PNR no: Int (10): Foreign key
Journey Date: Date
No-of-seats: Int (10)
Addr: Varchar (30)
Phno: Varchar (20)
Status: Varchar (10): Yes/No
All these entities are used for extract data when necessary while writing the queries in the
coming weeks. There are some extra entities which are not used for writing queries.
DRIVER Entity
Did: Int (10): Primary key
Dname: Varchar (20)
Addr: Varchar (30)
Age: Int (10)
Brno: Int (10): Foreign key
Bus no: Varchar (20): Foreign key
Phno: Varchar (20)
MD Entity
MDname: Varchar (20)
Phno: Varchar (20)
Addr: Varchar (30)
Gender: Varchar (10): Male/Female
Age: Int (10)
BRANCH Entity
Brno: Int (10): Primary key
Brname: Varchar (20)
Addr: Varchar (30)
Phno: Varchar (20)
Email-Id: Varchar (30)
Week 2
Relationship: A relationship type between two entity types defines the set of all
associations between these entity types
Each instance of the relationship between members of these entity types is called a
relationship instance
Eg: passenger travels-in bus, here travels-in is a relation between passenger n bus
entity.
Entity diagrams with Attributes:
Bus Entity:
Bus no
No-of-
Bus seats
Type
Ticket Entity:
Tno Journe
y Date
Seat no
Bus no
Ticket
Src
Gender
Dest
Deptime
Passenger Entity:
PNR
no
Gender
Passenger Pname
Addr
Age
Phno
Driver Entity:
Phno
Bus no
Did
Driver Brno
Dname
Addr Age
MD Entity:
MDnam
e
MD Phno
Age
Addr
Gender
Branch Entity:
Brno
Email-Id Branch
Brnam
e
Phno
Addr
Cardinalities: Cardinality
* Relationships can have different connectivity
A ratio, such as 1:1, 1: N, M: 1, and M: N, gives a cardinality constraint or numeric
restriction on the possible relationships
One-to-one (1:1): relationship from entity type S to entity type T is one in which an
entity from S is related to at most one entity from T and vice versa.
Journe Seat
PNR y Date No
Pname no
Tno
Src
Reserve Ticket
Age Passenger s
Dest
Bus no
Gender
One-to-many (1: N): relationship from entity type S to entity type T is one in which two
or more entities from T can be related to an entity from S.
Pname
PNR
Bus no no
Age
No-of- Contain
Bus Passenger
seats s
Gender
Type Phno
Addr
Many-to- One (M: 1): relationship from entity type S to entity type T is one in which an
entity from S can be related to two or more entities from T.
MDName
Phno Brno Brnam
e
Age
MD Ow Branch
ns Addr
Addr
Gender Email-Id
Phno
Many-to-many (M:N): relationship from entity type S to entity type T is one in which
an entity from S can be related to two or more entities from T, and an entity from T can
be related to two or more entities from S.
E.g.:
Passenger Ticket (1:1)
Bus Passenger(1:N)
Branch MD (M: 1)
Driver Branch (M: N)
Entity-Relationship Diagrams:
Driv Reserve
es s
Ow
ns
Driver Issue
Ticket
s Branch
Types of Entities:
There are 2 types of entities
1. Strong Entity: A Strong Entity is one that exists on its own, independent of other
entities. An entity set that has primary key is termed as strong entity set.
2. Weak Entity: A weak entity can be identified uniquely only by considering the
primary key of another (owner) entity.
Owner entity set and weak entity set must participate in a one-to-many
relationship set (one owner, many weak entities).
Weak entity set must have total participation in this identifying relationship set.
Aggregation: Aggregation refers to an abstraction in which a relationship between
objects is regarded as a higher-level object.
Generalization: It is a relationship that exists between a high level entity set and one or
more lower level entity set.
Specialization: Is the process of defining a set of subclasses of a super class .The set of
subclasses is based upon some distinguishing characteristics of the entities in the super
class. Super class/subclass relationships and specialization can be diagrammatically
represented in ER diagrams.
Generalization Diagram:
MD
Ow
ns
Schema: The schema defines the tables, the fields in each table, and the relationships
between fields and tables.
Schemas for Entities:
Bus Schema:
Bus no No-of-seats Type
Bus (Bus no Primary key Varchar (20), No-of-seats Int (10), Type Varchar (20))
Ticket Schema:
Ticket (Tno Primary key Int (10), Journey Date Date, Day Varchar (10), Src Varchar
(20), Dest Varchar (20), Deptime Varchar (20), Gender Varchar (10), Bus no Foreign key
Varchar (20))
Passenger Schema:
Reservation Schema:
Reservation (PNR no Foreign key Int (10), Journey Date Date, No-of-seats Int (10),
Cancellation Schema:
Cancellation (PNR no Foreign key Int (10), Journey Date Date, No-of-seats Int (10),
Bus Table:
Ticket Table:
Date no
2100 15/08/10 Sun 35 Hyd Delhi 10hrs M AP1000
2710 20/08/10 Mon 28 B’lore Hyd 15hrs F AP2014
1020 22/08/10 Wed 38 Delhi B’lore 18hrs F AP2387
6100 10/08/10 Tue 20 Hyd Chennai 14hrs M AP09525
1478 25/08/10 Wed 15 Chennai Delhi 06hrs F AP29457
Passenger Table:
PNR no Pname Age Gender Addr Phno
457 Satish 27 M Plot No. 5 RK. Puram, Hyderabad 9008745625,
147 Sheela 25 F H.No:1-9-88,DilsukhNagar,Hyderabad 9000998683,
258 Vinay 24 M H.No:11-12-31, Hospet 9908730203
Reservation Table:
PNR Journey No-of- Address Phno Status
no Date seats
457 10/08/10 40 Plot No. 5 RK. Puram, Hyderabad 9008745625 Yes
,
147 20/08/10 40 H.No:188,DilsukhNagar,Hyderabad 9000998683 No
,
258 25/08/10 40 H.No:11-12-31, Hospet 9908730203 Yes
501 14/08/10 40 Plot no:35, Gandhinagar Hyderabad 9247881452 No
605 18/08/10 40 Plot no:34,Arandalpet, Guntur 9052980015 Yes
Cancellation Table:
PNR Journey No-of- Addr Phno Status
no Date seats
457 10/08/10 40 Plot No. 5 RK. Puram, Hyderabad 9008745625 Yes
,
147 20/08/10 40 H.No:188,DilsukhNagar,Hyderabad 9000998683 No
,
258 25/08/10 40 H.No:11-12-31, Hospet 9908730203 Yes
501 14/08/10 40 Plot no:35, Gandhinagar Hyderabad 9247881452 No
605 18/08/10 40 Plot no:34,Arandalpet, Guntur 9052980015 Yes
Week 4
1st Normal Form: A relation in the 1st normal form if it contains no multivalue attributes.
2nd Normal Form: A relation is in 2nd normal form if it is in 1st normal form and every
non-key attribute is fully functionally dependent on the primary key.
Partial Dependency is functional dependencies in which one or more non-key
attributes are functionally depends on part of the primary key.
3rd Normal Form: A relation is in 3rd normal form if it is in 2nd normal form and no
transitive dependency exists.
Transitive dependency is a relation is a functional dependency between two (or
more) non-key attributes.
Boyce Codd Normal Form: A relation in a Boyce Codd normal form iff every
determinant in the relation is a candidate key.
4th Normal Form: When a relation is in BCNF there are no longer any anomalies that
result from functional dependency.
Eg: 1st Normal Form:
PNR no Pname Age Gender Addr Phno
456 Satish 28 M Plot No. 5 RK. Puram, Hyderabad 9008745625,
746 Sheela 25 F H.No:1-9-88,DilsukhNagar,Hyderabad 9000998683,
129 Vinay 24 M H.No:11-12-31, Hospet 9908730203
231 Kiran 29 M
654 Bindu 22 F
Date no
2100 15/08/10 Sun 35 Hyd Delhi 10hrs M AP1000
2710 20/08/10 Mon 28 B’lore Hyd 15hrs F AP2014
1020 22/08/10 Wed 38 Delhi B’lore 18hrs F AP2387
6100 10/08/10 Tue 20 Hyd Chennai 14hrs M AP09525
1478 25/08/10 Wed 15 Chennai Delhi 06hrs F AP29457
no
2100 35 Hyd Delhi 10:00 M AP1000
15/08/10 Sun
20/08/10 Mon
22/08/10 Wed
10/08/10 Tue
25/08/10 Wed
Week 5
Step 1
Make sure you already downloaded the MySQL essential 5.0.45 win32.msi file. Double
click on the .msi file.
Step 2
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.
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.
Step 5
The program features you selected are being installed. Please wait while the setup wizard
installs MySQL 5.0. This may take several minutes.
Step 6
To continue, click next.
Step 7
To continue, click next.
Step 8
Wizard Completed. Setup has finished installing MySQL 5.0. Check the configure the
MySQL server now to continue. Click Finish to exit the wizard
d.
Step 9
The configuration wizard will allow you to configure the MySQL Server 5.0 server
instance. To continue, click next.
Step 10
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.
Step 12
Please set the security options by entering the root password and confirm retype the
password. To continue, click next.
Step 13
Ready to execute? Clicks execute to continue.
Step 14
Processing configuration in progress.
Step 15
Configuration file created. Windows service MySQL5 installed. Press finish to close the
wizard.
WEEK 6
PNR_ Ticke name age sex PPNO category journe source Destination PBUS_
NO t_No y_time NO
1 20 RAMU 35 M abc123 AC 12 CHENNAI HYD 8Y08
2 33 HANSIKA 25 F abc139 AC 10 HYD BANGLORE 4B08
3 22 AMITH 26 M abc125 AC 11 HYD CHENNAI 9W01
6 23 AVINASH 36 M abc127 AC 13 MUMBAI CHENNAI 6Z07
7 24 ANAND 28 M abc128 NONAC 8 MUMBAI BANGLORE 3A06
Week 7
PNR_NO
1
2
3
6
7
5 rows in set (0.02 sec)
select name from passenger where sex IN(select sex from passenger where sex='M');
Name
RAMU
AMITH
AVINASH
ANAND
Ticket_No Name
20 RAMU
233 HANSIKA
22 AMITH
23 AVINASH
24 ANAND
select Ticket_No from passenger where name IN(select name from passenger
where name like 'A%H');
Ticket_No
22
23
24
3 rows in set (0.02 sec)
select * from passenger where name IN(select name from passenger where name like
'A%');
PNR Ticke name age sex PPNO category journey source Destination PBUS_
_NO t_No _time NO
3 22 AMITH 26 M abc125 AC 11 HYD CHENNAI 9W01
6 23 AVINASH 36 M abc127 AC 13 MUMBAI CHENNAI 6Z07
7 24 ANAND 28 M abc128 NON AC 8 MUMBAI BANGLORE 3A06
RBUS_NO
9W01
6Z07
2 rows in set (0.00 sec)
PNR_ Ticket name age sex PPNO category journe source Destination
PBUS
NO _No y_time _NO
1 20 RAMU 35 M abc123 AC 12 CHENNAI HYD 8Y08
2 33 HANSIKA 25 F abc139 AC 10 HYD BANGLORE 4B08
3 22 AMITH 26 M abc125 AC 11 HYD CHENNAI 9W01
6 23 AVINASH 36 M abc127 AC 13 MUMBAI CHENNAI 6Z07
7 24 ANAND 28 M abc128 NON AC 8 MUMBAI BANGLORE 3A06
Week 8&9
1) Write a query to display the info present in the passenger and cancellation table .
(HINT: use UNION operator)
PNR_NO PBUS_NO
1 8Y08
2 4B08
3 9W01
6 6Z07
7 3A06
1 9W01
2 6Z07
3 4B08
6 9W01
7 4B08
10 rows in set (0.00 sec)
3) Display the number of days in a week on which the 9W01 bus is available?
count(Ticket_No) PNR_NO
1 1
1 2
1 3
1 6
1 7
6) find the number of tickets booked in each class where the number of seats is grater
then ‘1’Hint: use GROUP BY, WHERE and HAVING CLAUSES?
PNR_NO no_of_seats
1 2
2 5
3 4
6 5
7 8
5 rows in set (0.00 sec)
sum(no_of_seats)
20
1 row in set (0.03 sec)
8) Write a query to count the number of tickets for the buses,which traveled after the
date’14/3/2009’.Hint:use HAVING CLAUSES?
VIEWS
CREATING VIEWS:
DROPPING VIEWS:
WEEK 10
INSERT TRIGGER:
mysql>delimiter //
UPDATE TRIGGER:
DELETE TRIGGER:
The procedure is a program that performs an action and does not return a value (outside
of IN OUT and OUT parameters).
BEGIN
----
END;
BEGIN
IF p_name = 'Lewis'
THEN
DBMS_OUTPUT.PUT_LINE( p_name || ': ' ||
p_address );
END IF;
v_a_variable := 99;
p_an_in_out_parameter := v_a_variable;
p_an_out_parameter := SYSDATE;
END;
DECLARE
v_employee VARCHAR2(30) := 'BillyBob';
v_number NUMBER := 22;
v_date DATE;
BEGIN
my_first_proc(
p_name => v_employee,
p_an_in_out_parameter => v_number,
p_an_out_parameter => v_date );
DBMS_OUTPUT.PUT_LINE(
v_employee || ', ' ||
to_Char(v_number) || ', ' ||
to_char(v_date) );
my_first_proc(
p_an_in_out_parameter => v_number,
p_an_out_parameter => v_date );
DBMS_OUTPUT.PUT_LINE(
v_employee || ', ' ||
to_Char(v_number) || ', ' ||
to_char(v_date) );
END;
End;
Example:
mysql> delimiter //
WEEK 12
Cursors:
In SQL procedures, a cursor make it possible to define a result set (a set of
data rows) and perform complex logic on a row by row basis. By using the same
mechanics, an SQL procedure can also define a result set and return it directly to the
caller of the SQL procedure or to a client application.
A cursor can be viewed as a pointer to one row in a set of rows. The cursor can only
reference one row at a time, but can move to other rows of the result set as needed.
To work with cursors you must use the following SQL statements:
• DECLARE CURSOR
• OPEN
• FETCH
• CLOSE
The following example demonstrates the basic use of a read-only cursor within an SQL
procedure:
SET p_sum = 0;
OPEN c;
WHILE(SQLSTATE = '00000') DO
SET p_sum = p_sum + p_sal;
FETCH FROM c INTO p_sal;
END WHILE;
CLOSE c;
END;
Example-2:
mysql> delimiter //
mysql> create procedure myproc(IN pp VARCHAR(10))
-> BEGIN
-> DECLARE s_age INTEGER;
-> DECLARE s VARCHAR(10);
-> DECLARE cur CURSOR FOR SELECT age,sex from passenger where
PPNO=pp;
-> OPEN cur;
-> FETCH cur into s_age,s;
-> insert into Ticket values(21,'2012/12/12',s_age,s,'EARTH','SWARGAM',
'10:00:00','9W01');
-> Close cur;
-> END;
-> //
Query OK, 0 rows affected (0.00 sec)
mysql> CALL myproc('abc123');
-> //
Query OK, 1 row affected (0.03 sec)