You are on page 1of 34

Data Base Management System Laboratory with Mini Project 21CSL55

EXPERIMENT 1:

1. Consider the following schema for a Library Database:


BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone)
BOOK_COPIES (Book_id, Programme_id, No-of_Copies)
BOOK_LENDING (Book_id, Programme_id, Card_No, Date_Out, Due_Date)
LIBRARY_PROGRAMME (Programme_id, Programme_Name, Address)
Write SQL queries to
1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of copies in each
programme, etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to Jun 2017.
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation operation.
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.

RELATIONAL SCHEMA DIAGRAM:

ER DIAGRAM :

Dept of Computer science & Engg,RLJIT Page 1


Data Base Management System Laboratory with Mini Project 21CSL55

CREATING TABLES:

SQL> create table publisher(name varchar2(10) primary key, address varchar2(20) ,phone number(10));

SQL> create table book(book_id varchar2(5) primary key,title varchar2(15) not null, publisher_name
varchar2(10) references publisher(name), pub_year number(5))
partition by range(pub_year)
(partition p1 values less than (2014), partition p2 values less than (2015), partition p3 values less
than (2016),partition p4 values less than (2017), partition p5 values less than (MAXVALUE));

SQL> create table book_authors(book_id varchar2(5) references book(book_id) on delete cascade,


author_name varchar2(20),primary key(book_id));

Dept of Computer science & Engg,RLJIT Page 2


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> create table library_programme (programme_id varchar2(5) primary key, programme _name
varchar2(15) not null, address varchar2(20));
SQL> create table book_copies(book_id varchar2(5) references book(book_id) on delete cascade,
programme _id varchar2(5) references library_branch(programme _id), no_of_copies number(4) ,
primary key(book_id,branch_id));
SQL>create table book_lending(book_id varchar2(5) references book(book_id) on delete cascade,
programme _id varchar2(5) references library_branch(programme _id),card_no number(4), date_out
date, due_date date,primary key(book_id,branch_id,card_no));

INSERTING VALUES:

SQL> insert into publisher values('&name','&address',&phone);


SQL>insert into book values ('&book_id','&title','&publisher_name',&pub_year);
SQL>insert into book_authors values('&book_id','&author_name');
SQL>insert into library_programme values('&programme_id','&programme _name',
'&address' );
SQL>insert into book_copies values('&book_id','& programme _id',&no_of_copies);
SQL> insert into book_lending values('&book_id', '& programme _id', '&cardno',
'&date_out', '&due_date');

DISPLAYING TABLES:

SQL>select *from publisher;

NAME ADDRESS PHONE


php blore 9215673151
phi mysore 9215673152
harper blore 9215673153
wiley chennai 9215673154
sapna pune 9215673155
SQL> select *from book; SQL> select *from book_authors;

BOOK_ ID TITLE PUBLISHER_ NAME PUB_YEAR BOOK_ID AUTHOR_NAME


b1 ada php 2014 b1 padmareddy
b2 c++ phi 2015 b2 balaguru
b3 dbms harper 2016 b3 navathe
b4 java wiley 2017 b4 herbashild
b5 c sapna 2017 b5 ritchie

Dept of Computer science & Engg,RLJIT Page 3


Data Base Management System Laboratory with Mini Project 21CSL55

select *from library_programme; SQL> select *from book_copies;

PROGRAMME PROGRAMME ADDRESS BOOK_ID PROGRAMM NO_OF_COPIES


_ID _NAME E _ID
pr11 jayanagar blore b1 pr11 10
pr12 btmlayout blore b1 pr12 20
pr13 hebbal blore b2 pr12 50
pr14 rtnagar blore b2 pr13 40
pr15 rajajinagar blore b2 pr14 10
b3 pr14 15
b3 pr15 10
b4 pr11 40
b4 pr12 10
b4 pr15 15
b5 pr14 15
b5 pr13 12
b5 pr12 10

SQL> select *from book_lending;

BOOK_ID PROGRAMME _ID CARD_NO DATE_OUT DUE_DATE


b1 pr11 c1 01-JAN-17 30-JAN-17
b2 pr13 c2 01-FEB-17 28-FEB-17
b1 pr12 c3 05-MAR-17 28-MAR-17
b2 pr12 c3 06-APR-17 30-APR-17
b4 pr12 c3 10-MAY-17 30-MAY-17
b5 pr12 c3 10-FEB-17 02-MAR-17

Queries:

1. Retrieve details of all books in the library – id, title, name of publisher, authors, number of
copies in each programme, etc.

SQL> SELECT b.book_id,b.title,b.publisher_name,ba.author_name,l.programme_id,


l.programme _name, bc.no_of_copies
FROM book b,book_authors ba,book_copies bc,library_ programme l
WHERE b.book_id=ba.book_id and b.book_id=bc.book_id and

Dept of Computer science & Engg,RLJIT Page 4


Data Base Management System Laboratory with Mini Project 21CSL55

bc. programme _id=l. programme _id;


