0% found this document useful (0 votes)
39 views9 pages

Dbms Lab 2

Uploaded by

vvce22cse0114
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)
39 views9 pages

Dbms Lab 2

Uploaded by

vvce22cse0114
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/ 9

1.

COMPANY DATABASE

Create table Depts (Dname varchar(20),Dno int Primary Key,Mgrstrdate date);

Create table Emp(SSN int Primary Key, FName varchar(20),LName varchar(20), Address varchar(20),
Sex char(1), Salary int,superssn references emp(ssn) on delete cascade,Dno references Depts(Dno)
on delete cascade);

Alter table Depts add Mgrssn references Emp(SSN) on delete set NULL;

Create table Deptloc ( Dno references Depts(dno) , Dloc varchar(20));

Create table Project (Pno int Primary Key , Pname varchar(20), Ploc varchar(20), Dno references
Depts(dno));

Create table Work(SSN references Emp(SSN), Pno references Project(pno), hours number(2,2));

Insert into Depts(Dname,Dno,Mgrstrdate)values('accounts',1,'12-AUG-98');

Insert into Depts(Dname,Dno,Mgrstrdate)values('research',2,'24-JAN-00');

Insert into Depts(Dname,Dno,Mgrstrdate)values('production',3,'14-FEB-09');

Insert into Depts(Dname,Dno,Mgrstrdate)values('sales',4,'15-MAR-00');

Insert into Depts(Dname,Dno,Mgrstrdate)values('testing',5,'23-APR-02');

Insert into Emp values('111','james','scott','mysore','m','123456','111',1);

Insert into Emp values('222','ramesh','Aravind','banglore','m','345672','111',1);

Insert into Emp values('333','David','hush','honlulu','m','70000','222',1);

Insert into Emp values('444','kavya','bind','guwahati','f','750000','444',1);

Insert into Emp values('555','hashmi','khan','mumbai','f','450000','333',1);

Insert into Emp values('666','pinky','khanna','punjab','f','560000','666',1);

Insert into Emp values('777','kushal','kundan','assam','m','35000','555',4);

Insert into Emp values('888','arijith','gundal','assam','m','660000','444',5);


Insert into Deptloc values('1','banglore');

Insert into Deptloc values('2','mysore');

Insert into Deptloc values('3','hubli');

Insert into Deptloc values('4','punjab');

Insert into Deptloc values('5','guwahati');

Insert into Project values('10','P1','bangalore',1);

insert into Project values('20','P2','mysore',3);

insert into Project values('30','P3','mumbai',4);

insert into Project values('40','P4','assam',4);

insert into Project values('50','IOT','punjab',5);

insert into Project values('60','P6','Honululu',1);

UPDATE Depts set Mgrssn=333 where Dno=1;

UPDATE Depts set Mgrssn=111 where Dno=2;

UPDATE Depts set Mgrssn=444 where Dno=3;

UPDATE Depts set Mgrssn=555 where Dno=4;

UPDATE Depts set Mgrssn=777 where Dno=5;

alter table Work

drop column hours;

alter table Work add hours int;

insert into Work values(111,50,5);

insert into Work values(222,50,10);

insert into Work values(333,50,7);

insert into Work values(444,20,8);

insert into Work values(555,10,3);

insert into Work values(666,30,1);

insert into Work values(777,40,12);


QUERIES

Select distinct p.pno from Project p,Depts d,Emp e where e.dno=d.dno and d.dno=p.dno and
(e.lname='scott' or d.mgrssn in (select ssn from Emp where lname='scott'));

select e.ssn,e.fname,e.lname,e.salary*1.10 as new_salary

from emp e,work w,project p

where p.pname='IOT' and e.ssn = w.ssn and w.pno=p.pno;

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

from Emp E,Depts D

where dname='accounts' AND

E.dno=D.dno;

Select e.fname,e.lname

from Emp e

where Not Exists ((select pno from Project where dno='5')minus(select pno from Work where
e.ssn=ssn));

select d.dno,d.dname,e.fname,e.lname, count(*) as noofemp

from Emp e ,Depts d

where e.dno=d.dno and e.salary>600000 and d.dno in(select e1.dno from Emp e1 group by e1.dno
having count(*)>5) group by d.dno,d.dname,e.fname,e.lname;
2.AIRLINE DATABASE

create table flights(flno int primary key, source varchar(20), destination varchar(20), distance int,
departs int, arrives int, price int);

create table aircraft(aid int primary key, aname varchar(20), cruisingrange int);

create table employees(eid int primary key, ename varchar(20), salary int);

create table certified(eid references employees(eid) on delete cascade, aid references aircraft(aid) on
delete cascade);

insert into flights values(101,'mysuru','banglore',140,9,10,5000);

insert into flights values(102,'mysuru','hassan',120,13,14,4500);

insert into flights values(103,'mysuru','manglore',600,6,9,15000);

insert into flights values(104,'mysuru','ballary',750,7,12,19500);

