Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

Dbms Lab Manual r20 Syllabus

Uploaded by

swamylns707
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Dbms Lab Manual r20 Syllabus

Uploaded by

swamylns707
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 48

CMR Engineering College CSE-CSM

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING (AI & ML)


Applicable From 2020-21 Admitted Batch

LAB MANUAL (MASTER)

Name of the Lab : DATABASE MANAGEMENT SYSTEMS

Subject Code : IT406PC

Branch : Computer Science & Engineering (AI&ML)

Year : II Year I semester

Regulation : R20

A.Y. : 2022-23

DBMS Lab Manual


1
CMR Engineering College CSE-CSM

CSE (AI & ML) Department Vision & Mission


Vision:
 To produce admirable and competent graduates and experts in CSE Artificial Intelligence
& Machine Learning through the quality technical education, innovations and research
to improve the life style and need of the society.

Mission:
 M1: To impart value based technical education in CSE specific to AI & ML through
innovative teaching and learning methods.
 M2: To produce outstanding professionals by imparting quality training, hands-on-
experience and value based education.
 M3: To produce competent graduates suitable for industries and organizations at global
level including research and development for Social responsibility.

DBMS Lab Manual


2
CMR Engineering College CSE-CSM

CSE (AI &ML) Proposed Program Outcomes (POs):


Engineering Graduates will be able to satisfy these NBA graduate attributes:

1. Engineering knowledge: An ability to apply knowledge of computing, mathematics,


science and engineering fundamentals appropriate to the discipline.
2. Problem analysis: An ability to analyze a problem, and identify and formulate the
computing requirements appropriate to its solution.
3. Design/development of solutions: An ability to design, implement, and evaluate a
computer-based system, process, component, or program to meet desired needs with
appropriate consideration for public health and safety, cultural, societal and
environmental considerations.
4. Conduct investigations of complex problems: An ability to design and conduct
experiments, as well as to analyze and interpret data.
5. Modern tool usage: An ability to use current techniques, skills, and modern tools
necessary for computing practice.
6. The engineer and society: An ability to analyze the local and global impact of computing
on individuals, organizations, and society.
7. Environment and sustainability: Knowledge of contemporary issues.
8. Ethics: An understanding of professional, ethical, legal, security and social issues and
responsibilities.
9. Individual and team work: An ability to function effectively individually and on teams,
Including diverse and multidisciplinary, to accomplish a common goal.
10. Communication: An ability to communicate effectively with a range of audiences.
11. Project management and finance: An understanding of engineering and management
principles and apply these to one’s own work, as a member and leader in a team, to
manage projects.
12. Life-long learning: Recognition of the need for and an ability to Engage in continuing
professional development.

DBMS Lab Manual


3
CMR Engineering College CSE-CSM

CSE (AI & ML) Program Educational Outcomes (PEOs)


PEO1. Excel in professional career and higher education by acquiring knowledge of
mathematical computing and engineering principles.
PEO2. To provide intellectual environment to successfully pursue higher education
specific to AI & ML.
PEO3. To impart knowledge in cutting edge Artificial Intelligence technologies in par
with industrial standards.
PEO4. To create an atmosphere to explore research areas and produce outstanding
contribution in various areas of Artificial Intelligence and Machine Learning

CSE (AI & ML) Proposed Program specific Outcome (PSOs)


1. Professional Skills and Foundations of Software development: Ability to use
knowledge in emerging technologies in identifying research gaps and provide solutions
with innovative ideas.
2. Applications of Computing and Research Ability: Ability to analyze the problem to
provide optimal solution by fundamental knowledge and skills in Professional,
Engineering Sciences.

DBMS Lab Manual


4
CMR Engineering College CSE-CSM

LAB CODE

 Students should report to the concerned lab as per the time table.

 Students who turn up late to the labs will in no case be permitted to do the program
schedule for the day.

 After completion of the program, certification of the concerned staff in-charge in the
observation book is necessary.

 Student should bring a notebook of 100 pages and should enter the readings /observations
into the notebook while performing the experiment.

 The record of observations along with the detailed experimental procedure of the
experiment in the immediate last session should be submitted and certified staff member
in-charge.

 The group-wise division made in the beginning should be adhered to and no mix up of
students among different groups will be permitted.

 When the experiment is completed, should disconnect the setup made by them, and
should return all the components/instruments taken for the purpose.

 Any damage of the equipment or burn-out components will be viewed seriously either by
putting penalty or by dismissing the total group of students from the lab for the
semester/year.

 Students should be present in the labs for total scheduled duration.

 Students are required to prepare thoroughly to perform the experiment before coming to
laboratory.

DBMS Lab Manual


5
CMR Engineering College CSE-CSM

B.TECH II Year I Sem LTPC


0 0 21
IT406PC: DATABASE MANAGEMENT SYSTEMS LAB
SI.NO List Of Experiments Page No
1 Concept design with E-R Model 7
2 Relational Model 16
3 Normalization 17
4 Practicing DDL commands 19
5 Practicing DML commands 23
6 Querying (using ANY, ALL, IN, Exists, NOT EXISTS, 30
UNION, INTERSECT, Constraints etc.)
7 Queries using Aggregate functions, GROUP BY, 40
HAVING and Creation and dropping of Views
8 Triggers (Creation of insert trigger, delete trigger, update 43
trigger)
9 Procedures 44
10 Usage of Cursors 45

DBMS Lab Manual


6
CMR Engineering College CSE-CSM

INTRODUCTION
Database Management System
This model is like a hierarchical tree structure, used to construct a hierarchy of records in the
form of nodes and branches. The data elements present in the structure have Parent-Child
relationship. Closely related information in the parent-child structure is stored together as a
logical unit. A parent unit may have many child units, but a child is restricted to have only one
parent.
The drawbacks of this model are:
The hierarchical structure is not flexible to represent all the relationship proportions,
 Which occur in the real world.
 It cannot demonstrate the overall data model for the enterprise because of the non
