Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create database student;
- use student;
- create table student(snum integer,sname varchar(10),major varchar(2),level varchar(2),age integer,PRIMARY KEY(snum));
- create table faculty(fid integer,fname varchar(20),deptid integer,PRIMARY KEY(fid));
- create table class(cname varchar(20),meets_at varchar(10),room varchar(10),fid integer,PRIMARY KEY(cname),FOREIGN KEY(fid) REFERENCES faculty(fid));
- create table enrolled(snum integer,cname varchar(20),PRIMARY KEY(snum,cname),FOREIGN KEY(snum) REFERENCES student(snum),FOREIGN KEY(cname) REFERENCES class(cname));
- insert into student(snum,sname,major,level,age) values
- (1,'jhon','CS','Sr',19),(2,'smith','CS','Jr',20),(3,'jacob','CV','Sr',20),(4,'tom','CS','Jr',20),(5,'sid','CS','Jr',20),(6,'harry','CS','Sr',21);
- insert into faculty(fid,fname,deptid) values(11,'Harshith',1000),(12,'Mohan',1000),(13,'Kumar',1001),(14,'Shobha',1002),(15,'Shan',1000);
- insert into student(snum,sname,major,level,age) values(7,'Kala','AI','Sr',21);
- insert into class(cname,meets_at,room,fid) values
- ('class1','noon','room1',14),('class10','morning','room128',14),
- ('class2','morning','room2',12),('class3','morning','room3',11),
- ('class4','evening','room4',14),('class5','night','room3',15),
- ('class6','morning','room2',14),('class7','morning','room3',14);
- insert into enrolled(snum,cname) values
- (1,'class1'),(2,'class1'),(3,'class3'),(4,'class3'),(3,'class4'),(5,'class4'),
- (1,'class5'),(2,'class5'),(3,'class5'),(4,'class5'),(5,'class5'),(6,'class5');
- select * from student;
- select * from class;
- select * from enrolled;
- select * from faculty;
- select distinct s.sname from student s,class c,faculty f,enrolled e where s.snum=e.snum and e.cname=c.cname and s.level='Jr' and f.fname='Harshith' and f.fid=c.fid;
- select distinct cname from class where room='room128' or cname in (select e.cname from enrolled e group by e.cname having COUNT(*)>=5);
- INSERT INTO enrolled (snum,cname) VALUES (3,'class2');
- SELECT DISTINCT s.sname FROM student s WHERE s.snum IN (SELECT el.snum FROM enrolled el,enrolled e2,class c1,class c2 WHERE el.snum=e2.snum AND el.cname=c1.cname AND e2.cname=c2.cname AND c1.meets_at=c2.meets_at);
- select f.fname, f.fid from faculty f where f.fid in (select fid from class group by fid having count(*)=(select count(distinct room) from class));
- select f.fname from faculty f WHERE 5>(select count(e.snum) from class c, enrolled e where c.cname=e.cname and c.fid=f.fid);
- Output:
- +------+-------+-------+-------+------+
- | snum | sname | major | level | age |
- +------+-------+-------+-------+------+
- | 1 | jhon | CS | Sr | 19 |
- | 2 | smith | CS | Jr | 20 |
- | 3 | jacob | CV | Sr | 20 |
- | 4 | tom | CS | Jr | 20 |
- | 5 | sid | CS | Jr | 20 |
- | 6 | harry | CS | Sr | 21 |
- | 7 | Kala | AI | Sr | 21 |
- +------+-------+-------+-------+------+
- +---------+----------+---------+------+
- | cname | meets_at | room | fid |
- +---------+----------+---------+------+
- | class1 | noon | room1 | 14 |
- | class10 | morning | room128 | 14 |
- | class2 | morning | room2 | 12 |
- | class3 | morning | room3 | 11 |
- | class4 | evening | room4 | 14 |
- | class5 | night | room3 | 15 |
- | class6 | morning | room2 | 14 |
- | class7 | morning | room3 | 14 |
- +---------+----------+---------+------+
- +------+--------+
- | snum | cname |
- +------+--------+
- | 1 | class1 |
- | 2 | class1 |
- | 3 | class3 |
- | 4 | class3 |
- | 3 | class4 |
- | 5 | class4 |
- | 1 | class5 |
- | 2 | class5 |
- | 3 | class5 |
- | 4 | class5 |
- | 5 | class5 |
- | 6 | class5 |
- +------+--------+
- +-----+----------+--------+
- | fid | fname | deptid |
- +-----+----------+--------+
- | 11 | Harshith | 1000 |
- | 12 | Mohan | 1000 |
- | 13 | Kumar | 1001 |
- | 14 | Shobha | 1002 |
- | 15 | Shan | 1000 |
- +-----+----------+--------+
- +-------+
- | sname |
- +-------+
- | tom |
- +-------+
- +---------+
- | cname |
- +---------+
- | class10 |
- | class5 |
- +---------+
- +-------+
- | sname |
- +-------+
- | jhon |
- | smith |
- | jacob |
- | tom |
- | sid |
- | harry |
- +-------+
- +--------+-----+
- | fname | fid |
- +--------+-----+
- | Shobha | 14 |
- +--------+-----+
- +----------+
- | fname |
- +----------+
- | Harshith |
- | Mohan |
- | Kumar |
- | Shobha |
- +----------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement