SlideShare a Scribd company logo
1 of 29
Download to read offline
Department of Electrical and Electronics Engineering
III B. Tech - II Semester
Sub: Fundamentals of Data Base Management Systems
Laboratory Manual Subject Code : A0562
Academic Year 2021-22
Regulations: MR18
Malla Reddy Engineering College
(Autonomous)
(An UGC Autonomous Institution, Approved by AICTE and Affiliated to JNTUH Hyderabad,
Recognized under 2(f) &12 (B) of UGC Act 1956, Accredited by NAAC with ‘A’ Grade (II Cycle)
Maisammaguda, Dhulapally (Post Via Kompally), Secunderabad-500 100
www.mrec.ac.in E-mail: principal@mrec.ac.in
2021-2022
Malla Reddy Engineering College
(Autonomous)
Maisammaguda, Dhulapally (Post Via Kompally), Secunderabad-500100
Institute Vision
To be a premier centre of professional education and research, offering quality programs
in a socio - economic and ethical ambience.
Institute Mission
1. To impart knowledge of advanced technologies using state-of-the-art infrastructural
facilities.
2. To inculcate innovation and best practices in education, training and research.
3. To meet changing socio-economic needs in an ethical ambience.
2020-21
Onwards
(MR-20)
MALLA REDDY ENGINEERING COLLEGE
(Autonomous)
B.Tech.
V Semester
Code: A0562 Fundamentals of Database Management Systems Lab
(Common for CE, EEE, ME, ECE, MiE)
L T P
Credits: 2 - 1 2
Prerequisites: NIL
Course Objectives: This course enables the students to practice the concepts learnt in the subject
DBMS by developing a database for an example project. The student is expected to practice the
querying a relational database i.e., “Mysql” with various functionalities of SQL and PL/SQL
statements using a sample database.
Sample Database:
Railway Reservation System -(Redesigning IRCTC database)
Train (train Number, name, source, destination, start_time, reach_time, traveltime, distance, class,
days, type)
Ticket (PNRNo, Transactionid, from_station, To_station, date_of_journey, class date_of_booking,
total_ticket_fare, train number)
Passenger (PNR No, Serial no, Name, Age, Reservation_status)
Train_Route(Train_No, route_no, station_code, name, arrival_time, depart_time, distance, day)
Train_Ticket_fare(Train_No, class, base_fare, reservation_charge, superfast_charge, other_charge,
tatkal_charge, service_tax)
List of experiments:
1 SQL Data Definition Language Commands: Create all the tables specified above.
Make underlined columns as primary key. (use number, number(m,n), varchar(n),
date, time, timestamp data types appropriately)
Insert at least 5 rows to each table. (Check www.irctc.co.in website for actual data)
2 SQL Data Manipulation Language Commands:
1. Change the name of the Passenger table to Passenger_Details.
2. List all train details.
3. List all passenger details.
4. Give a list of trains in ascending order of number.
5. List the senior citizen passengers details.
6. List the station names where code starts with 'M'.
7. List the trains details within a range of numbers.
8. Change the super fast charge value in train fare as zero, if it is null.
9. List the passenger names whose tickets are not confirmed.
10. Remove all the rows from Passenger table permanently.
3 Create (Alter table to add constraint) the necessary foreign keys by identifying the
relationships in the table.
1) Add a suitable constraint to train table to always have train no in the range
10001 to 99999.
2) Add a suitable constraint for the column of station name, so that does not take
duplicates.
3) Change the data type of arrival time, depart time (date -> timestamp or
timestamp to date), and do the necessary process for updating the table with new
values.
4) Add a suitable constraint for the class column that it should take values only as
1A, 2A, 3A, SL, C.
5) Add a not null constraint for the column distance in train_route.
4 Designing Employee Database with MySQL (Create and insert data in EMP table, DEPT
table and SALGRADE table)
5 Multi row functions, GROUP By clause, HAVING clause, ORDER BY clause in SQL on
sample database.
6 Use Join Query
1. Find the train names that stop in 'Katpadi'.
2. Find the train names that are superfast and the service tax is zero.
3. Find the Passenger name (and train name) who have booked for the train that
starts from 'Chennai'.
4. Display the trains names, each type of class and the total fare for each type of
class.
5. Display all the train details and the ticket details (if booked any).
7 Use Nested Query(in Operators)
1. Find the train names that stop in 'Warangal'.
2. Find the train names that are superfast and the service tax is zero.
3. Find the Passenger name who have booked for the train that starts from
'Secunderabad'.
4. Find the trains names that have all the AC coaches and the base fare is less than 3000
for each case.
8 Create sample Views and practice basic operation
9 Write a PL/SQL procedures to practice Conditional Statements
10 Write a PL/SQL procedures to practice Iterative Statements
11 Implementing simple trigger
12 Implementing simple cursor
Equipment required for laboratory
i. Computers
ii. Mysql software
Textbooks:
1. Database Management Systems, Raghurama Krishnan, Johannes Gehrke, Tata Mc Graw Hill 3rd
Edition
2. Database System Concepts, Silberschatz, Korth, Mc Graw hill, V edition.
References:
1. Database Systems design, Implementation, and Management, Peter Rob & Carlos Coronel, 7th
Edition.
2. SQL The Complete Reference, James R. Groff, Paul N. Weinberg, 3rd Edition,
3. Oracle for Professionals, The X Team, S.Shah and V. Shah, SPD.
4. Database Systems Using Oracle: A Simplified guide to SQL and PL/SQL,Shah,PHI.
CO- PO, PSO Mapping
(3/2/1 indicates strength of correlation) 3-Strong, 2-Medium, 1-Weak
CO
S
Programme Outcomes (POs) PSOs
PO
1
PO
2
PO
3
PO
4
PO
5
PO
6
PO
7
PO
8
PO
9
PO1
0
PO1
1
PO1
2
PSO
1
PSO
2
PSO
3
CO1 2 2 2
CO2 - 2 2 1 2
CO3 2 2 2 3 2 2
SQL is a standard language for storing, manipulating and retrieving data in databases.
SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other
database systems.
Introduction to SQL
SQL is a standard language for accessing and manipulating databases.
What is SQL?
● SQL stands for Structured Query Language
● SQL lets you access and manipulate databases
● SQL became a standard of the American National Standards Institute (ANSI)
in 1986, and of the International Organization for Standardization (ISO) in
1987
What Can SQL do?
● SQL can execute queries against a database
● SQL can retrieve data from a database
● SQL can insert records in a database
● SQL can update records in a database
● SQL can delete records from a database
● SQL can create new databases
● SQL can create new tables in a database
● SQL can create stored procedures in a database
● SQL can create views in a database
● SQL can set permissions on tables, procedures, and views
RDBMS
RDBMS stands for Relational Database Management System.
RDBMS is the basis for SQL, and for all modern databasesystems such as MS SQL
Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
Keep in Mind That...
● SQL keywords are NOT case sensitive: select
is the same as SELECT In this tutorial we will write
all SQL keywords in upper-case.
Semicolon after SQL Statements?
Some database systems require a semicolon at the end of each SQL statement.
Semicolon is the standard way to separate each SQL statement in database systems
that allow more than one SQL statement to be executed in the same call to the server.
In this tutorial, we will use semicolon at the end of
each SQL statement. Some of The Most Important
SQL Commands
1. DDL: Data Definition Language (DDL) statements are used to define the database
structure or schema.
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index
 RENAME - rename an object