availability of actual data at the time of designing the data model.
 It cannot represent the Many-to-Many relationship.
Network Model
It supports the One-To-One and One-To-Many types only. The basic objects in this model are
Data Items, Data Aggregates, Records and Sets. It is an improvement on the Hierarchical Model.
Here multiple parent-child relationships are used. Rapid and easy access to data is possible in
this model due to multiple access paths to the data elements.
Relational Model
 Does not maintain physical connection between relations
 Data is organized in terms of rows and columns in a table
 The position of a row and/or column in a table is of no importance
 The intersection of a row and column must give a single value
Features of an RDBMS
 The ability to create multiple relations and enter data into them
 An attractive query language
 Retrieval of information stored in more than one table
 An RDBMS product has to satisfy at least seven of the 12 rules of Codd to be accepted as
a full- fledged RDBMS.

DBMS Lab Manual


7
CMR Engineering College CSE-CSM

Relational Database Management System


RDBMS is acronym for Relation Database Management System. Dr. E. F. Codd first introduced
the Relational Database Model in 1970. The Relational model allows data to be represented in a
simple row- column. Each data field is considered as a column and each record is considered as a
row. Relational Database is more or less similar to Database Management System. In relational
model there is relation between their data elements. Data is stored in tables. Tables have
columns, rows and names. Tables can be related to each other if each has a column with a
common type of information. The most famous RDBMS packages are Oracle, Sybase and
Informix.
Simple example of Relational model is as follows :
Student Details Table
Roll_no Sname S_Address
1 Rahul Satelite
2 Sachin Ambawadi
3 Saurav Naranpura

Student Marksheet Table


Rollno Sub1 Sub2 Sub3
1 78 89 94
2 54 65 77
3 23 78 46
Here, both tables are based on students details. Common field in both tables is Rollno. So we can
say both tables are related with each other through Rollno column.
Degree of Relationship
 One to One (1:1)
 One to Many or Many to One (1:M / M: 1)
 Many to Many (M: M)
The Degree of Relationship indicates the link between two entities for a specified occurrence of
each.
One to One Relationship : (1:1)

DBMS Lab Manual


8
CMR Engineering College CSE-CSM
1 1 Student Has Roll No.
One student has only one Rollno. For one occurrence of the first entity, there can be, at the
most one related occurrence of the second entity, and vice-versa.

One to Many or Many to One Relationship: (1:M/M: 1)


1 M Course Contains Students
As per the Institutions Norm, One student can enroll in one course at a time however, in one
course, there can be more than one student.
For one occurrence of the first entity there can exist many related occurrences of the second
entity and for every occurrence of the second entity there exists only one associated
occurrence of the first.
Many to Many Relationship: (M:M)
M M Students Appears Tests
The major disadvantage of the relational model is that a clear-cut interface cannot be determined.
Reusability of a structure is not possible. The Relational Database now accepted model on which
major database system are built. Oracle has introduced added functionality to this by
incorporated object-oriented capabilities. Now it is known is as Object Relational Database
Management System (ORDBMS). Object- oriented concept is added in Oracle8. Some basic
rules have to be followed for a DBMS to be relational. They are known as Codd’s rules,
designed in such a way that when the database is ready for use it encapsulates the relational
theory to its full potential. These twelve rules are as follows.
E. F. Codd Rules
1. The Information Rule
All information must be store in table as data values.
2. The Rule of Guaranteed Access
Every item in a table must be logically addressable with the help of a table name.
3. The Systematic Treatment of Null Values
The RDBMS must be taken care of null values to represent missing or
inapplicable information.
4. The Database Description Rule
A description of database is maintained using the same logical structures with
which data was defined by the RDBMS.

DBMS Lab Manual


9
CMR Engineering College CSE-CSM
5. Comprehensive Data Sub Language
According to the rule the system must support data definition, view definition,
data manipulation, integrity constraints, authorization and transaction
management operations.
6. The View Updating Rule
All views that are theoretically updateable are also updateable by the system.
7. The Insert and Update Rule
This rule indicates that all the data manipulation commands must be operational on sets of rows
having a relation rather than on a single row.
8. The Physical Independence Rule
Application programs must remain unimpaired when any changes are made in storage
representation or access methods.
9. The Logical Data Independence Rule
The changes that are made should not affect the user’s ability to work with the data.The change
can be splitting table into many more tables.
10. The Integrity Independence Rule
The integrity constraints should store in the system catalog or in the database.
11. The Distribution Rule
The system must be access or manipulate the data that is distributed in other
12. The Non-subversion Rule
If a RDBMS supports a lower level language then it should not bypass any
integrity constraints defined in the higher level.
Object Relational Database Management System
Oracle8 and later versions are supported object-oriented concepts. A structure once created can
be reused is the fundamental of the OOP’s concept. So we can say Oracle8 is supported Object
Relational model, Object – oriented model both. Oracle products are based on a concept known
as a client-server technology. This concept involves segregating the processing of an application
between two systems. One performs all activities related to the database (server) and the other
performs activities that help the user to interact with the application (client). A client or front-end
database application also interacts with the database by requesting and receiving information
from database server. It acts as an interface between the user and the database. The database
server or back end is used to manage the database tables and also respond to client requests.

DBMS Lab Manual


10
CMR Engineering College CSE-CSM

Roadway Travels
"Roadway Travels" is in business since 1997 with several buses connecting different places in
india. Its main office is located in Hydearabd.

The company wants to computerize its operations in the following areas:

 Reservation and Ticketing


 Cancellations

Reservation & Cancellation:

