Вы находитесь на странице: 1из 5

Database Laboratory 4MH08CS013

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)

(i) Create the above tables by properly specifying the primary keys and the foreign keys.

Customer
create table customer
(
cno int primary key,
cname varchar(10) not null,
city varchar(15) not null
);

Order
create table order1
(
ono int primary key,
odate date,
cno int references customer(cno),
oamt int,
);

Item
create table item
(
ino int primary key,
uprice int,
);

Order - Item
create table oitem
(
ono int references order1(ono)
ino int references item(ino)
qty int,
);

Warehouse
create table warehouse
(
wno int primary key,
city varchar(15)
);

Shipment
create table shipment
1
Database Laboratory 4MH08CS013
(
ono int references order1(ono)
wno int references warehouse(wno)
sdate date,
);

customer
insert into customer values(1,'abc','bangalore');
insert into customer values(2,'cde','mysore');
insert into customer values(3,'def','chennai');
insert into customer values(4,'efg','mumbai');
insert into customer values(5,'adf','kolkata');

order1
insert into order1 values(1,'1-01-2006',1,20000);
insert into order1 values(2,'26-03-2006',2,10000);
insert into order1 values(3,'12-06-2006',1,5000);
insert into order1 values(4,'15-09-2006',3,9000);
insert into order1 values(5,'5-j01-2007',4,2500);
insert into order1 values(6,'10-01-2007',4,2400);
insert into order1 values(7,'3-02-2007',5,3500);

item
insert into item values(1,500);
insert into item values(2,300);
insert into item values(3,2500);
insert into item values(4,800);
insert into item values(5,700);

oitem
insert into oitem values(1,1,40);
insert into oitem values(2,1,20);
insert into oitem values(3,3,2);
insert into oitem values(5,3,1);
insert into oitem values(4,2,30);
insert into oitem values(6,4,3);
insert into oitem values(7,5,5);

warehouse
insert into warehouse values(100,'bangalore');
insert into warehouse values(101,'chennai');
insert into warehouse values(102,'mumbai');
insert into warehouse values(103,'kolkata');
insert into warehouse values(104,'mysore');

shipment
insert into shipment values(1,100,'3-01-2006');
insert into shipment values(2,100,'28-03-2006');
insert into shipment values(3,101,'13-06-2006');
insert into shipment values(4,102,'18-09-2006');
insert into shipment values(5,103,'11-01-2007');
insert into shipment values(6,104,'13-01-2007');
2
Database Laboratory 4MH08CS013
insert into shipment values(7,103,'3-03-2007');

(iii) 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 cname as custname,count(ono) as no_of_orders,avg(oamt) as avg_ord_amt from customer


c,order1 o where c.ono=o.ono group by c.cno,cname;

(iv) List the order# for orders that were shipped from all the warehouses that the company has in a
specific city.

select ono from order1 o where not exist ((select wno from warehouse where city='bangalore')
minus (select wno from shipment s where s.ono=o.ono))
and exists (select wno from warehouse where city='bangalore');

(v) Demonstrate the deletion of an item from the ITEM table and demonstrate a method of handling
the rows in the ORDER_ITEM table that contain this particular item.

delete from item where ino=5;

desc customer;
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| customer_id | int(10) | NO | PRI | NULL | |
| name | varchar(15) | YES | | NULL | |
| city | varchar(15) | YES | | NULL | |
+-------------+-------------+------+-----+---------+-------+

desc order;
+---------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+-------+
| order_id | int(10) | NO | PRI | NULL | |
| order_date | date | YES | | NULL | |
| customer_id | int(10) | YES | | NULL | |
| order_ammount | int(10) | YES | | NULL | |
+---------------+---------+------+-----+---------+-------+

desc item;
+------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| item_no | int(10) | NO | PRI | NULL | |
| unit_price | int(10) | YES | | NULL | |
+------------+---------+------+-----+---------+-------+

desc orderitem;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| order_id | int(10) | YES | | NULL | |
| item_no | int(10) | YES | | NULL | |
| qty | int(10) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+

3
Database Laboratory 4MH08CS013
desc warehouse;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| warehouse_id | int(10) | NO | PRI | NULL | |
| city | varchar(15) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

desc shipment;
+--------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| order_id | int(10) | YES | | NULL | |
| warehouse_id | int(10) | YES | | NULL | |
| shipdate | date | YES | | NULL | |
+--------------+---------+------+-----+---------+-------+

select *from customer;


+-------------+------------+----------+
| customer_id | name | city |
+-------------+------------+----------+
| 1 | anup gowda | mumbai |
| 2 | anirudh | mysore |
| 3 | rakshith | banglore |
| 4 | anantha | banglore |
| 5 | meher | bidadi |
+-------------+------------+----------+

select *from orders;


+----------+------------+-------------+---------------+
| order_id | order_date | customer_id | order_ammount |
+----------+------------+-------------+---------------+
| 101 | 2010-12-09 | 1 | 12000 |
| 100 | 2010-12-03 | 1 | 10000 |
| 102 | 2010-12-02 | 2 | 120000 |
| 103 | 2010-12-02 | 4 | 1200 |
| 104 | 2010-01-18 | 1 | 12300 |
+----------+------------+-------------+---------------+

select *from item;


+---------+------------+
| item_no | unit_price |
+---------+------------+
| 1 | 12 |
| 2 | 15 |
| 3 | 105 |
| 4 | 155 |
+---------+------------+

select *from orderitem;


+----------+---------+------+
| order_id | item_no | qty |

4
Database Laboratory 4MH08CS013
+----------+---------+------+
| 101 | 1 | 21 |
| 100 | 2 | 25 |
| 102 | 3 | 50 |
| 103 | 4 | 55 |
| 104 | 5 | NULL |
+----------+---------+------+

select *from warehouse;


+--------------+----------+
| warehouse_id | city |
+--------------+----------+
| 19 | mysore |
| 20 | banglore |
| 21 | manglore |
| 22 | mumbai |
| 23 | mandya |
+--------------+----------+

select *from shipment;


+----------+--------------+------------+
| order_id | warehouse_id | shipdate |
+----------+--------------+------------+
| 101 | 19 | 2008-05-16 |
| 101 | 20 | 2008-06-18 |
| 102 | 21 | 2008-06-18 |
| 103 | 22 | 2008-09-12 |
| 104 | 23 | 2008-10-12 |
+----------+--------------+------------+

1)
select name,count(order_id),avg(order_ammount) from customer c,orders o where
c.customer_id=o.customer_id group by c.customer_id;
+------------+-----------------+--------------------+
| name | count(order_id) | avg(order_ammount) |
+------------+-----------------+--------------------+
| anup gowda | 3 | 11433.3333 |
| anirudh | 1 | 120000.0000 |
| anantha | 1 | 1200.0000 |
+------------+-----------------+--------------------+

2)
mysql> select order_id,w.warehouse_id from warehouse w,shipment s where
s.warehouse_id=w.warehouse_id and city="mysore";
+----------+--------------+
| order_id | warehouse_id |
+----------+--------------+
| 101 | 19 |
+----------+--------------+

Вам также может понравиться