-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.sql
43 lines (37 loc) · 1.46 KB
/
update.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
ALTER TABLE inventory
ADD CONSTRAINT foreign_key_inventory
FOREIGN KEY (prod_id) REFERENCES products (prod_id) ON DELETE CASCADE;
ALTER TABLE orderdetail
ADD CONSTRAINT foreign_key_orderdetail_order
FOREIGN KEY (orderid) REFERENCES orders (orderid) ON DELETE CASCADE;
ALTER TABLE orderdetail
ADD CONSTRAINT foreign_key_orderdetail_prod
FOREIGN KEY (prod_id) REFERENCES products (prod_id) ON DELETE CASCADE;
ALTER TABLE orders
ADD CONSTRAINT foreign_key_orders_customerid
FOREIGN KEY (customerid) REFERENCES customers (customerid) ON DELETE CASCADE;
ALTER TABLE imdb_actormovies
ADD CONSTRAINT foreign_key_imdb_actormovies_actorid
FOREIGN KEY (actorid) REFERENCES imdb_actors (actorid) ON DELETE CASCADE;
ALTER TABLE imdb_actormovies
ADD CONSTRAINT foreign_key_imdb_actormovies_movieid
FOREIGN KEY (movieid) REFERENCES imdb_movies (movieid) ON DELETE CASCADE;
ALTER TABLE imdb_actormovies
add primary key (actorid, movieid);
SELECT orderid, prod_id, count(*) as contador
INTO holdkey
FROM orderdetail
GROUP BY orderid, prod_id
HAVING count(*) > 1;
SELECT DISTINCT orderdetail.*
INTO holddups
FROM orderdetail, holdkey
WHERE orderdetail.orderid = holdkey.orderid
AND orderdetail.prod_id = holdkey.prod_id;
DELETE
FROM orderdetail using holdkey
where orderdetail.prod_id=holdkey.prod_id and orderdetail.orderid=holdkey.orderid;
ALTER TABLE orderdetail
add primary key (orderid, prod_id);
drop table holddups;
drop table holdkey;