0% found this document useful (0 votes)
3 views50 pages

Final dbms lab

Dbms lab manual

Uploaded by

Seema Kolagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views50 pages

Final dbms lab

Dbms lab manual

Uploaded by

Seema Kolagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

1.

Create the following relation for the student


E-R diagram

Student(regno:string,name:string,class:string,bdate:date,marks1:int,Marks2:int,
Marks3:int)

a. Create table

SQL>create table student(regno varchar(20),name varchar(50),class varchar(20),bdate


date,marks1 int,marks2 int,marks3 int);

SQL>desc student;

b. Enter atleast five tuples of above relation

SQL>insert into student values('19u11201','brunda','bca1sem','02-dec-1992',68,74,80);

SQL>insert into student values('19u11301','bharati','bca1sem','02-jan-1990',68,84,85);

SQL>insert into student values('19u11221','anil','bca1sem','16-mar-1991',88,84,80);

SQL>insert into student values('19u11325','naveen','bca1sem','19-dec-1989',62,54,80);


SQL>insert into student values('19u11281','rashmi','bca1sem','05-feb-1991',68,74,80);

SQL>select *from student;

c. Demonstrate the usage of following clauses for above relation


 Where

SQL> select * from student

where regno > '19u11229';

 Having

SQL>SELECT
regno,marks1 from
student
group by regno,marks1
having marks1 > 80;

 Order by
SQL> select * from student
order by regno asc;
 Group by

select count(name),regno
from student
group by regno;

b. Demonstrate the usage of the following clauses for the above relation

a. Sum
SQL> select sum(marks1) from student;

b. avg
select avg(marks1) from student;

c. Count
select count(*) from student;

d. Like

select * from student;


where name like 'b%';
e. Between
select * from student
where marks3 between 60 and 80;

f. Max and Min


select min(marks1) ,max(marks1)
from student;
2. Consider the following database that maintain information about
employees and departments

Employee(empid:int,ename:string,age:int,salary:int ,deptno:int)
Department(deptno:int,dname,string,managerid:int)

E-R diagram

a. Create the above tables by specifying the primary keys and foreign keys

SQL>create table employee(empid int primary key,ename varchar(50),age int,salary


int,deptno int references department(deptno));

SQL>desc employee;
SQL>create table department(deptno int primary key,dname varchar(50),managerid int);

SQL>desc department

b. Enter at least 5 tuples for each relation

SQL>

insert into employee values(11,'e1',25,10000,1);


insert into employee values(22,'e2',25,20000,2);
insert into employee values(23,'e3',26,8000,2);
insert into employee values(24,'e2',25,20000,5);

SQL> Select *from employee;

SQL> insert into department values(1,'d1',55);


insert into department values(2,'d2',56);
insert into department values(3,'d3',57);
insert into department values(4,'cs',57);
insert into department values(5,'cs',57);

SQL>Select *from department;

c. Display emp-id and emp-name whose salary lies between 10000 and 50000

SQL> select empid,ename from employee


where salary between 10000 and 50000;
d. List empname and salary for all the employee working for CS Dept

SQL> select ename ,salary from employee where deptno in


(select deptno from department where dname='cs' );

e. Display empname and deptname for all the managers

SQL>select e.ename,d.dname from employee e, department d


where e.deptno=d.deptno;
3. DBMS LAB - ORDER DATABASE
Consider the following schema for OrderDatabase:
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’saverage.
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 the salesman with id 1000. All his orders must
also be deleted.

a. Create table

SQL>create table salesman(salesman_id int primary key, name


varchar(50),city varchar(50),commission int);
SQL>desc salesman;
SQL>create table customer(customer_id int primary key,cust_name
varchar(50),city varchar(50),grade int, salesman_id int references
salesman(salesman_id) on delete set null);
SQL>desc customer;

SQL> create table orders(ord_no int primary key,purchase_amt int, ord_date


int,customer_id int references customer(customer_id),salesman_id int references
salesman(salesman_id) on delete cascade);