Reservations are directly handled by booking office. Reservations can be made 30 days in
advance and tickets issued to passenger. One Passenger/person can book many tickets (to his/her
family).

Cancellations are also directly handed at the bokking office.

In the process of computerization of Roadway Travels you have to design and develop a
Database which consists the data of Buses, Passengers, Tickets, and Reservation and cancellation
details. You should also develop query's usinf SQL to retrieve the data from database.

The above process involves many steps like

1. Analyzing the problem and identifying the Entities and Relationships,

2. E-R Model

3. Relational Model

4. Normalization

5. Creating the database

6. Querying.

Students are suppossed to work on these steps week wise and finally create a complete "Database
System" to Roadway Travels. Examples are given at every experiment for guidance to students.

DBMS Lab Manual


11
CMR Engineering College CSE-CSM

Experiment 1: E-R Model for "Roadway Travels"


"Roadway Travels"

AIM :To Study And identify the entity in road way travels database
system

Analyze the problem carefully and come up with the entities in it. Identify what data has
to be persisted in the database. This contains the entities, attributes etc.

Entity:
An entity may be defined as a thing which is recognized as being capable of an
independent existence and which can be uniquely identified. An entity is an abstraction from the
complexities of some domain. When we speak of an entity we normally speak of some aspect of
the real world which can be distinguished from other aspects of the real world. Entities can be
thought of as nouns.

Examples:
A computer, an employee, a song, and a mathematical theorem.

The entities identified in “Roadway Travels” are :Bus, Ticket, Passenger

Relationship:
A relationship captures how two or more entities are related to one another. Relationships
can be thought of as verbs, linking two or more nouns.

Examples:
An “owns” relationship between a company and a computer, a supervises relationship
between an employee and a department, a performs relationship between an artist and a song, a
proved relationship between a mathematician and a theorem.

The relationships identified in “Roadway Travels” are:


1. Reservation
2. Cancellation

Attributes:

Entities and relationships can both have attributes.

Examples:

An employee entity might have a Social Security Number (SSN) attribute; the proved
relationship may have a date attribute.

DBMS Lab Manual


12
CMR Engineering College CSE-CSM
The attributes identified for entities in “Roadway Travels” are:

1. Bus:
a. BUS NUMBER,
b. SOURCE,
c. DESTINATION.
d. NO OF DAYS AVAILBLE

2. Passenger:
a. PASSEBGER _ID
b. NAME,
c. AGE,
d. GENDER
e. ADDRESS
3. Ticket:
a. TICKET_ID
b. JOUTNEY_DATE
c. PNR NO
d. SOURCE
e. DESTINATION
f. BUS NO

4. Pastic
a. PID,
b. TID

The attributes identified for relationships in “Roadway Travels” are:

1. Reservation:
a. PNRNO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS

2. Cancellation:
a. PNR _NO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS
d.

Primary keys:

Bus NO. (Bus entity)

Passenger_id ( passenger entity

Ticket_id (ticket entity).

DBMS Lab Manual


13
CMR Engineering College CSE-CSM
E-R Diagrams:

Entity-relationship diagrams don't show single entities or single


instances of relations. Rather, they show entity sets and relationship sets.

Example:
A particular song is an entity. The collection of all songs in a
database is an entity set. The eaten relationship between a child and her
lunch is a single relationship. The set of all such child-lunch relationships in
a database is a relationship set. In other words, a relationship set corresponds
to a relation in mathematics, while a relationship corresponds to a member of
the relation.

Entity sets are drawn as rectangles, relationship sets as diamonds. If an entity


set participates in a relationship set, they are connected with a line. Attributes
are drawn as ovals and are connected with a line to exactly one entity or
relationship set.

An underlined name of an attribute indicates that it is a key: two different


entities or relationships with this attribute always have different values for
this attribute.

Result: All the entities and relationships of database system is performed

DBMS Lab Manual


14
CMR Engineering College CSE-CSM

Concept design with E-R Model for "Roadway


Travels"

DBMS Lab Manual


15
CMR Engineering College CSE-CSM
The E-R Diagram for “Roadway travels”.

Passe
No. days available nger_i
ddd
Pasgr id _ i

Address
Ticket_id

Ticket_id
Passenger_ID

PNR_No Source

Bus_n
o

Experiment 2

DBMS Lab Manual


16
CMR Engineering College CSE-CSM

Relational Model "Roadway Travels" database

Aim : To perform the all the entities and relationships in tabular fashion

Description :There are different ways of representing relationships as tables


based on the cardinality. Represent attributes as columns in table or as tables
based on the requirement. Different types of attributes based on your E-R
model.

The relational models for the entities and relations are as follows:

Bus:
Bus_No Source Destination No of days available

Passenger:
P_ID NAME Age Gender Address Ticket_ID

Ticket:
Ticket_ID Journey_Date PNR_NO BUS_NO Source Destination

Reservation:
PNR_No Journey_Date No_of_seats

Cancellation:
PNR_No Journey_Date No_of_seats

Result:Relation model of database system is performed

DBMS Lab Manual


17
CMR Engineering College CSE-CSM

Experiment 3
Normalization for database tables
AIM: To perform the normalization in a database system

Description: Database Normalisation is a technique of organizing the data in

the database. Normalization is a systematic approach of decomposing tables

to eliminate data redundancy


Normalization rule are divided into following normal form.

1. First Normal Form

2. Second Normal Form

3. Third Normal Form

4. BCNF

5. Fourth Normal Form

6. Fifth Normal Form

To remove the redundancy from the following table

Passenger:
P_ID NAME Age Gender Address Ticket_ID

DBMS Lab Manual


18
CMR Engineering College CSE-CSM

After the normalization of above table

Passenger:
P_ID NAME Age Gender Address

Pastic
P_ID Ticket_ID

Result: Normalization is performed in database system.

Experiment 4
Practicing DDL commands
DBMS Lab Manual
19
CMR Engineering College CSE-CSM

DDL COMMANDS

AIM: To execute and manipulate all the DDL commands in a database


system.

DESCRIPTION: A database system provides a data definition language


(DDL) to specify the database schema. DDL also updates a special set of
tables called the data dictionary or data directory.

Definitions:

1. create: ‘create’ command is used to define an SQL relation.


Syntax: create table table-name(field-name-1 data-type,
field-name-2 data-type,
…….
……
field-name-n data-type);

2. desc: ‘desc’ command is used to read the created relation.


Syntax: desc table-name;

3. a) alter add: ‘alter add’ command adds the attributes to an existing