BOOK_ID BOOK_ PUBLISHER_NAME AUTHOR_NAM BRANCH_ID BRANCH_NAME NO_OF_COPIES
TITLE E
b4 java wiley herbashild pr11 jayanagar 40
b1 ada php padmareddy pr11 jayanagar 10
b5 c sapna ritchie pr12 btmlayout 10
b4 java wiley herbashild pr12 btmlayout 10
b2 c++ phi balaguru pr12 btmlayout 50
b1 ada php padmareddy pr12 btmlayout 20
b5 c sapna ritchie pr13 hebbal 12
b2 c++ phi balaguru pr13 hebbal 40
b5 c sapna ritchie pr14 rtnagar 15
b3 dbms harper navathe pr14 rtnagar 15
b2 c++ phi balaguru pr14 rtnagar 10
b4 java wiley herbashild pr15 rajajinagar 15
b3 dbms harper navathe pr15 rajajinagar 10

2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan 2017 to
Jun 2017.

SQL> SELECT card_no,count(book_id)


FROM book_lending
WHERE date_out between '01-jan-2017' and '30-jun-2017'
GROUP BY card_no
HAVING count(book_id)>3;

CARD_NO COUNT(BOOK_ID)
c3 4

5. Create a view of all books and its number of copies that are currently available in the Library.

SQL> CREATE VIEW branch_book_count(book_id,title, programme _id, programme _name,


no_of_copies)
as select b.book_id,b.title,l. programme _id,l. programme _name,bc.no_of_copies
FROM book b,book_copies bc,library_ programme l
WHERE b.book_id=bc.book_id and bc. programme _id=l. programme _id;

View created.

Dept of Computer science & Engg,RLJIT Page 5


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> select *from branch_book_count;

BOOK_ID TITLE PROGRAMME_ID PROGRAMME_NAME NO_OF_COPIES


b4 java pr11 jayanagar 40
b1 ada pr11 jayanagar 10
b5 c pr12 btmlayout 10
b4 java pr12 btmlayout 10
b2 c++ pr12 btmlayout 50
b1 ada pr12 btmlayout 20
b5 c pr13 hebbal 12
b2 c++ pr13 hebbal 40
b5 c pr14 rtnagar 15
b3 dbms pr14 rtnagar 15
b2 c++ pr14 rtnagar 10
b4 java pr15 rajajinagar 15
b3 dbms pr15 rajajinagar 10

4. Partition the BOOK table based on year of publication. Demonstrate its working with a simple
query.

Book table partioned into 5 partions as p1,p2,p3,p4,p5 while creating the table.

SQL> select *from book1 partition (p4);

BOOK_ID TITLE PUBLISHER_NAME PUB_YEAR


b3 dbms harper 2016

3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation.

SQL> delete from book where book_id='b1';

1 row deleted.

SQL> select *from book;

BOOK_ ID TITLE PUBLISHER_ NAME PUB_YEAR


b2 c++ phi 2015
b3 dbms harper 2016
b4 java wiley 2017
b5 c sapna 2017

Dept of Computer science & Engg,RLJIT Page 6


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> select *from book_authors;

BOOK_ID AUTHOR_NAME
b2 balaguru
b3 navathe
b4 herbashild
b5 ritchie

SQL> select *from book_copies;

BOOK_ID PROGRAMME_ID NO_OF_COPIES


b2 pr12 50
b2 pr13 40
b2 pr14 10
b3 pr14 15
b3 pr15 10
b4 pr11 40
b4 pr12 10
b4 pr15 15
b5 pr14 15
b5 pr13 12
b5 pr12 10

Dept of Computer science & Engg,RLJIT Page 7


Data Base Management System Laboratory with Mini Project 21CSL55

EXPERIMENT 2:

2. 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 salesman who had more than one customer.
3. List all the salesman 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.

RELATIONAL SCHEMA DIAGRAM:

Dept of Computer science & Engg,RLJIT Page 8


Data Base Management System Laboratory with Mini Project 21CSL55

ER DIAGRAM :

CREATING TABLES:

SQL> create table SALESMAN( Salesman_id int primary key,Name varchar2(20) not null, city
varchar2(10),commission float);

SQL> create table CUSTOMER(Customer_id int primary key, Cust_Name varchar(10) not null,
City varchar(10),Grade float, Salesman_id int references SALESMAN (Salesman_id) on delete set
null);

SQL> create table ORDERS( Ord_No int primary key , Purchase_Amt float, Ord_Date date,
Customer_id int references CUSTOMER(Customer_id) ,Salesman_id int references
SALESMAN(Salesman_id) on delete cascade);

INSERTING VALUES:
SQL> insert into SALESMAN values(&Salesman_id,'&Name','&city',&commission);
SQL> insert into CUSTOMER values(&Customer_id,'&Cust_Name','&City',&Grade,&Salesman_id);
SQL>insert into ORDERS values(&Ord_No,’& Purchase_Amt’,’& Ord_Date’,’& Customer_id’,’&
Salesman_id’);

Dept of Computer science & Engg,RLJIT Page 9


Data Base Management System Laboratory with Mini Project 21CSL55

DISPLAYING TABLES:

SQL> select *from salesman;


