38
1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar (M), Medchal Dist.

Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

1

Laboratory Manual

of

Database Management System

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

ANURAG COLLEGE OF ENGINEERING Aushapur (V) Ghatkesar (M) Medchal Dist

2

Week 1 E-R Model

Analyze the problem carefully and come up with entities in it Identify what date 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 Definitions

1 Entity It is a ldquothingrdquo or ldquoobjectrdquo in the real world that is distinguishable from all other objects ER Model represents an entity with an independent existence

Eg

2 Attributes The properties that characterize an entity set are called its attributes An attribute is referred to by the terms data items data element data field item

Ex attributes for bus entity and ticket entity

21 Types of Attributes 211 Simple and Composite attributes Simple attribute cannot be divided into simpler

components Ex Busno in Bus entity

Composite attribute can be split into components Ex DataOfBirth

212 Single valued and multivalued attributes Single valued attribute can take on only a single value for each entity instance

Ex Age of passenger in Passenger entity Multivalued attribute can take up many values

Ex Phno in Passenger entity

213 Derived attributesIn some cases two or more attribute values are related With the help of one attribute we get the value of another attribute Age and DOB attributes With the DOB we get the age of the person to the current date

3

22 Types of Keys

221 Candidate key It can be defined as minimal super key or irreducible super key In other words an

attribute or combination of attributes that identifies the record uniquely but none of its proper subsets can identify the record uniquely Busno serviceno---------------gtcandidate key

222 Primary key A candidate key that is used by the database designer for unique identification of each

row in a table is known as primary key A primary key consists of one or more attributes of the table

223 Foreign key An attribute (or) combination of attributes in a table whose value matches a primary

key in another table Ex Busno is a primary key in Bus entity Busno is a foreign key in Ticket entity

224 Partial key A weak entity type normally has a partial key which is the set of attributes that can

uniquely identify weak entity that are related to the same owner entity The entities in the ldquoRoadway travelsrdquo are-

1) Bus 2) Ticket 3) Passenger 4) Branch Entities and their attributes

I Bus (Busno Name Type) II Ticket (Tktno jour_date source destination busno amount dept_time reach_time)

III Passenger (Pnrno name age Gender Tktno ppno) IV Branch (Name location)

Datatypes

1 Bus entity Busno Varchar(8) Primary key Name Varchar(15) Type Varchar (10)

2 Ticket entity Tktno Numeric(8) Primary key Jour_date Date Src Varchar(10) dest Varchar(10) Dept_time Time Busno Varchar(8) Foreign key Amt Varchar(4) Reach_time Time

4

3 Passenger entity Pnr_no Numeric(9) Primary key Tktno Numeric(8) Foreign key Name Varchar(15) Age Int(4) Gender Char(10) MaleFemale PpnoVarchar(15)

4 Reservation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

5 Cancellation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

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

6 Branch entity Name Varchar(8) Location Varchar(20)

5

Week 2 Concept design with E ndash R model

Relate the entities appropriately Apply cardinalities for each relationship Identify strong entities and weak entities (if any) Indicate the type of relationship (totalpartial) Try to incorporate generalization aggregation specialization etc wherever required ER-Model

Describes data as entities relationships and attributes The ER-Model is important preliminary for its role in database design ER Model is usually shown pictorially using entity relationship diagrams

Relationship - it is defined as an association among two or more entities

Entity diagram for BUS

Bus

Bus no

Name

Type

6

Entity diagram for Ticket

Entity diagram for Passenger

Entity diagram for Reservation

Reservation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Passenger

Ppno

Age

Name Tktno

Gender

Pnrno

Ticket

Busno

Tktno

Reach_time

Dest

Src

Dept_time Amt

Jour_date

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 2: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

2

Week 1 E-R Model

Analyze the problem carefully and come up with entities in it Identify what date 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 Definitions

1 Entity It is a ldquothingrdquo or ldquoobjectrdquo in the real world that is distinguishable from all other objects ER Model represents an entity with an independent existence

Eg

2 Attributes The properties that characterize an entity set are called its attributes An attribute is referred to by the terms data items data element data field item

Ex attributes for bus entity and ticket entity

21 Types of Attributes 211 Simple and Composite attributes Simple attribute cannot be divided into simpler

components Ex Busno in Bus entity

Composite attribute can be split into components Ex DataOfBirth

212 Single valued and multivalued attributes Single valued attribute can take on only a single value for each entity instance

Ex Age of passenger in Passenger entity Multivalued attribute can take up many values

Ex Phno in Passenger entity

213 Derived attributesIn some cases two or more attribute values are related With the help of one attribute we get the value of another attribute Age and DOB attributes With the DOB we get the age of the person to the current date

3

22 Types of Keys

221 Candidate key It can be defined as minimal super key or irreducible super key In other words an

attribute or combination of attributes that identifies the record uniquely but none of its proper subsets can identify the record uniquely Busno serviceno---------------gtcandidate key

222 Primary key A candidate key that is used by the database designer for unique identification of each

row in a table is known as primary key A primary key consists of one or more attributes of the table

223 Foreign key An attribute (or) combination of attributes in a table whose value matches a primary

key in another table Ex Busno is a primary key in Bus entity Busno is a foreign key in Ticket entity

224 Partial key A weak entity type normally has a partial key which is the set of attributes that can

uniquely identify weak entity that are related to the same owner entity The entities in the ldquoRoadway travelsrdquo are-

1) Bus 2) Ticket 3) Passenger 4) Branch Entities and their attributes

I Bus (Busno Name Type) II Ticket (Tktno jour_date source destination busno amount dept_time reach_time)

III Passenger (Pnrno name age Gender Tktno ppno) IV Branch (Name location)

Datatypes

