PART 1 DATABASE

Week 1 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 may be a physical object such as a house or a car, an event such as a house sale or a car service, or a concept such as a customer transaction or order.

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 Marital status Married Widowed Single

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

4. Composite Attribute: The attribute which can be split into components. 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 e Street City

Hno

7. Stored Attribute: Attribute that need to be stored permanently.

Eg: Name of employee 8. Derived Attribute: Attribute that can be calculated based on other attributes. It is represented with dotted lines.

EMP

Joining Date

Curren t Date

Types of keys: There are different types of keys in the database. 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 2. Candidate key: A candidate key is a combination of attributes that can be uniquely used to identify a database record without any extraneous data. Each table may have one or more candidate keys. One of these candidate keys is selected as the table primary key. Eg: An entity Ticket contains the attributes tno, bus no, PNR no, no-of-seats (candidate keys) 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.

Email-Id) Entity-Attributes with Data Types: BUS Entity Bus no: Varchar (20): Primary key No-of-seats: Int (10) Type: Varchar2 (20) . Partial key: A weak entity type normally has a partial key. No-of-seats. A primary key that made up of more than one attribute is known as a composite key. Age. age) Tno & bus no are primary keys. Deptime. Composite key: Sometimes it requires more than one attribute to uniquely identify an entity. Pname. Phno. Addr. Eg: bus (bus no. Dname. src. Src. Addr. age. which is the set of attribute that can uniquely identify weak entities that are related to the same owner entity. Bus no) MD (MDname. src. Journey Date. Age. src. src> <bname. Entities: Bus (Bus no. dest. gender) Bus no is the foreign key in the ticket entity. Bus no) Passenger (PNR no. Phno. 4. dest) Ticket (tno. Brname. A table might have many super keys. bus no. Gender. Type) Ticket (Tno. Addr. 5. Phno. Type) Driver (Did. Addr. Gender. bname. bname. Dest. Brno. bus no. Gender. dest> 6. src> <bus no. Phno. Super key: A super key is a combination of attributes that can be uniquely used to identify a database record. Seat no. dest) <Bus no> <bus no. Age) Branch (Brno.` Eg: ticket (tno.Eg: bus (bus no.

TICKET Entity Tno: Int (10): Primary key Journey Date: Date Seat no: Int (10) Src: Varchar (20) Dest: Varchar (20) Deptime: Time Gender: Varchar (10): Male/Female Bus no: Varchar (20): Foreign key 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) .

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) .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.

Entity diagrams with Attributes: Bus Entity: Bus no Bus No-ofseats Type Ticket Entity: Tno Journe y Date Seat no Bus no Ticket Src Gender Deptime Dest .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.

Passenger Entity: PNR no Gender Passenger Addr Age Phno Pname Driver Entity: Phno Did Driver Dname Addr MD Entity: Bus no Brno Age .

M: 1. . and M: N. 1: 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.MDnam e MD Age Addr Phno Gender Branch Entity: Brno Branch Brnam e Phno Email-Id Addr Cardinalities: Cardinality * Relationships can have different connectivity A ratio. such as 1:1.

Bus no No-ofseats Type PNR no Pname Age Bus Contain s Phno Addr Passenger Gender Many-to.Pname PNR no Tno Journe y Date Seat No Src Age Passenger Reserve s Bus no Addr Phno Deptime Ticket Dest Gender 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. .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.

g. and an entity from T can be related to two or more entities from S. E.: Passenger Ticket (1:1) Bus Passenger(1:N) Branch MD (M: 1) Driver Branch (M: N) Entity-Relationship Diagrams: Bus Contain s Passenger MD Reserve s Driv es Ow ns Issue s Driver Ticket Branch .MDName Phno Age MD Addr Brno Brnam e Addr Ow ns Gender Email-Id Branch 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.

The set of subclasses is based upon some distinguishing characteristics of the entities in the super class. Generalization: It is a relationship that exists between a high level entity set and one or more lower level entity set. 2. many weak entities). An entity set that has primary key is termed as strong entity set. Strong Entity: A Strong Entity is one that exists on its own. Specialization: Is the process of defining a set of subclasses of a super class . 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. Super class/subclass relationships and specialization can be diagrammatically represented in ER diagrams. 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. Generalization Diagram: MD Ow ns Branch1 HYD Branch2 Mumbai Branch3 Chennai . independent of other entities.Types of Entities: There are 2 types of entities 1.

and the relationships between fields and tables. Day Varchar (10). No-of-seats Int (10). Bus no Foreign key Varchar (20)) Passenger Schema: PNR no Pname Age Gender Addr Phno . Schemas for Entities: Bus Schema: Bus no No-of-seats Type Bus (Bus no Primary key Varchar (20). Src Varchar (20). the fields in each table.Week 3 Schema: The schema defines the tables. Gender Varchar (10). Deptime Varchar (20). Dest Varchar (20). Journey Date Date. Type Varchar (20)) Ticket Schema: Tno Journey Date Day Seat no Src Dest Deptime Gender Bus no Ticket (Tno Primary key Int (10).

Addr Varchar (30). Phno Varchar (20).Passenger (PNR no Primary key Int (10). Addr Varchar (30). Age Int (10). Journey Date Date. Status Varchar (10)) Tables for Each Entity: Bus Table: Bus no AP10A05 AP23B25 AP29B78 AP10A05 AP29J501 Ticket Table: Tno 2100 2710 Journey Day Seat no 35 28 Src Hyd B’lore Dest Delhi Hyd Deptime 10hrs 15hrs Gender M F Bus no AP1000 AP2014 No-of-seats 40 40 50 40 50 Type A/C Super Luxury Express Super Deluxe Express Date 15/08/10 Sun 20/08/10 Mon . Phno Varchar (20). Journey Date Date. Pname Varchar (20). Status Varchar (10)) Cancellation Schema: PNR no Journey Date No-of-seats Addr Phno Status Cancellation (PNR no Foreign key Int (10). Gender Varchar (10). No-of-seats Int (10). Phno Varchar (20)) Reservation Schema: PNR no Journey Date No-of-seats Addr Phno Status Reservation (PNR no Foreign key Int (10). No-of-seats Int (10). Addr Varchar (30).

Hospet Plot no:35. Gandhinagar Hyderabad Plot no:34. Gandhinagar Hyderabad Plot no:34.DilsukhNagar.Hyderabad H. Guntur 9008745625 .Arandalpet.No:1-9-88. Puram.Arandalpet. Hospet Plot no:35. Status Yes . Puram. Hyderabad H.1020 6100 1478 22/08/10 Wed 10/08/10 Tue 25/08/10 Wed 38 20 15 Delhi Hyd Chennai B’lore Chennai Delhi 18hrs 14hrs 06hrs F M F AP2387 AP09525 AP29457 Passenger Table: PNR no 457 147 258 501 605 Pname Satish Sheela Vinay Kiran Bindu Age 27 25 24 29 22 Gender M F M M F Addr Plot No. 5 RK. 5 RK. Hyderabad H. Hyderabad Phno 9008745625 . 9000998683 . Puram.Hyderabad H. 9000998683. 9908730203 9247881452 9052980015 Yes No Yes No Yes Address Phno Status Cancellation Table: PNR no 457 Journey Date 10/08/10 No-ofseats 40 Addr Plot No.No:188.DilsukhNagar. 9908730203 9247881452 9052980015 Reservation Table: PNR no 457 147 258 501 605 Journey Date 10/08/10 20/08/10 25/08/10 14/08/10 18/08/10 No-ofseats 40 40 40 40 40 Plot No.No:11-12-31. Guntur Phno 9008745625.No:11-12-31. 5 RK.

Normal Form: A state of a relation that results from applying simple rules regarding functional dependencies to that relation. 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.Arandalpet. Guntur Week 4 9000998683 . Functional Dependencies: A functional dependency is a constraint between two attributes or two sets of attributes.No:11-12-31.147 258 501 605 20/08/10 25/08/10 14/08/10 18/08/10 40 40 40 40 H. Gandhinagar Hyderabad Plot no:34. 9908730203 9247881452 9052980015 No Yes No Yes Normalization: The process of decomposing relations with anomalies to produce smaller well structured relations. 3rd Normal Form: A relation is in 3rd normal form if it is in 2nd normal form and no transitive dependency exists.No:188.DilsukhNagar. 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. A functional dependency of B on A represented by an arrow A->B Eg: Bus no->Bname. Eg: 1st Normal Form: .Hyderabad H. Partial Dependency is functional dependencies in which one or more non-key attributes are functionally depends on part of the primary key. Transitive dependency is a relation is a functional dependency between two (or more) non-key attributes. Hospet Plot no:35. Type 1st Normal Form: A relation in the 1st normal form if it contains no multivalue attributes.

5 RK. Hospet Plot no:35. Hyderabad Plot No.DilsukhNagar. PNR no 457 457 746 746 129 231 231 654 Pname Satish Satish Sheela Sheela Vinay Kiran Kiran Bindu Age 27 27 25 25 24 29 29 22 Gender M M F F M M M F Addr Plot No.DilsukhNagar.No:11-12-31.Hyderabad H. 5 RK. Guntur Phno 9008745625.DilsukhNagar. Gandhinagar Hyderabad Plot no:35. Puram. Gandhinagar Hyderabad Plot no:34. Hyderabad H.Arandalpet.Hyderabad H. 9908730203 9247881452 9052980015 Multivalue attributes are normalized into single value attributes.No:11-12-31. Puram. 5 RK.Arandalpet.Hyderabad H.No:1-9-88. Puram. Gandhinagar Hyderabad Plot no:35. 9008745625.DilsukhNagar. 9000998683.PNR no 456 746 129 231 654 Pname Satish Sheela Vinay Kiran Bindu Age 28 25 24 29 22 Gender M F M M F Addr Plot No. Guntur Phno 9008745625. Hyderabad H. 9008745625. 5 RK.DilsukhNagar.No:1-9-88. 9000998683.Hyderabad H. Gandhinagar Hyderabad Plot no:34.No:1-9-88. Puram.Hyderabad H. 9908730203 9247881452 9247881452 9052980015 . 9000998683. Puram. 9908730203 9247881452 9247881452 9052980015 Eg: 2nd Normal Form: PNR no 457 457 746 746 129 231 231 654 Pname Satish Satish Sheela Sheela Vinay Kiran Kiran Bindu Age 27 27 25 25 24 29 29 22 Gender M M F F M M M F Addr Plot No. Hospet Plot no:35. 5 RK.No:11-12-31.No:1-9-88.Arandalpet. Hospet Plot no:35.No:1-9-88. Hyderabad Plot No. 9000998683. Gandhinagar Hyderabad Plot no:34. 9000998683. Hyderabad H. Guntur Phno 9008745625.

Gender PNR no -> Addr.DilsukhNagar.Arandalpet. Age.No:1-9-88. 5 RK. Guntur Phno 9008745625. Puram. 9000998683. 9908730203 9247881452 9052980015 Src Hyd B’lore Delhi Hyd Chennai Dest Delhi Hyd B’lore Chennai Delhi Deptime 10hrs 15hrs 18hrs 14hrs 06hrs Gender M F F M F Bus no AP1000 AP2014 AP2387 AP09525 AP29457 Date 15/08/10 Sun 20/08/10 Mon 22/08/10 Wed 10/08/10 Tue 25/08/10 Wed Tno -> Journey Date Journey Date -> Day .PNR no is the only primary key .so the partial dependency is as follows: PNR no -> Pname. Phno PNR no 457 746 129 231 654 Pname Satish Sheela Vinay Kiran Bindu Age 27 25 24 29 22 Gender M F M M F PNR no 457 746 129 231 654 Eg: 3rd Normal Form: Tno 2100 2710 1020 6100 1478 Journey Day Seat no 35 28 38 20 15 Addr Plot No. Gandhinagar Hyderabad Plot no:34.Hyderabad H. Hospet Plot no:35.No:11-12-31. Hyderabad H.

45 win32.msi file. Step 2 This is MySQL Server 5.msi file. .0. The setup wizard will install MySQL Server 5.45 on your computer.The Table is normalized as follows: Tno 2100 2710 1020 6100 1478 Seat no 35 28 38 20 15 Src Hyd B’lore Delhi Hyd Chennai Dest Delhi Hyd B’lore Chennai Delhi Deptime 10:00 15:00 18:00 14:00 06:00 Gender M F F M F Bus no AP1000 AP2014 AP2387 AP09525 AP29457 Journey Date 15/08/10 20/08/10 22/08/10 10/08/10 25/08/10 Day Sun Mon Wed Tue Wed Week 5 1. To continue. click next.0 release 5.0 setup wizard. Double click on the .0. Steps for installing MySQL Step 1 Make sure you already downloaded the MySQL essential 5.

For common program features select Typical and it’s recommended for general use. click next.Step 3 Choose the setup type that best suits your needs. Step 4 . To continue.

This may take several minutes. Destination folder will be in C:\Program Files\MySQL\MySQL Server 5. . click next.This wizard is ready to begin installation.0. Step 5 The program features you selected are being installed. Please wait while the setup wizard installs MySQL 5. To continue.0\.

Step 7 To continue.Step 6 To continue. click next. . click next.

. To continue. Setup has finished installing MySQL 5.0 server instance.0. Step 9 The configuration wizard will allow you to configure the MySQL Server 5.Step 8 Wizard Completed. Click Finish to exit the wizard d. click next. Check the configure the MySQL server now to continue.

click next. click next. To continue.Step 10 Select a standard configuration and this will use a general purpose configuration for the server that can be tuned manually. Step 11 Check on the install as windows service and include bin directory in windows path. To continue. .

Step 13 Ready to execute? Clicks execute to continue.Step 12 Please set the security options by entering the root password and confirm retype the password. To continue. . click next.

Step 14 Processing configuration in progress. Windows service MySQL5 installed. Press finish to close the wizard. Step 15 Configuration file created. .

02 sec) mysql> insert into BUS values('3A06'.'MUMBAI'.WEEK 6 Mysql> create table BUS(BUS_NO varchar(10) primary key.'HYD'. 0 rows affected (0.06 sec) Mysql> desc BUS. Field Type BUS_NO varchar(10) Source varchar(10) destination varchar(10) 3 rows in set (0.00 sec) Null NO YES YES Key PRI Default NULL NULL Extra mysql> insert into BUS values('9W01'. Query OK. Query OK. 1 row affected (0.'HYD'.'CHENNAI').destination varchar(10)).'BANGLORE'). Query OK.'BANGLORE'). .03 sec) mysql> insert into BUS values('4B08'. 1 row affected (0. source varchar(10).

TBUS_NO varchar(10) references BUS(BUS_NO)).’HYD’. 1 row affected (0.’M’.'MUMBAI'. Query OK.'CHENNAI'.’CHENNAI’. Field Ticket No journey date age sex source destination dep_time TBUS_NO 8 rows in set (0.Query OK.’F’.’CHENNAI’.. source varchar(10). 1 row affected mysql>insert into Ticket values(22.’20100810’.36.’8Y08’).’9W01’).’HYD’. Query OK.02 sec) source MUMBAI HYD MUMBAI CHENNAI HYD destination BANGLORE BANGLORE CHENNAI HYD CHENNAI mysql>create table Ticket(Ticket_No integer(10) primary key. 1 row affected (0. age integer(10).06 sec) mysql>insert into Ticket values(20. 1 row affected Type int(10) datetime int(10) varchar(10) varchar(10) varchar(10) varchar(10) varchar(10) Null NO YES YES YES YES YES YES YES Key PRI Default NULL NULL NULL NULL NULL NULL NULL Extra . mysql> desc Ticket.sex varchar(10).’19:30:00’.03 sec) mysql> insert into BUS values('6Z07'.'CHENNAI'). journey_date date. BUS_NO 3A06 4B08 6Z07 8Y08 9W01 5 rows in set (0. 1 row affected (0. destination varchar(10). Query OK. dep_time varchar(10).’18:30:00’.25.. Query OK.’20101010’.03 sec) mysql> insert into BUS values('8Y08'.'HYD').01 sec) mysql> SELECT * FROM BUS.

Query OK.source varchar(10).’20101120’.’M’.’MUMBAI’.06 sec) mysql> desc passenger.’22:30:00’. 1 row affected mysql>insert into Ticket values(33. 1 row affected mysql> select * from Ticket.35.Ticket_No integer(1 0) references Ticket(Ticket_No).’BANGLORE’.’20100909’. Field PNR_NO Ticket_No Name Age Sex PPNO Category journey_time source destination Type int(10) int(10) varchar(10) int(10) varchar(10) varchar(10) varchar(10) int(10) varchar(10) varchar(10) Null NO YES YES YES YES YES YES YES YES YES Key PRI Default NULL NULL NULL NULL NULL NULL NULL NULL NULL Extra 10 rows in set (0.sex varchar(10) .’17:30:00’.’3A06’).’6Z07’).'CHENNAI'.05 sec) mysql> insert into passenger values(1.'AC'. Ticket_No 20 22 23 23 33 journey_date 2010-10-10 00:00:00 2010-08-10 00:00:00 2010-09-09 00:00:00 2010-09-10 00:00:00 2010-11-20 00:00:00 age 36 25 44 34 30 sex F M M F F source CHENNAI HYD MUMBAI MUMBAI HYD destination HYD CHENNAI CHENNAI BANGLORE BANGLORE dep_time 19:30:00 18:30:00 12:30:00 17:30:00 22:30:00 TBUS_No 8Y08 9W01 6Z07 3A06 4B08 mysql> create table passenger(PNR_NO integer(10) primary key.’F’.'HYD'.’20100910’.name varchar(10)..destination varchar(10)).’4B08’).’F’. Query OK.PPNO varchar(10).age integer(10). Query OK.’CHENNAI’.’12:30:00’.’HYD’.30.’MUMBAI’.. 1 row affected mysql>insert into Ticket values(23.'Ramu'. 0 rows affected (0.mysql>insert into Ticket values(23. 1 row affected (0.44. Query OK.34.journey_time integer(10). Query OK.'abc123'.’BANGLORE’.05 sec) .category varchar(10).'M'..'8Y08').12.20.

06 sec) mysql> desc Reservation.26.'hansika'.'YES '.'Amith'.28.address varchar(10).'abc128'.'4B08'). 0 rows affected (0.'AC'. Query OK.'abc125'. Query OK.00 sec) Type int(10) date int(10) varchar(10) varchar(10) varchar(10) varchar(10) Null YES YES YES YES YES YES YES Key Default NULL NULL NULL NULL NULL NULL NULL Extra mysql> insert into Reservation values(1.'CHENNAI'.'M'.RBUS_NO varchar(10) references BUS(BUS_NO)). 1 row affected mysql> insert into passenger values(7.’MUMBAI’.'abc139'. Query OK. Field PNR_NO journey_date no_of_seats address Contact_NO Status RBUS_NO 7 rows in set (0. .25.no_of_seats integer(10). 'CHENNAI'.'HYD'.8.'AC'.'M'.'M'.'Anand'.23.’MUMBAI’.Contact_NO varchar (10). 1 row affected mysql> insert into passenger values(3.'2010/10/10'.36. Query OK.'NON AC'.22.status varchar(10).00 sec) mysql> create table Reservation(PNR_NO integer(10) references passenger(PNR_NO).10. 'HYD'.'abc127'.33.'9W01'). PNR_ NO 1 2 3 6 7 Ticke t_No 20 33 22 23 24 name RAMU HANSIKA AMITH AVINASH ANAND age sex PPNO 35 25 26 36 28 M F M M M abc123 abc139 abc125 abc127 abc128 category AC AC AC AC NONAC journe y_time 12 10 11 13 8 source CHENNAI HYD HYD MUMBAI MUMBAI Destination HYD BANGLORE CHENNAI CHENNAI BANGLORE PBUS_ NO 8Y08 4B08 9W01 6Z07 3A06 5 rows in set (0.2.'3A06').24.11.’BANGALORE’ . 1 row affected mysql> select * from passenger.’BANGALORE’.'HYD'.'AC'. 1 row affected mysql> insert into passenger values(6. Query OK.'6Z07').mysql> insert into passenger values(2.'Avinash'.'M'.13.'9W01').'9959190929'. journey_date date.

'9985512524'.00 sec) Type int(10) date int(10) varchar(10) varchar(10) varchar(10) varchar(10) Null YES YES YES YES YES YES YES Key Default NULL NULL NULL NULL NULL NULL NULL Extra mysql> insert into Cancellation values(61'2010/12/09'.'YES '.'MUMBAI'.'BANGALORE'.'9985512234'.'4B08').'2010/08/11'.'2010/09/10'.00 sec) no_of_seats 2 4 5 8 5 address HYD MUMBAI BANGLORE HYD CHENNAI Contact_NO 9959190929 9985512234 9985587564 9985547564 9985432123 status YES YES YES NO.'HYD'.8. Query OK. Query OK.'9985432123'.'9985587564'. Query OK.'9W01').no_of_seats integer(10).'YES '. Query OK. 1 row affected mysql> select * from Reservation. 1 row affected mysql> select * from cancellation.5.'9W01'). 1 row affected mysql> insert into Reservation values(2.'2010/11/20'. 1 row affected mysql> insert into Reservation values(7.'9985587564'.3.CBUS_NO varchar(10) references BUS(BUS_NO)). 1 row affected (0.'NO '.02 sec) mysql> insert into Reservation values(3. 1 row affected mysql> insert into Reservation values(6.4. journey_date date.'NO '. | Field PNR_NO journey_date no_of_seats address Contact_NO Status CBUS_NO 7 rows in set (0.5.'6Z07').'CHENNAI'.address varchar(10).'2010/11/20'. .status varchar(10). mysql> desc cancellation.'HYD'. Query OK.'YES'.Query OK. PNR_NO journey_date 1 2010-10-10 3 2010-08-11 6 2010-09-10 7 2010-11-20 2 2010-11-20 5 rows in set (0. NO RBUS_NO 9W01 6Z07 3A06 9W01 4B08 Mysql>create table Cancellation(PNR_NO integer(10) references passenger(PNR_NO).'3A06').Contact_NO varchar (10).

PNR_NO 1 2 3 6 7 journey_date 2010-12-09 2010-08-10 2010-09-10 2010-09-10 2010-11-20 no_of_seats 3 4 7 1 5 address HYD CHENNAI CHENNAI HYD MUMBAI Contact_NO 9985512524 9985512224 9985511224 9985511294 9985511264 status YES YES YES YES NO CBUS_NO 9W01 6Z07 4B08 9W01 4B08 5 rows in set (0. .00 sec) Week 7 1) Display unique PNR_no of all passengers ? select PNR_NO from passenger where PNR_NO IN(select PNR_NO from passenger).02 sec) 2) DISPLAY ALL THE NAMES OF MALE PASSENGERS? select name from passenger where sex IN(select sex from passenger where sex='M'). PNR_NO 1 2 3 6 7 5 rows in set (0.

02 sec) 6) FIND THE NAMES OF PASSENGERS WHOS AGE IS BEETWIN 30 AND 40? Name RAMU HANSIKA AMITH AVINASH ANAND .Name RAMU AMITH AVINASH ANAND 3) DISPLAY THE TICKET NUMBERS AND NAMES OF ALL THE PASSENGERS? select Ticket_No.06 sec) 5) FIND THE TICKET NUMBERS OF THE PASSENGERS WHO’S NAME START WITH ‘A’ AND ENDS WITH ‘H’? select Ticket_No from passenger where name IN(select name from passenger where name like 'A%H'). Ticket_No 20 233 22 23 24 5 rows in set (0. Ticket_No 22 23 24 3 rows in set (0. source destination CHENNAI HYD HYD CHENNAI MUMBAI CHENNAI 3 rows in set (0.name from passenger where PNR_NO IN(select PNR_NO from passenger).00 sec) 4) DISPLAY THE SOURCE AND DESTINATION HAVING JOURNEY TIME MORE THEN 10HRS? select source.destination from passenger where journey_time IN(select jou rney_time from passenger where journey_time>10).

select name from passenger where age IN(select age from passenger where age between 30 and 45).00 sec) 8) DISPLAY THE SORTED LIST OF PASSENGERS NAMES? select name from passenger order by name. name AMITH ANAND AVINASH HANSIKA RAMU 5 rows in set (0.00 sec) 9) DISPLAY THE BUS NUMBERS THAT TRAVEL ON SUNDAY AND WEDNESDAY? select RBUS_NO from Reservation where journey_date IN(select journey_date from Reservation where dayname(journey_date)='Sunday' UNION select journey_date from Reservation where dayname(journey_date)='Wednesday'). PNR _NO 3 6 7 Ticke t_No 22 23 24 name AMITH AVINASH ANAND age 26 36 28 sex PPNO M M M abc125 abc127 abc128 category AC AC NON AC journey _time 11 13 8 source HYD MUMBAI MUMBAI Destination CHENNAI CHENNAI BANGLORE PBUS_ NO 9W01 6Z07 3A06 3 rows in set (0.01 sec) 7) DISPLAY THE ALL THE PASSENGERS NAMES STARTS WITH ‘A’? select * from passenger where name IN(select name from passenger where name like 'A%'). RBUS_NO 9W01 6Z07 . name RAMU AVINASH 2 rows in set (0.

02 sec) Week 8&9 1) Write a query to display the info present in the passenger and cancellation table .CBUS_NO from cancellation. PNR_ NO 1 2 3 6 7 Ticket _No 20 33 22 23 24 name RAMU HANSIKA AMITH AVINASH ANAND age 35 25 26 36 28 sex M F M M M PPNO abc123 abc139 abc125 abc127 abc128 category AC AC AC AC NON AC journe y_time 12 10 11 13 8 source CHENNAI HYD HYD MUMBAI MUMBAI Destination PBUS _NO HYD 8Y08 BANGLORE 4B08 CHENNAI 9W01 CHENNAI 6Z07 BANGLORE 3A06 5 rows in set (0.00 sec) 10) DISPLAY THE DETAILS OF PASSENGERS WHO ARE TRAVELING EITHER IN ‘AC’ OR ‘NON_AC’ (USING ONLY ‘IN’ OPERATOR) select * from passenger where category IN('AC'.PBUS_NO from passenger UNION select PNR_NO.'NON AC'). (HINT: use UNION operator) select PNR_NO. PNR_NO 1 2 3 6 7 1 2 3 6 7 PBUS_NO 8Y08 4B08 9W01 6Z07 3A06 9W01 6Z07 4B08 9W01 4B08 .2 rows in set (0.

Count(journey_date) 2 1 row in set (0.RBUS_NO.Hint:use GROUP BY on PNR_No? select count(Ticket_No). count(Ticket_No) 1 1 1 1 1 PNR_NO 1 2 3 6 7 5) Find the distinct PNR numbers that are present? select distinct PNR_NO from Reservation. BUS_NO 3A06 4B08 6Z07 8Y08 9W01 5 rows in set (0.00 sec) RBUS_NO 9W01 journey_date 2010-10-10 4) Find number of tickets booked for each PNR-no using GROUP BY CLAUSE.10 rows in set (0.05 sec) source MUMBAI HYD MUMBAI CHENNAI HYD destination BANGLORE BANGLORE CHENNAI HYD CHENNAI 3) Display the number of days in a week on which the 9W01 bus is available? select count(journey_date).journey_date from Reservation where RBUS_NO='9W01' GROUP BY RBUS_NO HAVING journey_date IN(select journey_date from Reservation where dayname(journey_date) between 'Monday'and 'Sunday').00 sec) 2) Write quarry to display different traveling options available in British airway? select * from bus.PNR_NO from passenger GROUP BY PNR_NO. .

no_of_seats from Reservation GROUP BY PNR_NO HAVING no_of_seats>1. 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.Hint:use HAVING CLAUSES? select COUNT(Ticket_No). PNR_NO 1 2 3 6 7 5 rows in set (0.PNR_NO 1 3 6 7 2 5 rows in set (0.TBUS_NO.02 sec) 6) find the number of tickets booked in each class where the number of seats is grater then ‘1’Hint: use GROUP BY.journey_date from Ticket GROUP BY TBUS_NO HAVING journey_date>'2009/3/14'.which traveled after the date’14/3/2009’. WHERE and HAVING CLAUSES? select PNR_NO.00 sec) 7) Find the total number of cancelled seats? select sum(no_of_seats) from cancellation. COUNT(Ticket_No) 1 1 1 1 1 TBUS_NO 3A06 4B08 6Z07 8Y08 9W01 journey_date 2010-09-10 00:00:00 2010-11-20 00:00:00 2010-09-09 00:00:00 2010-10-10 00:00:00 2010-08-10 00:00:00 no_of_seats 2 5 4 5 8 .

Query OK.02 sec) mysql> select * from busview. 0 rows affected (0. 0 rows affected (0. -> end.00 sec) DROPPING VIEWS: mysql> drop view busview.00 sec) VIEWS CREATING VIEWS: mysql> create view busview as select * from BUS. BUS_NO 3A06 4B08 6Z07 8Y08 9W01 5 rows in set (0.5 rows in set (0. -> // Query OK.00 sec) mysql> select * from BUS.  // BUS_NO 3A06 4B08 6Z07 source MUMBAI HYD MUMBAI CHENNAI HYD destination BANGLORE BANGLORE CHENNAI HYD CHENNAI source MUMBAI HYD MUMBAI destination BANGLORE BANGLORE CHENNAI . INSERT TRIGGER: mysql>delimiter // mysql> create trigger insr before INSERT ON cancellation -> for each row -> begin -> update bus set source='HYDERABAD' where source='HYD'. Triggers: To write a TRIGGER to ensure that Bus table does not contain duplicate of null values in Bus_No column. WEEK 10 1.

-> end.  // BUS_NO 3A06 4B08 6Y01 6Z07 8Y08 source MUMBAI HYD TIRUPATHI MUMBAI CHENNAI destination BANGLORE BANGLORE VIJAYAWADA CHENNAI HYD .13 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from BUS.13 sec) mysql> update passenger set category='NON AC' where name='Ramesh'.4.'2010/12/11'.03 sec) mysql> select * from BUS. -> // Query OK.'YES'. -> // Query OK.'VIJAYAWADA').'9W01').'9987654321'. 1 row affected (0.  // BUS_NO 3A06 4B08 6Z07 8Y08 9W01 source MUMBAI HYD MUMBAI CHENNAI HYD destination BANGLORE BANGLORE CHENNAI HYD CHENNAI UPDATE TRIGGER: mysql> create trigger updt before update on passenger -> for each row -> begin -> insert into BUS values('6Y01'. 0 rows affected (0.'KOLKATA'.'TIRUPATI'.8Y08 9W01 5 rows in set (0.00 sec) CHENNAI HYD HYD CHENNAI mysql>insert into cancellation values(7. -> // Query OK. 1 row affected (0.

 // BUS_NO 3A06 4B08 6Y01 6Z07 7D0$ 8Y08 9W01 7 rows in set (0.03 sec) mysql> select * from BUS. 1 row affected (0.'SIMLA'). -> // Query OK.9W01 6 rows in set (0.00 sec) DELETE TRIGGER: HYD CHENNAI mysql> create trigger del before delete on passenger -> for each row -> begin -> insert into BUS values('7D0$'.'KOCHIN'.02 sec) mysql> delete from passenger where name='Ramesh'.00 sec) source MUMBAI HYD TIRUPATI MUMBAI KOCHIN CHENNAI HYD destination BANGLORE BANGLORE VIJAYAWADA CHENNAI SIMLA HYD CHENNAI . -> end. -> // Query OK. 0 rows affected (0.

A procedure is declared as: CREATE OR REPLACE PROCEDURE ( ) AS BEGIN ---END. A simple PL/SQL Procedure Example: CREATE OR REPLACE PROCEDURE my_first_proc (p_name IN VARCHAR2 := 'Lewis'. p_an_out_parameter OUT DATE ) AS . p_address IN VARCHAR2 := '123 Mockingbird Ln'.WEEK 11 Procedures: The procedure is a program that performs an action and does not return a value (outside of IN OUT and OUT parameters). p_an_in_out_parameter IN OUT NUMBER.

' || to_char(v_date) ). p_an_out_parameter => v_date ). v_date DATE. v_number NUMBER := 22.PUT_LINE( p_name || ': ' || p_address ). END. END. p_an_out_parameter := SYSDATE.of tickets passengers who age is greater than 40 Create procedure myproc() . call this procedure as: DECLARE v_employee VARCHAR2(30) := 'BillyBob'. p_an_in_out_parameter := v_a_variable. ' || to_Char(v_number) || '. p_an_in_out_parameter => v_number. BEGIN my_first_proc( p_name => v_employee. my_first_proc( p_an_in_out_parameter => v_number. BEGIN IF p_name = 'Lewis' THEN DBMS_OUTPUT.v_a_variable VARCHAR2(30).PUT_LINE( v_employee || '. p_an_out_parameter => v_date ). ' || to_Char(v_number) || '. END IF. DBMS_OUTPUT. v_a_variable := 99. ' || to_char(v_date) ). Example: To find no.PUT_LINE( v_employee || '. DBMS_OUTPUT.

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. but can move to other rows of the result set as needed.00 sec) Query OK. Open the cursor to establish the result set. 2. 0 rows affected (0. The cursor can only reference one row at a time. Example: mysql> delimiter // mysql> create procedure retrieve() -> begin -> select COUNT(Ticket_No) from Ticket where age>=22. Close the cursor when done To work with cursors you must use the following SQL statements: • • • • DECLARE CURSOR OPEN FETCH CLOSE . By using the same mechanics. you need to do the following: 1.02 sec) WEEK 12 Cursors: In SQL procedures. Fetch the data into local variables as needed from the cursor. 4. mysql> CALL retrieve(). 3. 0 rows affected (0.05 sec) mysql> delimiter . Declare a cursor that defines a result set. -> end. COUNT(Ticket_No) 5 1 row in set (0. To use cursors in SQL procedures. End.Begin Select count(ticket_id) from ticket where age>=40. one row at a time. -> // Query OK. 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.

-> insert into Ticket values(21. 0 rows affected (0.'9W01'). FETCH FROM c INTO p_sal. '10:00:00'. OPEN c. -> // Query OK. DECLARE c CURSOR FOR SELECT SALARY FROM EMPLOYEE.'SWARGAM'. FETCH FROM c INTO p_sal. SET sum = p_sum. END WHILE.s.s_age. CLOSE c. -> Close cur. Example-2: mysql> delimiter // mysql> create procedure myproc(IN pp VARCHAR(10)) -> BEGIN -> DECLARE s_age INTEGER.'2012/12/12'. END. DECLARE p_sum INTEGER.'EARTH'. SET p_sum = 0. -> DECLARE s VARCHAR(10). DECLARE p_sal INTEGER.s.sex from passenger where PPNO=pp. -> END.00 sec) . WHILE(SQLSTATE = '00000') DO SET p_sum = p_sum + p_sal. -> DECLARE cur CURSOR FOR SELECT age. -> OPEN cur. -> FETCH cur into s_age.The following example demonstrates the basic use of a read-only cursor within an SQL procedure: CREATE PROCEDURE sum_salaries(OUT sum INTEGER) LANGUAGE SQL BEGIN DECLARE SQLSTATE CHAR(5) DEFAULT '00000'.

-> // Query OK.00 sec) .mysql> CALL myproc('abc123'). 1 row affected (0. Ticket_No 2000 2100 2200 2300 2400 3300 TBUS_NO 8Y08 9W01 9W01 6Z07 3A06 4B08 journey_date 2010-10-10 00:00:00 2012-12-12 00:00:00 2010-08-10 00:00:00 2010-09-09 00:00:00 2010-09-10 00:00:00 2010-11-20 00:00:00 age 36 35 25 44 34 30 sex F M M M F F source CHENNAI EARTH HYD MUMBAI MUMBAI HYD destination HYD SWARGAM CHENNAI CHENNAI BANGLORE BANGLORE dep_ti 19:30: 10:00: 18:30: 12:30: 17:30: 22:30: 6 rows in set (0.03 sec) mysql> select * from Ticket.

Sign up to vote on this title
UsefulNot useful

Master Your Semester with Scribd & The New York Times

Special offer: Get 4 months of Scribd and The New York Times for just $1.87 per week!

Master Your Semester with a Special Offer from Scribd & The New York Times