0% found this document useful (0 votes)
102 views12 pages

SQL

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 12

create table employee

(fname varchar(15) not null,minit char not null,


lname varchar(15) not null,
ssn char(9) not null,
bdate date,
Address varchar(30),
sex char,
salary float,
superssn char(9),
dno int,
primary key(ssn));

create table department(


dname varchar(15) not null,
dnumber int primary key,
mgrssn char(9),
mgrstartdate date,
unique(dname),
foreign key(mgrssn) references employee(ssn));

create table dept_locations


( dnumber int not null,
dlocation varchar(15) not null,
primary key(dnumber,dlocation),
foreign key(dnumber) references department(dnumber));

create table project


( pname varchar(15) unique,
pnumber int primary key,
plocation varchar(15),
dnum int not null references department(dnumber));

create table works_on


( essn char(9) not null references employee(ssn),
pno int not null,
hours float not null,
primary key(essn,pno),
foreign key(pno) references project(pnumber));

create table dependent


( essn char(9) not null,
dependent_name varchar(15) not null,
sex char,
bdate date,
relationship varchar(8),
primary key(essn,dependent_name),
foreign key(essn) references employee(ssn));

insert into employee values('Alicia','J','Zelaya','999887777',date '1965-09-


23','6357 Windy Lane,Katy,TX','F',28000,Null,4);

insert into employee values('Georgia','L','Gonsalves','999823489',date '1965-02-


24','Dharwad','M',26000,783567423,2);

insert into employee values('Peter','N','Gonsalves','999823356',To_date( '1965-02-


22','yyyy-mm-dd'),'Dandeli','M',24900,783562345,1);

update employee
set sex = 'F' where ssn = 999823489
___________________________________________________________________________________
__________

Termwork-1
----------
create table person
( driver_id varchar(10) primary key,
name varchar(15) not null,
address varchar(20) not null);

create table car


( reg_no varchar(10) primary key,
model varchar(10) not null,
year int not null);

create table accident


( report_no int primary key,
acc_date date not null,
location_date varchar(15));

create table owns


( driver_id varchar (10) not null references person(driver_id),
reg_no varchar(10) not null references car(reg_no),
primary key(driver_id,reg_no));

create table participated


( driver_id varchar(10) not null references person(driver_id),
reg_no varchar(10) not null references car(reg_no),
report_no int null references accident(report_no),
damage_amt int null,
primary key(driver_id,report_no),
unique(reg_no,report_no));

insert into person values('1234','amith','no a-1-12 koppal');


insert into person values('2345','anil','23 vijaya apts');
insert into person values('3412','john','no 3423 vicky apts');
insert into person values('4567','arun','kamal nivas koppal');
insert into person values('4522','sunil','no 54 ravi nagar');

insert into car values('ka37k32','hyundai',2004);


insert into car values('ka05d34','maruti 800',1998);
insert into car values('ka23j90','zen',2002);
insert into car values('ka35f45','fiat',2001);
insert into car values('ka36m78','benz',2000);

insert into accident values(12,'12-feb-1990','vit cross');


insert into accident values(34,'31-jan-1999','jayanagar');
insert into accident values(56,'12-dec-1998','btm layout');
insert into accident values(67,'07-jul-2003','jp nagar');
insert into accident values(87,'01-may-2001','allalsandra');

insert into owns values('1234','ka37k32');


insert into owns values('2345','ka05d34');
insert into owns values('3412','ka23j90');
insert into owns values('4567','ka35f45');
insert into owns values('4522','ka36m78');
insert into participated values('1234','ka37k32',12,12000);
insert into participated values('2345','ka05d34',34,13000);
insert into participated values('3412','ka23j90',56,14000);
insert into participated values('4567','ka35f45',67,12450);
insert into participated values('4522','ka36m78',87,10000);

update participated set damage_amt = 25000 where reg_no = '&reg_no' and report_no =
12;

___________________________________________________________________________________
__________
Termwork-2
-----------
create table customer(custno int,cname varchar(10),city varchar(10),primary
key(custno));

create table order1(orderno int,odate date,custno int,ord_amt int,primary


key(orderno),foreign key(custno) references customer(custno));

create table item(itemno int,unitprice int,primary key(itemno));

create table order_item(orderno int,itemno int,quantity int,primary


key(orderno,itemno),foreign key(orderno) references order1(orderno),foreign
key(itemno) references item(itemno) on delete cascade);

create table warehouse(warehouseno int,city varchar(10),primary key(warehouseno));

create table shipment(orderno int,warehouseno int,ship_date date,primary


key(orderno,warehouseno),foreign key(orderno) references order1(orderno),foreign
key(warehouseno) references warehouse(warehouseno));

insert into customer values(&custno,'&cname','&city');

insert into order1 values(&orderno,'&odate',&custno,&ord_amt);

insert into item values(&itemno,&unitprice);

insert into order_item values(&orderno,&itemno,&quantity);

insert into warehouse values(&warehouseno,'&city');

insert into shipment values(&orderno,&warehouseno,'&ship_date');

___________________________________________________________________________________
___________
create schema 5_Shopping;

#----------------------------------------------------------------------------------
--------------------------------------
create table customer(
c_no int,
c_name varchar(20),
city varchar(20),
constraint pk_c_noCustomer
primary key(c_no));

insert into customer values(101,'Ataa','Bangalore');


insert into customer values(102,'Nawaal','Bangalore');
insert into customer values(103,'Taha','Hyderabad');
insert into customer values(104,'Zaki','Hyderabad');

#----------------------------------------------------------------------------------
--------------------------------------
create table orders(
o_no int,
o_date date,
c_no int,
o_amt int,
constraint pk_o_noOrders
primary key(o_no),
constraint fk_c_noOrders
foreign key(c_no) references customer(c_no));

insert into orders values(1001,'2018-08-10',101,500);


insert into orders values(1002,'2018-08-10',102,700);
insert into orders values(1003,'2018-08-11',101,1200);
insert into orders values(1004,'2018-08-12',102,500);
insert into orders values(1005,'2018-08-13',103,1300);

#----------------------------------------------------------------------------------
--------------------------------------
create table item(
i_no int,
i_price int,
constraint pk_i_noItem
primary key(i_no));

insert into item values(1,500);


insert into item values(2,700);
insert into item values(3,100);
insert into item values(4,900);
insert into item values(5,1000);

#----------------------------------------------------------------------------------
--------------------------------------
create table orderItem(
o_no int,
i_no int,
quantity int,
constraint fk_o_noOrderItem
foreign key(o_no) references orders(o_no),
constraint fk_i_noOrderItem
foreign key(i_no) references item(i_no));

insert into order_Item values(1001,1,1);


insert into order_Item values(1001,2,1);
insert into order_Item values(1002,3,1);
insert into order_item values(1003,4,1);
insert into order_item values(1004,2,2);
insert into order_item values(1005,3,1);

#----------------------------------------------------------------------------------
--------------------------------------
create table warehouse(
w_no int,
city varchar(20),
constraint pk_w_noWarehouse
primary key(w_no));

insert into warehouse values(2001,'Bangalore');


insert into warehouse values(2002,'Delhi');
insert into warehouse values(2003,'Bangalore');
insert into warehouse values(2005,'Dharwad');
insert into warehouse values(2004,'Mangalore');

#----------------------------------------------------------------------------------
--------------------------------------
create table shipment(
o_no int,
w_no int,
s_date date,
constraint pk_o_noShipment
primary key(o_no),
constraint fk_o_noShipment
foreign key(o_no) references orders(o_no),
constraint fk_w_noShipment
foreign key(w_no) references warehouse(w_no));

insert into shipment values(1001,2001,'10-Aug-2018');


insert into shipment values(1002,2001,'10-Aug-2018');
insert into shipment values(1003,2002,'11-Aug-2018');
insert into shipment values(1004,2003,'12-Aug-2018');
insert into shipment values(1005,2001,'13-Aug-2018');

#----------------------------------------------------------------------------------
--------------------------------------
#1. Produce a listing: CUSTNAME, #oforders, AVG_ORDER_AMT, where the middle
# column is the total numbers of orders by the customer and the last column is
the
# average order amount for that customer.

select c_name,count(o_no) as No_of_orders ,avg(o_amt) as Avg_Order_amt


from orders, customer
where
customer.c_no = orders.c_no
group by(c_name);

#----------------------------------------------------------------------------------
--------------------------------------
#2. List the order# for orders that were shipped from all warehouses that the
company has
# in a specific city.
select o_no as Order_Number
from shipment, warehouse
where
shipment.w_no = warehouse.w_no and
warehouse.city = 'Bangalore';

#----------------------------------------------------------------------------------
--------------------------------------
#3. Demonstrate how you delete item# 10 from the ITEM table and make that field
null in
# the ORDER_ITEM table.
alter table orderItem drop foreign key fk_i_noOrderItem;

alter table orderItem


add constraint fk_i_noOrderItem
foreign key(i_no) references item(i_no)
on delete set NULL;

delete from item where i_no = 1;


#----------------------------------------------------------------------------------
--------------------------------------

select * from customer;


select * from orders;
select * from item;
select * from orderitem;
select * from warehouse;
select * from shipment;

insert into employee values('John','B','Smith','123456789','09-Jan-1965','731


Fondren, Houston, TX','M',30000,'3334445555',5);

insert into order1 values(1001,'10-Aug-2018',101,500);


insert into order1 values(1002,'10-Aug-2018',102,700);
insert into order1 values(1003,'11-Aug-2018',101,1200);
insert into order1 values(1004,'12-Aug-2018',102,500);
insert into order1 values(1005,'13-Aug-2018',103,1300);

___________________________________________________________________________________
_____________

a. List the Order# and Ship_date for all orders shipped from Warehouse# "W2".

select Order#, Ship_date


from Shipment
where Warehouse# = 'W2'

b. List the Warehouse information from which the Customer named "Jose Lopez" was
supplied his orders. Produce a listing of Order#, Warehouse#.

select Order.Order#, Shipment.Warehouse#


from Customer, Shipment, Order
where Order.Order# = Shipment.Order#
and Customer.Cust# = Order.Cust#
and Customer.Cname = 'Jose Lopez'

c. Produce a listing: Cname, #ofOrders, Avg_Order_Amt, where the middle column is


the total number of orders by the customer and the last column is the average order
amount for that customer. (Use aggregate functions -- Figure 2.29 on page 64 (5th
ed) is a good example for incorporating multiple aggregate functions on different
attributes but using the same attribute to group results))

select Cname, count(*) as #ofOrders, avg(Ord_Amt) as Avg_Order_Amt


from Customer, Order
where Customer.Cust# = Order.Cust#
group by Cname
d. List the Order# for orders that were shipped from warehouses in New York.

select Shipment.Order#
from Shipment, Warehouse
where Shipment.Warehouse# = Warehouse.Warehouse#
and Warehouse.City = "New York"

e. Delete all orders for customer named "Jose Lopez"

Note: the order is important; delete from Order last.

Delete from Order_Item


where Order# in (select Order.Order#
from Customer, Order
where Customer.Cname = "Jose Lopez"
and Customer.Cust# = Order.Cust#)

Delete from Shipment


where Order# in (select Order.Order#
from Customer, Order
where Customer.Cname = "Jose Lopez"
and Customer.Cust# = Order.Cust#)

Delete from Order


where Order# in (select Order.Order#
from Customer, Order
where Customer.Cname = "Jose Lopez"
and Customer.Cust# = Order.Cust#)

f. Move the shipping date by a week for all those orders originating from
warehouses
in Baltimore.

update Shipment
set Ship_date = Ship_date + 7
where Shipment.Warehouse# in (select Warehouse.Warehouse# from Warehouse where
City = "Baltimore")

g. List all items that have a price greater than the average price.

select Item#
from Item
where Unit_Price > all (select avg(Unti_Price) as Unit_Price
from Item)

Note: we are using > all construct here and the word "all" is needed and not
optional,
even though the subquery results in a single value relation.

h. Find out the maximum number of orders shipped out of warehouses in Baltimore.

select max(Num_Orders)
from (Select Warehouse#, count(#Orders)
from Shipment as S, Warehouse as W
where S.Warehouse# = W.Warehouse# and W.City = "Baltimore"
group by Warehouse#) as WOrders (Warehouse#, Num_Orders)

i. Find the item with the maximum unit price.


Select Item#
from Item
where Unit_Price in (select max(Unit_Price) as Unit_Price
from Item)

j. List all customer names whose orders were shipped from a warehouse in the same
city as they live in.

Select Cname
from Customer as C, Order as O, Shipment as S, Warehouse as W
where C.Cust# = O.Cust#
and O.Order# = S.Order#
and S.warehouse# = W.warehouse#
and C.City = W.City

___________________________________________________________________________________
____________________________________________________

Termwork -3

create table student(regno varchar2(12) primary key,name varchar2(12),major


varchar2(12),bdate date);
create table course(courseno number(6)primary key,cname varchar2(10),dept
varchar2(12));
create table enroll(regno varchar2(12)references student(regno),courseno number(6)
references course(courseno),sem number(6),marks number(6,2));
create table text(bookisbn number(sekect5)primary key,booktitle
varchar2(15),publisher varchar2(12),author varchar2(12));
create table bookadoption(courseno number(6)references course(courseno),sem
number(5),bookisbn number(5)references text(bookisbn));

insert into student values('&regno','&name','&major','&bdate');


insert into course values('&courseno','&cname','&dept');
insert into enroll values('&regno','&course','&sem','&marks');
insert into text values('&bookisbn','&booktitle','&publisher','&author');
insert into bookadoption values('&course','&sem','&bookisbn');

INSERT INTO STUDENT VALUES('1BM02IS012','ANSHUMAN ATRI','DATBASE','15-JAN-84');


INSERT INTO STUDENT VALUES('1BM02CS012','A MNC','DMS','25-FEB-84');
INSERT INTO STUDENT VALUES('1BM02TC012','TELE TECHNO','SSDT','11-DEC-84');
INSERT INTO STUDENT VALUES('1BM02EE012','ELCTRA','POWER GEN','1-APR-84');
INSERT INTO STUDENT VALUES('1BM02EC012','GG','POWER ELE','5-NOV-84');

INSERT INTO COURSE VALUES(1,'DATABASE','IS');


INSERT INTO COURSE VALUES(2,'DMS','IS');
INSERT INTO COURSE VALUES(3,'SSDT','TC');
INSERT INTO COURSE VALUES(4,'POWER GEN','EE');
INSERT INTO COURSE VALUES(5,'POWER ELE','EC');

INSERT INTO TEXT VALUES(1,'DATABASE A SYSTEMATIC APPROACH','JOHN WILEY','R ASHOK


KUMAR');
INSERT INTO TEXT VALUES(2,'DMS FOR DUMMIES','PEARSON','MADHUPRIYA');
INSERT INTO TEXT VALUES(3,'SSDT NO ONE CAN TEACH BETTER','PEARSON','GAURA');
INSERT INTO TEXT VALUES(4,'POWER GENERATION BIBLE','TMH','MEENA');
INSERT INTO TEXT VALUES(5,'POWER OF POWER ELECTRONICS','O REILLY','GG THE GREAT');

INSERT INTO ENROLL VALUES('1BM02IS012',1,5,98);


INSERT INTO ENROLL VALUES('1BM02CS012',2,3,88);
INSERT INTO ENROLL VALUES('1BM02TC012',3,5,88);
INSERT INTO ENROLL VALUES('1BM02EE012',4,5,88);
INSERT INTO ENROLL VALUES('1BM02EC012',5,5,88);

INSERT INTO BOOKADOPTION VALUES(1,5,1);


INSERT INTO BOOKADOPTION VALUES(2,3,2);
INSERT INTO BOOKADOPTION VALUES(3,5,3);
INSERT INTO BOOKADOPTION VALUES(4,5,4);
INSERT INTO BOOKADOPTION VALUES(5,5,5);

3)insert into text values(&bookisbn,'&booktitle','&publisher','&author');


insert into bookadoption values('&course','&sem','&bookisbn');

4)select distinct(c.course),c.cname,b.bookisbn,t.booktitle
from course c,textt,bookadoption b
where c.course=b.course and b.bookisbn=t.bookisbn and c.dept='&dept'
and c.course in(select course
from bookadoption
group by course
having count(course)>=2)
order by c.cname;

5) select dept
from((select c.dept,count(b.bookisbn)
from course c,bookadoption b
where c.course=b.course
group by c.dept)
intersect
(select c.dept,count(b.bookisbn)
from course c,bookadoption b,text t
where c.course=b.course and b.bookisbn=t.bookisbn and t.publisher='&publisher'
group by c.dept));

============================================================
CREATE TABLE STUDENT(
2 REGNO VARCHAR(10),
3 NAME VARCHAR(10),
4 MAJOR VARCHAR(10),
5 BDATE DATE,
6 PRIMARY KEY(REGNO));

CREATE TABLE COURSE(


2 COURSENO INT,
3 CNAME VARCHAR(10),
4 DEPT VARCHAR(10),
5 PRIMARY KEY(COURSENO));

CREATE TABLE TEXT(


2 BOOKISBN INT,
3 TITLE VARCHAR(20),
4 PUBLISHER VARCHAR(20),
5 AUTHOR VARCHAR(20),
6 PRIMARY KEY(BOOKISBN));

CREATE TABLE ENROLL(


2 REGNO VARCHAR(10),
3 COURSENO INT,
4 SEM INT,
5 MARKS INT,
6 PRIMARY KEY(REGNO,COURSENO,SEM),
7 FOREIGN KEY(REGNO) REFERENCES STUDENT(REGNO) ON DELETE SET NULL,
8 FOREIGN KEY(COURSENO) REFERENCES COURSE(COURSENO) ON DELETE SET NULL);

CREATE TABLE BOOKADOPTION(


2 COURSENO INT,
3 SEM INT,
4 BOOKISBN INT,
5 PRIMARY KEY(COURSENO,SEM),
6 FOREIGN KEY(COURSENO) REFERENCES COURSE(COURSENO) ON DELETE SET NULL,
7 FOREIGN KEY(BOOKISBN) REFERENCES TEXT(BOOKISBN) ON DELETE SET NULL);

3)SELECT C.COURSENO COURSE_NO,T.BOOKISBN


2 BOOKISBN,T.TITLE BOOK_TITLE
3 FROM BOOKADOPTION B,TEXT T,COURSE C
4 WHERE B.BOOKISBN=T.BOOKISBN AND
5 B.COURSENO=C.COURSENO AND C.DEPT='COMPUTERS'
6 GROUP BY C.COURSENO,T.BOOKISBN,T.TITLE
7 HAVING COUNT(B.COURSENO)>2;

4) SELECT DISTINCT DEPT


