0% found this document useful (0 votes)
26 views

DBMS Lab Mannual

Uploaded by

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

DBMS Lab Mannual

Uploaded by

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

Database Management Systems Lab

Program 1

1. Consider the following schema:


EMPLOYEE (Ename, Ssn, Bdate, Sex, Address,salary,Mgrssn, Dno)
DEPARTMENT (Dname, Dnumber, Mgrssn, Mgr_start_date)
PROJECT (Pname, Pnumber, Plocation, Dnum)
WORKS_ON (Essn, Pno, Hours)
DEPENDENT (Essn,Dependent_name,Sex)
Create above tables by specifying primary key, foreign key and other suitable constraints.
Insert atleast 5 tuples to each created table.
i. Retrieve the name and address of all employees who work for the "ISE" department.
ii. For each employee, retrieve the employee's name and the name of his or her
immediate supervisor
iii. Find the sum of all salaries of all employees
iv. For each department, retrieve the department number, the number of employees in the
department and their average salary.
create database Company040;
A. use Company040; create table project(pname varchar(20), pnumber int Primary Key, plocation
varchar(20),dnum varchar(20), foreign key(dnum) references department(dnumber));
B. create table workson(Essn int, Pno int, Hours time, Foreign Key(Essn) references
employee(Ssn),Foreign Key(Pno) references project(pnumber),Primary Key(Essn,Pno));

--Create Table DEPARTMENT with PRIMARY KEY as DNO

CREATE TABLE DEPARTMENT


(Dname VARCHAR(20),
Dnumber VARCHAR(20) PRIMARY KEY,
Mgr_ssn VARCHAR(20),
Mgr_start_date DATE);

DESC DEPARTMENT;

----------------------------------

--Create Table EMPLOYEE with PRIMARY KEY as SSN

CREATE TABLE EMPLOYEE


(Ename VARCHAR(20),
Ssn VARCHAR(20) PRIMARY KEY,
Bdate date, Gender varchar(4),Address VARCHAR(20),
Salary INTEGER,
Mgr_ssn VARCHAR(20),
Dno varchar(20),
FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE (Ssn),
FOREIGN KEY (Dno) REFERENCES DEPARTMENT (Dnumber));

DESC EMPLOYEE;

----------------------------------
-- ADD FOREIGN KEY Constraint to DEPARTMENT table

ALTER TABLE DEPARTMENT


ADD FOREIGN KEY (Mgr_ssn) REFERENCES EMPLOYEE(Ssn);

----------------------------------

--Create Table DLOCATION with PRIMARY KEY as DNO and DLOC and FOREIGN KEY
DNO referring DEPARTMENT table

CREATE TABLE DLOCATION


(DLOC VARCHAR(20),
DNO VARCHAR(20),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT(DNO),
PRIMARY KEY (DNO, DLOC));

DESC DLOCATION;

----------------------------------

--Create Table PROJECT with PRIMARY KEY as PNO and FOREIGN KEY DNO referring
DEPARTMENT table

CREATE TABLE PROJECT


(PNO INTEGER PRIMARY KEY,
PNAME VARCHAR(20),
PLOCATION VARCHAR(20),
DNO VARCHAR(20),
FOREIGN KEY (DNO) REFERENCES DEPARTMENT(DNO));

DESC PROJECT;

----------------------------------

--Create Table WORKS_ON with PRIMARY KEY as PNO and SSN and FOREIGN KEY SSN
and PNO referring EMPLOYEE and PROJECT table

CREATE TABLE WORKS_ON


(HOURS INTEGER,
SSN VARCHAR(20),
PNO INTEGER,
FOREIGN KEY (SSN) REFERENCES EMPLOYEE(SSN),
FOREIGN KEY (PNO) REFERENCES PROJECT(PNO),
PRIMARY KEY (SSN, PNO));

DESC WORKS_ON;

----------------------------------

INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES


