Repository navigation
Expand file tree
/
Copy path01-create-shop.sql
More file actions
59 lines (51 loc) · 1.87 KB
/
Copy path01-create-shop.sql
File metadata and controls
59 lines (51 loc) · 1.87 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
-- MiniSQL User Manual: run once in a NEW, empty teaching database.
-- Schema changes are deliberately outside an explicit transaction.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(80) NOT NULL,
city VARCHAR(60),
active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
price DECIMAL(10,2) NOT NULL,
stock INTEGER NOT NULL DEFAULT 0,
CHECK (stock >= 0)
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers(customer_id),
order_status VARCHAR(20) NOT NULL DEFAULT 'new'
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL REFERENCES orders(order_id),
product_id INTEGER NOT NULL REFERENCES products(product_id),
quantity INTEGER NOT NULL CHECK (quantity > 0),
unit_price DECIMAL(10,2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
BEGIN;
INSERT INTO customers(customer_id,name,city,active) VALUES
(1,'Ada','London',TRUE),
(2,'Grace','New York',TRUE),
(3,'Linus','Helsinki',TRUE),
(4,'Margaret',NULL,FALSE);
INSERT INTO products(product_id,name,price,stock) VALUES
(101,'Mechanical Keyboard',79.90,12),
(102,'Optical Mouse',24.50,30),
(103,'USB-C Cable',9.50,100),
(104,'Monitor Stand',39.00,8),
(105,'Notebook',4.50,50);
INSERT INTO orders(order_id,customer_id,order_status) VALUES
(1001,1,'new'),(1002,2,'shipped'),
(1003,1,'paid'),(1004,3,'new');
INSERT INTO order_items(order_id,product_id,quantity,unit_price) VALUES
(1001,101,1,79.90),(1001,103,2,9.50),
(1002,102,2,24.50),(1002,103,1,9.50),
(1003,105,3,4.50),(1004,104,1,39.00);
COMMIT;
SELECT COUNT(*) AS customer_count FROM customers;
SELECT COUNT(*) AS product_count FROM products;
SELECT COUNT(*) AS order_count FROM orders;
SELECT COUNT(*) AS item_count FROM order_items;