SALESMAN_ID NAME CITY COMMISSION
1000 adarsh bangalore 2
1001 bharath mysore 8
1002 chandru bangalore 3
1003 dinesh pune 1
1004 eshwar chennai 9
1005 kiran hyd 4
1006 raj mumbai 5

SQL> select *from customer;

CUSTOMER_ID CUST_NAME CITY GRADE SALESMAN_ID


10 farhan bangalore 2 1000
11 ganesh bangalore 4 1000
12 harsha mysore 8 1001
13 Indra mysore 2 1001
14 jagadish pune 10 1003
15 karthik chennai 9 1004
16 ram mysore 4 1002
17 mahesh pune 8 1002
18 akshay chennai 8 1002

SQL> select *from orders;

ORD_NO PURCHASE_AMT ORD_DATE CUSTOMER_ID SALESMAN_ID


1 10000 12-MAR-10 10 1000
2 20000 12-MAR-10 11 1000
3 40000 12-MAR-10 12 1001
4 15000 10-JAN-10 13 1001
5 5000 10-JAN-10 14 1003

Queries:
1. Count the customers with grades above Bangalore’s average.

SELECT grade, COUNT (DISTINCT customer_id)


FROM CUSTOMER
GROUP BY grade
HAVING grade >(SELECT AVG(grade)

Dept of Computer science & Engg,RLJIT Page 10


Data Base Management System Laboratory with Mini Project 21CSL55

FROM CUSTOMER
WHERE city = 'bangalore');

GRADE COUNT(DISTINCTCUSTOMER_ID)
4 2
8 3
9 1
10 1

2. Find the name and numbers of all salesman who had more than one customer.

SELECT salesman_id,name
FROM SALESMAN a
WHERE 1 < (SELECT COUNT(*)
FROM CUSTOMER
WHERE salesman_id=a.salesman_id);
OR
SELECT salesman_id,name
FROM SALESMAN WHERE salesman_id in ( SELECT salesman_id
FROM CUSTOMER
GROUP BY salesman_id
HAVING COUNT(customer_id)>1)
SALESMAN_ID NAME
1000 adarsh
1001 bharath
1002 chandru

3. List all the salesman and indicate those who have and don’t have customers in their cities (Use
UNION operation.)

SQL>SELECT salesman.salesman_id, name, cust_name, commission


FROM salesman, customer
WHERE salesman.city = customer.city
UNION
SELECT salesman_id, name, 'NO MATCH', commission
FROM salesman
WHERE NOT city = ANY
(SELECT city
FROM customer)
ORDER BY 2 DESC;

Dept of Computer science & Engg,RLJIT Page 11


Data Base Management System Laboratory with Mini Project 21CSL55

SALESMAN_ID NAME CUST_NAME COMMISSION


1006 raj NO MATCH 5
1005 kiran NO MATCH 4
1004 eshwar akshay 9
1004 eshwar karthik 9
1003 dinesh jagadish 1
1003 dinesh mahesh 1
1002 chandru farhan 3
1002 chandru ganesh 3
1001 bharath harsha 8
1001 bharath indra 8
1001 bharath ram 8
1000 adarsh farhan 2
1000 adarsh ganesh 2

4.Create a view that finds the salesman who has the customer with the highest order of a day.
SQL>CREATE VIEW elitsalesman
AS SELECT s.salesman_id,s.name ,PURCHASE_AMT,ord_date from ORDERS o, SALESMAN s
WHERE s.salesman_id=o.salesman_id
and PURCHASE_AMT in(SELECT MAX (Purchase_Amt)
FROM orders group by ord_date);

SQL> select *from elitsalesman1;

SALESMAN_ID NAME PURCHASE_AMT ORD_DATE


1001 bharath 40000 12-MAR-10
1001 bharath 15000 10-JAN-10

5. Demonstrate the DELETE operation by removing salesman with id 1000. All his orders must also
be deleted.
SQL> DELETE from SALESMAN WHERE salesman_id=1000;
1 row deleted.
NOTE: The following changes will happen to all the three tables when the above query is executed.
 1 row deleted from salesman table
 2 rows deleted from orders where salesman_id is 1000
 In customer table salesman_id value will be set to null where salesman_id is 1000.

AFTER DELETING

Dept of Computer science & Engg,RLJIT Page 12


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> select *from SALESMAN;

SALESMAN_ID NAME CITY COMMISSION


1001 bharath mysore 8
1002 chandru bangalore 3
1003 dinesh pune 1
1004 eshwar chennai 9
1005 kiran hyd 4
1006 raj mumbai 5

SQL> select *from orders;

ORD_NO PURCHASE_AMT ORD_DATE CUSTOMER_ID SALESMAN_ID


3 40000 12-MAR-10 12 1001
4 15000 10-JAN-10 13 1001
5 5000 10-JAN-10 14 1003

SQL> select *from customer;

CUSTOMER_ID CUST_NAME CITY GRADE SALESMAN_ID


10 farhan bangalore 2
11 ganesh bangalore 4
12 harsha mysore 8 1001
13 Indra mysore 2 1001
14 jagadish pune 10 1003
15 karthik chennai 9 1004
16 ram mysore 4 1002
17 mahesh pune 8 1002
18 akshay chennai 8 1002