1 Bus entity Busno Varchar(8) Primary key Name Varchar(15) Type Varchar (10)

2 Ticket entity Tktno Numeric(8) Primary key Jour_date Date Src Varchar(10) dest Varchar(10) Dept_time Time Busno Varchar(8) Foreign key Amt Varchar(4) Reach_time Time

4

3 Passenger entity Pnr_no Numeric(9) Primary key Tktno Numeric(8) Foreign key Name Varchar(15) Age Int(4) Gender Char(10) MaleFemale PpnoVarchar(15)

4 Reservation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

5 Cancellation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

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

6 Branch entity Name Varchar(8) Location Varchar(20)

5

Week 2 Concept design with E ndash R model

Relate the entities appropriately Apply cardinalities for each relationship Identify strong entities and weak entities (if any) Indicate the type of relationship (totalpartial) Try to incorporate generalization aggregation specialization etc wherever required ER-Model

Describes data as entities relationships and attributes The ER-Model is important preliminary for its role in database design ER Model is usually shown pictorially using entity relationship diagrams

Relationship - it is defined as an association among two or more entities

Entity diagram for BUS

Bus

Bus no

Name

Type

6

Entity diagram for Ticket

Entity diagram for Passenger

Entity diagram for Reservation

Reservation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Passenger

Ppno

Age

Name Tktno

Gender

Pnrno

Ticket

Busno

Tktno

Reach_time

Dest

Src

Dept_time Amt

Jour_date

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 3: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

3

22 Types of Keys

221 Candidate key It can be defined as minimal super key or irreducible super key In other words an

attribute or combination of attributes that identifies the record uniquely but none of its proper subsets can identify the record uniquely Busno serviceno---------------gtcandidate key

222 Primary key A candidate key that is used by the database designer for unique identification of each

row in a table is known as primary key A primary key consists of one or more attributes of the table

223 Foreign key An attribute (or) combination of attributes in a table whose value matches a primary

key in another table Ex Busno is a primary key in Bus entity Busno is a foreign key in Ticket entity

224 Partial key A weak entity type normally has a partial key which is the set of attributes that can

uniquely identify weak entity that are related to the same owner entity The entities in the ldquoRoadway travelsrdquo are-

1) Bus 2) Ticket 3) Passenger 4) Branch Entities and their attributes

I Bus (Busno Name Type) II Ticket (Tktno jour_date source destination busno amount dept_time reach_time)

III Passenger (Pnrno name age Gender Tktno ppno) IV Branch (Name location)

Datatypes

1 Bus entity Busno Varchar(8) Primary key Name Varchar(15) Type Varchar (10)

2 Ticket entity Tktno Numeric(8) Primary key Jour_date Date Src Varchar(10) dest Varchar(10) Dept_time Time Busno Varchar(8) Foreign key Amt Varchar(4) Reach_time Time

4

3 Passenger entity Pnr_no Numeric(9) Primary key Tktno Numeric(8) Foreign key Name Varchar(15) Age Int(4) Gender Char(10) MaleFemale PpnoVarchar(15)

4 Reservation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

5 Cancellation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

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

6 Branch entity Name Varchar(8) Location Varchar(20)

5

Week 2 Concept design with E ndash R model

Relate the entities appropriately Apply cardinalities for each relationship Identify strong entities and weak entities (if any) Indicate the type of relationship (totalpartial) Try to incorporate generalization aggregation specialization etc wherever required ER-Model

Describes data as entities relationships and attributes The ER-Model is important preliminary for its role in database design ER Model is usually shown pictorially using entity relationship diagrams

Relationship - it is defined as an association among two or more entities

Entity diagram for BUS

Bus

Bus no

Name

Type

6

Entity diagram for Ticket

Entity diagram for Passenger

Entity diagram for Reservation

Reservation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Passenger

Ppno

Age

Name Tktno

Gender

Pnrno

Ticket

Busno

Tktno

Reach_time

Dest

Src

Dept_time Amt

Jour_date

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 4: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

4

3 Passenger entity Pnr_no Numeric(9) Primary key Tktno Numeric(8) Foreign key Name Varchar(15) Age Int(4) Gender Char(10) MaleFemale PpnoVarchar(15)

4 Reservation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

5 Cancellation entity Pnr_no Numeric(9) Foreign key Jour_date Date Noofseats Int(8) Address Varchar(50) Contactno numeric(9) Status Char (3) YesNo

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

6 Branch entity Name Varchar(8) Location Varchar(20)

5

Week 2 Concept design with E ndash R model

Relate the entities appropriately Apply cardinalities for each relationship Identify strong entities and weak entities (if any) Indicate the type of relationship (totalpartial) Try to incorporate generalization aggregation specialization etc wherever required ER-Model

Describes data as entities relationships and attributes The ER-Model is important preliminary for its role in database design ER Model is usually shown pictorially using entity relationship diagrams

Relationship - it is defined as an association among two or more entities

Entity diagram for BUS

Bus

Bus no

Name

Type

6

Entity diagram for Ticket

Entity diagram for Passenger

Entity diagram for Reservation

Reservation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Passenger

Ppno

Age

Name Tktno

Gender

Pnrno

Ticket

Busno

Tktno

Reach_time

Dest

Src

Dept_time Amt

Jour_date

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 5: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

5

Week 2 Concept design with E ndash R model

Relate the entities appropriately Apply cardinalities for each relationship Identify strong entities and weak entities (if any) Indicate the type of relationship (totalpartial) Try to incorporate generalization aggregation specialization etc wherever required ER-Model

Describes data as entities relationships and attributes The ER-Model is important preliminary for its role in database design ER Model is usually shown pictorially using entity relationship diagrams

Relationship - it is defined as an association among two or more entities

