-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05-ddl-dml-solutions.sql
More file actions
64 lines (56 loc) · 1.39 KB
/
Copy path05-ddl-dml-solutions.sql
File metadata and controls
64 lines (56 loc) · 1.39 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-- Solutions 05: DDL, DML, and Constraints
-- 1.
DROP TABLE IF EXISTS product_discount_targets;
CREATE TABLE product_discount_targets (
product_id VARCHAR(50) PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
target_discount NUMERIC(4, 2) CHECK (target_discount >= 0),
category VARCHAR(50),
active_flag BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 2.
INSERT INTO product_discount_targets (product_id, product_name, target_discount, category)
SELECT DISTINCT
product_id,
product_name,
0.15 AS target_discount,
category
FROM superstore
ORDER BY product_id
LIMIT 10;
-- 3.
ALTER TABLE product_discount_targets
ADD COLUMN notes VARCHAR(255);
-- 4.
UPDATE product_discount_targets
SET notes = 'Review furniture pricing'
WHERE category = 'Furniture';
-- 5.
DELETE FROM product_discount_targets
WHERE category = 'Office Supplies';
-- 6.
INSERT INTO product_discount_targets (
product_id,
product_name,
target_discount,
category,
notes
)
VALUES (
'FUR-BO-10001798',
'Bush Somerset Collection Bookcase',
0.20,
'Furniture',
'Updated by upsert'
)
ON CONFLICT (product_id)
DO UPDATE SET
product_name = EXCLUDED.product_name,
target_discount = EXCLUDED.target_discount,
category = EXCLUDED.category,
notes = EXCLUDED.notes;
-- 7.
SELECT *
FROM product_discount_targets
ORDER BY category, product_name;