SQL>desc orders;

b. Enter atleast 5 tuples for above relation


Salesman table:

SQL>insert all
into salesman(salesman_id,name,city,commission)values(5001,'james','Dharwad',0.15)
into salesman(salesman_id,name,city,commission)values(5002,'Navya','Bangalore',0.15)
into salesman(salesman_id,name,city,commission)values(5003,'Vinay','Mumbai',0.15)
into salesman(salesman_id,name,city,commission)values(5004,'Shruti','Pune',0.15)
into salesman(salesman_id,name,city,commission)values(5005,'Kavya','goa',0.15)
select *from dual;

SQL>Select *from salesman;


Customer table

insert all
into customer(customer_id,cust_name,city,grade,salesman_id) values(3002,'vijay','davangere',100,5001)
into customer(customer_id,cust_name,city,grade,salesman_id) values(3003,'ramya','mysore',200,5002)
into customer(customer_id,cust_name,city,grade,salesman_id) values(3004,'ramesh','hubli',300,5003)
into customer(customer_id,cust_name,city,grade,salesman_id) values(3005,'raj','belagavi',100,5001)
into customer(customer_id,cust_name,city,grade,salesman_id) values(3001,'brunda','shimogga',400,5004)
select *from dual;

SQL>select *from customer;

Orders table

SQL> insert all


into orders(ord_no,purchase_amt,ord_date,customer_id,salesman_id)values(7001,150.5,2012-10-
05,3005,5002)
into orders(ord_no,purchase_amt,ord_date,customer_id,salesman_id)values(7002,270.5,2012-09-
10,3001,5001)
into orders(ord_no,purchase_amt,ord_date,customer_id,salesman_id)values(7003,65.5,2012-10-
05,3002,5002)
into orders(ord_no,purchase_amt,ord_date,customer_id,salesman_id)values(7004,110, 2012-08-
17,3004,5001)
into orders(ord_no,purchase_amt,ord_date,customer_id,salesman_id)values(7005,270.5,2012-07-
27,3003,5004)
select *from dual;
SQL>select *from orders;

c. Count the customers with grades above Bangalore’saverage.

select count(*)
from customer
where grade > (select avg(grade)
from customer
where city = 'mysore');
output:

d. Find the name and numbers of all salesmen who had more than one customer.

select salesman_id,
name
from salesman
where salesman_id in (select salesman_id
from customer
group by salesman_id
having count(*) > 1);

output:

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

select s.salesman_id, s.name, c.cust_name, s.commission


from salesman s, customer c
where s.city=c.city
union
select s.salesman_id,s.name,'no match',s.commission
from salesman s
where city not in
(select city
from customer)
order by 2 asc;

f. Create a view that finds the salesman who has the customer with the highest order of a day.

create view elitsalesman as select b.ord_date, a.salesman_id, a.name from salesman a, orders b where
a.salesman_id = b.salesman_id and b.purchase_amt=(select max (purchase_amt) from orders c where c.ord_date
= b.ord_date);

Select *from elitsalesman;

g. Demonstrate the DELETE operation by removing the salesman with id 1000. All his orders
must also be deleted.

delete from salesman


where salesman_id=5003;

1 row deleted
4. INSURANCE DATABASE
Consider the following insurance database given below. The Primary Keys are
underlined and data types are specified.
 PERSON(DRIVER_ID: string, name: string, address: string)
 CAR(RegNo: string, model: string, year: int)
 ACCIDENT(report_number: int, Accident_date: date, location: string)
 OWNS(driver_id: string(PERSON), Regno: string(CAR))
 PARTICIPATED(driver_id: string(PERSON), Regno: string(CAR),
report_number: int(ACCIDENT), damage_amount: int)

Queries:
1) Create the above tables by properly specifying the primary keys and the foreign
keys.
2) Enter at least five tuples for relation.
3) Demonstrate how you
a) Update the damage amount for the car with a specific Regno in the
accident with report_number 4 to 25000.
b) Add a new accident to the database.
4) Find the total number of people who owned cars that were involved in
accidents in 2002.
5) Find the number of accidents in which cars belonging to a specific model were
involve.