2 FROM COURSE
3 WHERE COURSENO IN(SELECT COURSENO
4 FROM BOOKADOPTION
5 WHERE BOOKISBN IN(SELECT BOOKISBN
6 FROM TEXT
7
WHERE PUBLISHER='PEARSON')
8 );

___________________________________________________________________________________
_______________________________________________________________________________

Termwork-4

create table author(author_id int,name varchar(10),city varchar(10),country


varchar(10),primary key(author_id));
create table publisher(publisher_id int,name varchar(10),city varchar(10),country
varchar(10),primary key(publisher_id));
create table category(category_id int,description varchar(10),primary
key(category_id));
create table catalog(book_id int,title varchar(10),author_id int,publisher_id
int,category_id int,year int,price int,primary key(book_id),foreign key(author_id)
references author(author_id),foreign key(publisher_id) references
publisher(publisher_id),foreign key(category_id) references category(category_id));
create table order_details(order_no int,book_id int,quantity int,primary
key(order_no,book_id),foreign key(book_id) references catalog(book_id));

insert into author values(&author_id,'&name','&city','&country');


insert into publisher values(&publisher_id,'&name','&city','&country');
insert into category values(&category_id,'&description');
insert into catalog
values(&book_id,'&title',&author_id,&publisher_id,&category_id,&year,&price);
insert into order_details values(&order_no,&book_id,&quantity);