Dept of Computer science & Engg,RLJIT Page 13


Data Base Management System Laboratory with Mini Project 21CSL55

3. Consider the 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.

RELATIONAL SCHEMA DIAGRAM:

Dept of Computer science & Engg,RLJIT Page 14


Data Base Management System Laboratory with Mini Project 21CSL55

ER DIAGRAM :

CREATING TABLES:

SQL> create table actor( act_id varchar2(4) primary key, act_name varchar2(20) not null,
act_gender varchar(6));
SQL> create table director(dir_id varchar2(4) primary key,dir_name varchar2(20) not null,
dir_phone number(11));
SQL> create table movies(mov_id varchar2(4) primary key,mov_title varchar(20) not null,
mov_year int,mov_lang varchar2(10), dir_id varchar2(4) references director(dir_id));
SQL> create table movie_cast(act_id varchar2(4) references actor(act_id),mov_id varchar2(4)
references movies(mov_id),role varchar2(10),primary key(act_id,mov_id));
SQL>create table rating(mov_id varchar2(4) references movies(mov_id), rev_stars number(5,2) ,
primary key(mov_id));

INSERTING VALUES:
SQL>insert into actor values('&act_id','&act_name','&act_gender');
SQL>insert into director values('&dir_id','&dir_name',&dir_phone);
SQL>insert into movies values('&mov_id','&mov_title',&mov_year,'&mov_lang','&dir_id');
SQL>insert into movie_cast values('&act_id','&mov_id','&role');
SQL>insert into rating values('&mov_id',&rev_stars);

Dept of Computer science & Engg,RLJIT Page 15


Data Base Management System Laboratory with Mini Project 21CSL55

DISPLAYING TABLES:

SQL> select *from actor; SQL> select *from director;

ACT_ ID ACT_NAME ACT_GENDER


DIR_ID DIR_NAME DIR_PHONE
a1 sharuk male
d1 hitchcock 9845612345
a2 amir male
d2 steven spielberg 9845612346
a3 salman male
d3 karan 9845612347
a4 madhuri female
d4 ramgopal 9845612348
a5 priyanka female
d5 anurag 9845612349
a6 nasir male

SQL> select *from movies;


MOV_ID MOV_TITLE MOV_YEAR MOV_LANG DIR_id
m1 ddlj 1999 hindi d1
m2 bazigar 2002 hindi d2
m3 darr 2016 hindi d1
m4 dil 1990 hindi d2
m5 pk 2016 hindi d3
m6 sulthan 2016 hindi d1
m7 devdas 2002 hindi d4
m8 marykom 2016 hindi d3

SQL> select *from movie_cast; SQL> select *from rating;

ACT_ID MOV_ID ROLE MOV_ID REV_STARS


a1 m1 hero m1 .1
a1 m2 hero m2 2
a1 m3 hero m3 4
a2 m4 hero m5 6
a2 m5 costar m6 .5
a3 m6 hero m7 1
a4 m7 heroine m8 .4
a5 m1 costar m4 .2
a6 m1 support
a4 m2 heroine

Dept of Computer science & Engg,RLJIT Page 16


Data Base Management System Laboratory with Mini Project 21CSL55

Queries:
1. List the titles of all movies directed by ‘Hitchcock’.

SQL>SELECT m.mov_id,m.mov_title,m.dir_id,d.dir_name
FROM movies m,director d
WHERE m.dir_id=d.dir_id and d.dir_name='hitchcock';

MOV_ID MOV_TITLE DIR_ID DIR_NAME


m6 ddlj d1 hitchcock
m3 darr d1 hitchcock
m1 sulthan d1 hitchcock

2.Find the movie names where one or more actors acted in two or more movies.

SQL>SELECT mov_title, act_name, role


FROM movies
JOIN movie_cast
ON movie_cast.mov_id=movies.mov_id
JOIN actor
ON movie_cast.act_id=actor.act_id
WHERE actor.act_id IN (
SELECT act_id
FROM movie_cast
GROUP BY act_id HAVING COUNT(*)>=2);

MOV_TITLE ACT_NAME ROLE


ddlj sharuk hero
bazigar sharuk hero
darr sharuk hero
dil amir hero
pk amir costar
devdas madhuri heroine
bazigar madhuri heroine

Dept of Computer science & Engg,RLJIT Page 17


Data Base Management System Laboratory with Mini Project 21CSL55

3. List all actors who acted in a movie before 2000 and also in a movie after 2015 (use JOIN
operation).
SQL>(SELECT a.act_id,a.act_name
FROM actor a
JOIN movie_cast
ON a.act_id=movie_cast.act_id
JOIN movies
ON movie_cast.mov_id=movies.mov_id
WHERE mov_year< 2000 )
INTERSECT
(SELECT a.act_id,a.act_name
FROM actor a
JOIN movie_cast
ON a.act_id=movie_cast.act_id
JOIN movies
ON movie_cast.mov_id=movies.mov_id
WHERE mov_year> 2015 );

ACT_ID ACT_NAME
a1 sharuk
a2 amir

4. Find the title of movies and number of stars for each movie that has at least on rating and find the
highest number of stars that movie received. Sort the result by movie title.