ER-diagram
Q1: Create the tables by properly specifying the primary keys and the foreign keys.

SQL> create table person(driver_id char(10) primary key,name char(10),addresschar(10));

Table created.

SQL> desc person;

SQL> create table car(


reg_no char(10) primary key,
model char(10),
year int);

Table created.

SQL>desc car;

SQL> create table accident(


report_no int primary key,
adate date,
location char(10));
Table created.

SQL>desc accident;

SQL> create table owns(


driver_id char(10) foreign key references person(driver_id),
reg_no char(10) references car(reg_no));

Table created.

SQL>desc owns;

SQL> create table participated(


driver_id char(10) references person(driver_id),
reg_no char(10) references car(reg_no),
report_no int references accident(report_no),
damage_amount int);

Table created.

SQL>desc participated;
Q2: Enter at least five tuples for each relation.

PERSON Table:

SQL> insert all


into person(driver_id, name, address) values('a21', 'rahul', 'gokulroad')
into person(driver_id, name, address) values('a22', 'rani', 'hosur')
into person(driver_id, name, address) values('a23', 'ravi', 'keshwapur')
into person(driver_id, name, address) values('a24', 'roja', 'mgroad')
into person(driver_id, name, address) values('a25', 'sandesh', 'jaynagar')
select *from dual;

5 row(s) inserted.

SQL>select *from person;

CAR Table:
SQL> insert all
into car(reg_no, model, year) values('ka20', 'santro', 2000)
into car(reg_no, model, year) values('ka21', 'benz', 2001)
into car(reg_no, model, year) values('ka22', 'ferari', 2002)
into car(reg_no, model, year) values('ka23', 'hatiz', 2003)
into car(reg_no, model, year) values('ka24', 'swift', 2004)
select *from dual;

5 row(s) inserted.

SQL> select *from car;


ACCIDENT Table:

SQL>insert all

into accident(report_no,adate,location)values(2,'01/FEB/02','hubli')

into accident(report_no,adate,location)values(3,'01/MAR/03','bombay')

into accident(report_no,adate,location)values(4,'01/APR/04','delhi')

into accident(report_no,adate,location)values(5,'01/MAY/05','goa')

into accident(report_no,adate,location)values(6,'22/MAY/02','dandeli')

select *from dual;

5 row(s) inserted.

SQL> select *from accident;

OWNS Table:

SQL>insert all

into owns(driver_id ,reg_no ) values('a21', 'ka20')

into owns(driver_id ,reg_no ) values('a22', 'ka21')

into owns(driver_id ,reg_no ) values('a23', 'ka22')


into owns(driver_id ,reg_no ) values('a24', 'ka23')

into owns(driver_id ,reg_no ) values('a25', 'ka24')

select *from dual;

5 row(s) inserted.

SQL> select * from owns;

PARTICIPATED Table:

SQL>insert all into


participated(driver_id,reg_no,report_no,damage_amount)values('a21','ka20',2,1000)

into participated(driver_id,reg_no,report_no,damage_amount)values('a22','ka21',3,2000)

into participated(driver_id,reg_no,report_no,damage_amount)values('a23','ka22',4,3000)

into participated(driver_id,reg_no,report_no,damage_amount)values('a24','ka23',5,5000)

into participated(driver_id,reg_no,report_no,damage_amount)values('a25','ka24',6,5000)

select *from dual;

5 row(s) inserted.

SQL>select *from participated;

Q3: Demonstrate how you


a) Update the damage amount for the car with a specific regno in the accident with
report number 5 to 25000
SQL> update participated set damage_amount=25000 where report_no='5';

1 row updated.

SQL >select *from participated;

b) Add a new accident to the database

SQL> insert into accident values(23, '23/Feb/07', 'gadag');

1 row created

SQL> select * from accident;