relation.
Syntax: alter table table-name add(field-name-1 data-type,……..);
b) alter modify: ‘alter modify’ command modifies the data types for the
existing attributes.
Syntax: alter table table-name modify(field-name data-type);

4. truncate: ‘truncate’ command truncates the table i.e. all the rows will
be deleted in the table.
Syntax: truncate table table-name;

5. drop: the ‘drop’ command deletes all information about the relation
from the database i.e. it deletes the schema of the relation.
Syntax: drop table table-name;

Creating tables:

Create table with following schma

5. Bus:
a. BUS NUMBER,

DBMS Lab Manual


20
CMR Engineering College CSE-CSM
b. SOURCE,
c. DESTINATION.
d. NO OF DAYS AVAILBLE

6. Passenger:
a. PASSEBGER _ID
b. NAME,
c. AGE,
d. GENDER
e. ADDRESS
7. Ticket:
a. TICKET_ID
b. JOUTNEY_DATE
c. PNR NO
d. SOURCE
e. DESTINATION
f. BUS NO

8. Pastic
a. PID,
b. TID

The attributes identified for relationships in “Roadway Travels” are:

3. Reservation:
a. PNRNO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS

4. Cancellation:
a. PNR _NO
b. JOUTNEY_DATE
c. NUMBER_OF_SEATS
d.

Primary keys:

Bus NO. (Bus entity)

Passenger_id ( passenger entity

Ticket_id (ticket entity).

BUS Table:

DBMS Lab Manual


21
CMR Engineering College CSE-CSM
SQL> CREATE TABLE BUS
( BUS_NO VARCHAR2(10),
SOURCE VARCHAR2(20),
DESTINATION VARCHAR2(20),
PRIMARY KEY (BUS_NO)
);

SQL> Table created.

SQL>desc BUS;

Passenger table:

SQL> CREATE TABLE PASSENGER (PID VARCHAR(10) PRIMARY


KEY,
NAME VARCHAR(20) NOT NULL,
AGE INTEGER,
GENDER VARCHAR(5),
ADDRESS VARCHAR(20));

TABLE CREATED.

SQL> desc PASSENGER;

Reservation Table:

SQL> CREATE TABLE RESERVATION (PNRNO INTEGER,


JOURNEYDATE DATE,
NOOFSEATS NUMBER)
SQL> TABLE CREATED.

SQL>DESC RESERVATION;

Cancellation Table:

SQL> CREATE TABLE CANCELLATION


PNRNO INTEGER,
JOURNEYDATE DATE,
NOOFSEATS NUMBER)

SQL> TABLE CREATED.

SQL>DESC CANCELLATION;

Ticket Table:

DBMS Lab Manual


22
CMR Engineering College CSE-CSM
SQL> CREATE TABLE TICKET(TID NUMBER(15) PRIMARY KEY,
JOURNDATE TIMESTAMP,
SOURCE CHAR(10),
DESTINATION CHAR(8),
BUSNO VARCHAR2(10),
PNRNO NUMBER(5),
FOREIGN KEY(BUSNO) REFERENCES BUS1540(BUSNO));

SQL> TABLE CREATED.

SQL>desc TICKET;

To truncate a table:

Syntax is : truncate table table_name;

Ex: SQL> truncate table bus;


SQL> Table Truncated.

To DROP a table:

Syntax is: drop table table_name;


Ex: SQL> Drop table bus;
SQL> Table Dropped.

Result: All the roadway travels tables are created

Experiment 5
Practicing DML commands

DBMS Lab Manual


23
CMR Engineering College CSE-CSM

DML COMMANDS
AIM: To study and execute all the DML commands in a
database system.

DESCRIPTION: A data manipulating language(DML) is a language that


enables users to access or manipulate data as organized by the appropriate
data model. They am basically two types:
1. Procedural DML.
2. Declarative DML.
The DML component of the SQL language is non-procedural. The DML
commands in a database system are
DML commands are used for managing the data within schema objects.
Some examples are.
I. SELECT- retrieve data from the database.
II. INSERT- insert data into a table.
III. Updates existing data within a table.
DELETE- delete all records from a table, the space for the records remain
Definitions:
1. insert : 'insert' command is used to load the data into the relation

Syntax: insert into table-name(field-names) values (field-


values);
2. delete: 'delete' command deletes only tuples .
Syntax: delete from table-name;
3. update: 'update' command is used to update the existing data in the
relation

Syntax: update table-name set field-name—value where


condition;

INSERT Command:

SQL> create table bus540(busno varchar2(10),source varchar2(20),


destination varchar2(20));

DBMS Lab Manual


24
CMR Engineering College CSE-CSM
Table created.

SQL> insert into bus540 values('&busno','&source','&des');


Enter value for busno: 1234
Enter value for source: hyd
Enter value for des: guntur
old 1: insert into bus540 values('&busno','&source','&des')
new 1: insert into bus540 values('1234','hyd','guntur')

1 row created.