('ABC01','BEN SCOTT','BANGALORE','M', 450000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC02','HARRY SMITH','BANGALORE','M', 500000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC03','LEAN BAKER','BANGALORE','M', 700000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC04','MARTIN SCOTT','MYSORE','M', 500000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC05','RAVAN HEGDE','MANGALORE','M', 650000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC06','GIRISH HOSUR','MYSORE','M', 450000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC07','NEELA SHARMA','BANGALORE','F', 800000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC08','ADYA KOLAR','MANGALORE','F', 350000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC09','PRASANNA KUMAR','MANGALORE','M', 300000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC10','VEENA KUMARI','MYSORE','M', 600000);
INSERT INTO EMPLOYEE (SSN, NAME, ADDRESS, SEX, SALARY) VALUES
('ABC11','DEEPAK RAJ','BANGALORE','M', 500000);

SELECT * FROM EMPLOYEE;

----------------------------------

--Inserting records into DEPARTMENT table

INSERT INTO DEPARTMENT VALUES ('1','ACCOUNTS','ABC09', '2016-01-03');


INSERT INTO DEPARTMENT VALUES ('2','IT','ABC11', '2017-02-04');
INSERT INTO DEPARTMENT VALUES ('3','HR','ABC01', '2016-04-05');
INSERT INTO DEPARTMENT VALUES ('4','HELPDESK', 'ABC10', '2017-06-03');
INSERT INTO DEPARTMENT VALUES ('5','SALES','ABC06', '2017-01-08');

SELECT * FROM DEPARTMENT;

----------------------------------

--Updating EMPLOYEE records

UPDATE EMPLOYEE SET


SUPERSSN=NULL, DNO='3'
WHERE SSN='ABC01';

UPDATE EMPLOYEE SET


SUPERSSN='ABC03', DNO='5'
WHERE SSN='ABC02';

UPDATE EMPLOYEE SET


SUPERSSN='ABC04', DNO='5'
WHERE SSN='ABC03';

UPDATE EMPLOYEE SET


SUPERSSN='ABC06', DNO='5'
WHERE SSN='ABC04';

UPDATE EMPLOYEE SET


DNO='5', SUPERSSN='ABC06'
WHERE SSN='ABC05';

UPDATE EMPLOYEE SET


DNO='5', SUPERSSN='ABC07'
WHERE SSN='ABC06';

UPDATE EMPLOYEE SET


DNO='5', SUPERSSN=NULL
WHERE SSN='ABC07';

UPDATE EMPLOYEE SET


DNO='1', SUPERSSN='ABC09'
WHERE SSN='ABC08';

UPDATE EMPLOYEE SET


DNO='1', SUPERSSN=NULL
WHERE SSN='ABC09';

UPDATE EMPLOYEE SET


DNO='4', SUPERSSN=NULL
WHERE SSN='ABC10';

UPDATE EMPLOYEE SET


DNO='2', SUPERSSN=NULL
WHERE SSN='ABC11';

SELECT * FROM EMPLOYEE;

-------------------------------

--Inserting records into DLOCATION table

INSERT INTO DLOCATION VALUES ('BENGALURU', '1');


INSERT INTO DLOCATION VALUES ('BENGALURU', '2');
INSERT INTO DLOCATION VALUES ('BENGALURU', '3');
INSERT INTO DLOCATION VALUES ('MYSORE', '4');
INSERT INTO DLOCATION VALUES ('MYSORE', '5');

SELECT * FROM DLOCATION;

--------------------------------

--Inserting records into PROJECT table

INSERT INTO PROJECT VALUES (1000,'IOT','BENGALURU','5');


INSERT INTO PROJECT VALUES (1001,'CLOUD','BENGALURU','5');
INSERT INTO PROJECT VALUES (1002,'BIGDATA','BENGALURU','5');
INSERT INTO PROJECT VALUES (1003,'SENSORS','BENGALURU','3');
INSERT INTO PROJECT VALUES (1004,'BANK MANAGEMENT','BENGALURU','1');
INSERT INTO PROJECT VALUES (1005,'SALARY MANAGEMENT','BANGALORE','1');
INSERT INTO PROJECT VALUES (1006,'OPENSTACK','BENGALURU','4');
INSERT INTO PROJECT VALUES (1007,'SMART CITY','BENGALURU','2');

SELECT * FROM PROJECT;

------------------------------

--Inserting records into WORKS_ON table

INSERT INTO WORKS_ON VALUES (4, 'ABC02', 1000);


INSERT INTO WORKS_ON VALUES (6, 'ABC02', 1001);
INSERT INTO WORKS_ON VALUES (8, 'ABC02', 1002);
INSERT INTO WORKS_ON VALUES (10,'ABC03', 1000);
INSERT INTO WORKS_ON VALUES (3, 'ABC05', 1000);
INSERT INTO WORKS_ON VALUES (4, 'ABC06', 1001);
INSERT INTO WORKS_ON VALUES (5, 'ABC07', 1002);
INSERT INTO WORKS_ON VALUES (6, 'ABC04', 1002);
INSERT INTO WORKS_ON VALUES (7, 'ABC01', 1003);
INSERT INTO WORKS_ON VALUES (5, 'ABC08', 1004);
INSERT INTO WORKS_ON VALUES (6, 'ABC09', 1005);
INSERT INTO WORKS_ON VALUES (4, 'ABC10', 1006);
INSERT INTO WORKS_ON VALUES (10,'ABC11', 1007);

SELECT * FROM WORKS_ON;

1. Select ename, address from Employee, department where dname=”ISE” and dno=dnumber;
2. Select E.ename , S.ename from employee E, employee S where E.superssn=S.ssn;
3. Select sum(salary) from Employee;
4. Select dno, count(ssn),AVG(salary) from employee group by dno;
C. Create table employee(ename varchar(15), ssn int, bdate int, sex varchar(10),address
varchar(20),salary int, Primary Key(ssn));
desc employee;

alter table employee add column(superssn int, foreign key(superssn) references


dependent(essn));
alter table employee add column(dno int, foreign key(dno) references department(dnum));

insert into employee values("Ram",1,21,"Male","Bangalore",25000,null,null);


insert into employee values("Shyam",2,21,"Male","Ahemadabad",25000,null,null);
insert into employee values("Bheem",3,21,"Male","Pune",30000,null,null);
insert into employee values("Rajat",4,21,"Male","Sirsi",40000,null,null);
insert into employee values("Supreet",5,21,"Male","Bangalore",38000,null,null);
select *from employee ;

D. create table project(pname varchar(20), pnum int,plocation varchar(20),dnumber int,Primary


Key(pnum),foreign key(dnumber) references department(dnum));
desc project;
insert into project values("ProductX",100,"Delhi",1);
insert into project values("ProductY",101,"Bangalore",2);
insert into project values("ProductZ",102,"Pune",3);
insert into project values("Management",103,"Hassan",1);
insert into project values("Finance",104,"Hassan",1);
select *from project ;

E. create table workson(essn int,pnum int,hours time,Foreign Key(essn) references


employee(ssn),Foreign Key(pnum) references project(pnum),Primary Key(essn,pnum));
desc workson;
insert into workson values(1,100,10:20:30);
insert into workson values(2,101,10:20:30);
insert into workson values(3,102,10:20:30);
insert into workson values(4,103,10:20:30);
insert into workson values(5,104,10:20:30);
select *from workson ;

F. create table dependent(essn int, dependent_name varchar(30),sex char(1),Foreign Key(essn)


references employee(ssn),Primary Key(essn,dependent_name));
desc project;
insert into dependent values(1,"Ramesh",'M');
insert into dependent values(2,"Suresh",'M');
insert into dependent values(3,"Pruthvi",'M');
insert into dependent values(4,"Rama",'M');
insert into dependent values(1,"Karthik",'M');
select *from dependent ;

UPDATE EMPLOYEE SET dno=30 WHERE SSN=1;


UPDATE EMPLOYEE SET SUPERSSN=2,DNO=‘2‘ WHERE SSN=‘2‘;
UPDATE EMPLOYEE SET SUPERSSN=3,DNO=‘2‘ WHERE SSN=‘3‘;
UPDATE EMPLOYEE SET SUPERSSN=4,DNO=‘1‘ WHERE SSN=‘4‘;
UPDATE EMPLOYEE SET SUPERSSN=1,DNO=‘1‘ WHERE SSN=‘5‘;

5. Select ename, address from Employee, department where dname=”ISE” and dno=dnumber;
6. Select E.ename , S.ename from employee E, employee S where E.superssn=S.ssn;
7. Select sum(salary) from Employee;
8. Select dno, count(ssn),AVG(salary) from employee group by dno;

Program 2
2. Consider the following relation schema:
SAILORS (Sid: integer, Sname: string, Rating: integer, Age: real)
BOATS (Bid: integer, Bname: string, Color: string)
RESERVES (sid: integer, Bid: integer, Day:date)
Create above tables by specifying primary key, foreign key and other suitable constraints. Insert
atleast 5 tuples to each created table. Design a database to the satisfy the above requirements and
answer following queries
i. Find all sailors with a rating above 7
ii. Find the names of sailors who have reserved boat number 103
iii. Find the names of sailors who have reserved a red boat
iv. Find the names of sailors who have reserved a red or a green boat

create database Sailor;


use Sailor;
A. create table sailor(Sid int, Sname varchar(15),Rating int,Age real, Primary Key(Sid));
desc sailor;
insert into sailor values(1,"Ajay T",4,34);
insert into sailor values(2,"Ram",5,27);
insert into sailor values(3,"Dev",7,39);
insert into sailor values(4,"Raj",10,22);
insert into sailor values(5,"Raju",9,23);
select *from Sailor ;

B. create table boat(Bid int, Bname varchar(25),color varchar(25), Primary Key(Bid));


desc boat;
insert into boat values(101,"AA","Red");
insert into boat values(102,"BB","Grey");
insert into boat values(103,"CC","Yellow");
insert into boat values(104,"DD","Blue");
insert into boat values(105,"EE","White");
select *from Boat ;

C. create table reserve(Sid int, Bid int, Day date, Primary Key(Sid,Bid),Foreign Key(Sid) references
sailor(Sid),Foreign Key(Bid) references boat(Bid));
desc reserve;
insert into reserve values(1,101,"2021-10-10");
insert into reserve values(2,102,"2021-11-10");
insert into reserve values(3,103,"2021-12-10");
insert into reserve values(4,104,"2021-10-15");
insert into reserve values(5,105,"2021-11-14");
select *from Reserve ;

1. select Sid, Sname from Sailor where rating>7 ;


2. select Sname from Sailor where Bid=103 and Reserve.Sid=Sailor.Sid ;
3. select sailor.Sname from Sailor, Boat, Reserve where Boat.color="Red" and
Boat.Bid=Reserve.Bid and Reserve.Sid=Sailor.Sid ;
4. select sailor.Sname from Sailor, Boat, Reserve where (Boat.color="Red" OR Boat.color="Blue")
and Boat.Bid=Reserve.Bid and Reserve.Sid=Sailor.Sid ;
5. select sailor.Sname from Sailor, Boat, Reserve where (Boat.color="Red" and Boat.color="Blue")
and Boat.Bid=Reserve.Bid and Reserve.Sid=Sailor.Sid ;

Program 3
3. Consider the following relation schema:
STUDENT (Snum: integer, Sname: string, Major: string, Level: string, Age: integer)
CLASS (Cname: string, Meets at: string, Room: string, Fid: integer) ENROLLED (Snum:
integer, Cname: string)
FACULTY (Fid: integer, Fname: string, Deptid: integer)
The meaning of these relations is straightforward; for example, enrolled has one record per
student-class pair such that the student is enrolled in the class. Level is a two character code with
4 different values (example: Junior: JR etc) Write the following queries in SQL. No duplicates
should be printed in any of the answers.
i. Find the names of all Juniors (level = JR) who are enrolled in a class taught by Prof.
Harshith
ii. Find the names of all classes that either meet in room R128 or have five or more Students
enrolled.
iii. Find the names of all students who are enrolled in two classes that meet at the same time.
iv. Find the names of faculty members who teach in every room in which some class is taught.
create database Student4MC19IS040;
use Student4MC19IS040;
A. create table student(Snum int, Sname varchar(15),Major varchar(15),Level varchar(15),Age real,
Primary Key(Snum));
desc student;
insert into student values(1,"Rajat","IS","Third",21);
insert into student values(2,"Rajeeva","CS","Third",21);
insert into student values(3,"Suhas","Mech","Third",21);
insert into student values(4,"Reeshu","Civil","Third",21);
insert into student values(5,"Likhith","EC","Fourth",21);
insert into student values(6,"Hemashree","EC","Second",21);
insert into student values(7,"Sanvitha","EC","First",21);
insert into student values(8,"Anushree","EC","Second",21);
select *from student ;

B. create table faculty(Fid int, Fname varchar(15),Deptid int,Primary Key(Fid));


desc faculty;
insert into faculty values(101,"Nandita",11);
insert into faculty values(102,"Shruti",11);
insert into faculty values(103,"Anand Babu",11);
insert into faculty values(104,"Sudarshan",11);
insert into faculty values(105,"Chandrika",11);
select *from faculty ;

C. create table class(Cname varchar(15), Meetsat varchar(25),Room varchar(25),Fid int, Primary


Key(Cname),Foreign Key(Fid) references faculty(Fid));
desc class;
insert into class values("FAFL","11:00","LH1",101);
insert into class values("DBMS","11:00","LH2",102);
insert into class values("DC","12:00","LH1",103);
insert into class values("IS","11:00","LH1",104);
insert into class values("JAVA","11:00","LH1",105);
select *from class ;

D. create table enrolled(Snum int, Cname varchar(15),Primary Key(Snum,Cname),Foreign


Key(Snum) references student(Snum),Foreign Key(Cname) references class(Cname));
desc enrolled;
insert into enrolled values(1,"FAFL");
insert into enrolled values(2,"DBMS");
insert into enrolled values(3,"DC");
insert into enrolled values(4,"IS");
insert into enrolled values(5,"JAVA");
insert into enrolled values(6,"DBMS");
insert into enrolled values(7,"DBMS");
insert into enrolled values(8,"DBMS");
select *from enrolled ;

1. select student.Sname from student,faculty,class,enrolled where student.Level="First" and


faculty.Fname="Shruti" and student.Snum=enrolled.Snum and enrolled.Cname=class.Cname and
class.Fid=faculty.Fid ;
2. select class.Cname from class where class.Room="LH1" OR Cname in(select enrolled.Cname
from enrolled Group by Cname having count(*)>=5);
3. select student.Sname from student where student.Snum in(Select e1.Snum from enrolled
e1,enrolled e2,class c1,class c2 where e1.Snum=e2.Snum and e1.Cname!=e2.Cname and
e1.Cname=c1.Cname and e2.Cname=c2.Cname and c1.meetsat=c2.meetsat) ;
4. select fname from faculty where fid in(select fid from class group by fid having
count(*)>=(select count(distinct room)from class)) ;

Program 4
4. Consider the relation schema for book dealer database:
AUTHOR (Author-id:int, Name:string, City:string, Country:string)
PUBLISHER (Publisher-id:int, Name:string, City:string, Country:string)
CATALOG (Book-id:int, Title:string, Author-id:int, Publisher-id:int, Category-id:int, Year:int,
Price:int)
CATEGORY (Category-id:int, Description:string)
ORDER-DETAILS (Order-no:int, Book-id:int, Quantity:int)
Create the above tables by properly specifying the primary keys and the foreign keys. Enter
atleast five tuples for each relation.
i. 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.
ii. Find the author of the book which has maximum sales.
iii. Demonstrate how you increase the price of books published by a specific publisher by 10%
iv. List any department that has all its adopted books published by a specific publisher
create database Book040;
use Book040;
A. create table AUTHOR(Authorid int, Name varchar(15),City varchar(15),Country varchar(15),
Primary Key(Authorid));
desc AUTHOR;
insert into AUTHOR values(1,"Ram","Bangalore","India");
insert into AUTHOR values(2,"Shyam","San Fransisco","USA");
insert into AUTHOR values(3,"Bheem","Sydney","Australia");
insert into AUTHOR values(4,"Radhe","Colombo","Srilanka");
insert into AUTHOR values(5,"Alex","Pune","India");
select *from AUTHOR ;

B. create table PUBLISHER(Publisherid int, Name varchar(15),City varchar(15),Country


varchar(15),Primary Key(Publisherid));
desc PUBLISHER;
insert into PUBLISHER values(10,"Hemashree","Bangalore","India");
insert into PUBLISHER values(20,"Sanjana","San Fransisco","USA");
insert into PUBLISHER values(30,"Sanvitha","Colombo","Srilanka");
insert into PUBLISHER values(40,"Anushree","Sydney","Australia");
insert into PUBLISHER values(50,"Kavya","Pune","India");
select *from PUBLISHER ;

C. create table CATEGORY(Categoryid int, Description varchar(15),Primary Key(Categoryid));


desc CATEGORY;
insert into CATEGORY values(100,"Fiction");
insert into CATEGORY values(101,"Non-Fiction");
insert into CATEGORY values(102,"Horror");
insert into CATEGORY values(103,"Action");
insert into CATEGORY values(104,"Mystery");
select *from CATEGORY ;

D. create table CATALOG(Bookid int, Title varchar(50),Authorid int,Publisherid int,Categoryid


int,Year int,Price int,Primary Key(Bookid),Foreign Key(Publisherid) references
PUBLISHER(Publisherid),Foreign Key(Authorid) references AUTHOR(Authorid));
desc CATALOG;
insert into CATALOG values(50,"The girl in room 105",1,10,100,2020,999);
insert into CATALOG values(51,"Half Girlfriend",2,10,101,2021,899);
insert into CATALOG values(52,"Revolution 2020",3,20,102,2019,1999);
insert into CATALOG values(53,"5 point someone",4,30,103,2020,799);
insert into CATALOG values(54,"The secret of Nagas",1,20,104,2021,699);
insert into CATALOG values(55,"The Shiva triology",2,10,102,2018,899);
insert into CATALOG values(56,"Ram: The Scion of Ishvaku",3,30,101,2020,999);
insert into CATALOG values(57,"You are the password of my life",4,50,104,2021,799);
insert into CATALOG values(58,"Everyone has a story",5,50,103,2019,699);
insert into CATALOG values(59,"Everyone has a story-2",5,10,105,2021,999);
select *from CATALOG ;

E. create table ORDERDETAILS(Orderno int, Bookid int,Quantity int,Primary


Key(Orderno),Foreign Key(Bookid) references CATALOG(Bookid));
desc ORDERDETAILS;
insert into ORDERDETAILS values(150,50,10);
insert into ORDERDETAILS values(151,51,10);
insert into ORDERDETAILS values(152,52,10);
insert into ORDERDETAILS values(153,53,10);
insert into ORDERDETAILS values(154,54,10);
insert into ORDERDETAILS values(155,55,10);
insert into ORDERDETAILS values(156,56,10);
insert into ORDERDETAILS values(157,57,10);
insert into ORDERDETAILS values(158,56,10);
insert into ORDERDETAILS values(159,59,10);
insert into ORDERDETAILS values(160,55,10);
Select *from ORDERDETAIL

1. select * from AUTHOR where Authorid in(select Authorid from CATALOG where Year>=2000
and Price>(Select avg (Price) from CATALOG) Group by Authorid having count(*)>=2) ;
2. Select distinct Name from AUTHOR a,ORDERDETAILS o,CATALOG c where
o.quantity=(select max(Quantity) from ORDERDETAILS) and o.Bookid=c.Bookid and
c.Authorid=a.Authorid;
3. update CATALOG set Price=Price*1.1 where Publisherid in(select Publisherid from
PUBLISHER p where p.Name="Hemashree");

Program 5
5. 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 Create the above tables by properly specifying the primary keys and the
foreign keys. Enter atleast five tuples for each relation.
i. List the titles of all movies directed by ‘Hitchcock’.
ii. Find the movie names where one or more actors acted in two or more movies.
iii. List all actors who acted in a movie before 2000 and also in a movie after 2015.
iv. Update rating of all movies directed by ‘Steven Spielberg’ to 5.
create database movie;
use movie;
A. create table actor(act_id int,act_name varchar(30),act_gender varchar(5), primary key (act_id));
insert into actor values(1,'darshan','male');
insert into actor values(2,'punith','male');
insert into actor values(3,'vijay','male');
insert into actor values(4,'vinod','male');

B. create table director(dir_id int,dir_name varchar(20),dir_phone int(11), primary key(dir_id));


insert into director values(1,'rajmouli',456511554);
insert into director values(2,'sunil',996588965);
insert into director values(3,'shankar',984512353);
insert into director values(4,'guru',908443264);

C. create table movies(mov_id int,mov_title varchar(20),mov_year int,mov_lang varchar(10),dir_id


int, primary key (mov_id),foreign key (dir_id) references director(dir_id));
insert into movies values(1,'odeya',2020,'kannada',2);
insert into movies values(2,'rajakumara',2020,'kannada',3);
insert into movies values(3,'badava rascal',2021,'kannada',3);
insert into movies values(4,'tiger',2020,'kannada',4);

D. create table moviecast(act_id int,mov_id int,role varchar(10), foreign key(act_id) references


actor(act_id),foreign key (mov_id) references movies(mov_id));
insert into moviecast values(1,1,'hero');
insert into moviecast values(2,2,'hero');
insert into moviecast values(2,2,'hero');
insert into moviecast values(3,3,'hero');

E. create table rating(mov_id int,rev_stars int(5),foreign key (mov_id) references movies(mov_id));


insert into rating values(1,4);
insert into rating values(2,5);
insert into rating values(3,3);
insert into rating values(4,5);

1. select mov_title from movies m ,director d where d.dir_name="sunil" and d.dir_id = m.dir_id;
2. select mov_title from movies m,moviecast mc where m.mov_id=mc.mov_id and act_id in (select
act_id from moviecast groupby act_id having count (act_id)>1);
3. select m.mov_title, a.act_id, m.mov_year from actor a, movies m, moviecast mc where
m.mov_year not between 2000 and 2015;
4. select mov_title,max(rev_stars) from movies m, rating r where m.mov_id = r.mov_id groupby
mov_title having count(rev_stars)>0 orderby m.mov_title;

Program 6
5. Consider the following database for a banking enterprise
BRANCH (branch-name: String, branch-city: String, assets: real)
ACCOUNTS (accno: int, branch-name: String, balance: real)
DEPOSITOR (customer-name: String, customer-street: String, customer-city: String)
LOAN (loan-number: int, branch-name: String, amount: real)
BORROWER (customer-name: String, loan-number: int)
Create the above tables by properly specifying the primary keys and the foreign keys. Enter
atleast five tuples for each relation.
i.Find all the customers who have at least two accounts at the Main branch.
ii.Find all the customers who have an account at all the branches located in a specific city.
iii.Demonstrate how you delete all account tuples at every branch located in a specific city.

create database Bank040;


use Bank040;
A. create table BRANCH(Branch_name varchar(25), Branch_city varchar(15),Assets REAL,
Primary Key(Branch_name));
desc BRANCH;
insert into BRANCH values("MG ROAD","HASSAN",10000);
insert into BRANCH values("Salagame Road","HASSAN",20000);
insert into BRANCH values("Natraj Road","Sirsi",90000);
insert into BRANCH values("Navarang Road","Bangalore",10000);
insert into BRANCH values("Magadi Road","Bangalore",10000);
insert into BRANCH values("SBM1","Hassan",10000);
select *from BRANCH ;

B. create table ACCOUNTS(Acc_no int, Branch_name varchar(25),Balance REAL,Foreign


Key(Branch_name) references BRANCH(Branch_name),Primary Key(Acc_no));
desc ACCOUNTS;
insert into ACCOUNTS values(10,"MG ROAD",1000);
insert into ACCOUNTS values(11,"MG ROAD",1000);
insert into ACCOUNTS values(12,"Salagame Road",1000);
insert into ACCOUNTS values(505,"SBM1",25000);
select *from ACCOUNTS ;
C. create table CUSTOMER(Customer_name varchar(25), Customer_street varchar(15),
Customer_city varchar(15),Primary Key(Customer_name));
desc CUSTOMER;
insert into CUSTOMER values("Rajat","Salagame","Hassan");
insert into CUSTOMER values("Rajeeva","MG Road","Hassan");
insert into CUSTOMER values("Suhas","SBI colony","Hassan");
insert into CUSTOMER values("Reeshu","Hemavati nagar","Hassan");
insert into CUSTOMER values("Likhith","SMK Layout","Hassan");
insert into CUSTOMER values("Ramesh","Arsikere","Hassan");
select *from CUSTOMER ;

D. create table LOAN(Loan_no int, Branch_name varchar(25),Amount real,Primary


Key(Loan_no),Foreign Key(Branch_name) references BRANCH(Branch_name));
desc LOAN;
insert into LOAN values(100,"MG ROAD",1000);
insert into LOAN values(101,"Salagame Road",1000);
insert into LOAN values(102,"SBM1",1000);
insert into LOAN values(103,"MG ROAD",1000);
select *from LOAN ;

E. create table BORROWER(Customer_name varchar(25), Loan_no int,Foreign


Key(Customer_name) references CUSTOMER(Customer_name),Foreign Key(Loan_no)
references LOAN(Loan_no),Primary Key(Customer_name,Loan_no));
desc BORROWER;
insert into BORROWER values("Rajat",100);
insert into BORROWER values("Ramesh",101);
insert into BORROWER values("Reeshu",102);
insert into BORROWER values("Rajeeva",103);
select *from BORROWER;

F. create table DEPOSITOR(Customer_name varchar(25), Acc_no int,Foreign


Key(Customer_name) references CUSTOMER(Customer_name) on delete Cascade,Foreign
Key(Acc_no) references ACCOUNTS(Acc_no) on delete Cascade,Primary
Key(Customer_name,Acc_no));
desc DEPOSITOR;
insert into DEPOSITOR values("Rajat",10);
insert into DEPOSITOR values("Ramesh",505);
insert into DEPOSITOR values("Reeshu",11);
insert into DEPOSITOR values("Suhas",12);
select *from DEPOSITOR ;

1. select Customer_name from Depositor d,Accounts a where a.acc_no=d.acc_no and


a.branch_name='MG ROAD' group by d.Customer_name having count(*)>=2;
2. select branch_name from branch where branch_city='Hassan';
3. delete from accounts where branch_name in("Hassan");
4. select distinct c.customer_name from accounts a, depositor d,customer c,branch b where
a.acc_no=d.acc_no and d.customer_name=c.customer_name and
b.branch_name=a.branch_name and b.branch_city='hassan';

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy