Professional Documents
Culture Documents
DBMS Final Manual-1
DBMS Final Manual-1
EXPERIMENT 1:
ER DIAGRAM :
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 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:
DISPLAYING TABLES:
Queries:
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.
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.
View created.
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.
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data manipulation
operation.
1 row deleted.
BOOK_ID AUTHOR_NAME
b2 balaguru
b3 navathe
b4 herbashild
b5 ritchie
EXPERIMENT 2:
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’);
DISPLAYING TABLES:
Queries:
1. Count the customers with grades above Bangalore’s average.
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.)
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);
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
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);
DISPLAYING TABLES:
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';
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).
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.
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)
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);
Table altered.
DISPLAYING TABLES:
SQL> select *from semsec; SQL> select *from clas; SQL> select *from subject;
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.
View created.
4.Calculate the FinalIA (average of best two test marks) and update the corresponding table for all
students.
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
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;
1 row updated.
1 row updated.
12 rows updated.
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
ER DIAGRAM :
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);
DISPLAYING TABLES:
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:
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.
PNO
12
11
13
2. Show the resulting salaries if every employee working on the ‘IoT’ project is given a 10 percent
raise.
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 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.
DNO COUNT(*)
2 2