-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
66 lines (59 loc) · 1.94 KB
/
schema.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
-- Children table
CREATE TABLE children(
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
location TEXT NOT NULL,
status TEXT CHECK(status IN ('naughty', 'nice')) NOT NULL,
birth DATE NOT NULL
);
-- Letters table
CREATE TABLE letters(
id INTEGER PRIMARY KEY AUTOINCREMENT,
child_id INTEGER NOT NULL,
content TEXT NOT NULL,
year INTEGER NOT NULL,
FOREIGN KEY(child_id) REFERENCES children(id)
);
-- Gifts table
CREATE TABLE gifts(
id INTEGER PRIMARY KEY AUTOINCREMENT,
model TEXT NOT NULL,
description TEXT NOT NULL,
quantity INTEGER NOT NULL CHECK(quantity >= 0),
category TEXT CHECK(category IN ('Action Figures & Dolls', 'Educational Toys', 'Plush Toys', 'Outdoor Toys', 'Electronic Toys', 'Board Games & Puzzles', 'Creative Arts & Crafts', 'Musical Instruments')) NOT NULL
);
-- Deliveries connection table
CREATE TABLE deliveries(
id INTEGER PRIMARY KEY AUTOINCREMENT,
gift_id INTEGER NOT NULL,
child_id INTEGER NOT NULL,
year INTEGER NOT NULL,
FOREIGN KEY(child_id) REFERENCES children(id),
FOREIGN KEY(gift_id) REFERENCES gifts(id)
);
-- Create a trigger for updating quantities of gifts
CREATE TRIGGER decrease_quantity
AFTER INSERT ON deliveries
FOR EACH ROW
BEGIN
UPDATE gifts
SET quantity = quantity - 1
WHERE id = NEW.gift_id;
END;
-- Create a view for new letters
CREATE VIEW Letters2024 AS
SELECT children.name AS Name, content AS Letter
FROM letters
JOIN children ON letters.child_id = children.id
WHERE letters.year = 2024;
-- Create a view for who haven't written letters
CREATE VIEW Blank2024 AS
SELECT name AS Name
FROM children
LEFT JOIN letters ON children.id = letters.child_id
WHERE letters.child_id IS NULL;
-- Create index on important sections
CREATE INDEX idx_name ON children(name);
CREATE INDEX idx_year_deliveries ON deliveries(year);
CREATE INDEX idx_year_letters ON letters(year);
CREATE INDEX idx_model ON gifts(model);