Q4: find the total number of people who owned cars that were involved in
accidents in 2002
SQL>select count(driver_id)
from participated
where reportno in
(select report_no from accident where adate between '01/jan/02' and 31/dec/02');

Q5: Find the number of accidents in which cars belonging to a specific model were
involved

SQL> select count(report_no)


from participated p, car c
where p.reg_no=c. reg_no AND c.model='santro';
5. BOOK PUBLISHER
Consider the following data base of business relations for an order processing
database application in a company.
 AUTHOR (author-id: int, name: string, city: string, country: string)
 PUBLISHER (publisher_id: int, name: string, city: string, country:
string)
 CATEGORY(category_id: int, description: string)
 CATALOG (book_id: int, title: string, author_id: int(AUTHOR),
publisher_id: int(PUBLISHER), category_id: int(CATEGORY),
year: int, price: int)
 ORDER-DETAILS(order_no: int, book-id: int(CATALOG), quantity: int)

Queries:
1) Create the above tables by properly specifying the primary keys and the
foreign keys.
2) Enter at least five tuples for each relation.
3) Give the details of the authors who have 2 or more books in the catalog and the price of
the books is greater than the average price of the books in the catalog and the year of
publication is after 2000.
4) Find the author of the book which has maximum sales.
5) Demonstrate how you increase the price of books published by a specific publisher by
10%.
Q1: Create the tables by properly specifying the primary keys and the foreign keys.

Table author:

SQL> create table author(


author_id int primary key,
name varchar(20),
city varchar(20),
country varchar(20));

Table created.

SQL>desc author;

Table publisher:

SQL> create table publisher(


publisher_id int primary key,
name varchar(20),
city varchar(20),
country varchar(20));

Table created.

SQL>desc publisher;
Table category:

SQL> create table category(


category_id int primary key,
description varchar(45));
Table created

SQL>desc category;

Table catalog1:

SQL> create table catalog1(


book_id int primary key,
title varchar(25),
author_id int references author(author_id),
publisher_id int references publisher(publisher_id),
category_id int references category(category_id),
year int,price int);
Table created.

SQL>desc catalog1;

Table order_details:

SQL> create table order_details(


order_no int primary key,
book_id int references catalog1(book_id),
quantity int);
Table created.

SQL>desc order_details;

Q2: Enter at least five tuples for each relation.

AUTHOR Table:

SQL>insert all
into author values(101, 'p b kottur', 'india', 'banglore')
into author values(202, 'balguruswamy', 'india', 'delhi')
into author values(303, 'padma reddy', 'india', 'hyderbad')
into author values(404, 'robert hook', 'usa', 'paris')
into author values(505, 'james wilson', 'india', 'mumbai')
select *from dual;

5 row(s) inserted.

SQL>select *from author;


PUBLISHER Table:

SQL>insert all
into publisher values(1001, 'pearson', 'pune', 'india')
into publisher values(2002, 'tata mcgraw', 'delhi', 'india')
into publisher values(3003, 'pearson', 'banglore', 'india')
into publisher values(4004, 'tata mcgraw', 'hyderbad', 'india')
into publisher values(5005, 'pearson', 'gadag', 'india')
select *from dual;

5 row(s) inserted.
SQL>select *from publisher;

CATEGORY Table:

SQL>insert all
into category values(100, 'networking')
into category values(200, 'programming')
into category values(300, 'developing software')
into category values(400, 'security protection')
into category values(500, 'electronics')
select * from dual;

5 row(s) inserted.

SQL>select *from category;


CATALOG1 Table:
SQL>insert all

into catalog1 values(111, 'java', 101, 1001, 100, 2002, 1500)


into catalog1 values(222, 'j2ee', 101, 2002, 200, 2003, 2000)
into catalog1 values(333, 'operating system', 101, 3003, 300, 2005, 1500)
into catalog1 values(444, 'c++', 202, 4004, 400, 2008, 500)
into catalog1 values(555, 'data structures', 202, 5005, 500, 2006, 500)
select *from dual;

5 row(s) inserted.

SQL> select * from catalog1;