SQL> /
Enter value for busno: 5678
Enter value for source: guntur
Enter value for des: vizaq
old 1: insert into bus540 values('&busno','&source','&des')
new 1: insert into bus540 values('5678','guntur','vizaq')

1 row created.

SQL> /
Enter value for busno: 1345
Enter value for source: vinukonda
Enter value for des: guntur
old 1: insert into bus540 values('&busno','&source','&des')
new 1: insert into bus540 values('1345','vinukonda','guntur')

1 row created.

SQL> update bus540 set des='kcp' where busno='1345';

1 row updated.

SQL> select * from bus540;

BUSNO SOURCE DES


-------------------- ---------- --------------------
1234 hyd guntur
5678
vizaq
1345 vinukonda kcp

SQL> create table passenger1540(pid varchar(10) primary key,name


varchar(20) not null,
age integer,gender varchar(5),address varchar(20));

DBMS Lab Manual


25
CMR Engineering College CSE-CSM
Table created.

SQL> insert into passenger1540


values('&pid','&name',&age,'&gender','&address');
Enter value for pid: 111
Enter value for name: ruchin
Enter value for age: 10
Enter value for gender: m
Enter value for address: hyderabad
old 1: insert into passenger540
values('&pid','&name',&age,'&gender','&address')
new 1: insert into passenger540 values('111','ruchin',10,'m','hyderabad')

1 row created.

SQL> /
Enter value for pid: 222
Enter value for name: sunay
Enter value for age: 11
Enter value for gender: m
Enter value for address: kcp
old 1: insert into passenger540
values('&pid','&name',&age,'&gender','&address')
new 1: insert into passenger540 values('222','sunay',11,'m','kcp')

1 row created.

SQL> /
Enter value for pid: 333
Enter value for name: nari
Enter value for age: 27
Enter value for gender: m
Enter value for address: guntur
old 1: insert into passenger540
values('&pid','&name',&age,'&gender','&address')
new 1: insert into passenger540 values('333','nari',27,'m','guntur')

1 row created.

SQL> select * from passenger540;

PID NAME AGE GENDE ADDRESS


---------- -------------------- ---------- ----- --------------------
111 ruchin 10 m hyderabad
222 sunay 11 m kcp
333 nari 27 m guntur

DBMS Lab Manual


26
CMR Engineering College CSE-CSM
SQL> create table ticket1540(tid number(15) primary key,journdate
timestamp,source char(10),desti
nation char(8),busno varchar2(10),pnrno number(5),foreign key(busno)
references bus1540(busno));

Table created.

SQL> insert into ticket1540