SQL> SELECT m.mov_id,m.mov_title, MAX(rev_stars) FROM Rating


JOIN Movies m on m.mov_id=Rating.mov_id
and rating.rev_stars>=1
GROUP BY m.mov_id,m.mov_title
ORDER BY m.mov_title;

MOV_ID MOV_TITLE MAX(REV_STARS)


m2 bazigar 4
m3 darr 2
m7 devdas 1
m5 pk 6

Dept of Computer science & Engg,RLJIT Page 18


Data Base Management System Laboratory with Mini Project 21CSL55

5.Update rating of all movies directed by ‘Steven Spielberg’ to 5.

SQL>UPDATE rating set rev_stars=5


WHERE mov_id in( SELECT m.mov_id FROM movies m ,director d
WHERE d.dir_id=m.dir_id and d. dir_name='steven spielberg');

2 rows updated.

MOV_ID REV_STARS
m1 .1
m2 5 -- updated to 5 (steven spielberg directed 2 movies m2 and m4)
m3 4
m5 6
m6 .5
m7 1
m8 .4
m4 5 -- updated to 5 (steven spielberg directed 2 movies m2 and m4)

Dept of Computer science & Engg,RLJIT Page 19


Data Base Management System Laboratory with Mini Project 21CSL55

4. Consider the 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.

RELATIONAL SCHEMA DIAGRAM:

Dept of Computer science & Engg,RLJIT Page 20


Data Base Management System Laboratory with Mini Project 21CSL55

ER DIAGRAM :

CREATING TABLES:

SQL>create table student(usn varchar(11) primary key, Sname varchar(20) not null, address
varchar(30), phone number(10),gender varchar(6));
SQL>create table semsec(ssid varchar(5) primary key,sem int,sec varchar(5));
SQL> create table clas(usn varchar(11) references student(usn),ssid varchar(5) references
semsec(ssid), primary key(usn,ssid));
SQL> create table subject(subcode varchar(7) primary key,title varchar(10), sem int, credits float);
SQL> create table iamarks(usn varchar(11) references student(usn),subcode varchar(7) references
subject(subcode), ssid varchar(5) references semsec(ssid),test1 number(6,3),test2 number(6,3),
test3 number(6,3),finalIA number(6,3));
INSERTING VALUES:
SQL> insert into student values('&usn','&sname','&address',&phone,'&gender');
SQL> insert into semsec values('&ssid',&sem,'&sec');
SQL>insert into clas values('&usn','&ssid');
SQL> insert into subject values('&subcode','&title',&sem,&credits);

SQL> alter table iamarks drop column finalIa;

Table altered.

Dept of Computer science & Engg,RLJIT Page 21


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> desc iamarks;

Name Null? Type


----------------------------------------- -------- ---------------------------
USN VARCHAR2(11)
SUBCODE VARCHAR2(7)
SSID VARCHAR2(5)
TEST1 NUMBER(6,3)
TEST2 NUMBER(6,3)
TEST3 NUMBER(6,3)

SQL> insert into iamarks values('&usn','&subcode','&ssid',&test1,&test2,&test3);

SQL> alter table iamarks add finalia number(6,3);

DISPLAYING TABLES:

SQL> select *from student;

USN SNAME ADDRESS PHONE GENDER


1va12cs001 anwar blore 9845611111 male
1va12cs002 bharath mysore 9845611112 male
1va12cs003 chaithra hubli 9845611113 female
1va13cs004 daniel blore 9845611114 male
1bi15cs101 rajesh pune 9845611115 male
1bi15cs102 gayathri blore 9845611116 female

SQL> select *from semsec; SQL> select *from clas; SQL> select *from subject;

SSID SEM SEC USN SSID SUBCODE TITLE SEM CREDITS


s1 4 c 1bi15cs101 s5 10cs51 me 5 4
s2 8 a 1bi15cs102 s3 10cs52 dbms 5 4
s3 8 b 1va12cs001 s1 10cs81 aca 8 3
s4 8 c 1va12cs002 s2 10cs82 nm 8 3
s5 5 a 1va12cs003 s3 10cs83 dos 8 3
s6 5 b 1va13cs004 s4 10cs41 ada 4 4
s7 4 b 10cs42 c++ 4 4
s8 4 a
SQL> select *from iamarks;

USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA


1va12cs001 10cs41 s7 11 15 18
1va12cs001 10cs42 s6 13 19 12
1bi15cs101 10cs51 s5 19 20 12
1bi15cs101 10cs52 s5 12 14 16

Dept of Computer science & Engg,RLJIT Page 22


Data Base Management System Laboratory with Mini Project 21CSL55

1va12cs002 10cs81 s2 16 17 18
1va12cs002 10cs82 s2 12 16 15
1va12cs002 10cs83 s2 16 18 20
1va12cs003 10cs81 s3 14 12 11
1va12cs003 10cs82 s3 12 11 16
1va12cs003 10cs83 s3 18 12 10
1va13cs004 10cs82 s4 16 17 18

Queries:

1. List all the student details studying in fourth semester ‘C’ section.