ORDER_DETAILS Table:

SQL>insert all
into order_details values(1010, 111, 100)
into order_details values(2020, 222, 200)
into order_details values(3030, 333, 300)
into order_details values(4040, 444, 400)
into order_details values(5050, 555, 500)
select *from dual;

5 row(s) inserted.

SQL>select *from order_details;


Q3: Give the details of the authors who have 2 or more books in the catalog
and the price of the books is greater than the average price of the books in the
catalog and the year of publication is after 2000.

SQL>select author_id, name, city, country


from author where author_id in
(select author_id from catalog1 where year>2000 and price>(select avg(price) from
catalog1)
group by author_id, name, city, country having count(*)>=2);

Q4: Find the author of the book which has maximum sales.

SQL>select a.name
from author a, catalog1 c, order_details o
where a.author_id=c.author_id AND o.book_id=c.book_id AND
o.quantity =(select max(quantity) from order_details);
Q5: Demonstrate how you increase the price of books published by a specific
publisher by 10%.

SQL>select *from catelog1;

SQL>update catalog1 set price=(price*0.1)+price where publisher_id=1001;


SQL>update catalog1 set price=(price*0.1)+price where publisher_id=2002;
SQL>select *from catalog1;
6. Student Database
Consider the following data base of business relations for an order processing database
application in a company.

 STUDENT(regno: string, name: string, major: string, bdate: date)


 COURSE(course_no: int, cname: string, dept: string)
 ENROLL(regno: string(STUDENT), course_no: int(COURSE), sem: int, marks: int)
 TEXT_Book(book_ISBN: int, book-title: string, publisher: string, author: string)
 BOOK_ADOPTED(course_no: int(Course), sem: int, book_ISBN(TEXT_BOOK): int)
Queries:

1) Create the above tables by properly specifying the primary keys and the foreign keys.
2) Enter at least five tuples for each relation.
3) Demonstrate how you add a new textbook to the database and make this book be
adopted by some department.
4) Produce a list of text books(include Course #,Book-ISBN, Book-title) in the
alphabetical order for courses offered by the 'CS' department that use more than two
books.
5) List any department that has all its adopted books published by a specific publisher.
Q1: Create the tables by properly specifying the primary keys and the
foreign keys.

Table 1
SQL>create table student(regno char(8) primary key,
name varchar(20),
major varchar(20),
bdate date);

Table created.

SQL>Desc student;

Table 2
SQL>create table course(Course_no int primary key,
course_name varchar(20),
dept varchar(20));
Table created.
SQL>Desc course;

Table 3
SQL>create table enroll(regno char(8) references student(regno),
course_no int references course(course_no),
sem int,
marks int);

Table created.
SQL>Desc enroll;

Table 4

SQL>create table text_book(book_ISBN int primary key,


book_title varchar(20),
publisher varchar(20),
author varchar(20));

Table created.
SQL>Desc text_book;

Table 5
SQL>create table book_adopted(Course_no int references course(course_no),
sem int,book_ISBN int references text_book(book_ISBN));

Table created.

SQL>Desc book_adopted;
Q2: Enter at least five tuples for each relation.

STUDENT Table:
SQL>insert all
into student values ('18u11301','arpita','computer science','2/APR/2001')
into student values ('18u11302','ashwini','computer science','2/JUN/2001')
into student values ('18u11303','goutami','science','15/MAR/1999')
into student values ('18u11304','archana','computer science','17/JAN/2000')
into student values ('18u11305','chandrakant','science','12/JUL/2000')
select *from dual;

5 row(s) inserted.

SQL>select *from student;

COURSE Table:

SQL>insert all
into course values (11, 'BCA', 'computer science')
into course values (22, 'BBA', 'commerce')
into course values (33, 'BSc', 'physics')
into course values (44, 'Bcom', 'commerce')
into course values (55, 'BA', 'Arts')
select *from dual;

5 row(s) inserted.

SQL> select *from course;