___________________________________________________________________________________
_________________________________________________________________________________

Termwork-5

create table Bank( Bank_Code int, Name varchar(10),address varchar(15), primary key
(Bank_Code));
create table Branch(Branch# int,Name varchar(10),address varchar(15), Bank_Code int
references Bank(Bank_Code), primary key(Bank_Code,Branch#));
create table Customer1(customer# int, Name varchar(10),telephone# int,address
varchar(20),primary key(customer#));
create table Account(acc# int, acc_type varchar(10),Branch# int, Bank_Code,primary
key(acc#),Foreign key (Branch#,Bank_Code) references Branch);
create table Loan(loan# int, loan_type varchar(10),Branch# int, Bank_Code
int,primary key(loan#),Foreign key (Branch#,Bank_Code) references Branch);
create table Availed_by(loan# int references Loan(loan#),customer# references
Customer1(customer#),primary key(loan#,customer#));
create table Held_by(acc# int references Account(acc#),customer# int references
Customer1(customer#),primary key(acc#, customer#));

insert into Bank values(11,'RBI','Karwar');


insert into Bank values(12,'SBI','Karwar');
insert into Bank values(13,'Union','Karwar');
insert into Bank values(14,'Syndicate','Karwar');
insert into Bank values(15,'Canara','Karwar');

insert into Branch values(111,'RBI-South','Karwar',11);


insert into Branch values(112,'SBI-South','Karwar',12);
insert into Branch values(113,'Union-S','Karwar',13);
insert into Branch values(114,'Syndi-S','Karwar',14);
insert into Branch values(115,'Canara-S','Karwar',15);

insert into Customer1 values(1,'Shivani',08382,'Karwar');


insert into Customer1 values(2,'Rohit',21218,'Karwar');
insert into Customer1 values(3,'Manik',72882,'Belgaum');
insert into Customer1 values(4,'Nimisha',87292,'Dharwad');
insert into Customer1 values(5,'Sai',98372,'Bengaluru');

insert into Account values(221,'Saving',111,11);


insert into Account values(323,'Joint',112,12);
insert into Account values(543,'Deposit',112,12);
insert into Account values(876,'Joint',113,13);
insert into Account values(476,'Saving',115,15);

insert into Loan values(21,'A');


insert into Loan values(22,'B');
insert into Loan values(23,'C');
insert into Loan values(24,'X');
insert into Loan values(25,'Y');

insert into Availed_by values(22,3);


insert into Availed_by values(24,1);
insert into Availed_by values(25,4);
insert into Availed_by values(21,2);
insert into Availed_by values(21,5);

insert into held_by values(221,1);


insert into held_by values(221,3);
insert into held_by values(323,2);
insert into held_by values(476,4);
insert into held_by values(876,5);

You might also like