SQL> SELECT s.usn,s.sname,s.address,s.phone,s.gender,u.sem,u.sec


FROM student s,clas c ,semsec u
WHERE s.usn=c.usn and u.ssid=c.ssid and u.sem=4 and u.sec='c';

SNAME USN ADDRESS PHONE GENDER SEM SEC


1va12cs001 anwar blore 984561111 male 4 c
1
2.Compute the total number of male and female students in each semester and in each section.

SQL> SELECT u.sem,u.sec,s.gender,count(*)


FROM student s,semsec u,clas c
WHERE s.usn=c.usn and c.ssid=u.ssid
GROUP BY u.sem,u.sec,s.gender;

SEM SEC GENDER COUNT(*)


4 c male 1
8 a male 1
8 c male 1
5 a male 1
8 b female 2

3.Create a view of Test1 marks of student USN ‘1BI15CS101’ in all subjects.

SQL> CREATE VIEW TEST1_MARKS_OF_1bi15cs101(usn,name,subcode,title,test1_marks)


as SELECT s.usn,s.sname,i.subcode,sub.title,i.test1
FROM student s,iamarks i,subject sub
WHERE s.usn=i.usn and i.subcode=sub.subcode and s.usn='1bi15cs101';

View created.

Dept of Computer science & Engg,RLJIT Page 23


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> select *from TEST1_MARKS_OF_1bi15cs101;

USN NAME SUBCODE TITLE TEST1_MARKS


1bi15cs101 rajesh 10cs51 me 19
1bi15cs101 rajesh 10cs51 dbms 12

4.Calculate the FinalIA (average of best two test marks) and update the corresponding table for all
students.

Before updating finalia marks of all students

USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA


1va12cs001 10cs41 s7 11 15 18
1va12cs001 10cs42 s6 13 19 12
1bi15cs101 10cs51 s5 19 20 12
1bi15cs101 10cs52 s5 12 14 16
1va12cs002 10cs81 s2 16 17 18
1va12cs002 10cs82 s2 12 16 15
1va12cs002 10cs83 s2 16 18 20
1va12cs003 10cs81 s3 14 12 11
1va12cs003 10cs82 s3 12 11 16
1va12cs003 10cs83 s3 18 12 10
1va13cs004 10cs82 s4 16 17 18

NOTE: Query to update finalia marks of all the students;

SQL> UPDATE iamarks


SET finalia=case
WHEN (test1<test2 and test1<test3) then ceil((test2+test3)/2)
WHEN (test2<test1 and test2<test3) then ceil((test1+test3)/2)
ELSE ceil((test1+test2)/2)
END;
12 rows updated.

After updating finalia marks of all students


SQL> select *from iamarks;

USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA


1va12cs001 10cs41 s7 11 15 18 17
1va12cs001 10cs42 s6 13 19 12 16
1bi15cs101 10cs51 s5 19 20 12 20
1bi15cs101 10cs52 s5 12 14 16 15
1va12cs002 10cs81 s2 16 17 18 18
1va12cs002 10cs82 s2 12 16 15 16
1va12cs002 10cs83 s2 16 18 20 19
Dept of Computer science & Engg,RLJIT Page 24
Data Base Management System Laboratory with Mini Project 21CSL55

1va12cs003 10cs81 s3 14 12 11 13
1va12cs003 10cs82 s3 12 11 16 14
1va12cs003 10cs83 s3 18 12 10 15
1va13cs004 10cs81 s4 16 12 11 14
1va13cs004 10cs82 s4 16 17 18 18

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.

SELECT s.usn,s.sname,sec.sem,sec.sec,i.subcode,i.finalia,
case
when (I.finalia>=17 and i.finalia<=20) then 'outstanding'
when (i.finalia>=12 and i.finalia<=16) then 'average'
when (i.finalia<12) then 'weak'
else 'un_known'
end cat
FROM student s,iamarks i,semsec sec
WHERE s.usn=i.usn and i.ssid=sec.ssid and sec.sem=8 and sec.sec in('a','b','c')
ORDER BY cat;

USN SNAME SEM SEC SUBCODE FINALIA CAT


1va12cs003 chaithra 8 b 10cs83 15 average
1va12cs003 chaithra 8 b 10cs81 13 average
1va12cs003 chaithra 8 b 10cs82 14 average
1va12cs002 bharath 8 a 10cs82 16 average
1va13cs004 daniel 8 c 10cs81 14 average
1va12cs002 bharath 8 a 10cs81 18 outstanding
1va13cs004 daniel 8 c 10cs82 18 outstanding
1va12cs002 bharath 8 a 10cs83 19 outstanding

Dept of Computer science & Engg,RLJIT Page 25


USN SUBCODE
Data Base Management SystemSSID TEST1
Laboratory TEST2
with Mini TEST3 FINALIA
Project 21CSL55
1va12cs001 10cs41 s7 11 15 18 17
1va12cs001 10cs42 s6 13 19 12 16
1bi15cs101 10cs51 s5 19 20 12 20
1bi15cs101 10cs52 s5 12 14 16 15 SQL> update iamarks
1va12cs002 10cs81 s2 10 9 8 18 set
1va12cs002 10cs82 s2 12 16 15 16
1va12cs002 10cs83 s2 16 18 20 19
1va12cs003 10cs81 s3 14 12 11 13
1va12cs003 10cs82 s3 12 11 16 14
1va12cs003 10cs83 s3 10 6 7 15
1va13cs004 10cs82 s4 16 17 18 14
1va13cs004 10cs82 s4 16 17 18 18
test1=10,test2=9,test3=8 where usn='1va12cs002' and subcode='10cs81' ;