values(&tid,'&journdate','&source','&destination','&busno',&pnrno);
Enter value for tid: 112233
Enter value for journdate: 15-mar-13 10:30:54
Enter value for source: hyd
Enter value for destination: chennai
Enter value for busno: 5678
Enter value for pnrno: 112
old 1: insert into ticket1540
values(&tid,'&journdate','&source','&destination','&busno',&pnrno
)
new 1: insert into ticket1540 values(112233,'15-mar-13
10:30:54','hyd','chennai','5678',112)

1 row created.

SQL> /
Enter value for tid: 223344
Enter value for journdate: 28-jul-13 9:30:33pm
Enter value for source: tirupathi
Enter value for destination: guntur
Enter value for busno: 1345
Enter value for pnrno: 114
old 1: insert into ticket1540
values(&tid,'&journdate','&source','&destination','&busno',&pnrno
)
new 1: insert into ticket1540 values(223344,'28-jul-13
9:30:33pm','tirupathi','guntur','1345',1
14)

1 row created.

SQL> /
Enter value for tid: 334455
Enter value for journdate: 29-mar-13 9:20:22pm
Enter value for source: manglore
Enter value for destination: hyd
Enter value for busno: 1234
Enter value for pnrno: 115

DBMS Lab Manual


27
CMR Engineering College CSE-CSM
old 1: insert into ticket1540
values(&tid,'&journdate','&source','&destination','&busno',&pnrno
)
new 1: insert into ticket1540 values(334455,'29-mar-13
9:20:22pm','manglore','hyd','1234',115)

1 row created.

SQL> select * from ticket1540;

TID JOURNDATE SOURCE


DESTINAT BUSNO PNRNO

112233 15-MAR-13 10.30.54.000000 AM hyd chennai


5678 112

223344 28-JUL-13 09.30.33.000000 PM tirupathi guntur


1345 114

334455 29-MAR-13 09.20.22.000000 PM manglore hyd


1234 115

SQL> create table pastic11(tid integer,pid varchar(10),primary key(tid,pid),


foreign key(tid) references ticket540,foreign key(pid) references
passenger540);

Table created.

SQL> insert into pastic11 values(112233,'111');

1 row created.

SQL> insert into pastic11 values(1223344,'222');

1 row created.

SQL> insert into pastic11 values(223344,'333');

1 row created.
SQL> select * from pastic;

TID PID
---------- ----------
112233 111
1223344 222

DBMS Lab Manual


28
CMR Engineering College CSE-CSM
223344 333

SQL> create table reservation540(pnrno integer,journeydate date,noofseats


number);

Table created.

SQL> insert into reservation540 values(&pnrno,'&date',&noofseats);


Enter value for pnrno: 123
Enter value for date: 15-mar-2013
Enter value for noofseats: 3
old 1: insert into reservation540 values(&pnrno,'&date',&noofseats)

new 1: insert into reservation540 values(123,'15-mar-2013',3)

1 row created.

SQL> /
Enter value for pnrno: 124
Enter value for date: 28-jul-2013
Enter value for noofseats: 2
old 1: insert into reservation540 values(&pnrno,'&date',&noofseats)
new 1: insert into reservation540 values(124,'28-jul-2013',2)

1 row created.

SQL> /
Enter value for pnrno: 111
Enter value for date: 29-mar-2013
Enter value for noofseats: 3
old 1: insert into reservation540 values(&pnrno,'&date',&noofseats)
new 1: insert into reservation540 values(111,'29-mar-2013',3)

1 row created.

SQL> select * from reservation540;

PNRNO JOURNEYDA NOOFSEATS


---------- --------- ----------
123 15-MAR-13 3
124 28-JUL-13 2
111 29-MAR-13 3

SQL> create table cance540(pnrno integer,journeydate date,noofseats


number)

Table created.

DBMS Lab Manual


29
CMR Engineering College CSE-CSM

SQL>insert into cance540(&pnrno,'&date','&noofseats');


Enter value for pnrno: 123
Enter value for date: 09-apr-2013
Enter value for noofseats: 2
old 1: insert into cance540 values(&pnrno,'&date',&noofseats)
new 1: insert into cance540 values(123,'09-apr-2013',2)

1 row created.

SQL> /
Enter value for pnrno: 111

Enter value for date: 21-dec-3013


Enter value for noofseats: 4
old 1: insert into cance540 values(&pnrno,'&date',&noofseats)
new 1: insert into cance540 values(111,'21-dec-3013',4)

1 row created.

SQL> select * from cance540;

PNRNO JOURNEYDA NOOFSEATS


---------- --------- ----------
123 09-APR-13 2
111 21-DEC-13 4

SELECT Command:

Selecting values from the bus table.

SQL> SELECT * FROM BUS; (Selects all the attributes and display)

UPDATE Command:

SQL> UPDATE BUS SET bus_no=’boo4’ WHERE bus_no=’b003’;

Delete Command:

SQL> DELETE FROM BUS; (To delete entire rows from a table)
SQL> DELETE * FROM BUS WHERE bus_no=’boo1’; (To delete an
individual row)

Result: Inserting of values into roadway travels tables are


performed using with DML commands

DBMS Lab Manual


30
CMR Engineering College CSE-CSM

Experiment 6
Querying (EXISTS, NOT EXIST, UNION, INTERSECT SET
OPERATIONS)

AIM: To execute all the set operations in a SQL language.

Description: If the answer to a query is a multiset of rows then


we use the set operations. Set operations include:

1. Union
2. Intersect
3. Except
4. Any
5. ALL
6. In
7.Avg
8.Min
9.Max
10.Sum
11.Count

Definitions:

1. Union: RV S returns a relation instance containing all


tuples that occur in either relation instance R or relation
instance S (or both).

2. Intersect: R Λ S returns a relation instance containing all


tuples that occur in both R and S.

DBMS Lab Manual


31
CMR Engineering College CSE-CSM

3. Except: R-S returns a relation instance containing all tuples


that occur in R but not in S.

EXISTS Operator:

The EXISTS sub query is used when we want to display all rows
where we have a matching column in both tables.

SQL> SELECT pnr_no,age FROM PASSENGER WHERE


EXISTS(SELECT age FROM TICKET WHERE age>22);

SQL> select name from passenger where exists (select * from ticket540
where tid='112233');

NAME
--------------------------------------------------
tarun
manu
deepak
shona

SQL> select name from passenger540 where exists (select * from ticket540
where tid='112233');

NAME
--------------------
ruchin
sunay
nari

SQL> select name from passenger540 where exists (select name from
ticket540
where tid='112233');

NAME
--------------------
ruchin
sunay
nari

DBMS Lab Manual


32
CMR Engineering College CSE-CSM

NOT EXISTS Operator:

The NOT EXISTS subquery is used to display cases where a selected


column does not appear in another table.

SQL> SELECT pnr_no,age FROM PASSENGER WHERE NOT


EXISTS(SELECT age FROM TICKET WHERE age>22);

SQL> (select name from passenger540 where address='hyderabad') minus


(select name from passenger
where address='guntur');

NAME
--------------------
ruchin

DBMS Lab Manual


33
CMR Engineering College CSE-CSM
SQL> (select name from passenger540 where address='kcp')minus (select
name from passenger540 whe
ddress='guntur');

NAME
--------------------

UNION Operator:

Combine the results of two SELECT statements into one result set,
and then eliminates and duplicate rows from that result set.

SQL> SELECT pnr_no FROM PASSENGER WHERE age>20 UNION


SELECT pnr_no FROM RESERVATION WHERE no_of_seats > 2;

SQL> (select *from passenger540 where address='hyderabad')union (select


*from passenger540
where dress='kcp');

PID NAME AGE GENDE ADDRESS


---------- -------------------- ---------- ----- --------------------
111 ruchin 10 m hyderabad
222 sunay 11 m kcp

SQL> (select * from passenger540) union (select *from passenger540);

PID NAME AGE GENDE ADDRESS


---------- -------------------- ---------- ----- --------------------
111 ruchin 10 m hyderabad
222 sunay 11 m kcp
333 nari 27 m guntur

SQL> (select * from passenger540) union all (select *from passenger540);

PID NAME AGE GENDE ADDRESS


---------- -------------------- ---------- ----- --------------------
111 ruchin 10 m hyderabad
222 sunay 11 m kcp
333 nari 27 m guntur
111 ruchin 10 m hyderabad
222 sunay 11 m kcp
333 nari 27 m guntur

DBMS Lab Manual


34
CMR Engineering College CSE-CSM
6 rows selected.

SQL> (select pid,name from passenger540) union (select pid,name from


passenger540);

PID NAME
---------- --------------------
111 ruchin
222 sunay
333 nari

SQL> (select pid,name from passenger540)union (select name,pid from


passenger540);

PID NAME
-------------------- --------------------
111 ruchin
222 sunay
333 nari
nari 333
ruchin 111
sunay 222

6 rows selected.

INTERSECT Operator:

Combine the results of two SELECT statements into one result set,
and returns only those rows that are returned by each of two statements.

SQL> >SELECT pnr_no FROM PASSENGER WHERE age>20


INTERSECT SELECT pnr_no FROM RESERVATION WHERE
no_of_seats > 2;

SQL> (select *from passenger540)intersect (select * from passenger540);

PID NAME AGE GENDE ADDRESS


---------- -------------------- ---------- ----- --------------------
111 ruchin 10 m hyderabad
222 sunay 11 m kcp
333 nari 27 m guntur

Result: All the set operation queries are performed

DBMS Lab Manual


35
CMR Engineering College CSE-CSM

Definitions:

The ANY Operator:the any operator allows you to specify multiple values
in a where clause and select any value from that list

The All Operator:the all operator allows you to specify multiple values in a
where clause and select all value from that list

The IN Operator:the in operator allows you to specify multiple values in a


where clause.

The BETWEEN Operator:The BETWEEN operator selects values within


a range. The values can be numbers, text, or dates.

ANY Operator:

Compares values to each value returned by sub query.

SQL> SELECT pnr_no,age FROM PASSENGER WHERE


age>ANY(SELECT age FROM TICKET WHERE age>22);

ALL Operator:

Compare values to every value returned by sub query.

SQL> SELECT pnr_no,age FROM PASSENGER WHERE


age>ALL(SELECT age FROM TICKET WHERE age>22);

DBMS Lab Manual


36
CMR Engineering College CSE-CSM

IN Operator:

Equal to any member in the list.

SQL> SELECT pnr_no,age FROM PASSENGER WHERE age IN(SELECT


age FROM TICKET WHERE age>22);

BETWEEN Operator
SQL> SELECT name FROM PASSENGER WHERE age
BETWEEN 30 AND 45;

1. Display unique pnr_no of all passengers.

SQL> SELECT DISTINCT pnr_no FROM PASSENGER;

2. Display all the names of male passengers.

DBMS Lab Manual


37
CMR Engineering College CSE-CSM
SQL> SELECT name FROM PASSENGER WHERE sex = ’M’;

3. Display the ticket numbers and names of all passengers.

SQL> SELECT ticket_no, name FROM PASSENGER;

4. Display the source and destinations of all busses.

SQL> SELECT source, destination FROM BUS;

5. Find the ticket numbers of the passengers whose name starts with
‘A’ and ends with ‘H’.

SQL> SELECT ticket_no FROM PASSENGER WHERE name


LIKE ‘A%H’;

6. Find the names of all the passengers whose age is between 30 and
45.

SQL> SELECT name FROM PASSENGER WHERE age


BETWEEN 30 AND 45;

7. Display all the passengers names beginning with ‘A’.

SQL> SELECT name FROM PASSENGER WHERE name LIKE ‘A


%’;

8. Display the sorted list of passenger’s names.

SQL> SELECT name FROM PASSENGER ORDER BY name


ASC;

Result: All the set comparison operation queries are performed

DBMS Lab Manual


38
CMR Engineering College CSE-CSM
Queries using Aggregate functions (COUNT, SUM, AVG, MAX,and
MIN)

AIM: To execute all the group functions in a SQL language.

Description: These can be applied on any column of a relation.


Aggregate functions include:

1.Avg
2.Min
3.Max
4.Sum
5.Count

Definitions:

1. Sum: The input to sum must be a collection of numbers and it gives sum
of those numbers.

Syntax: select Sum (field-name) from table-name;

2. Min: It presents the minimum value among tuples of a given attribute.

Syntax: select Min (field-name) from table-name;

3. Max: It presents the maximum value among tuples of a given attribute.

Syntax: select Max (field-name) from table-name;

4. Avg: It gives the average of the numbers.

Syntax: select Avg (field-name) from table-name;

5. Count: It counts the number of tuples in the relation.

Syntax: select Count (Distinct field-name) from table-name;

COUNT: It returns the number of rows in the query.

SQL> SELECT COUNT(*) FROM PASSENGER;

DBMS Lab Manual


39
CMR Engineering College CSE-CSM

SUM: It returns the sum value of column.

SQL> SELECT SUM(ticket_no) FROM PASSENGER;

AVG: It returns the average value of column.

SQL> SELECT AVG(age) FROM PASSENGER;

MAX: It returns the maximum value of column.

SQL> SELECT MAX(age) FROM PASSENGER;

MIN: It returns the minimum value of column.

SQL> SELECT AVG(age) FROM PASSENGER;

Result: All the Aggregate functions queries are performed

DBMS Lab Manual


40
CMR Engineering College CSE-CSM

Experiment 7
Queries using GROUP BY, HAVING and Creation and droping of
VIEWS.

AIM: To execute all the group by Having clause and view in a


SQL language.

Description: To write queries using clauses such as GROUP BY,


HAVING and creation of VIEW, etc. and retrieving
information by joining tables.
Definitions:

GROUP BY CLAUSE: The GROUP BY statement is used in conjunction


with the aggregate functions to group the result-set by one or more columns.

SQL> SELECT name, AVG(age) FROM PASSENGER GROUP BY name;

HAVING CLAUSE: The HAVING clause was added to SQL because the
WHERE keyword could not be used with aggregate functions.

SQL> SQL> SELECT name, AVG(age) FROM PASSENGER GROUP BY


name HAVING AVG(age)>30;

Q1)WAQ to display infor pressent in reservation and cancellation tables


using union operator

SQL> select pnrno from reservation540 union select pnrno from cance540;

PNRNO
----------
111
123
124

Q)WAQ to find no of seats booked fro each pnrno using group by clause