Entity diagram for BUS

Bus

Bus no

Name

Type

6

Entity diagram for Ticket

Entity diagram for Passenger

Entity diagram for Reservation

Reservation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Passenger

Ppno

Age

Name Tktno

Gender

Pnrno

Ticket

Busno

Tktno

Reach_time

Dest

Src

Dept_time Amt

Jour_date

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 6: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

6

Entity diagram for Ticket

Entity diagram for Passenger

Entity diagram for Reservation

Reservation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Passenger

Ppno

Age

Name Tktno

Gender

Pnrno

Ticket

Busno

Tktno

Reach_time

Dest

Src

Dept_time Amt

Jour_date

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 7: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

7

Entity diagram for Cancellation

Entity diagram for Branch

The cardinality ratio - specifies the number of entities to which another entity can be associated via a relationship set For a binary relationship set R between entity sets A amp B the mapping cardinality must be one of the following

1 One-to-One An entity in A is associated with at most one entity in B and vice versa Ex ldquoIssued tordquo relation between ticket and passenger entities

2 One-to-many An entity in A is associated with any no of entities in B An entity in B is at most associated with at most one entity in A

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name tktno

Gend

er

age

ppno

Cancellation

Address

Noofseats

Jour_date Status

contactno

Pnrno

Branch

Name Location

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 8: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

8

Ex ldquocontainsrdquo relation between bus and passenger entities

3 Many-to-One An entity in A is associated with at most one entity in B However an entity in B can be associated with any no of entities in A

Ex ldquoTravels inrdquo relation between passenger and bus entities

4 Many-to-many An entity in A is associated with an no of entities in B and an entity in

B can be associated with any no of entities in A

Passenger Passenger Travels in

pnrno

Age

Name

Ppno

Busno

Type

name

Gender

tktno

Bus Passenger Contains

Busno

Type

Name

Ppno

tktno

Pnrno

Age

Gender

name

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 9: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

9

Types of entities - Weak and strong entity - an entity set may not have sufficient attributes to form a primary key Such an entity set is termed a weak entity set An entity set that has primary key is termed a strong entity set Entity Relationship diagram consisting of Bus ticket Passenger and Branch entites

Branch

Name Location

Ticket Passenger Issued to

tktno Journey

Date

Src

Dest

Dept_time

Reach_time

Pnrno

name

Gend

er

age

ppno tktno

Bus

Bus no

Name

Type

Consists

of

Deals

with

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 10: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

10

1 Generalization It consists of identifying some common characteristics of a collection of entity set and creating new entity set that contains entities possessing these common characteristics

It is defined by using lsquoISArsquo (Is a) relationship Ex

2 Aggregation It allows us to indicate that a relationship set participates in another relationship set

Ex

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 11: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

11

3 Specialization It is the process of identifying subsets of an entity set (the super set) that share some distinguishing characteristics This entity type is called the super class of the specialization

Ex

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 12: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

12

Week-3 Relational model