1 row updated.

SQL> update iamarks set test1=10,test2=6,test3=7 where usn='1va12cs003' and subcode='10cs83';

1 row updated.

SQL> UPDATE iamarks


SET finalia=case
WHEN (test1<test2 and test1<test3) then ceil((test2+test3)/2)
WHEN (test2<test1 and test2<test3) then ceil((test1+test3)/2)
ELSE ceil((test1+test2)/2)
END;

12 rows updated.

SQL> select *from iamarks;

USN SUBCODE SSID TEST1 TEST2 TEST3 FINALIA


1va12cs001 10cs41 s7 11 15 18 17
1va12cs001 10cs42 s6 13 19 12 16
Dept of Computer science & Engg,RLJIT Page 26
Data Base Management System Laboratory with Mini Project 21CSL55

1bi15cs101 10cs51 s5 19 20 12 20
1bi15cs101 10cs52 s5 12 14 16 15
1va12cs002 10cs81 s2 10 9 8 10
1va12cs002 10cs82 s2 12 16 15 16
1va12cs002 10cs83 s2 16 18 20 19
1va12cs003 10cs81 s3 14 12 11 13
1va12cs003 10cs82 s3 12 11 16 14
1va12cs003 10cs83 s3 10 6 7 9
1va13cs004 10cs81 s4 16 12 11 14
1va13cs004 10cs82 s4 16 17 18 18

SQL>SELECT s.usn,s.sname,sec.sem,sec.sec,i.subcode,i.finalia,
case
WHEN (I.finalia>=17 and i.finalia<=20) then 'outstanding'
WHEN (i.finalia>=12 and i.finalia<=16) then 'average'
WHEN (i.finalia<12) then 'weak'
ELSE 'un_known'
END cat
FROM student s,iamarks i,semsec sec
WHERE s.usn=i.usn and i.ssid=sec.ssid and sec.sem=8 and sec.sec in('a','b','c')
ORDER BY cat;
USN SNAME SEM SEC SUBCODE FINALIA CAT
1va12cs002 bharath 8 a 10cs82 16 average
1va13cs004 daniel 8 c 10cs81 14 average
1va12cs003 chaithra 8 b 10cs82 14 average
1va12cs003 chaithra 8 b 10cs81 13 average
1va13cs004 daniel 8 c 10cs82 18 outstanding
1va12cs002 bharath 8 a 10cs83 19 outstanding
1va12cs002 bharath 8 a 10cs81 10 weak
1va12cs003 chaithra 8 b 10cs83 9 weak

Dept of Computer science & Engg,RLJIT Page 27


Data Base Management System Laboratory with Mini Project 21CSL55

5. Consider the 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 controlledby 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.

RELATIONAL SCHEMA DIAGRAM:

Dept of Computer science & Engg,RLJIT Page 28


Data Base Management System Laboratory with Mini Project 21CSL55

ER DIAGRAM :

Dept of Computer science & Engg,RLJIT Page 29


Data Base Management System Laboratory with Mini Project 21CSL55

CREATING TABLES:

SQL> create table employee(ssn int primary key,name varchar(30) not null,address varchar(30)
not null, sex char(1) ,salary dec(10,2));
SQL> create table department(dno int primary key,dname varchar(5) not null unique,
mgrssn int references employee(ssn),mgrstdate date);

SQL> create table dlocation(dno int references department(dno) , dloc varchar(10),


primary key(dno,dloc));
SQL> create table project(pno int primary key,pname varchar(10) not null unique,
plocation varchar(10) not null,dno int references department(dno));
SQL> create table works_on(ssn int references employee(ssn),pno int references project(pno),
hours int ,primary key(ssn,pno));
INSERTING VALUES:
SQL>insert into employee values(&ssn,'&name','&address','&sex',&salary);
SQL>insert into department values(&dno,’&dname’,&mgrssn,’& mgrstdate);
SQL> ALTER TABLE employee add superssn int references employee(ssn);
Table altered.
SQL> ALTER TABLE employee add dno int references department(dno);
Table altered.
Example:
SQL> update table employee
set superssn=1000,dno=1
Where ssn=1000;

Dept of Computer science & Engg,RLJIT Page 30


Data Base Management System Laboratory with Mini Project 21CSL55

SQL>insert into dlocation values (&dno,’&dloc’);


SQL>insert into project values (&pno,’& pname’, ’& plocation’,&dno);
SQL>insert into works_on values (&ssn,&pno,&hours);

DISPLAYING TABLES:

SQL> select *from employee;

SSN NAME ADDRESS S SALARY SUPERSSN DNO