SQL> select pnrno,sum(noofseats) from reservation540 group by pnrno;

PNRNO NOOFSEATS
---------- --------------

DBMS Lab Manual


41
CMR Engineering College CSE-CSM
123 3

124 2

111 3

Q)WAQ to find no of pnrno's in a tableon which 123 pnr no is available


using distinct

SQL> select count(distinct source)from bus540 where busno=1234;

COUNT(DISTINCTSOURCE)
---------------------
1

Q)WAQ to find distinct pnrno's that are present

SQL> select distinct pname from reservation540;

PNRNO
----------
123
124
111

Q)WAQ to find total no of cancellation seats

SQL> select sum(noofseats) from cance540;

SUM(NOOFSEATS)
--------------
6

A view is, in essence, a virtual table. It does not physically exist. Rather, it is
created by a query joining one or more tables.

Creating VIEWS:

SQL> CREATE VIEW PASSENGERVIEW AS SELECT


ticket_no,name,ppno,sex FROM PASSENGER;

SQL> VIEW CREATED.

SQL> SELECT * FROM PASSENGERVIEW;

DBMS Lab Manual


42
CMR Engineering College CSE-CSM
Updating a View

SQL> CREATE OR REPLACE PASSENGERVIEW AS SELECT


ticket_no,ppno,sex from PASSENGER;