2. DML: Data Manipulation Language (DML) statements are used for managing
data within schema objects and to manipulate data of a database objects.
 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 TRUNCATE - remove all records from a table, including all spaces allocated
for the records are removed
3. DCL: Data Control Language (DCL) statements are used to create roles,
permissions, and referential integrity as well it is used to control access to database by
securing it. To control the data of a database.
 DCL Commands: Grant, Revoke
 GRANT - gives user's access privileges to database
 REVOKE -withdraw access privileges given with the GRANT command
4. TCL: Transaction Control (TCL) statements are used to manage the changes
made by DML statements. It allows statements to be grouped together into logical
transactions.
 TCL Commands: Commit, Rollback, Save point
 COMMIT - save work done
 SAVEPOINT - identify a point in a transaction to which you can later roll back
 ROLLBACK - restore database to original since the last COMMIT
Create table by using Constraints: Constraints are two types:
1. Table Level Constraints.
2. Column Level Constraints.
1. Not Null
2. Unique:
3. Primary Key
4. Check
5. Foreign Key
Aggregate Operators:
COUNT integer of values in the column
SUM The sum of all values in the column
AVG The average of all values in the column
MAX The maximum value in the column
MIN
The minimum value in the column
Variations of COUNT, SUM, AVG using DISTINCT
Experiment 1
Sample Database:
Railway Reservation System -(Redesigning IRCTC database)
Train (train Number, name, source, destination, start_time, reach_time, traveltime, distance, class,
days, type)
Ticket (PNRNo, Transactionid, from_station, To_station, date_of_journey, class date_of_booking,
total_ticket_fare, train number)
Passenger (PNR No, Serial no, Name, Age, Reservation_status)
Train_Route(Train_No, route_no, station_code, name, arrival_time, depart_time, distance, day)
Train_Ticket_fare(Train_No, class, base_fare, reservation_charge, superfast_charge, other_charge,
tatkal_charge, service_tax
1.SQL Data Definition Language Commands: Create all the tables specified above.
Make underlined columns as primary key. (use number, number(m,n), varchar(n), date,
time, timestamp data types appropriately)
Insert at least 5 rows to each table. (Check www.irctc.co.in website for actual data)
CREATE DATABASE RailwayReservationSystem;
Use RailwayReservationSystem;
CREATE TABLE Train
(
TrainNumber NUMERIC(6),
TrainName VARCHAR(20),
Source VARCHAR(20),
Destination VARCHAR(20),
start_time TIME,
reach_time TIME,
Travaltime TIME,
distance NUMERIC(10),
Class VARCHAR(4),
days NUMERIC(2),
type VARCHAR(10),
PRIMARY KEY (TrainNumber)
);
INSERT into Train
VALUES (10001,
'hyderabad exp',
'hyderabad',
'vizag',
100023,
231123,
24,
2456,
'A',
3,
'AC'
);
INSERT into Train
VALUES (12260,
'Vijayawada exp',
'Vijayawada',
'Warangal',
120322,
230222,
8,
2000,
'S',
1,
'General'
);
INSERT into Train
VALUES (11111,
'Banglore exp',
'hyderabad',
'banglore',
'140225',
062450,
48,
256,
'B',
2,
'AC'
);
INSERT into Train
VALUES (12280,
'mumbai exp',
'chennai',
'mumbai',
'154555',
202453,
25,
8000,
'S',
2,
'General'
);
INSERT into Train
VALUES (12255,
'delhi exp',
'delhi',
'mumbai',
202542,
052322,
12,
800,
'A',
1,
'General'
);
Select * from Train;
***
--TICKET
CREATE TABLE Ticket
(
PNRNo NUMERIC(20),
Transactionid VARCHAR(20),
from_station VARCHAR(20),
To_station VARCHAR(20),
date_of_journey DATE,
Class VARCHAR(4),
date_of_booking DATE,
total_ticket_fare NUMERIC(10),
TrainNumber NUMERIC(6),
PRIMARY KEY (PNRNo)
);
INSERT into Ticket
VALUES (122500023,
123456789,
'hyderabad',
'vizag',
'20220310',
'A',
'20220309',
240,
10001
);
INSERT into Ticket
VALUES (122500024,
1234567810,
'vijayawada',
'Warangal',
'20220311',
'S',
'20220310',
100,
12260
);
INSERT into Ticket
VALUES (122500025,
1234567811,
'hyderabad',
'Banglore',
'20220312',
'B',
'20220311',
1000,
11111
);
INSERT into Ticket
VALUES (122500026,
1234567812,
'Chennai',
'Mumbai',
'20220313',
'S',
'20220312',
1020,
12280
);
INSERT into Ticket
VALUES (122500027,
1234567813,
'Delhi',
'Mumbai',
'20220314',
'A',
'20220313',
2020,
12255
);
Select * from Ticket;
--PASSENGER
CREATE TABLE Passenger
(
PNRNo NUMERIC(20),
SerialNo VARCHAR(20),
Name VARCHAR(20),
Age NUMERIC(3),
Reservation_Status VARCHAR(20),
PRIMARY KEY (PNRNo,SerialNo)
);
INSERT into Passenger
VALUES (122500023,
12345,
'Allen',
'20',
'Waiting_List'
);
--
INSERT into Passenger
VALUES (122500024,
12346,
'Alex',
'28',
'Waiting_List'
);
--
INSERT into Passenger
VALUES (122500025,
12347,
'Bhuvan',
'38',
'Confirmed'
);
--
INSERT into Passenger
VALUES (122500027,
12349,
'Charls',
'88',
'Confirmed'
);
-
INSERT into Passenger
VALUES (122500028,
12310,
'keri',
'19',
'Waiting_List'
);
Select * from Passenger;
--TRAIN ROUTE
CREATE TABLE TrainRoute
(
TrainNumber NUMERIC(6),
RouteNo NUMERIC(20),
Station_code VARCHAR(20),
Name VARCHAR(10),
arrival_time TIME,
Departure_time TIME,
distance NUMERIC(10),
days NUMERIC(2),
PRIMARY KEY (TrainNumber,RouteNo)
);
INSERT into TrainRoute
VALUES (10001,
'2564444',
'hyd',
'Allen',
'111110',
'231110',
2456,
3
);
INSERT into TrainRoute
VALUES (12260,
'2564445',
'bza',
'Alex',
'121110',
'231210',
245,
2
);
INSERT into TrainRoute
VALUES (11111,
'2564446',
'mum',
'Bhuvan',
'121120',
'231420',
655,
1
);
INSERT into TrainRoute
VALUES (12280,
'2564447',
'mum',
'Bhuvana',
'122020',
'241420',
2555,
4
);
-
INSERT into TrainRoute
VALUES (12255,
'2564448',
'delhi',
'Charls',
'121440',
'243020',
555,
1
);
Select * from TrainRoute;
********
--TRAIN TICKET FARE
CREATE TABLE TrainTicketFare
(
TrainNumber NUMERIC(6),
Class VARCHAR(2),
Base_fare VARCHAR(20),
Reservation_charge VARCHAR(20),
Superfast_charge VARCHAR(10),
Other_charge VARCHAR(10),
Tatkal_charge NUMERIC(3,2),
Service_tax NUMERIC(10),
PRIMARY KEY (TrainNumber, Class)
);
INSERT into TrainTicketFare
VALUES (10001,
'A',
'200',
'200',
'300',
'50',
2.42,
26
);
-
INSERT into TrainTicketFare
VALUES (12260,
'A',
'200',
'200',
'300',
'50',
2.42,
26
);
INSERT into TrainTicketFare
VALUES (11111,
'A',
'200',
'200',
'300',
'50',
2.42,
26
);
INSERT into TrainTicketFare
VALUES (12280,
'A',
'200',
'200',
'300',
'50',
2.42,
26
);
INSERT into TrainTicketFare
VALUES (12255,
'A',
'200',
'200',
'300',
'50',
2.42,
26
);
Select * from TrainTicketFare;
Experiment 2
SQL Data Manipulation Language Commands:
1. Change the name of the Passenger table to Passenger_Details.
2. List all train details.
3. List all passenger details.
4. Give a list of trains in ascending order of number.
5. List the senior citizen passengers details.
6. List the station names where code starts with 'M'.
7. List the trains details within a range of numbers.
8. Change the super fast charge value in train fare as zero, if it is null.
9. List the passenger names whose tickets are not confirmed.
10. Remove all the rows from Passenger table permanently.
1.
Use RailwayReservationSystem;
ALTER TABLE Passenger RENAME TO Passenger_Details;
Select * from Passenger;
Select * from Passenger_Details;
********************************
2.
select * from Train;
*******************************
3
Select * from Passenger_Details;
********************************
4
SELECT * FROM Train
ORDER BY TrainNumber;
*********************************
5
Select Age from Passenger_Details where Age>=60;
*********************************
6
Select * from TrainRoute where station_code like 'M%';
************************************
7
Select * from Train where TrainNumber Between 1220 and 1300;
************************************
8
CREATE TABLE TrainTicketFare1
(
TrainNumber NUMERIC(4),
Class VARCHAR(2),
Base_fare VARCHAR(20),
Reservation_charge VARCHAR(20),
Superfast_charge VARCHAR(10),
Other_charge VARCHAR(10),
Tatkal_charge NUMERIC(3,2),
Service_tax NUMERIC(10),
PRIMARY KEY (TrainNumber, Class)
);
INSERT into TrainTicketFare1
VALUES (1228,
'A',
'200',
'200',
'450',
'50',
4.20,
26
);
INSERT into TrainTicketFare1
VALUES (1229,
'A',
'200',
'200',
'450',
'50',
4.20,
26
);
INSERT into TrainTicketFare1
VALUES (1231,
'A',
'200',
'200',
NULL,
'50',
4.20,
26
);
UPDATE TrainTicketFare1
SET Superfast_charge = 0
WHERE Superfast_charge IS NULL;
*************************************
9
Select * from Passenger_Details where Reservation_status like 'Waiting_List';
*************************************
10
Delete from Passenger_Details;
Select * from Passenger_Details;
Experiment 3
3.Create (Alter table to add constraint) the necessary foreign keys by identifying the
relationships in the table.
1) Add a suitable constraint to train table to always have train no in the range 10001 to
99999.
2) Add a suitable constraint for the column of station name, so that does not take duplicates.
3) Change the data type of arrival time, depart time (date -> timestamp or timestamp to date),
and do the necessary process for updating the table with new values.
4) Add a suitable constraint for the class column that it should take values only as 1A, 2A,
3A, SL, C.
5) Add a not null constraint for the column distance in train_route.
1.
CREATE DATABASE RailwayReservationSystem;
Use RailwayReservationSystem;
CREATE TABLE Train1
(
TrainNumber NUMERIC(6),
TrainName VARCHAR(20),
Source VARCHAR(20),
Destination VARCHAR(20),
start_time TIME,
reach_time TIME,
Travaltime TIME,
distance NUMERIC(10),
Class VARCHAR(4),
days NUMERIC(2),
type VARCHAR(10),
PRIMARY KEY (TrainNumber)
);
----------------------
INSERT into Train1
VALUES (10001,
'hyderabad exp',
'hyderabad',
'vizag',
100023,
231123,
24,
2456,
'A',
3,
'AC'
);
--------------------
INSERT into Train1
VALUES (12260,
'Vijayawada exp',
'Vijayawada',
'Warangal',
120322,
230222,
8,
2000,
'S',
1,
'General'
);
--------------------
INSERT into Train1
VALUES (11111,
'Banglore exp',
'hyderabad',
'banglore',
'140225',
062450,
48,
256,
'B',
2,
'AC'
);
---------------------
INSERT into Train1
VALUES (12280,
'mumbai exp',
'chennai',
'mumbai',
'154555',
202453,
25,
8000,
'S',
2,
'General'
);
------------------------
INSERT into Train1
VALUES (12255,
'delhi exp',
'delhi',
'mumbai',
202542,
052322,
12,
800,
'A',
1,
'General'
);
------------------------
INSERT into Train1
VALUES (9000,
'delhi exp',
'delhi',
'mumbai',
202542,
052322,
12,
800,
'A',
1,
'General'
);
------------------------
INSERT into Train1
VALUES (200000,
'delhi exp',
'delhi',
'mumbai',
202542,
052322,
12,
800,
'A',
1,
'General'
);
Select * from Train1;
Select * from Train1 where TrainNumber>=10001 and TrainNumber<=99999;
***************************************************************************
***********************
2.alter table TrainRoute add constraint Train_Route_name_unique unique(Station_code);
INSERT into TrainRoute
VALUES (1228,
2564444,
'del',
'Allen',
111110,
052322,
1233,
1
);
***************************************************************************
***********************
3.alter table TrainRoute drop column arrival_time;
alter table TrainRoute drop column Departure_time;
alter table TrainRoute add Departure_time timestamp;
alter table TrainRoute add arrival_time timestamp;
update TrainRoute set arrival_time=timestamp('2018-08-08
22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=1228;
update TrainRoute set arrival_time=timestamp('2018-08-08
22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=10001;
update TrainRoute set arrival_time=timestamp('2018-08-08
22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=11111;
update TrainRoute set arrival_time=timestamp('2018-08-08
22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=12255;
update TrainRoute set arrival_time=timestamp('2018-08-08
22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=12260;
update TrainRoute set arrival_time=timestamp('2018-08-08
22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=12280;
***************************************************************************
***********************
4.ALTER TABLE TrainTicketFare ADD CONSTRAINT CHECKCLASS
CHECK(Class='1A' and Class='2A' and Class='3A' and Class='SL' and Class='C');
***************************************************************************
************************
5.ALTER TABLE TrainRoute MODIFY distance int NOT NULL
Experiment 4
4.Designing Employee Database with MySQL (Create and insert data in EMP table, DEPT table and
SALGRADE table)
CREATE TABLE emp (
empno NUMERIC(4),
ename VARCHAR(10),
job VARCHAR(9),
mgr NUMERIC(4),
hiredate DATE,
sal NUMERIC(7),
comm NUMERIC(7),
deptno NUMERIC(2),
PRIMARY KEY (empno)
);
INSERT INTO emp
VALUES(
7838,
'KING',
'PRESIDENT',
null,
'19811117',
5000,
null,
10 );
INSERT INTO emp
VALUES(
7698,
'BLAKE',
'MANAGER',
7839,
'19810510',
2850,
null,
30);
INSERT INTO emp
VALUES(
7782,
'CLARK',
'MANAGER',
7839,
'19810609',
2450,
null,
10);
INSERT INTO emp
VALUES(
7566,
'JONES',
'MANAGER',
7839,
'19810402',
2975,
null,
20);
INSERT INTO emp
VALUES(
7902,
'FORD',
'ANALYST',
7566,
'19811203',
3000,
null,
20 );
INSERT INTO emp
VALUES(
7839,
'KING',
'PRESIDENT',
null,
'19811117',
5000,
null,
10 );
****************************************
CREATE TABLE dept (
deptno NUMERIC(2),
dname VARCHAR(14),
loc VARCHAR(13),
PRIMARY KEY (deptno)
);
INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO dept VALUES(20, 'RESEARCH', 'DALLAS');
INSERT INTO dept VALUES(30, 'SALES', 'CHICAGO');
INSERT INTO dept VALUES(40, 'OPERATIONS', 'BOSTON');
INSERT INTO dept VALUES(50, 'DEVELOPEMENT', 'BOSTON');
CREATE TABLE salgrade (
grade NUMERIC(20),
min_sal NUMERIC(20),
max_sal NUMERIC(20),
empno Numeric(10),
job varchar(20),
Primary key (empno)
);
INSERT INTO salgrade VALUES (1, 700, 1200, 7838,'PRESIDENT');
INSERT INTO salgrade VALUES (2, 1201, 1400,7698,'MANAGER');
INSERT INTO salgrade VALUES (3, 1401, 2000,7782,'MANAGER');
INSERT INTO salgrade VALUES (4, 2001, 3000,7566,'MANAGER');
INSERT INTO salgrade VALUES (5, 3001, 9999,7902,'ANALYST');
Experiment 5
5.Multi row functions, GROUP By clause, HAVING clause, ORDER BY clause in SQL on sample
database.
GROUPBY clause
---------------
SELECT deptno "Department Code",
COUNT(*) "empno"
FROM emp
GROUP BY deptno;
*************************************
GROUP BY with SUM() function
-----------------------------
SELECT deptno, SUM(sal)
FROM emp
GROUP BY deptno;
***********************************
SQL GROUP BY with COUNT() and SUM() function
---------------------------------------------
SELECT deptno "Department Code",
COUNT(*) "empno",
SUM(sal) "Total Salary"
FROM emp
GROUP BY deptno;
****************************************
SQL GROUP BY on more than one columns
-------------------------------------
SELECT deptno "Department Code", ename,
SUM(sal) "Total Salary"
FROM emp
GROUP BY deptno,ename;
********************************************
SQL GROUP BY with WHERE clause
-------------------------------
SELECT deptno"Department Code",
SUM(sal) "Total Salary"
FROM emp
WHERE mgr = 7566
GROUP BY deptno;
********************************************
SQL GROUP BY with HAVING clause
-------------------------------
SELECT deptno, count(*) "empno"
FROM emp
GROUP BY deptno
HAVING count(*)>2;
*********************************************
SQL HAVING using where
--------------------------
SELECT job,SUM(empno),
AVG(min_sal),MAX(max_sal)
FROM emp
WHERE grade=2
GROUP BY Job
HAVING AVG(min_sal)>500;
******************************************
SQL ORDER BY clause - Sorting on column names
---------------------------------------------
SELECT * FROM emp ORDER BY deptno;
*********************************************
Usage of the ORDER BY clause with the DESC argument
----------------------------------------------------
SELECT * FROM emp ORDER BY deptno DESC;
**********************************************
Ordering by more than one columns
--------------------------------------
SELECT * FROM emp ORDER BY empno,deptno;
***********************************************
Ordering by more than one columns in ascending or descending order
------------------------------------------------------------------
SELECT * FROM emp ORDER BY sal DESC,empno ASC;
******************************************************************

More Related Content

Similar to Fdms 1st cycle exp.pdf

dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.pptMARasheed3
 
Database Management Lab -SQL Queries
Database Management Lab -SQL Queries Database Management Lab -SQL Queries
Database Management Lab -SQL Queries shamim hossain
 
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...Editor IJCATR
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-convertedgayaramesh
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredDanish Mehraj
 
BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfKeerthanaP37
 
Analysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptxAnalysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptxBrendan Furey
 
Lecture on DBMS & MySQL.pdf v. C. .
Lecture on DBMS & MySQL.pdf v.  C.     .Lecture on DBMS & MySQL.pdf v.  C.     .
Lecture on DBMS & MySQL.pdf v. C. .MayankSinghRawat6
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxEliasPetros
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...TAISEEREISA
 
MODULE 5.pptx
MODULE 5.pptxMODULE 5.pptx
MODULE 5.pptxlathass5
 

Similar to Fdms 1st cycle exp.pdf (20)

dbs class 7.ppt
dbs class 7.pptdbs class 7.ppt
dbs class 7.ppt
 
KCS-501-3.pdf
KCS-501-3.pdfKCS-501-3.pdf
KCS-501-3.pdf
 
Database Management Lab -SQL Queries
Database Management Lab -SQL Queries Database Management Lab -SQL Queries
Database Management Lab -SQL Queries
 
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
An Extension of RETRO Framework: Translating SQL Insert, Update and Delete Qu...
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
Database programming
Database programmingDatabase programming
Database programming
 
sql-commands.pdf
sql-commands.pdfsql-commands.pdf
sql-commands.pdf
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql commands
Sql commandsSql commands
Sql commands
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Unit 3 rdbms study_materials-converted
Unit 3  rdbms study_materials-convertedUnit 3  rdbms study_materials-converted
Unit 3 rdbms study_materials-converted
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
BCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdfBCS4L1-Database Management lab.pdf
BCS4L1-Database Management lab.pdf
 
Analysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptxAnalysing Performance of Algorithmic SQL and PLSQL.pptx
Analysing Performance of Algorithmic SQL and PLSQL.pptx
 
7. SQL.pptx
7. SQL.pptx7. SQL.pptx
7. SQL.pptx
 
Lecture on DBMS & MySQL.pdf v. C. .
Lecture on DBMS & MySQL.pdf v.  C.     .Lecture on DBMS & MySQL.pdf v.  C.     .
Lecture on DBMS & MySQL.pdf v. C. .
 
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptxMy lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
My lablkxjlkxjcvlxkcjvlxckjvlxck ppt.pptx
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
 
MODULE 5.pptx
MODULE 5.pptxMODULE 5.pptx
MODULE 5.pptx
 

Recently uploaded

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 

Recently uploaded (20)

APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 

Fdms 1st cycle exp.pdf

  • 1. Department of Electrical and Electronics Engineering III B. Tech - II Semester Sub: Fundamentals of Data Base Management Systems Laboratory Manual Subject Code : A0562 Academic Year 2021-22 Regulations: MR18 Malla Reddy Engineering College (Autonomous) (An UGC Autonomous Institution, Approved by AICTE and Affiliated to JNTUH Hyderabad,
  • 2. Recognized under 2(f) &12 (B) of UGC Act 1956, Accredited by NAAC with ‘A’ Grade (II Cycle) Maisammaguda, Dhulapally (Post Via Kompally), Secunderabad-500 100 www.mrec.ac.in E-mail: principal@mrec.ac.in 2021-2022
  • 3. Malla Reddy Engineering College (Autonomous) Maisammaguda, Dhulapally (Post Via Kompally), Secunderabad-500100 Institute Vision To be a premier centre of professional education and research, offering quality programs in a socio - economic and ethical ambience. Institute Mission 1. To impart knowledge of advanced technologies using state-of-the-art infrastructural facilities. 2. To inculcate innovation and best practices in education, training and research. 3. To meet changing socio-economic needs in an ethical ambience.
  • 4. 2020-21 Onwards (MR-20) MALLA REDDY ENGINEERING COLLEGE (Autonomous) B.Tech. V Semester Code: A0562 Fundamentals of Database Management Systems Lab (Common for CE, EEE, ME, ECE, MiE) L T P Credits: 2 - 1 2 Prerequisites: NIL Course Objectives: This course enables the students to practice the concepts learnt in the subject DBMS by developing a database for an example project. The student is expected to practice the querying a relational database i.e., “Mysql” with various functionalities of SQL and PL/SQL statements using a sample database. Sample Database: Railway Reservation System -(Redesigning IRCTC database) Train (train Number, name, source, destination, start_time, reach_time, traveltime, distance, class, days, type) Ticket (PNRNo, Transactionid, from_station, To_station, date_of_journey, class date_of_booking, total_ticket_fare, train number) Passenger (PNR No, Serial no, Name, Age, Reservation_status) Train_Route(Train_No, route_no, station_code, name, arrival_time, depart_time, distance, day) Train_Ticket_fare(Train_No, class, base_fare, reservation_charge, superfast_charge, other_charge, tatkal_charge, service_tax) List of experiments: 1 SQL Data Definition Language Commands: Create all the tables specified above. Make underlined columns as primary key. (use number, number(m,n), varchar(n), date, time, timestamp data types appropriately) Insert at least 5 rows to each table. (Check www.irctc.co.in website for actual data) 2 SQL Data Manipulation Language Commands: 1. Change the name of the Passenger table to Passenger_Details. 2. List all train details. 3. List all passenger details. 4. Give a list of trains in ascending order of number. 5. List the senior citizen passengers details. 6. List the station names where code starts with 'M'. 7. List the trains details within a range of numbers. 8. Change the super fast charge value in train fare as zero, if it is null. 9. List the passenger names whose tickets are not confirmed. 10. Remove all the rows from Passenger table permanently. 3 Create (Alter table to add constraint) the necessary foreign keys by identifying the relationships in the table. 1) Add a suitable constraint to train table to always have train no in the range
  • 5. 10001 to 99999. 2) Add a suitable constraint for the column of station name, so that does not take duplicates. 3) Change the data type of arrival time, depart time (date -> timestamp or timestamp to date), and do the necessary process for updating the table with new values. 4) Add a suitable constraint for the class column that it should take values only as 1A, 2A, 3A, SL, C. 5) Add a not null constraint for the column distance in train_route. 4 Designing Employee Database with MySQL (Create and insert data in EMP table, DEPT table and SALGRADE table) 5 Multi row functions, GROUP By clause, HAVING clause, ORDER BY clause in SQL on sample database. 6 Use Join Query 1. Find the train names that stop in 'Katpadi'. 2. Find the train names that are superfast and the service tax is zero. 3. Find the Passenger name (and train name) who have booked for the train that starts from 'Chennai'. 4. Display the trains names, each type of class and the total fare for each type of class. 5. Display all the train details and the ticket details (if booked any). 7 Use Nested Query(in Operators) 1. Find the train names that stop in 'Warangal'. 2. Find the train names that are superfast and the service tax is zero. 3. Find the Passenger name who have booked for the train that starts from 'Secunderabad'. 4. Find the trains names that have all the AC coaches and the base fare is less than 3000 for each case. 8 Create sample Views and practice basic operation 9 Write a PL/SQL procedures to practice Conditional Statements 10 Write a PL/SQL procedures to practice Iterative Statements 11 Implementing simple trigger 12 Implementing simple cursor Equipment required for laboratory i. Computers ii. Mysql software Textbooks: 1. Database Management Systems, Raghurama Krishnan, Johannes Gehrke, Tata Mc Graw Hill 3rd Edition 2. Database System Concepts, Silberschatz, Korth, Mc Graw hill, V edition. References: 1. Database Systems design, Implementation, and Management, Peter Rob & Carlos Coronel, 7th Edition. 2. SQL The Complete Reference, James R. Groff, Paul N. Weinberg, 3rd Edition, 3. Oracle for Professionals, The X Team, S.Shah and V. Shah, SPD. 4. Database Systems Using Oracle: A Simplified guide to SQL and PL/SQL,Shah,PHI.
  • 6. CO- PO, PSO Mapping (3/2/1 indicates strength of correlation) 3-Strong, 2-Medium, 1-Weak CO S Programme Outcomes (POs) PSOs PO 1 PO 2 PO 3 PO 4 PO 5 PO 6 PO 7 PO 8 PO 9 PO1 0 PO1 1 PO1 2 PSO 1 PSO 2 PSO 3 CO1 2 2 2 CO2 - 2 2 1 2 CO3 2 2 2 3 2 2
  • 7. SQL is a standard language for storing, manipulating and retrieving data in databases. SQL in: MySQL, SQL Server, MS Access, Oracle, Sybase, Informix, Postgres, and other database systems. Introduction to SQL SQL is a standard language for accessing and manipulating databases. What is SQL? ● SQL stands for Structured Query Language ● SQL lets you access and manipulate databases ● SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987 What Can SQL do? ● SQL can execute queries against a database ● SQL can retrieve data from a database ● SQL can insert records in a database ● SQL can update records in a database ● SQL can delete records from a database ● SQL can create new databases ● SQL can create new tables in a database ● SQL can create stored procedures in a database ● SQL can create views in a database ● SQL can set permissions on tables, procedures, and views RDBMS RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern databasesystems such as MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. Keep in Mind That... ● SQL keywords are NOT case sensitive: select is the same as SELECT In this tutorial we will write all SQL keywords in upper-case. Semicolon after SQL Statements? Some database systems require a semicolon at the end of each SQL statement. Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server. In this tutorial, we will use semicolon at the end of each SQL statement. Some of The Most Important SQL Commands 1. DDL: Data Definition Language (DDL) statements are used to define the database structure or schema.
  • 8.  CREATE DATABASE - creates a new database  ALTER DATABASE - modifies a database  CREATE TABLE - creates a new table  ALTER TABLE - modifies a table  DROP TABLE - deletes a table  CREATE INDEX - creates an index (search key)  DROP INDEX - deletes an index  RENAME - rename an object 2. DML: Data Manipulation Language (DML) statements are used for managing data within schema objects and to manipulate data of a database objects.  SELECT - extracts data from a database  UPDATE - updates data in a database  DELETE - deletes data from a database  INSERT INTO - inserts new data into a database  TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed 3. DCL: Data Control Language (DCL) statements are used to create roles, permissions, and referential integrity as well it is used to control access to database by securing it. To control the data of a database.  DCL Commands: Grant, Revoke  GRANT - gives user's access privileges to database  REVOKE -withdraw access privileges given with the GRANT command 4. TCL: Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.  TCL Commands: Commit, Rollback, Save point  COMMIT - save work done  SAVEPOINT - identify a point in a transaction to which you can later roll back  ROLLBACK - restore database to original since the last COMMIT Create table by using Constraints: Constraints are two types: 1. Table Level Constraints. 2. Column Level Constraints. 1. Not Null 2. Unique: 3. Primary Key 4. Check 5. Foreign Key Aggregate Operators: COUNT integer of values in the column SUM The sum of all values in the column AVG The average of all values in the column
  • 9. MAX The maximum value in the column MIN The minimum value in the column Variations of COUNT, SUM, AVG using DISTINCT
  • 10. Experiment 1 Sample Database: Railway Reservation System -(Redesigning IRCTC database) Train (train Number, name, source, destination, start_time, reach_time, traveltime, distance, class, days, type) Ticket (PNRNo, Transactionid, from_station, To_station, date_of_journey, class date_of_booking, total_ticket_fare, train number) Passenger (PNR No, Serial no, Name, Age, Reservation_status) Train_Route(Train_No, route_no, station_code, name, arrival_time, depart_time, distance, day) Train_Ticket_fare(Train_No, class, base_fare, reservation_charge, superfast_charge, other_charge, tatkal_charge, service_tax 1.SQL Data Definition Language Commands: Create all the tables specified above. Make underlined columns as primary key. (use number, number(m,n), varchar(n), date, time, timestamp data types appropriately) Insert at least 5 rows to each table. (Check www.irctc.co.in website for actual data) CREATE DATABASE RailwayReservationSystem; Use RailwayReservationSystem; CREATE TABLE Train ( TrainNumber NUMERIC(6), TrainName VARCHAR(20), Source VARCHAR(20), Destination VARCHAR(20), start_time TIME, reach_time TIME, Travaltime TIME, distance NUMERIC(10), Class VARCHAR(4), days NUMERIC(2), type VARCHAR(10), PRIMARY KEY (TrainNumber) ); INSERT into Train VALUES (10001, 'hyderabad exp', 'hyderabad', 'vizag', 100023, 231123, 24, 2456, 'A', 3, 'AC' ); INSERT into Train
  • 11. VALUES (12260, 'Vijayawada exp', 'Vijayawada', 'Warangal', 120322, 230222, 8, 2000, 'S', 1, 'General' ); INSERT into Train VALUES (11111, 'Banglore exp', 'hyderabad', 'banglore', '140225', 062450, 48, 256, 'B', 2, 'AC' ); INSERT into Train VALUES (12280, 'mumbai exp', 'chennai', 'mumbai', '154555', 202453, 25, 8000, 'S', 2, 'General' ); INSERT into Train VALUES (12255, 'delhi exp', 'delhi', 'mumbai', 202542, 052322, 12, 800,
  • 12. 'A', 1, 'General' ); Select * from Train; *** --TICKET CREATE TABLE Ticket ( PNRNo NUMERIC(20), Transactionid VARCHAR(20), from_station VARCHAR(20), To_station VARCHAR(20), date_of_journey DATE, Class VARCHAR(4), date_of_booking DATE, total_ticket_fare NUMERIC(10), TrainNumber NUMERIC(6), PRIMARY KEY (PNRNo) ); INSERT into Ticket VALUES (122500023, 123456789, 'hyderabad', 'vizag', '20220310', 'A', '20220309', 240, 10001 ); INSERT into Ticket VALUES (122500024, 1234567810, 'vijayawada', 'Warangal', '20220311', 'S', '20220310', 100, 12260 ); INSERT into Ticket
  • 13. VALUES (122500025, 1234567811, 'hyderabad', 'Banglore', '20220312', 'B', '20220311', 1000, 11111 ); INSERT into Ticket VALUES (122500026, 1234567812, 'Chennai', 'Mumbai', '20220313', 'S', '20220312', 1020, 12280 ); INSERT into Ticket VALUES (122500027, 1234567813, 'Delhi', 'Mumbai', '20220314', 'A', '20220313', 2020, 12255 ); Select * from Ticket; --PASSENGER CREATE TABLE Passenger ( PNRNo NUMERIC(20), SerialNo VARCHAR(20), Name VARCHAR(20), Age NUMERIC(3), Reservation_Status VARCHAR(20), PRIMARY KEY (PNRNo,SerialNo) ); INSERT into Passenger
  • 14. VALUES (122500023, 12345, 'Allen', '20', 'Waiting_List' ); -- INSERT into Passenger VALUES (122500024, 12346, 'Alex', '28', 'Waiting_List' ); -- INSERT into Passenger VALUES (122500025, 12347, 'Bhuvan', '38', 'Confirmed' ); -- INSERT into Passenger VALUES (122500027, 12349, 'Charls', '88', 'Confirmed' ); - INSERT into Passenger VALUES (122500028, 12310, 'keri', '19', 'Waiting_List' ); Select * from Passenger; --TRAIN ROUTE CREATE TABLE TrainRoute ( TrainNumber NUMERIC(6), RouteNo NUMERIC(20), Station_code VARCHAR(20), Name VARCHAR(10),
  • 15. arrival_time TIME, Departure_time TIME, distance NUMERIC(10), days NUMERIC(2), PRIMARY KEY (TrainNumber,RouteNo) ); INSERT into TrainRoute VALUES (10001, '2564444', 'hyd', 'Allen', '111110', '231110', 2456, 3 ); INSERT into TrainRoute VALUES (12260, '2564445', 'bza', 'Alex', '121110', '231210', 245, 2 ); INSERT into TrainRoute VALUES (11111, '2564446', 'mum', 'Bhuvan', '121120', '231420', 655, 1 ); INSERT into TrainRoute VALUES (12280, '2564447', 'mum', 'Bhuvana', '122020', '241420', 2555,
  • 16. 4 ); - INSERT into TrainRoute VALUES (12255, '2564448', 'delhi', 'Charls', '121440', '243020', 555, 1 ); Select * from TrainRoute; ******** --TRAIN TICKET FARE CREATE TABLE TrainTicketFare ( TrainNumber NUMERIC(6), Class VARCHAR(2), Base_fare VARCHAR(20), Reservation_charge VARCHAR(20), Superfast_charge VARCHAR(10), Other_charge VARCHAR(10), Tatkal_charge NUMERIC(3,2), Service_tax NUMERIC(10), PRIMARY KEY (TrainNumber, Class) ); INSERT into TrainTicketFare VALUES (10001, 'A', '200', '200', '300', '50', 2.42, 26 ); - INSERT into TrainTicketFare VALUES (12260, 'A', '200', '200', '300', '50', 2.42,
  • 17. 26 ); INSERT into TrainTicketFare VALUES (11111, 'A', '200', '200', '300', '50', 2.42, 26 ); INSERT into TrainTicketFare VALUES (12280, 'A', '200', '200', '300', '50', 2.42, 26 ); INSERT into TrainTicketFare VALUES (12255, 'A', '200', '200', '300', '50', 2.42, 26 ); Select * from TrainTicketFare;
  • 18. Experiment 2 SQL Data Manipulation Language Commands: 1. Change the name of the Passenger table to Passenger_Details. 2. List all train details. 3. List all passenger details. 4. Give a list of trains in ascending order of number. 5. List the senior citizen passengers details. 6. List the station names where code starts with 'M'. 7. List the trains details within a range of numbers. 8. Change the super fast charge value in train fare as zero, if it is null. 9. List the passenger names whose tickets are not confirmed. 10. Remove all the rows from Passenger table permanently. 1. Use RailwayReservationSystem; ALTER TABLE Passenger RENAME TO Passenger_Details; Select * from Passenger; Select * from Passenger_Details; ******************************** 2. select * from Train; ******************************* 3 Select * from Passenger_Details; ******************************** 4 SELECT * FROM Train ORDER BY TrainNumber; ********************************* 5 Select Age from Passenger_Details where Age>=60; ********************************* 6 Select * from TrainRoute where station_code like 'M%'; ************************************ 7 Select * from Train where TrainNumber Between 1220 and 1300;
  • 19. ************************************ 8 CREATE TABLE TrainTicketFare1 ( TrainNumber NUMERIC(4), Class VARCHAR(2), Base_fare VARCHAR(20), Reservation_charge VARCHAR(20), Superfast_charge VARCHAR(10), Other_charge VARCHAR(10), Tatkal_charge NUMERIC(3,2), Service_tax NUMERIC(10), PRIMARY KEY (TrainNumber, Class) ); INSERT into TrainTicketFare1 VALUES (1228, 'A', '200', '200', '450', '50', 4.20, 26 ); INSERT into TrainTicketFare1 VALUES (1229, 'A', '200', '200', '450', '50', 4.20, 26 ); INSERT into TrainTicketFare1 VALUES (1231, 'A', '200', '200', NULL, '50', 4.20, 26 );
  • 20. UPDATE TrainTicketFare1 SET Superfast_charge = 0 WHERE Superfast_charge IS NULL; ************************************* 9 Select * from Passenger_Details where Reservation_status like 'Waiting_List'; ************************************* 10 Delete from Passenger_Details; Select * from Passenger_Details;
  • 21. Experiment 3 3.Create (Alter table to add constraint) the necessary foreign keys by identifying the relationships in the table. 1) Add a suitable constraint to train table to always have train no in the range 10001 to 99999. 2) Add a suitable constraint for the column of station name, so that does not take duplicates. 3) Change the data type of arrival time, depart time (date -> timestamp or timestamp to date), and do the necessary process for updating the table with new values. 4) Add a suitable constraint for the class column that it should take values only as 1A, 2A, 3A, SL, C. 5) Add a not null constraint for the column distance in train_route. 1. CREATE DATABASE RailwayReservationSystem; Use RailwayReservationSystem; CREATE TABLE Train1 ( TrainNumber NUMERIC(6), TrainName VARCHAR(20), Source VARCHAR(20), Destination VARCHAR(20), start_time TIME, reach_time TIME, Travaltime TIME, distance NUMERIC(10), Class VARCHAR(4), days NUMERIC(2), type VARCHAR(10), PRIMARY KEY (TrainNumber) ); ---------------------- INSERT into Train1 VALUES (10001, 'hyderabad exp', 'hyderabad', 'vizag', 100023, 231123, 24, 2456, 'A', 3, 'AC' ); --------------------
  • 22. INSERT into Train1 VALUES (12260, 'Vijayawada exp', 'Vijayawada', 'Warangal', 120322, 230222, 8, 2000, 'S', 1, 'General' ); -------------------- INSERT into Train1 VALUES (11111, 'Banglore exp', 'hyderabad', 'banglore', '140225', 062450, 48, 256, 'B', 2, 'AC' ); --------------------- INSERT into Train1 VALUES (12280, 'mumbai exp', 'chennai', 'mumbai', '154555', 202453, 25, 8000, 'S', 2, 'General' ); ------------------------ INSERT into Train1 VALUES (12255, 'delhi exp', 'delhi', 'mumbai', 202542, 052322, 12,
  • 23. 800, 'A', 1, 'General' ); ------------------------ INSERT into Train1 VALUES (9000, 'delhi exp', 'delhi', 'mumbai', 202542, 052322, 12, 800, 'A', 1, 'General' ); ------------------------ INSERT into Train1 VALUES (200000, 'delhi exp', 'delhi', 'mumbai', 202542, 052322, 12, 800, 'A', 1, 'General' ); Select * from Train1; Select * from Train1 where TrainNumber>=10001 and TrainNumber<=99999; *************************************************************************** *********************** 2.alter table TrainRoute add constraint Train_Route_name_unique unique(Station_code); INSERT into TrainRoute VALUES (1228, 2564444,
  • 24. 'del', 'Allen', 111110, 052322, 1233, 1 ); *************************************************************************** *********************** 3.alter table TrainRoute drop column arrival_time; alter table TrainRoute drop column Departure_time; alter table TrainRoute add Departure_time timestamp; alter table TrainRoute add arrival_time timestamp; update TrainRoute set arrival_time=timestamp('2018-08-08 22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=1228; update TrainRoute set arrival_time=timestamp('2018-08-08 22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=10001; update TrainRoute set arrival_time=timestamp('2018-08-08 22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=11111; update TrainRoute set arrival_time=timestamp('2018-08-08 22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=12255; update TrainRoute set arrival_time=timestamp('2018-08-08 22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=12260; update TrainRoute set arrival_time=timestamp('2018-08-08 22:30:00'),Departure_time=timestamp('2018-08-10 07:10:00') where TrainNumber=12280; *************************************************************************** *********************** 4.ALTER TABLE TrainTicketFare ADD CONSTRAINT CHECKCLASS CHECK(Class='1A' and Class='2A' and Class='3A' and Class='SL' and Class='C'); *************************************************************************** ************************ 5.ALTER TABLE TrainRoute MODIFY distance int NOT NULL
  • 25. Experiment 4 4.Designing Employee Database with MySQL (Create and insert data in EMP table, DEPT table and SALGRADE table) CREATE TABLE emp ( empno NUMERIC(4), ename VARCHAR(10), job VARCHAR(9), mgr NUMERIC(4), hiredate DATE, sal NUMERIC(7), comm NUMERIC(7), deptno NUMERIC(2), PRIMARY KEY (empno) ); INSERT INTO emp VALUES( 7838, 'KING', 'PRESIDENT', null, '19811117', 5000, null, 10 ); INSERT INTO emp VALUES( 7698, 'BLAKE', 'MANAGER', 7839, '19810510', 2850, null, 30); INSERT INTO emp VALUES( 7782, 'CLARK', 'MANAGER', 7839, '19810609', 2450, null, 10);
  • 26. INSERT INTO emp VALUES( 7566, 'JONES', 'MANAGER', 7839, '19810402', 2975, null, 20); INSERT INTO emp VALUES( 7902, 'FORD', 'ANALYST', 7566, '19811203', 3000, null, 20 ); INSERT INTO emp VALUES( 7839, 'KING', 'PRESIDENT', null, '19811117', 5000, null, 10 ); **************************************** CREATE TABLE dept ( deptno NUMERIC(2), dname VARCHAR(14), loc VARCHAR(13), PRIMARY KEY (deptno) ); INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK'); INSERT INTO dept VALUES(20, 'RESEARCH', 'DALLAS'); INSERT INTO dept VALUES(30, 'SALES', 'CHICAGO'); INSERT INTO dept VALUES(40, 'OPERATIONS', 'BOSTON'); INSERT INTO dept VALUES(50, 'DEVELOPEMENT', 'BOSTON'); CREATE TABLE salgrade ( grade NUMERIC(20), min_sal NUMERIC(20), max_sal NUMERIC(20),
  • 27. empno Numeric(10), job varchar(20), Primary key (empno) ); INSERT INTO salgrade VALUES (1, 700, 1200, 7838,'PRESIDENT'); INSERT INTO salgrade VALUES (2, 1201, 1400,7698,'MANAGER'); INSERT INTO salgrade VALUES (3, 1401, 2000,7782,'MANAGER'); INSERT INTO salgrade VALUES (4, 2001, 3000,7566,'MANAGER'); INSERT INTO salgrade VALUES (5, 3001, 9999,7902,'ANALYST');
  • 28. Experiment 5 5.Multi row functions, GROUP By clause, HAVING clause, ORDER BY clause in SQL on sample database. GROUPBY clause --------------- SELECT deptno "Department Code", COUNT(*) "empno" FROM emp GROUP BY deptno; ************************************* GROUP BY with SUM() function ----------------------------- SELECT deptno, SUM(sal) FROM emp GROUP BY deptno; *********************************** SQL GROUP BY with COUNT() and SUM() function --------------------------------------------- SELECT deptno "Department Code", COUNT(*) "empno", SUM(sal) "Total Salary" FROM emp GROUP BY deptno; **************************************** SQL GROUP BY on more than one columns ------------------------------------- SELECT deptno "Department Code", ename, SUM(sal) "Total Salary" FROM emp GROUP BY deptno,ename; ******************************************** SQL GROUP BY with WHERE clause ------------------------------- SELECT deptno"Department Code", SUM(sal) "Total Salary" FROM emp WHERE mgr = 7566 GROUP BY deptno; ******************************************** SQL GROUP BY with HAVING clause ------------------------------- SELECT deptno, count(*) "empno" FROM emp GROUP BY deptno HAVING count(*)>2; ********************************************* SQL HAVING using where --------------------------
  • 29. SELECT job,SUM(empno), AVG(min_sal),MAX(max_sal) FROM emp WHERE grade=2 GROUP BY Job HAVING AVG(min_sal)>500; ****************************************** SQL ORDER BY clause - Sorting on column names --------------------------------------------- SELECT * FROM emp ORDER BY deptno; ********************************************* Usage of the ORDER BY clause with the DESC argument ---------------------------------------------------- SELECT * FROM emp ORDER BY deptno DESC; ********************************************** Ordering by more than one columns -------------------------------------- SELECT * FROM emp ORDER BY empno,deptno; *********************************************** Ordering by more than one columns in ascending or descending order ------------------------------------------------------------------ SELECT * FROM emp ORDER BY sal DESC,empno ASC; ******************************************************************