Database System Concepts and PLSQL
Database System Concepts and PLSQL
2022 - 2023
CERTIFICATE
This is to certify that Mr. / Miss _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
of III Semester Diploma in Computer Science and Engineering has conducted the
practical in Database System Concepts and PL/SQL (20CS34P) satisfactorily
during the year 2022 - 2023.
Staff Member In charge with date Head of the Section with date
SAYYAN SHAIKH Mohan K S
REGISTER NUMBER
1 6 2 C S 2 2 0
2. _______________________
PRINCIPAL
Date: / /2023 GOVT. POLYTECHNIC, JOIDA
Place: JOIDA
TABLE OF CONTENTS
Chapter Page
Concepts Date Signature
No. No
2.2 ER Diagram 10
3.2 ER Diagram 18
4.2 ER Diagram 24
5.2 ER Diagram 31
5.2 ER Diagram 38
CHAPTER – 1
BASIC CONCEPTS
1.1 Introduction
SQL commands are instructions used to communicate with the database to perform specific
task that work with data. SQL commands can be used not only for searching the database but
also to perform various other functions like, for example, you can create tables, add data to
tables, or modify data, drop the table, set permissions for users. SQL commands are grouped
into four major categories depending on their functionality:
Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are
CREATE, ALTER, DROP and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving, modifying and deleting data. These commands are
SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for managing
changes affecting the data. These commands are COMMIT, ROLLBACK, and
SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing
security to database objects. These commands are GRANT and REVOKE.
column_nameN datatype);
table_name - is the name of the table.
column_name1, column_name2.... - is the name of the columns
datatype - is the datatype for the column like char, date, number etc.
MYSQL Data Types:
Character Datatypes:
MYSQL supports character data types for storing text values. MYSQL offers these character
data types: CHAR(n), VARCHAR(n).
Name Description
Numeric Datatypes:
MYSQL supports two distinct types of numbers:
Integers
Floating-point numbers
Store
Name size Range
smallint 2 bytes -32768 to +32767
integer 4 bytes -2147483648 to +2147483647
bigint 8 bytes -9223372036854775808 to 9223372036854775807
Real 4 bytes
6 decimal digits precision.
If you declare it as the number, you can include number up to 131072 digits
numeric variable
before the decimal point to 16383 digits after the decimal point
decimal . variable If you declared it as decimal datatype ranges from 131072 digits before the
decimal point to 16383 digits after the decimal point
double 8 bytes 15 decimal digits precision
Date Data Types:
The DATE data types is used to store date information.
referenced_table_name(column_name);
4) Unique Key:
This constraint ensures that a column or a group of columns in each row have adistinct value.
A column(s) can have a null value but the values cannot be duplicated.
Syntax to define a Unique key at column level:
[CONSTRAINT constraint_name] UNIQUE
5) Check Constraint:
This constraint defines a business rule on a column. All the rows must satisfy this rule. The
constraint can be applied for a single column or a group of columns.
Syntax to define a Check constraint:
[CONSTRAINT constraint_name] CHECK (condition)
For Example: To add a column "experience" to the employee table, the query would be like
For Example: To drop the column "location" from the employee table, the query would be like
For Example: To modify the column salary in the employee table, the query would be like
ALTER TABLE employee MODIFY name varchar(20);
column_name;
The second form specifies both the column names and the values to be inserted:
Syntax:
UPDATE table_name
SET column1=value, column2=value2,... WHERE some_column=some_value;
Syntax:
Commit command
Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
Syntax:: savepoint savepoint_name;
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
CHAPTER – 2
LIBRARY DATABASE
SCHEMA:
Table Creation:
PUBLISHER
Table created.
BOOK
Table created.
BOOK_AUTHORS
Table created.
LIBRARY_BRANCH
Table created.
BOOK_COPIES
Table created.
BOOK_LENDING
Table created.
PUBLISHER
BOOK
BOOK_AUTHORS
SQL> insert into book_authors values(1111,’Sommerville’);
SQL> insert into book_authors values(2222,’Navathe’);
SQL> insert into book_authors values(3333,’Henry gray’);
SQL> insert into book_authors values(4444,’Thomas’);
LIBRARY_BRANCH
SQL> insert into library_branch values(11,’central technical’,’mg
road’);
SQL> insert into library_branch values(22,’medical’,’bh road’);
SQL> insert into library_branch values(33,’children’,’ss puram’);
SQL> insert into library_branch values(44,’secretariat’,’siragate’);
SQL> insert into library_branch values(55,’general’,’jayanagar’);
BOOK_COPIES
BOOK_LENDING
4 rows selected.
3 rows selected.
1111 11 5
3333 22 6
4444 33 10
2222 11 12
4444 55 3
5 rows selected.
7 rows selected.
5 rows selected.
QUERIES:
1) Retrieve details of all books in the library-id, title, name of publisher, authors, number of copies
in each branch e.t.c.
select branch_name, b.book_id, title, publisher_name, author_name,
no_of_copies
from book b, book_authors ba, book_copies bc, library_branch lb
where b.book_id=ba.book_id and
ba.book_id=bc.book_id and
bc.branch_id=lb.branch_id;
2) Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun 2017.
select card_no
from book_lending
where date_out between '2017-01-01' and '2017-06-30'
group by card_no
having count(*)>3;
card_no
1
3) Delete a book from BOOK table. Update the contents of other tables to reflect this data
manipulation operation.
1 row deleted.
SQL> select * from book;
4) Partition the BOOK table based on year of publication. Demonstrate its working with a
simple query.
5) Create a view of all books and its number of copies that are currently available in the library.
view created.
CHAPTER – 3
ORDER DATABASE
Consider the following schema for Order Database:
SALESMAN (Salesman_id, Name, City, Commission)
CUSTOMER (Customer_id, Cust_Name, City, Grade, Salesman_id)
ORDERS (Ord_No, Purchase_Amt, Ord_Date, Customer_id, Salesman_id)
Write SQL queries to
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesmen who had more than one customer.
3. List all salesmen and indicate those who have and don’t have customers in their
cities (Use UNION operation.)
4. Create a view that finds the salesman who has the customer with the highest
order of a day.
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his
orders must also be deleted.
ER-Diagram:
SCHEMA:
Table Creation:
SALESMAN
Table created.
CUSTOMER
ORDERS
SALESMAN
CUSTOMER
SQL> insert into customer values (11,'infosys','bengaluru',5,1000);
SQL> insert into customer values (22,'tcs','bengaluru',4,2000);
SQL> insert into customer values (33,'wipro','mysore',7,1000);
SQL> insert into customer values (44,'tcs','mysore',6,2000);
SQL> insert into customer values (55,'oracle','tumkur',3,3000);
ORDERS
SQL> insert into orders values (1,200000,'2016-04-12',11,1000);
SQL> insert into orders values (2,300000,'2016-04-12',11,2000);
SQL> insert into orders values (3,400000,'2017-04-15',22,1000);
QUERIES:
Count(customer_id)
03
2. Find the name and the numbers of all salesmen who had more than one customer.
select name, count(customer_id)
from salesman s, customer c
where s.salesman_id=c.salesman_id
group by name
having count(customer_id)>1;
Name Count(customer_id)
Raj 2
ashwin 2
3. List all salesmen and indicate those who have and don’t have customers in their cities (USE
UNION operation).
(select name
from salesman s, customer c
where s.salesman_id=c.salesman_id and s.city=c.city)
union
(select name
from salesman
where salesman_id not in(select s1.salesman_id
from salesman s1,customer c1
where s1.salesman_id=c1.salesman_id and s1.city=c1.city));
Name
Raj
Ashwin
Bindu
Lavanya
rohit
4. Create a view that finds the salesman who has the customer with the highest order of a day.
create view sales_highorder as
select salesman_id, purchase_amt
from orders
where purchase_amt=(select max(o.purchase_amt)
from orders o
where o.ord_date='2016-04-12');
view created.
Salesman_id Purchase_amt
2000 300000
5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must
also be deleted .
1 row deleted.
CHAPTER – 4
MOVIE DATABASE
Consider the following schema for Movie Database:
ACTOR (Act_id, Act_Name, Act_gender)
DIRECTOR (Dir_id, Dir_name, Dir_phone)
MOVIES (Mov_id, Mov_title, Mov_year, Mov_lang, Dir_id)
MOVIE_CAST (Act_id, Mov_id, Role)
RATING (Mov_id, Rev_stars)
Write SQL queries to
1. List the titles of all movies directed by ‘Hitchcock’.
2. Find the movie names where one or more actors acted in two or more movies.
3. List all actors who acted in a movie before 2000 and also in a movie after 2015
(use JOIN operation).
4. Find the title of movies and number of stars for each movie that has at least one
rating and find the highest number of stars that movie received. Sort the result by
movie title.
5. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
ER-Diagram:
SCHEMA:
Table Creation:
ACTOR
create table actor(
act_id int primary key,
act_name varchar(20) not null,
act_gender varchar(2) not null);
Table created.
DIRECTOR
create table director(
dir_id int primary key,
dir_name varchar(20) not null,
dir_phone int not null);
Table created.
MOVIES
create table movies(
mov_id int primary key,
mov_title varchar(20) not null,
mov_year int not null,
mov_lang varchar(10) not null,
dir_id int,
foreign key(dir_id) references director(dir_id));
Table created
MOVIE_CAST
create table movie_cast(
act_id int,
foreign key(act_id) references actor(act_id),
mov_id int,
foreign key(mov_id) references movies(mov_id),
role varchar(20));
Table created.
RATING
Table created.
ACTOR
DIRECTOR
MOVIES
MOVIE_CAST
RATING
QUERIES
1. List the titles of all movies directed by ‘Hitchcock’.
select mov_title
from movies m,director d
where d.dir_id=m.dir_id and dir_name='Hitchcock';
Mov_title
Rear window
notorious
2. Find the movie names where one or more actors acted in two or more movies.
select mov_title
from movies m,movie_cast mc
where m.mov_id=mc.mov_id and
mc.act_id in ( select act_id
from movie_cast
group by act_id
having count(mov_id)>=2);
Mov_title
Male
Manasare
3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
(select act_name
from actor a join movie_cast c
on a.act_id=c.act_id
join movies m
on c.mov_id=m.mov_id
where m.mov_year < 2000)
union
(select act_name
from actor a join movie_cast c
on a.act_id=c.act_id
join movies m
on c.mov_id=m.mov_id
where m.mov_year > 2015);
Act_name
angela
4. Find the title of movies and number of stars for each movie that has at least one rating and find
the highest number of stars that movie received. Sort the result by movie title.
select mov_title,rev_stars
from movies m,rating r
where m.mov_id=r.mov_id and rev_stars>=1
order by mov_title;
Mov_title Rev_stars
Eega 4
Lastworld 3
Male 3
Manasare 4
paramathma 5
update rating
set rev_stars=5
where mov_id in ( select mov_id
from movies m, director d
where m.dir_id=d.dir_id and
dir_name='Steven Spielberg');
Mov_id Rev_stars
1111 5
2222 4
3333 3
5555 4
4444 5
CHAPTER – 5
COLLEGE DATABASE
Consider the following schema for college Database:
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, Subcode, SSID, Test1, Test2, Test3, FinalIA)
Write SQL queries to
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and in `each
section.
3. Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.
4. Calculate the FinalIA (average of best two test marks) and update the
corresponding table for all students.
5. Categorize students based on the following criterion:
If FinalIA = 17 to 20 then CAT = ‘Outstanding’
If FinalIA = 12 to 16 then CAT = ‘Average’
If FinalIA< 12 then CAT = ‘Weak’
Give these details only for 8th semester A, B, and C section students.
ER-DIAGRAM:
SCHEMA:
Table Creation:
STUDENT
create table student(
USN varchar(15) primary key,
sname varchar(20),
address varchar(20),
phone int,
gender char(1));
Table created.
SEMSEC
create table semsec(
SSID varchar(20) primary key,
sem int,
sec char(1));
Table created.
CLASS
create table class(
USN varchar(20),
SSID varchar(6),
foreign key(USN) references student(USN),
foreign key(SSID) references semsec(SSID));
Table created
SUBJECT
create table subject(
Subcode varchar(10) primary key,
title varchar(20),
sem int,
credits int));
Table created
IAMARKS
create table iamarks(
USN varchar(10),
Subcode varchar(10),
SSID varchar(6),
test1 int,
test2 int,
test3 int,
FinalIA int,
foreign key(USN) references student(USN),
foreign key(Subcode) references subject(Subcode),
foreign key(SSID) references semsec(SSID));
Table created.
SEMSEC
insert into semsec values('5A',5,’A’);
insert into semsec values('3B',3,’B’);
insert into semsec values('7A',7,’A’);
insert into semsec values('2C',2,’C’);
insert into semsec values('4B',4,’B’);
insert into semsec values('4C',4,’C’);
CLASS
insert into class values('1cg15cs001','5A');
insert into class values('1cg15cs002','5A');
insert into class values('1cg16me063','3B');
insert into class values('1cg14ec055','7A');
insert into class values('1cg15ee065','3B');
insert into class values('1cg15ee065','4C');
insert into class values('1cg15cs002','4C');
SUBJECT
insert into subject values('15cs53','dbms',5,4);
insert into subject values('15cs33','ds',3,4);
insert into subject values('15cs34','co',3,4);
insert into subject values('15csl58','dba',5,2);
insert into subject values('10cs71','oomd',7,4);
IAMARKS
insert into iamarks values('1cg15cs001','15cs53',’5A’,18,19,15,19);
insert into iamarks values('1cg15cs002','15cs53',’5A’,15,16,14,16);
insert into iamarks values('1cg16me063','15cs33',’3B’,10,15,16,16);
insert into iamarks values('1cg14ec055','10cs71',’7A’,18,20,21,21);
insert into iamarks values('1cg15ee065','15cs33',’3B’,16,20,17,19);
insert into iamarks values('1cg15ee065','15cs53',’4C’,19,20,18,20);
QUERIES
1. List all the student details studying in fourth semester ‘C’ section.
2. Compute the total number of male and female students in each semester and in each section.
Subcode Test1
15cs33 16
15cs53 19
4. Calculate the FinalIA (average of best two test marks) and update the corresponding table for all
students.
update iamarks
set FinalIA=greatest(test1+test2,test2+test3,test1+test3)/2;
Query OK.
MySQL> select * from iamarks;
select s.*,
case when ia.finalia between 17 and 20 then 'OUTSTANDING'
when ia.finalia between 12 and 16 then 'AVERAGE'
ELSE 'WEAK'
end as CAT
from student s, semsec ss, iamarks ia, subject sub
where s.usn=ia.usn and
ss.ssid=ia.ssid and
sub.subcode=ia.subcode and
sub.sem=5;
CHAPTER – 6
COMPANY DATABASE
Consider the following schema for company Database:
EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo)
DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
DLOCATION (DNo, DLoc)
PROJECT (PNo, PName, PLocation, DNo)
WORKS_ON (SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose last
name is ‘Scott’, either as a worker or as a manager of the department that controls the
project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given
a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well
as the maximum salary, the minimum salary, and the average salary in this department
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator).
5. For each department that has more than five employees, retrieve the department
number and the number of its employees who are making more than Rs. 6,00,000.
ER-DIAGRAM:
SCHEMA:
Table Creation
DEPARTMENT
create table department(
DNo int primary key,
DName varchar(15) not null,
MgrSSN int,
MgrStartDate date);
Table created.
EMPLOYEE
create table employee(
SSN int primary key,
name varchar(20) not null,
address varchar(20),
sex varchar(3),
salary real,
Super_SSN int,
DNo int,
foreign key(DNo) references department(DNo));
Table created.
DLOCATION
create table dlocation(
DLoc varchar(20) primary key,
DNo int,
foreign key(DNo) references department(DNo));
Table created.
PROJECT
create table project(
PNo int primary key,
PName varchar(20),
PLocation varchar(20),
DNo int,
foreign key(DNo) references department(DNo));
Table created.
WORKS_ON
create table works_on(
hours int,
ssn int,
foreign key(ssn) references employee(ssn),
PNo int,
foreign key(PNo) references project (PNo));
Table created.
EMPLOYEE
insert into employee values (111111, 'Raj', 'Bangalore', 'M', 700000, 111111, 1);
insert into employee values (222222, 'Rashmi', 'Mysore', 'F', 400000, 111111, 2);
insert into employee values (333333, 'Ragavi',' Tumkur', 'F', 800000, 222222, 3);
insert into employee values (444444, 'Rajesh', 'Tumkur', 'M', 650000, 444444, 3);
insert into employee values (555555, 'Raveesh', 'Bangalore', 'M', 500000, 555555, 3);
insert into employee values (666666, 'Scott', 'England', 'M', 700000, 666666, 5);
insert into employee values (777777, 'Niganth', 'Gubbi', 'M', 200000, 777777, 2);
insert into employee values (888888, 'Ramya', 'Gubbi', 'F', 400000, 888888, 3);
insert into employee values (999999, 'Vidya', 'Tumkur', 'F', 650000, 999999, 3);
insert into employee values (100000, 'Geetha', 'Tumkur', 'F', 800000, 100000, 3);
DLOACTION
insert into dlocation values ('Mysore',1);
insert into dlocation values ('Tumkur',1);
insert into dlocation values ('Bangalore',2);
insert into dlocation values ('Gubbi',3);
insert into dlocation values ('Delhi',4);
insert into dlocation values ('Karwar',5);
select * from dlocation;
DLoc DNo
Mysore 1
Tumkur 1
Bangalore 2
Gubbi 3
Delhi 4
Karwar 5
PROJECT
insert into project values(111,'IOT','Gubbi',3);
insert into project values(222,'TextSpeech','Gubbi',3);
insert into project values(333,'IPSecurity','Delhi',4);
insert into project values(444,'Trafficanal','Bangalore',5);
insert into project values(555,'CloudSec','Delhi',1);
insert into project values(666,'TextSpeech','Gubbi',2);
select * from project;
+-----+-------------+-----------+------+
| PNo | PName | PLocation | DNo |
+-----+-------------+-----------+------+
| 111 | IOT | Gubbi | 3 |
| 222 | TextSpeech | Gubbi | 3 |
| 333 | IPSecurity | Delhi | 4 |
| 444 | Trafficanal | Bangalore | 5 |
| 555 | CloudSec | Delhi | 1 |
| 666 | TextSpeech | Gubbi | 2 |
+-----+-------------+-----------+------+
WORKS_ON
4 666666 333
2 666666 111
3 111111 222
2 555555 222
4 333333 111
6 444444 111
2 222222 111
QUERIES
1. Make a list of all project numbers for projects that involve an employee whose name is ‘Scott’,
either as a worker or as a manager of the department that controls the project.
(select distinct PNo
from project p, department d, employee e
where p.dno=d.dno and e.ssn=d.mgrssn and name='Scott')
union
( select distinct PNo
from works_on w, employee e
where w.ssn=e.ssn and name='Scott');
PNo
444
333
111
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent
raise.
select name, 1.1*salary as incr_sal
from employee e, works_on w, project p
where e.ssn=w.ssn and w.pno=p.pno and p.pname='IOT';
name Incr_sal
Scott 770000.0000000001
Ragavi 880000.0000000001
Rajesh 715000
Rashmi 440000.00000000006
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as well as the
maximum salary, the minimum salary, and the average salary in this department.
4. Retrieve the name of each employee who works on all the projects controlled by department
number 5 (use NOT EXISTS operator).
select name
from employee e
where not exists (select DNo
from project p
where e.dno=p.dno and DNo<>5);
Name
Scott
5. For each department that has more than five employees, retrieve the department number and the
number of its employees who are making more than Rs. 6,00,000.
DNo Count(ssn)
3 4