Represent all entities (strong week) in tabular fashion Represent relationships in a tabular fashion There are different ways of representing as tables based on the cardinality Represent attributes as columns in the tables or as tables based on the requirement Different types of attributes (composite multivalued and derived) Schema Databases change over time as information is inserted and deleted The collection of information stored in a database at a particular moment is called an instance of the database The overall design of the database is called the database schema Entity sets to tables Relational shema for Bus relation Bus(Busnovarchar(8) namevarchar(15) typevarchar(10)) Relational shema for Ticket relation Ticket(Tktnonumeric jour_datedate srcvarchar(10) destvarchar(10) dept_timetime reach_timetime amtvarchar(4)busnovarchar(8)) Relational shema for Passenger relation Passenger(Pnrnonumeric(9)namevarchar(15)ageint(4)Genderchar(3)tktnonumeric(8)ppnovarchar(15)) Relationship sets to tables Relational shema for reservation relation Reservation(pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) Relational shema for Cancellation relation Cancellations((pnrnonumeric(9)jour_datedatenoofseatsint(8)addressvarchar(50)contactnonumeric(10)statuschar(3)) SAMPLE DATA IN TABLES Bus

Busno name type 100 Xyz ac 101 Pop Non ac 102 Xxx ac

Ticket

Tktno Jour_date Src Dest amt busno Dept_time Reach_time 10001 20-07-10 Hyd Delhi 800 100 0600 2200 10002 21-07-10 Hyd Chennai 700 101 0800 2300 10003 05-08-10 Delhi Hyd 800 102 0600 2200

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 13: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

13

Passenger Reservation

Pnrno Jour_date Noofseats Address Contactno Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes 1003 05-08-10 10 hyd 9949022222 No

Cancels

Pnrno Joudate Noofseats Address Contact_no Status 1001 20-07-10 4 Hyd 9492500000 Yes 1002 21-07-10 5 Sec 9492511111 Yes

Pnrno name tktno age Gender ppno 1001 Alekhya 10001 25 F ff11112 1002 Krupani 10002 27 F ff22332 1003 Prathima 10003 28 F F234444 1004 Prem 10004 30 M Ff202020

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 14: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

14

Week 4 Normalization

Database normalization is a technique for designing relational database tables to minimize duplication of information and in doing so to safeguard the database against certain types of logical or structural problems namely data anomalies The normalization forms are

1 First Normal Form 1NF requires that the values in each column of a table are atomic By atomic we mean that there are no sets of values within a column

2 Second Normal Form where the 1NF deals with atomicity of data the 2NF deals with relationships between composite key columns and non-key columns To achieve 2NF the tables should be in 1NF The 2NF any non-key columns must depend on the entire primary key In case of a composite primary key this means that non-key column canrsquot depend on only part of the composite key

3 Third Normal Form Any transitive dependencies have been removed 4 BoyceCodd normal Form Any remaining anomalies that result from functional

dependencies have been removed 5 Fourth Normal Form Any multivalued dependencies have been removed 6 Fifth Normal Form Any remaining anomalies have been removed

Applying Normalization to our Entities Consider Passenger Entity

A Passenger may consist of two phone numbers but atomic values should be there so we normalize the relation as follows Passenger The above relation is now in 1NF and the relation is 2NF as there are no partial functional dependencies and the relation is also in 3NF as there are no transitive dependencies Normalization of Bus entity Bus

Busno serviceno source destination bustype Noofseats 1001 3300 Hyd Delhi Ac 20 1002 4400 Hyd Chennai Ac 28 1003 5500 Hyd Bglore Non ac 30

In this relation the values in each column are atomic so it is already in 1NF In the Bus entity Busno+serviceno is the primary key

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000

9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

Pnrno pname age Gender ticketno address phno 2001 Alekhya 25 F 1111 Hno101 9999900000 2001 Alekhya 25 F 1111 Hno101 9999911111 2002 Krupani 26 F 2222 Hno102 9999912345 2003 pratima 28 F 3333 Hno103 9000000000

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 15: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

15

There exists following partial dependencies Busno ----gt BustypeNoofseats Serviceno----gtSourceDest So the relation will be in 2NF as follows

Busno serviceno 1001 3300 1002 4400 1003 5500

serviceno source destination 3300 Hyd Delhi 4400 Hyd Chennai 5500 Hyd Bglore

The above relation is 2NF And all columns directly depend on primary key So there is no transitive dependency and the relation is 3NF Normalization of Ticket entity

Ticketno Joudate Source Destination Amount Catcard 1111 2010-10-08 Hyd Delhi 1200 No 2222 2010-10-08 Hyd Chennai 1000 Yes 3333 2010-08-08 Hyd Bglore 800 Yes

In this relation the values in each column are atomic so it is already in 1NF In the above relation there are no partial functional dependencies so the relation is in 2NF The ticket entity might face the following transitive dependency Ticketno-------gt catcard Catcard---------gtamount So the relation is in 3NF

Ticketno Joudate Source Destination Catcard 1111 2010-10-08 Hyd Delhi No 2222 2010-10-08 Hyd Chennai Yes 3333 2010-08-08 Hyd Bglore Yes

Put the catcard and amount attributes in a separate table Then the relation should be in 3NF The above relation is 3NF as we have eliminated the transitive dependencies Finally all the tables are normalized and free from data redundancy partial functional dependencies and transitive dependencies

Busno bustype Noofseats 1001 Ac 20 1002 Ac 28 1003 Non ac 30

Catcard Amount No 1200 Yes 1000 Yes 800

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 16: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

16

Week 5 Installation of Mysql and Practicing DDL commands Installation of Mysql In this week you will learn creating databases how to create tables altering the tables dropping tables and databases if not required You will also try truncate rename commands etc Step 1 download mysql essential from the website wwwmysqlcomdownloads and save the exe file Steps 2amp3double click on the mysqlexe file to start installation

Steps 4amp5

Steps 6amp7

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 17: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

17

Steps 8amp9

Steps 10amp11

Step 12amp13

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 18: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

18

Steps 14amp15

Steps 16amp17

Steps 18amp19

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 19: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

19

1 Creation of databases mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (009 sec) mysqlgt create database dblab -gt Query OK 1 row affected (002 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+ 4 rows in set (000 sec) mysqlgt use dblab Database changed

2 Creation of tables mysqlgt create table groupamem(rollno numeric(10)name varchar(15)phone numeric (10) branch varchar(10)) Query OK 0 rows affected (042 sec) mysqlgt desc groupamem +--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 4 rows in set (003 sec)

3 Altering the table mysqlgt alter table groupamem add gender char(3) Query OK 0 rows affected (036 sec) Records 0 Duplicates 0 Warnings 0 mysqlgt desc groupamem

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 20: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

20

+--------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------+------+-----+---------+-------+ | rollno | decimal(100) | YES | | NULL | | | name | varchar(15) | YES | | NULL | | | phone | decimal(100) | YES | | NULL | | | branch | varchar(10) | YES | | NULL | | | gender | char(3) | YES | | NULL | | +--------+---------------+------+-----+---------+-------+ 5 rows in set (000 sec)

4 Dropping the table mysqlgt create table dd(name varchar(10)) Query OK 0 rows affected (009 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | dd | | groupamem | +------------------+ 2 rows in set (000 sec) mysqlgt drop table dd Query OK 0 rows affected (006 sec)

5 Dropping the database mysqlgt create database dbl Query OK 1 row affected (001 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dbl | | dblab | | mysql | | test | +--------------------+ 6 rows in set (001 sec) mysqlgt drop database dbl Query OK 0 rows affected (000 sec) mysqlgt show databases +--------------------+ | Database | +--------------------+ | information_schema | | dblab | | mysql | | test | +--------------------+

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 21: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

21

5 rows in set (000 sec)

6 Rename the tables mysqlgt rename table groupamem to ga Query OK 0 rows affected (003 sec) mysqlgt show tables +------------------+ | Tables_in_groupa | +------------------+ | ga | +------------------+ 1 row in set (000 sec)

7 Truncate the table mysqlgt insert into ga values(1111ram9885321456mbbsm) Query OK 1 row affected (006 sec) mysqlgt select from ga +--------+------+------------+--------+--------+ | rollno | name | phone | branch | gender | +--------+------+------------+--------+--------+ | 1111 | ram | 9885321456 | mbbs | m | +--------+------+------------+--------+--------+ 1 row in set (001 sec) mysqlgt truncate table ga Query OK 1 row affected (009 sec) mysqlgt select from ga Empty set (000 sec) Creation of tables for Roadway Travels

1 Bus mysqlgt create table bus(busno varchar(8)primary keyname varchar(15)type varchar(10)) Query OK 0 rows affected (017 sec)

2 Ticket mysqlgt create table ticket(tkt_no numeric(8)primary key jour_date date src varchar(10)dest varchar(10) busno varchar(8)amt varchar(4)dept_time time reach_time time) Query OK 0 rows affected (008 sec) mysqlgt alter table ticket add constraint tkt_fk foreign key(busno) references bus(busno) Query OK 0 rows affected (016 sec) Records 0 Duplicates 0 Warnings 0

3 Passenger mysqlgt create table passenger(pnr_no numeric(9)primary keytktno numeric(8)name varc har(15)age int(4)Gender char(10)ppno varchar(15)) Query OK 0 rows affected (006 sec) mysqlgt alter table passenger add constraint pas_fk foreign key(tktno) references ticket(tktno) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 22: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

22

4 Reservation mysqlgt create table reservation(pnr_no numeric(9)noofseats int(8)jour_date datetime address varchar (50) phno numeric(10)status char(3)) Query OK 0 rows affected (016 sec) mysqlgt alter table reservation add constraint res_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (017 sec) Records 0 Duplicates 0 Warnings 0

5 Cancel mysqlgt create table cancel(pnr_no numeric(9)noofseats int(4)address varchar(20) phno numeric(10)status char(3)) Query OK 0 rows affected (006 sec) mysqlgt alter table cancel add constraint cancel_fk foreign key(pnr_no) references passenger (pnr_no) Query OK 0 rows affected (014 sec) Records 0 Duplicates 0 Warnings 0

6 Branch mysqlgtcreate table branch(name varchar(8)location varchar(20)) Query OK 0 rows affected (006 sec)

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 23: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

23

Week 6 Practicing DML commandsDML commands are used to for managing data within the schema objects Use of insert command Inserting values into Bus table mysqlgt insert into bus values(1001rsquoMetrorsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1002rsquoMetro Journamrsquoac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1003rsquoExpressrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1004rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) mysqlgt insert into bus values(1005rsquoOrdinaryrsquononac) Query OK 1 row affected (003 sec) Inserting values into Ticket table mysqlgt insert into ticket values(11112010-10-08hyddelhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(22222010-10-08hyd delhi120010010900002 10000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(33332010-11-08hydbglore80010020900001 50000) Query OK 1 row affected (003 sec) mysqlgt insert into ticket values(44442010-11-08hydbglore 80010020900001 50000) Query OK 1 row affected (003 sec) Inserting values into Passenger table mysqlgt insert into passenger values(10011111alekhya25fpp1111) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10022222Krupani26fpp2222) Query OK 1 row affected (003 sec) mysqlgt insert into passenger values(10033333Pratima28fpp3333) Query OK 1 row affected (005 sec) mysqlgt insert into passenger values(10044444prem28mpp4444) Query OK 1 row affected (001 sec) Inserting values into reservation table mysqlgt insert into reservation values(20014rsquo2010-10-08rsquohno101msrd9999911111yes) Query OK 1 row affected (005 sec) mysqlgt insert into reservation values(20026 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (003 sec) mysqlgt insert into reservation values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (001 sec) mysqlgt insert into reservation values(20048 rsquo2010-08-08rsquohno 102chpy9000000000yes) Query OK 1 row affected (003 sec)

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 24: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

24

Inserting values into cancellation table mysqlgt insert into cancel values(20035 rsquo2010-08-08rsquohno101chpy9000011111yes) Query OK 1 row affected (005 sec) mysqlgt insert into cancel values(20048 rsquo2010-08-08rsquohno102chpy9000000000yes) Query OK 1 row affected (003 sec) mysqlgt insert into cancel values(20022 rsquo2010-10-08rsquohno102msrd9999900000yes) Query OK 1 row affected (005 sec) Inserting values into Branch table mysqlgtinsert into branch values(lsquokesinenirsquorsquosecrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquohydrsquo) Query OK 1 row affected (005 sec) mysqlgtinsert into branch values(lsquopunnamirsquorsquosecrsquo) Query OK 1 row affected (003 sec) Use of select command mysqlgt select from bus +--------+---------+ | busno | bustype | Type| +--------+---------+ | 1001 | Metro |ac| | 1002 | Metro Journam |ac| | 1003 | express |non ac| | 1004| Ordinary |non ac| | 1005 | Ordinary |non ac| +--------+---------+ 5 rows in set (000 sec) mysqlgt select from ticket +--------+------------+--------+------+----------+----------+-----------+--------+ | tkt_no | jour_date | src | dest | deptime | reachtime | busno |amt | +--------+------------+--------+------+----------+----------+-----------+--------+ | 1111 | 2010-10-08 | hyd | delhi | 090000 | 210000 | 1001 |1200| | 2222 | 2010-10-08| hyd | delhi | 090000 | 210000 | 1001 |1200| | 3333 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| | 4444 | 2010-11-08 | hyd | bglore | 090000 | 150000 | 1002 |800| +--------+------------+--------+------+----------+----------+-----------+ 4 rows in set (000 sec)set (000 sec) mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 1001 | 1111 | Alekhya | 25 | f | pp111 | | 1002 | 2222 | krupani | 26 | f | pp2222 | | 1003 | 3333 | pratima | 28| f | pp3333 | | 1004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003 sec)

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 25: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

25

mysqlgt select from reservation +-----------+-----------+--------------------+------------+--------+ | pnrnumber | noofseats | address | phno | status |jour_date| +-----------+-----------+--------------------+------------+--------+----------+ | 2001 | 4 | hno101msrd | 9999911111 | yes |2010-10-08| | 2001 | 6 | hno102msrd | 9999900000| yes |2010-10-08| | 2002 | 5 | hno101chpy | 9000011111 | yes |2010-10-08| | 2003 | 8 | hno102chpy | 9000000000 | no |2010-08-08| +-----------+-----------+--------------------+------------+--------+----------+ 4 rows in set (002 sec) mysqlgt select from cancel +-----------+-----------+--------------------+------------+--------+-----------+ | pnr_no | noofseats | address | phno | status |jour_date +-----------+-----------+--------------------+------------+--------+-----------+ | 2003 | 5 | hno101chpy | 9000011111 | yes |2010-08-08| | 2004 | 8 | hno102chpy | 9000000000| no |2010-08-08| | 2002 | 2 | hno102msrd | | 9999900000 | yes |2010-10-08| +-----------+-----------+--------------------+------------+--------+-----------+ 3 rows in set (000 sec) Use of update command mysqlgt update passenger set pnr_no=rsquo2001rsquo where tktno=1111 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2002rsquo where tktno=2222 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2003rsquo where tktno=3333 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt update passenger set pnr_no=rsquo2004rsquo where tktno=4444 Query OK 1 row affected (003 sec) Rows matched 1 changed 1 warnings 0 mysqlgt select from passenger +-------+-----------+---------+------+------+--------+ | pnrno | ticnumber | pname | age | Gender | ppno | +-------+-----------+---------+------+------+--------+ | 2001 | 1111 | Alekhya | 25 | f | pp111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | pratima | 28| f | pp3333 | | 2004 | 4444 | prem | 28 | m | pp444 4 | +-------+-----------+---------+------+------+--------+ 4 rows in set (003

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 26: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

26

Use of DELETE command mysqlgtdelete from branch where location =rsquosecrsquo Query OK 1 row affected (003 sec) mysqlgt select from branch +-----------+-----------+ | name |location | +-----------+-----------+ | kesineni | Hyd| | punnami| Hyd| +-----------+-----------+ 2 rows in set (000 sec)

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 27: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

27

Week 7 Querying In this week you are going to practice the queries (along with sub queries) using ANY ALL IN EXISTS NOT EXISTS UNION INTERSECT Constraints etc Practice the following queries 1 Display unique PNR_no of all passengers mysqlgt select distinct pnr_no from passenger +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Display all the names of male passengers mysqlgt select name from passenger where Gender=m +------+ | name | +------+ | prem | +------+ 1 row in set (001 sec) 3 Display ticket numbers and names of all the passengers mysqlgt select tktnoname from passenger +-------+----------+ | tktno | name | +-------+----------+ | 1111 | alekhya | | 2222 | krupani | | 3333 | prathima | | 4444 | prem | +-------+----------+ 4 rows in set (000 sec) 4 Display the source and destination having journey time more than 10 hours mysqlgt select srcdest from ticket where hour(timediff(reach_timedept_time))gt10 +------+-------+ | src | dest | +------+-------+ | hyd | delhi | | hyd | delhi | +------+-------+ 2 rows in set (000 sec)

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 28: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

28

5 Find the ticket numbers of passengers whose name starts with lsquoArsquo and ends with lsquoHrsquo mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) Empty set (003 sec) mysqlgt update passenger set name=amith where name=prem Query OK 1 row affected (007 sec) Rows matched 1 Changed 1 Warnings 0 mysqlgt select tktno from ticket where tktno=any(select tktno from passenger wher e name like ah) +-------+ | tktno | +-------+ | 4444 | +-------+ 1 row in set (000 sec) 6 Find the name of passengers whose age is between 30 and 45 mysqlgt select name from passenger where age between 30 and 45 +-------+ | name | +-------+ | amith | +-------+ 1 row in set (000 sec) 7 Display all the passengers names beginning with lsquoArsquo mysqlgt select all name from passenger where name like a +---------+ | name | +---------+ | alekhya | | amith | +---------+ 2 rows in set (000 sec) 8 Display the sorted list of passengers names mysqlgt select name from passenger order by name +----------+ | name | +----------+ | alekhya | | amith | | krupani | | prathima | +----------+ 4 rows in set (000 sec)

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 29: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

29

9 Display the Bus numbers that travel on Sunday and Wednesday mysqlgt select busno from bus where busno in(select busno from ticket where dayof week(jour_date)=1 or dayofweek(jour_date)=4) Empty set (005 sec) 10 Display the details of passengers who are traveling either in AC or NON_AC mysqlgt select pnr_nonameageGender from passenger where tktno in(select tktno fro m ticket where busno in(select busno from bus where type=ac or type=non ac )) +--------+----------+------+------+ | pnr_no | name | age | Gender | +--------+----------+------+------+ | 2001 | alekhya | 25 | f | | 2002 | krupani | 26 | f | | 2003 | prathima | 28 | f | | 2004 | amith | 35 | m | +--------+----------+------+------+ 4 rows in set (000 sec)

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 30: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

30

Week 8 amp 9 You are going to practice the queries using Aggregate functions (COUNT SUM AVG MAX and MIN) GROUP BY HAVING AND Creation of Views 1 Write a query to display the information present in the Passenger and Cancellation tables mysqlgt select pnr_no from passenger union select pnr_no from cancel +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 2 Write a query to display the busnumber with source and destination available in Roadway Travels mysqlgt select busnosrcdest from bus b ticket t where bbusno=tbusno group by busno +--------+--------+------+ | busno | source | dest | +--------+--------+------+ | 1001 | hyd | delhi | | 1001 | hyd | delhi | | 1002 | hyd | bglore | | 1002 | hyd | bglore | | +--------+--------+-----+ 4 rows in set (000 sec) 3 Display the number of days in a week on which AP444 bus is available mysqlgt select count(jour_date) from ticket where busno in(select busno from bus where busno=1111) and jour_date between 2010-10-04 and 2010-10-10 +------------------+ | count(jour_date) | +------------------+ | 0 | +------------------+ 1 row in set (000 sec) 4 Find the ticket numbers booked for each PNR_no using Group By clause mysqlgt select noofseatspnr_no from reservation where status=yes group by pnr_no +-----------+--------+ | noofseats | pnr_no | +-----------+--------+ | 4 | 2001 | | 6 | 2002 | | 5 | 2003 | +-----------+--------+ 3 rows in set (000 sec)

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 31: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

31

5 Find the distinct PNR numbers that are present mysqlgt select distinct pnr_no from reservation +--------+ | pnr_no | +--------+ | 2001 | | 2002 | | 2003 | | 2004 | +--------+ 4 rows in set (000 sec) 6 Find the number of tickets booked for each bus with bustype where the number of seats is greater than 1 mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes group by tktno having count()gt=1 +--------+---------+--------------+ | busno | type | booked_seats | +--------+---------+--------------+ | 1001 | ac | 4 | | 1002 | ac | 6 | | 1003 | non ac | 5 | | 1004 | non ac | 8 | +--------+---------+--------------+ 4 rows in set (000 sec) 7 Find the total number of cancelled seats mysqlgt select sum(noofseats) from cancel where status=yes +----------------+ | sum(noofseats) | +----------------+ | 7 | +----------------+ 1 row in set (000 sec) 8 Write a query to count the number of tickets for the buses which traveled after the date lsquo2010-08-06rsquo mysqlgt select busnotypenoofseats from bus breservation rticket tpassenger p where bbusno=tbusno and ttktno=ptktno and ppnr_no=rpnr_no and status=yes and jour_dategt2010-02-02 group by tktno having count()gt=1 +--------+---------+--------------+ | busno | bustype | booked_seats | +--------+---------+--------------+ | ap444 | nonac | 6 | | ap891 | nonac | 8 | | ap8830 | metro | 5 | +--------+---------+--------------+ 3 rows in set (001 sec)

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 32: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

32

Creation of Views mysqlgt create view takes as -gt select tktnoname from ticket tpassenger p where ttktno=ptktno Query OK 0 rows affected (044 sec) mysqlgt select tktno from takes +--------+ | tktno | +--------+ | 1111 | | 2222 | | 3333 | | 4444 | +--------+ 4 rows in set (005 sec) Dropping of Views mysqlgt drop view takes Query OK 0 rows affected (000 sec)

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 33: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

33

Week 10 TRIGGERS In this week you are going to work on the triggers Creation of insert trigger delete trigger update trigger Practice triggers using above database

A Trigger is a named database object which defines some action that the database should take when some databases related event occurs

Triggers are executed when you issues a data manipulation command like INSERT DELETE UPDATE on a table for which the trigger has been created They are automatically executed and also transparent to the user But for creating the trigger the user must have the CREATE TRIGGER privilege

In this section we will describe you about the syntax to create and drop the triggers and describe you some examples of how to use them

CREATE TRIGGER

CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_statement

By using above statement we can create the new trigger The trigger can associate only

with the table name and that must be refer to a permanent table Trigger_time means trigger action time It can be BEFORE or AFTER It is used to define that the trigger fires before or after the statement that executed it Trigger_event specifies the statement that executes the trigger The trigger_event can be any of the DML Statement INSERT UPDATE DELETE

We can not have the two trigger for a given table which have the same trigger action time and event For Instance we cannot have two BEFORE INSERT triggers for same table But we can have a BEFORE INSERT and BEFORE UPDATE trigger for a same table

Trigger_statement have the statement that executes when the trigger fires but if you want to execute multiple statement the you have to use the BEGINhellipEND compound statement We can refer the columns of the table that associated with trigger by using the OLD and NEW keyword OLDcolumn_name is used to refer the column of an existing row before it is deleted or updated and NEWcolumn_name is used to refer the column of a new row that is inserted or after updated existing row

In INSERT trigger we can use only NEWcolumn_name because there is no old row and in a DELETE trigger we can use only OLDcolumn_name because there is no new row But in UPDATE trigger we can use both OLDcolumn_name is used to refer the columns of a row before it is updated and NEWColumn_name is used to refer the column of the row after it is updated

1 Update Trigger

mysqlgt create trigger tri1 before update on reservation -gt for each row -gt begin -gt if newnoofseatsgt20 then -gt set newnoofseats=oldnoofseats -gt else -gt set newnoofseats=newnoofseats -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt update reservation set noofseats=10 where pnr_no=2001 Query OK 1 row affected (014 sec) Rows matched 1 Changed 1 Warnings 0

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 34: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

34

mysqlgt select from reservation -gt +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt update reservation set noofseats=25 where pnr_no=2003 Query OK 0 rows affected (000 sec) Rows matched 1 Changed 0 Warnings 0 mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec)

2 Insert Trigger mysqlgt create trigger tri2 before insert on passenger -gt for each row -gt begin -gt if newagegt29 then -gt set newppno=pp4567 -gt else -gt set newppno= -gt end if -gt end Query OK 0 rows affected (011 sec) mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | +--------+-------+----------+------+------+--------+ 4 rows in set (000 sec) mysqlgt insert into passenger values(20055555prem27m99) Query OK 1 row affected (007 sec)

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 35: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

35

mysqlgt select from passenger +--------+-------+----------+------+------+--------+ | pnr_no | tktno | name | age | Gender | ppno | +--------+-------+----------+------+------+--------+ | 2001 | 1111 | alekhya | 25 | f | pp1111 | | 2002 | 2222 | krupani | 26 | f | pp2222 | | 2003 | 3333 | prathima | 28 | f | pp3333 | | 2004 | 4444 | amith | 35 | m | pp4444 | | 2005 | 5555 | prem | 27 | m | | +--------+-------+----------+------+------+--------+ 5 rows in set (000 sec)

3 Delete Trigger mysqlgt create trigger tri3 before delete on cancel -gt for each row -gt begin -gt insert into reservation values(oldpnr_nooldnoofseatsoldjour_dateold addressoldphnooldstatus) -gt end Query OK 0 rows affected (011 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 4 rows in set (000 sec) mysqlgt delete from cancel where pnr_no=2004 Query OK 1 row affected (006 sec) mysqlgt select from reservation +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2001 | 10 | 2010-10-08 000000 | hno101msrd | 9999911111 | yes| | 2002 | 6 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| | 2004 | 8 | 2010-08-08 000000 | hno102chpy | 9000000000 | no| +--------+-----------+---------------------+--------------+------------+--------+ 5 rows in set (000 sec) mysqlgt select from cancel +--------+-----------+---------------------+--------------+------------+--------+ | pnr_no | noofseats | jour_date | address | phno | status| +--------+-----------+---------------------+--------------+------------+--------+ | 2003 | 5 | 2010-08-08 000000 | hno101chpy | 9000011111 | yes| | 2002 | 2 | 2010-10-08 000000 | hno102msrd | 9999900000 | yes| +--------+-----------+---------------------+--------------+------------+--------+ 2 rows in set (000 sec)

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 36: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

36

Week 11 Procedures In this session you are going to learn Creation of stored procedures execution of

procedure and modification of procedures Practice the procedures using above database

A stored procedure is a procedure (like a subprogram in a regular computing language) that is stored (in the database) Correctly speaking MySQL supports routines and there are two kinds of routines stored procedures which you call or functions whose return values you use in other SQL statements the same way that you use pre-installed MySQL functions like pi() mysqlgt create procedure p1(p_age int) -gt begin -gt select tktnonameGender from passenger where agegtp_age -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call p1(27) +-------+----------+------+ | tktno | name | Gender | +-------+----------+------+ | 3333 | prathima | f | | 4444 | amith | m | +-------+----------+------+ 2 rows in set (000 sec) Query OK 0 rows affected (001 sec) mysqlgt create procedure p2(pnr_id int) -gt begin -gt select jour_dateaddressphno from reservation where pnr_no=pnr_id -gt end -gt Query OK 0 rows affected (002 sec) mysqlgt call p2(2001) -gt +---------------------+--------------+------------+ | jour_date | address | phno | +---------------------+--------------+------------+ | 2010-10-08 000000 | hno101msrd | 9999911111 | +---------------------+--------------+------------+ 1 row in set (000 sec) Query OK 0 rows affected (002 sec)

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 37: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

37

Week 12 Cursors In this week you need to do the following Declare the cursor that defines the result set

Open the cursor to establish the result set Fetch the data into local variables as needed from the cursor one row at a time Close the cursor when done

Cursors are used when the SQL Select statement is expected to return more than one row Cursors are supported inside procedures and functions

Cursors must be declared and its definition contains the query The cursor must be defined in the DECLARE section of the program A cursor must be opened before processing and closed after processing

Syntax to declare the cursor DECLARE ltcursor_namegt CURSOR FOR ltselect_statementgt Multiple cursors can be declared in the procedures and functions but each cursor must

have a unique name And in defining the cursor the select_statement cannot have INTO clause Syntax to open the cursor

OPEN ltcursor_namegt By this statement we can open the previously declared cursor Syntax to store data in the cursor FETCH ltcursor_namegt INTO ltvar1gtltvar2gthelliphellip The above statement is used to fetch the next row if a row exists by using the defined open cursor Syntax to close the cursor CLOSE ltcursor_namegt By this statement we can close the previously opened cursor If it is not closed explicitly then a cursor is closed at the end of compound statement in which that was declared

mysqlgt create procedure cursor_ticket(tktid int) -gt begin -gt declare tkt_no int -gt declare tkt_src varchar(20) -gt declare tkt_dest varchar(20) -gt declare c1 cursor for select tktnosrcdest from ticket where tktno=tktid -gt open c1 -gt fetch c1 into tkt_no -gt tkt_srctkt_dest -gt select tkt_notkt_srctkt_dest -gt close c1 -gt end -gt Query OK 0 rows affected (003 sec) mysqlgt call cursor_ticket(4444) +--------+---------+----------+ | tkt_no | tkt_src | tkt_dest | +--------+---------+----------+ | 4444 | hyd | bglore | +--------+---------+----------+ 1 row in set (000 sec)

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)

Page 38: Database Management System · 1 Laboratory Manual of Database Management System DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ANURAG COLLEGE OF ENGINEERING Aushapur (V), Ghatkesar

38

Query OK 0 rows affected (002 sec) mysqlgt create procedure cursor_reserve(p_id int) -gt begin -gt declare res_id int -gt declare res_addr varchar(20) -gt declare c2 cursor for select pnr_noaddress from reservation where pnr_no=p_id -gt open c2 -gt fetch c2 into res_idres_addr -gt select res_idres_addr from reservation where pnr_no=p_id -gt close c2 -gt end -gt Query OK 0 rows affected (000 sec) mysqlgt call cursor_reserve(2002) -gt +--------+--------------+ | res_id | res_addr | +--------+--------------+ | 2002 | hno102msrd | +--------+--------------+ 1 row in set (010 sec) Query OK 0 rows affected (010 sec)