1000 kishore bangalore m 50000 1001 1
1001 ram mysore m 650000 1002 2
1002 lakshmi pune f 750000 1003 4
1003 raj hyderabad m 10000 1002 5
1004 ramya hubli f 15000 1002 2
1005 harsha chennai m 16000 1002 2
1006 kalyan bangalore m 670000 1002 2
1007 abhijith bagalore m 8500 1002 2
1008 mahi pune m 25000 1003 2
1009 rahul pune m 75000 1001 1

SQL> select *from department; SQL> select *from project;


DNO DNAME MGRSSN MGRSTDATE PNO PNAME PLOCATION DNO
1 accounts 1000 10-mar-00 10 iot bangalore 1
2 research 1001 20-jan-01 11 android new york 5
3 sales 1002 15-feb-00 12 dm pune 5
4 operations 1003 16-jul-03 13 bigdata noida 3
5 finance 1004 19-jul-10

SQL> select *from works_on;

SSN PN HOURS
O
1000 10 40
1003 11 35
1003 12 6
1003 13 4
1004 11 25
1004 12 16
1005 13 40
1004 13 7
1005 12 5
1006 11 3
Queries:

Dept of Computer science & Engg,RLJIT Page 31


Data Base Management System Laboratory with Mini Project 21CSL55

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.

SQL> (SELECT distinct p.pno from project p,department d,employee e


WHERE d.dno=p.dno and mgrssn=ssn and name like'%RAMYA')
UNION
(SELECT distinct p.pno from project p,works_on w,employee e
WHERE p.pno=w.pno and w.ssn=e.ssn and name like '%RAMYA');

PNO
12
11
13

2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent
raise.

SQL> SELECT e.name,1.1*e.salary as incr_salary


FROM employee e,works_on w,project p
WHERE e.ssn=w.ssn and w.pno=p.pno and p.pname='IOT';

NAME INCR_SALARY
Kishore 55000

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

SQL> SELECT sum(salary),max(salary),min(salary),avg(salary)


FROM (employee e join department d on e.dno=d.dno)
WHERE dname='ACCOUNTS';
SUM(SALARY) MAX(SALARY) MIN(SALARY) AVG(SALARY)
50000 50000 50000 50000

SQL> SELECT sum(salary),max(salary),min(salary),avg(salary)


FROM (employee e join department d on e.dno=d.dno)
WHERE dname='RESEARCH';

SUM(SALARY) MAX(SALARY) MIN(SALARY) AVG(SALARY)


1444500 670000 8500 240750
4.Retrieve the name of each employee who works on all the projects controlledby department number
5 (use NOT EXISTS operator).

Dept of Computer science & Engg,RLJIT Page 32


Data Base Management System Laboratory with Mini Project 21CSL55

SQL> SELECT e.name from employee e WHERE NOT EXISTS ( SELECT pno FROM
project where dno=5
MINUS
(SELECT pno FROM works_on w WHERE w.ssn=e.ssn));

NAME
Raj
Ramya

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.

SQL> SELECT e.dno,count(*)


FROM department d,employee e
WHERE d.dno=e.dno and salary>600000
AND e.dno in (select dno from employee group by dno having count(dno) >5)
GROUP BY e.dno;

DNO COUNT(*)
2 2

SAMPLE VIVA QUESTION

Dept of Computer science & Engg,RLJIT Page 33


Data Base Management System Laboratory with Mini Project 21CSL55

1. What is Database Management System (DBMS)?


2. What are the benefits of using DBMS?
3. What are the different types of databases?
4. Who are the different types of database users? What are their role and responsibilities?
5. What are the advantages of using Database system over File System?
6. What are the different applications of DBMS?
7. What is SQL?
8. Explain ER diagram for all the relational tables used in the lab manual.
9. What are the differences between Database and Relational Database?
10. Explain CREATE, INSERT, SELECT, UPDATE, DELETE, DROP, ALTER commands used in
SQL.
11. What are the different data types used in SQL-99?
12. How VARCHAR(Size) is better than CHAR(Size)?
13. What are the different Integrity Constraints? Or Explain different types of keys used in SQL?
14. Why we need to create and join multiple tables?
15. What are the different types of JOIN used in SQL?
16. Explain EQUIE JOIN and the significance of creating mirror copy? Why it is called PIG EAR
JOIN?
17. How to implement simple join? How it is Cartesian product of multiple tables?
18. What is the difference between DELETE and DROP commands?
19. How would you add or drop constraints from table give on example.
20. How to rename a table?
21. What is the usage of creating views?
22. Under what circumstances a VIEW cannot be updated?
23. Why we are using Normalization?
24. What are the different types of Normalization?
25. Which normalization is based on atomic property of attributes in a table?
26. What are the normalizations based on Functional Dependencies of attributes in a table?
27. Write relational algebra and calculus for any given queries.
28. Why we are using GROUP BY HAVING clause in SQL?
29. What are the different Aggregation functions in SQL?
30. What do you mean by embedded SQL?
31. What are assertions and triggers?
32. What do you mean by transaction
33. What are the different properties of a transaction?
34. What are the methods of doing concurrency control?
35. What are the different types of LOCKS?

Dept of Computer science & Engg,RLJIT Page 34

You might also like