ENROLL Table:
SQL>insert all
into enroll values ('18u11301', 11, 5, 90)
into enroll values ('18u11302', 33, 3, 96)
into enroll values ('18u11303', 44, 5, 95)
into enroll values ('18u11304', 11, 5, 95)
into enrol values ('18u11305', 22, 3, 99)
select *from dual;

5 row(s) inserted.

SQL>Select *from enroll;

TEXT_BOOK Table:

SQL>insert all
into text_book values (101, 'CPP', 'pearson', 'petrick naughton')
into text_book values (102, 'JAVA', 'tata mcgraw', 'robert jesin')
into text_book values (103, 'unix', 'tata mcgraw', 'lene kevin')
into text_book values (104, 'C', 'pearson', 'john martin')
into text_book values (105, 'DBMS', 'pearson', 'Ramakrshina')
select *from dual;

5 row(s) inserted.

SQL>Select *from text_book;

BOOK_ADOPTED Table:

SQL>insert all
into book_adopted values (11, 5, 101)
into book_adopted values (22, 3, 102)
into book_adopted values (33, 5, 104)
into book_adopted values (55, 5, 103)
into book_adopted values (44, 3, 105)
into book_adopted values (33, 5, 103)
select *from dual;

6 row(s) inserted.

SQL>Select *from book_adopted;


Q3: Demonstrate how you add a new text book to the database and make
this book be adopted by some department.

SQL>insert into text_book values (106, 'MATHS', 'prism', 'umarani');

1 row(s) inserted.

SQL>select *from text_book;

SQL>insert into book_adopted values (11, 5, 106);

1 row(s) inserted.

SQL>select *from book_adopted;