insert into flights values(105,'banglore','hyderabad',850,12,16,20000);

insert into aircraft values(1001,'kingfisher',2500);

insert into aircraft values(2002,'deccan',2800);

insert into aircraft values(3003,'dishan',3000);

insert into aircraft values(4004,'goibibo',750);

insert into aircraft values(5005,'local',900);

insert into employees values(11,'shivakumar',150000);

insert into employees values(12,'bhaskar',75000);

insert into employees values(13,'dishankrishna',200000);

insert into employees values(15,'vishnu',80000);

insert into employees values(20,'dhanu',90000);

insert into certified values(11,3003);

insert into certified values(11,1001);

insert into certified values(11,2002);

insert into certified values(13,4004);

insert into certified values(12,2002);


select * from flights;

select * from aircraft;

select * from employees;

select * from certified;

QUERIES

select a.aname

from aircraft a,certified c, employees e

where a.aid=c.aid and c.eid=e.eid and

e.salary>80000;

select c.eid,max(cruisingrange)

as max_cruisingrange

from aircraft a,employees e,certified c

where a.aid=c.aid and e.eid=c.eid

group by c.eid

having max(cruisingrange)>3;

select a.aname,avg(salary)

from employees e,certified c,aircraft a

where c.eid=e.eid and a.aid=c.aid and a.cruisingrange>1000

group by a.aname;
(select ename from employees e,certified c where c.aid in

(select a.aid from aircraft a,certified c where a.aid not in

(select aid from aircraft where aname='boeing' or cruisingrange=3000)

group by a.aid)

and e.eid=c.eid)

MINUS

(select ename from employees e,certified c where c.aid in

(select a.aid from aircraft a,certified c where a.aid in

(select aid from aircraft where aname='boeing')

group by a.aid)

and e.eid=c.eid);

select eid, ename, salary

from employees

where salary>(select avg(salary)

from employees) and eid

not in(select eid from certified);


3.COLLEGE DATABASE

create table student(snum int primary key, sname varchar(20), major varchar(20), levels varchar(20),
age int);

create table faculty(fid int primary key, fname varchar(20), deptid int);

create table class(cname varchar(20) primary key, meets_at varchar(20), room varchar(20), fid
references faculty(fid) on delete cascade);

create table enrolled(snum references student(snum) on delete cascade, cname references


class(cname) on delete cascade);

insert into student values(1, 'Andy','CSE','JR',21);

insert into student values(2, 'Helen','Arts','JR',19);

insert into student values(3, 'Bob','Psychology','GR',23);

insert into student values(4, 'Pham','History','SR',20);

insert into student values(5, 'Zola','CSE','SR',23);

insert into student values(6, 'Abhi','CSE','JR',21);

insert into student values(7, 'Tara','Arts','JR',18);

insert into class values('HIS38','12:00','R123',34);

insert into class values('CS293','12:00','R124',23);

insert into class values('CS262','2:00','R125',24);

insert into class values('PY331','9:00','R126',27);

insert into class values('AR32','9:00','R127',36);

insert into class values('AR36','10:00','R128',31);

insert into class values('PY332','8:00','R128',22);

insert into class values('HIS37','5:00','R128',34);

insert into class values('HIS34','4:00','R128',31);

insert into class values('CS289','2:00','R128',22);


insert into faculty values(22,'Kera',88);

insert into faculty values(23,'Nivali',77);

insert into faculty values(24,'Ravi',33);

insert into faculty values(27,'Lou',66);

insert into faculty values(31,'Jane',44);

insert into faculty values(34,'Payne',55);

insert into faculty values(36,'Rakesh',22);

insert into enrolled values(1,'CS262');

insert into enrolled values(1,'AR32');

insert into enrolled values(2,'AR32');

insert into enrolled values(2,'PY331');

insert into enrolled values(3,'PY331');

insert into enrolled values(3,'HIS34');

insert into enrolled values(4,'HIS38');

insert into enrolled values(5,'CS293');

insert into enrolled values(5,'HIS38');

insert into enrolled values(6,'CS262');

QUERIES

select distinct s.snum,s.sname

from student s, class c, enrolled e,faculty f

where s.snum=e.snum and e.cname=c.cname and c.fid=f.fid and f.fname='Rakesh' and s.levels='JR'

order by snum;

select max(s.age) as age

from student s

where(s.major='History') or s.snum in(select e.snum

from class c, enrolled e,faculty f

where e.cname=c.cname and c.fid=f.fid and f.fname='Ravi');


select distinct s.sname

from student s

where s.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.meets_at=c2.meets_at);

select DISTINCT f.fname,count(*) as coursecount

from class c,faculty f

where c.fid NOT IN(select fid

from class

where room IN(select room

from class

where room!='R128')) and

c.fid=f.fid

group by f.fname;

create view StudentCourseDetails as

select s.snum, s.sname, s.major, s.levels, s.age, e.cname

from student s,enrolled e

where s.snum=e.snum;

select * from StudentCourseDetails;

You might also like