Prog 2
Prog 2
Prog 2
Consider the following relations for an order processing database application in a company:
CUSTOMER(cust#:int,cname#:string,city:string)
ORDER(order#:int,odate:date,cust#:int,ord-amt:int)
ORDER_ITEM(order#:int,item#:int,qty:int)
ITEM(item#:int,unit-price:int)
SHIPMENT(order#:int,warehouse#:int,ship-date:date)
WAREHOUSE(warehouse#:int,city:string)
1)create the tables by properly specifying the primary keys and the forign keys..
2)enter atleast five tuples for each relation.
3)produce a listing:CUSTNAME,#oforders,AVG_ORDER_AMT,where the middle column is the
total number of orders by the customer and the last colum is the average
order amount for that customer.
4)list the order# for orders that were shipped from all the warehouse that the company has in a
specific city.
5)demonstrate how you delete item# 10 fromthe ITEM table and make that field null in the
ORDER_ITEM table.
TABLE CREATION:
1.CUSTOMER TABLE:
2.ORDER TABLE:
3.ORD_-ITEM TABLE:
SQL>CREATE TABLE ORDERITEM99
(
ORDER_ID INT,
ITEM_ID NUMBER(5),
QTY NUMBER(5),
PRIMARY KEY(ORDER_ID, ITEM_ID),
FOREIGN KEY(ORDER_ID) REFERENCES ORDER99(ORDER_ID) ,
FOREIGN KEY(ITEM_ID) REFERENCES ITEM99(ITEM_ID)
);
4.ITEM TABLE:
5.SHIPMENT TABLE:
6.WAREHOUSE TABLE:
TABLE FILLING:
1.INSERT TO CUSTOMER
2.INSERT TO ORDER
4.INSERT TO ITEM
QUERIES:
//1.PRODUCE A LISTING
SOLUTION:
SQL>SELECT CNAME,COUNT(*),AVG(ORDAMT)
FROM CUSTOMER99,ORDER99
WHERE CUSTOMER99.CUST_ID=ORDER99.CUST_ID
GROUP BY CNAME;
//2.LIST THE ORDERS THAT WERE SHIPPED FROM ALL THE WAREHOUSES THAT
THE COMPANY HAS IN A
//SPECIFIC CITY
SOLUTION:
SQL>SELECT ORDER_ID
FROM SHIPMENT99,WAREHOUSE99
WHERE SHIPMENT99.WAREHOUSE_ID=WAREHOUSE99.WAREHOUSE_ID AND
CITY='DELHI';
ORDER_ID
-------------
10
SQL>SELECT ORDER_ID
FROM SHIPMENT99,WAREHOUSE99
WHERE SHIPMENT99.WAREHOUSE_ID=WAREHOUSE99.WAREHOUSE_ID AND
CITY='CHENNAI';
ORDER_ID
-------------
12
//3.DELETE FROM THE ITEM TABLE AND MAKE THE FIELD NULL IN THE
ORD_ITEM TABLE
SOLUTION:
ITEM_ID UNITPRICE
-------- ----------
991 990
950 700
800 1600
1200 2000
990 500
ITEM_ID UNITPRICE
----- ----------
991 990
950 700
800 1600
1200 2000