Q4: Produce a list of text books(include Course #,Book-ISBN, Book-title)


in the alphabetical order for courses offered by the 'CS' department that
use more than two books.

SQL>select t.book_title, b.book_isbn, c.course_no


from course c, text_book t, book_adopted b
where c.course_no=b.course_no and b.book_isbn=t.book_isbn and
c.dept='computer science'
order by t.book_title ASC, b.book_isbn, c.course_no;

Q5: List any department that has all its adopted books published by a
specific publisher.

SQL>select c.dept, t.publisher


from course c, text_book t, book_adopted b
where c.course_no=b.course_no and b.book_isbn=t.book_isbn and
t.publisher='pearson'
group by c.dept, t.publisher;
7. Consider the following schema for a LibraryDatabase:
BOOK (Book_id, Title, Publisher_Name, Pub_Year)
BOOK_AUTHORS (Book_id, Author_Name)
PUBLISHER (Name, Address, Phone) BOOK_COPIES
(Book_id, Branch_id, No-of_Copies)
BOOK_LENDING (Book_id, Branch_id, Card_No, Date_Out, Due_Date)
LIBRARY_BRANCH (Branch_id, Branch_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 branch,etc.
2. Get the particulars of borrowers who have borrowed more than 3 books, but from Jan
2017 to Jun2017
3. Delete a book in BOOK table. Update the contents of other tables to reflect this data
manipulationoperation.
4. Create a view of all books and its number of copies that are currently available in the
Library.

ER-Diagram

SQL>create table publisher (name varchar (20) primary key, phone integer, address varchar (20));

SQL> desc publisher;


SQL>create table book(
book_id int Primary key,
title varchar(20),
pub_year varchar(20),
publisher_name references publisher (name) on delete cascade);

SQL>desc book;

SQL>create table book_authors (author_name varchar2 (20), book_id references


book (book_id) on delete cascade, primary key (book_id, author_name));

SQL>desc book_authors;

SQL>create table library_branch (branch_id integer primary key, branch_name varchar2 (50),
address varchar2 (50));

SQL> desc library_branch;

SQL>create table book_copies (no_of_copies integer, book_id references book (book_id) on


delete cascade, branch_id references library_branch (branch_id) on delete cascade, primary key
(book_id, branch_id));
SQL>desc book_copies;

SQL>create table card (card_no integer primary key);


SQL>desc card;

SQL> create table book_lending (date_out date, due_date date, book_id references
book (book_id) on delete cascade, branch_id references library_branch
(branch_id) on delete cascade, card_no references card (card_no) on delete
cascade, primary key (book_id, branch_id, card_no));

SQL>desc book_lending ;

Insertion of Values to Tables


insert into publisher values (‘mcgraw-hill’, 9989076587, ‘bangalore’);
insert into publisher values (‘pearson’, 9889076565, ‘newdelhi’);
insert into publisher values (‘random house’, 7455679345, ‘hydrabad’);
insert into publisher values ('hachette livre', 8970862340, 'chenai');
insert into publisher values('grupoplaneta',7756120238,'bangalore');
Select *from publisher;

insert into book values (1,'dbms','jan-2017','mcgraw-hill');


insert into book values (2,'adbms','jun-2016','mcgraw-hill');
insert into book values (3,'cn','sep-2016','pearson');
insert into book values (5,'os','may-2016','pearson');

select *from book ;

insert into book_authors values ('navathe', 1);


insert into book_authors values ('navathe', 2);
insert into book_authors values ('tanenbaum', 3);
insert into book_authors values ('galvin', 5);

select *from book_authors;

insert into library_branch values (10,'rr nagar','bangalore');


insert into library_branch values (11,'rnsit','bangalore');
insert into library_branch values (12,'rajaji nagar', 'bangalore');
insert into library_branch values (13,'nitte','mangalore');
insert into library_branch values (14,'manipal','udupi');
select *from library_branch;

insert into book_copies values (10, 1, 10);


insert into book_copies values (5, 1,11);
insert into book_copies values (2, 2,12);
insert into book_copies values (5, 2,13);
insert into book_copies values (7, 3,14);
insert into book_copies values (1, 5,10);

select *from book_copies;

insert into card values (100);


insert into card values (101);
insert into card values (102);
insert into card values (103);
insert into card values (104);

select *from card;

insert into book_lending values ('01-jan-17','01-jun-17', 1, 10, 101);


insert into book_lending values ('11-jan-17','11-mar-17', 3, 14, 101);
insert into book_lending values ('21-feb-17','21-apr-17', 2, 13, 101);
insert into book_lending values ('15-mar-17','15-jul-17', 4, 11, 101);
insert into book_lending values ('12-apr-17','12-may-17', 1, 11, 104);

select *from book_lending ;


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

select b.book_id, b.title, b.publisher_name, a.author_name, c.no_of_copies,l.branch_id from book


b, book_authors a, book_copies c, library_branch l where b.book_id=a.book_id and
b.book_id=c.book_id and l.branch_id=c.branch_id;

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

select card_no from


book_lending
where date_out between '01-jan-2017' and '01-jul-2017'
group by card_no
having count (*)>2;

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

delete from book


where book_id=3;
select *from book;
4. Create a view of all books and its number of copies that are currently available in the
Library.

create view v_books as


select b.book_id, b.title, c.no_of_copies
from book b, book_copies c, library_branch l
where b.book_id=c.book_id
and c.branch_id=l.branch_id;

select * from v_books;


8. 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)

E-R Digram

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 percentraise.

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. Create a view with column dept name and dept location, Display name of department located in
‘Dharwad’ on this view
create table department
(dno varchar2 (20) primary key,
dname varchar2 (20), mgrstartdate
date);

sql> desc department1;

create table employee2 (ssn varchar2 (20) primary key, fname varchar2(20), lname
varchar2(20), address varchar2 (20), sex char (1), salary integer, superssn
references employee2 (ssn), dno references department1 (dno));

SQL>desc employee2

NOTE: Once DEPARTMENT and EMPLOYEE tables are created we must alter department
table to add foreign constraint MGRSSN using sql command

alter table department1


add mgrssn references employee2 (ssn);

Desc department1
create table dlocation
(dloc varchar2 (20),
dno references department1 (dno),
primary key (dno, dloc));

SQL> desc dlocation;

create table project (pno


integer primary key, pname
varchar2(20),
plocation varchar2 (20),
dno references department1 (dno));

SQL> desc project;

create table works_on


(hours number (2),
ssn references employee2 (ssn),
pno references project(pno),
primary key (ssn, pno));

SQL> desc works_on;


Values Insertion

insert into employee2 (ssn, fname, lname, address, sex, salary) values
('rnsece01','john','scott','bangalore','m', 450000);

insert into employee2 (ssn, fname, lname, address, sex, salary) values
('rnscse01','james','smith','bangalore','m', 500000);

insert into employee2 (ssn, fname, lname, address, sex, salary) values
('rnscse02','hearn','baker','bangalore','m', 700000);

insert into employee2 (ssn, fname, lname, address, sex, salary) values
('rnscse03','edward','scott','mysore','m', 500000);

insert into employee2 (ssn, fname, lname, address, sex, salary) values
('rnscse04','pavan','hegde','mangalore','m', 650000);

insert into employee2 (ssn, fname, lname, address, sex, salary) values
('rnscse05','girish','malya','dharwad','m', 450000);

insert into department1 values ('1','accounts','01-jan-01','rnsece01');


insert into department1 values ('2','it','01-aug-16','rnsece01');
insert into department1 values ('3','ece','01-jun-08','rnsece01');
insert into department1 values ('4','ise','01-aug-15','rnscse04');
insert into department1 values ('5','cse','01-jun-02','rnscse05');
insert into department1 values ('7','accounts','01-jan-01','rnsece01');
insert into department1 values ('1','accounts','01-jan-01','rnsece01');
select *from department1;

Note: update entries of employee table to fill missing fields SUPERSSN and DN

update employee2 set


superssn=null,dno='2'
where ssn='rnsece01';

update employee2 set


superssn='rnscse03',dno='5'
where ssn='rnsece01';

update employee2 set


superssn='rnscse03',dno='6'
where ssn='rnsece01';

select *from employee2

insert into dlocation values ('bangalore', '1');


insert into dlocation values ('bangalore','2');
insert into dlocation values ('bangalore', '4');
insert into dlocation values('mangalore','4');
insert into dlocation values('mangalore','5');

select *from dlocation;


insert into project values (100,'iot','bangalore','5');
insert into project values (101,'cloud','bangalore','5');
insert into project values (102,'bigdata','bangalore','5');
insert into project values (103,'sensors','bangalore','4');
insert into project values (104,'bank management','bangalore','1');
insert into project values (105,'salarymanagement','bangalore','1');
insert into project values(106,'openstack','bangalore','4');
insert into project values (107,'smartcity','bangalore','2');

select *from project;

insert into works_on values (4, 'rnsece01', 100);


insert into works_on values (6, 'rnsece01', 101);
insert into works_on values (8, 'rnscse02', 102);
insert into works_on values (10, 'rnscse03', 100);
insert into works_on values (4, 'rnscse03', 101);
insert into works_on values (5, 'rnscse03', 102);

select *from works_on


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.

select distinct p.pno


from project p, department1 d,employee2 e
where e.dno=d.dno
and e.lname='scott';

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

select e.fname, e.lname, 1.1*e.salary as incr_sal from


employee2 e, works_on w, project p
where e.ssn=w.ssn
and w.pno=p.pno
and p.pname='iot';

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.

select sum (e.salary), max (e.salary), min (e.salary), avg


(e.salary)
from employee2 e, department1 d
where e.dno=d.dno
and d.dname='accounts';

4. Create a view with column dept name and dept location, Display name of department
located in ‘Dharwad’ on this view

create table departmentnew(dname varchar(50),dlocation varchar(50));

insert into departmentnew values ('cs','dharwad');


insert into departmentnew values ('Civil','dharwad');
insert into departmentnew values ('EC','mysore');

selct *from departmentnew;

CREATE VIEW V_dept


AS SELECT dname,dlocation
FROM departmentnew
WHERE dlocation='dharwad';

select *from v_dept;

You might also like