SQL> VIEW CREATED.

SQL> SELECT * FROM PASSENGERVIEW;

Dropping a View

SQL> DROP VIEW PASSENGERVIEW;

SQL> VIEW DROPPED.

Result: All the GROUP BY ,HAVING AND VIEW queries are


performed

Experiment 8

DBMS Lab Manual


43
CMR Engineering College CSE-CSM
Creation of insert trigger, delete trigger, update trigger. Practice
triggers using the roadway travels database.

Aim: To implement operations on relations using PL/SQL trigger

Definition: a trigger is a SQL procedure that initiates an action (i.e.,


fires an action) when an event (INSERT, DELETE or UPDATE)
occurs. Sincetriggers are event-driven specialized procedures, they
are stored in and managed by the DBMS.

SQL>CREATE OR REPLACE TRIGGER UPDATECHECK


BEFORE UPDATE ON PASSENGER
FOR EACH ROW
WHEN (NEW.TICKET_NO > 60)
BEGIN
:NEW.TICKET_NO := :OLD.TICKET_NO;
dbms_output.put_line('updcheck called');
END;
/

OUT PUT:

Result: A triggers in PL/SQL language are performed.

Experiment 9

DBMS Lab Manual


44
CMR Engineering College CSE-CSM
Creation of stored procedure, Execution of procedure and modification
of procedure. Practice procedures using the database.

Aim: To implement Procedure on relations using PL/SQL

Definition: A procedure is a subprogram that performs a specific action.

CREATE OR REPLACE PROCEDURE VOTER_VERIFICATION(Age


NUMBER)
IS
begin
if age > 18 then
dbms_output.put_line('Valid for Voting');
Else
dbms_output.put_line('Not valid for voting');
END IF;
ENd;

OUT PUT:

Result: A Procedure in PL/SQL language are performed.

Experiment 10

DBMS Lab Manual


45
CMR Engineering College CSE-CSM
Declare a curser that defines a result set. Open the curser to establish a
the result set. Fetch the data into local variables as needed from the
cursor, one row at a time. Close the cursor when done.

Aim: To implement cursors on relations using PL/SQL

Definition: A cursor is a temporary work area created in the system


memory when a SQL statement is executed. A cursor contains information
on a select statement and the rows of data accessed by it.

SQL> CREATE OR REPLACE PROCEDURE


DISPLAY_BUS_ROUTE(BID VARCHAR2)
IS
SRC VARCHAR2(10);
DEST VARCHAR2(10);
cursor c1 is select SOURCE,DESTINATION FROM BUS WHERE
BUS_NO LIKE BID;
begin
open c1;
fetch c1 into SRC,DEST;
dbms_output.put_line('BUS_NO:'||BID );
dbms_output.put_line('SRC:'||SRC);
dbms_output.put_line('DEST:'||DEST);
END;

OUTPUT:

Result: A cursors in PL/SQL language are performed.

LEAD EXPERIMENTS

DBMS Lab Manual


46
CMR Engineering College CSE-CSM

1)Create views to insert, delete and update tuples

Aim: To implement operations on relations using PL/SQL Views.

Definition: A view is, in essence, a virtual table. It does not physically


exist. Rather, it is created by a query joining one or more tables.

Create view employee dept-view as select t1.emp_name,t2.dept_name


From employee as t1 left join department as t2
Insert into employee(‘fname’,’lname’,’ssn’,dno)
values(‘robert’,’hitler’,9673553574,2);
Delete from employee
where fname=‘robert’;
Update employee
SET fname=’brown’,lname=’rob’,ssn=9680377384,Dno=2;

Result: Views in PL/SQL language are performed.

DBMS Lab Manual


47
CMR Engineering College CSE-CSM

2) Create trigger. the trigger as to execute on inserting a


table and create a backup data
Aim: To implement operations on relations using PL/SQL trigger

Definition: a trigger is a SQL procedure that initiates an action (i.e.,


fires an action) when an event (INSERT, DELETE or UPDATE)
occurs. Sincetriggers are event-driven specialized procedures, they
are stored in and managed by the DBMS.

CREATE TRIGGER PRINT_SALARY_CHANG IS


BEFORE DELETE OR INSERT OR UPDATE ON emp
FOR EACH ROW
WHEN (new.empno>0)
DECLARE
Sal-diff number
BEGIN
Sal-diff=new Sal-old sal;
Dbms_output.put(“old salary://:old sal”);
Dbms_output.put(“new salary://:new sal”);
END;
/

Result: A triggers in PL/SQL language are performed.

DBMS Lab Manual


48

You might also like