-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.sql
executable file
·92 lines (80 loc) · 2.38 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
\c postgres;
DROP DATABASE deawio;
CREATE DATABASE deawio;
\c deawio;
CREATE OR REPLACE FUNCTION auto_modified()
RETURNS TRIGGER AS $$
BEGIN
new.modified = now();
RETURN new;
END;
$$
LANGUAGE plpgsql;
CREATE TABLE product (
product_id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT now(),
modified TIMESTAMPTZ NOT NULL DEFAULT now(),
name VARCHAR(200) NOT NULL,
code VARCHAR(200) NOT NULL,
image_url VARCHAR(1000) NOT NULL,
UNIQUE (name),
UNIQUE (code)
);
CREATE TRIGGER auto_modified
BEFORE UPDATE
ON product
FOR EACH ROW EXECUTE PROCEDURE auto_modified();
CREATE TABLE store (
store_id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT now(),
modified TIMESTAMPTZ NOT NULL DEFAULT now(),
name VARCHAR(200) NOT NULL,
currency VARCHAR(3) NOT NULL,
UNIQUE (name)
);
CREATE TRIGGER auto_modified
BEFORE UPDATE
ON store
FOR EACH ROW EXECUTE PROCEDURE auto_modified();
CREATE TABLE deal (
deal_id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT now(),
modified TIMESTAMPTZ NOT NULL DEFAULT now(),
url VARCHAR(1000) NOT NULL,
high_price FLOAT NOT NULL,
low_price FLOAT NOT NULL,
discount FLOAT NOT NULL,
store_id INT NOT NULL REFERENCES store (store_id) ON DELETE CASCADE,
product_id INT NOT NULL REFERENCES product (product_id) ON DELETE CASCADE,
UNIQUE (url),
UNIQUE (store_id, product_id)
);
CREATE TRIGGER auto_modified
BEFORE UPDATE
ON deal
FOR EACH ROW EXECUTE PROCEDURE auto_modified();
CREATE TABLE guest (
guest_id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT now(),
modified TIMESTAMPTZ NOT NULL DEFAULT now(),
email VARCHAR(100) NOT NULL,
password VARCHAR(100) NOT NULL,
UNIQUE (email)
);
CREATE TRIGGER auto_modified
BEFORE UPDATE
ON guest
FOR EACH ROW EXECUTE PROCEDURE auto_modified();
CREATE TABLE watch (
watch_id SERIAL PRIMARY KEY,
created TIMESTAMPTZ NOT NULL DEFAULT now(),
modified TIMESTAMPTZ NOT NULL DEFAULT now(),
guest_id INT NOT NULL REFERENCES guest (guest_id) ON DELETE CASCADE,
product_id INT NOT NULL REFERENCES product (product_id) ON DELETE CASCADE,
price FLOAT NOT NULL,
UNIQUE (guest_id, product_id)
);
CREATE TRIGGER auto_modified
BEFORE UPDATE
ON watch
FOR EACH ROW EXECUTE PROCEDURE auto_modified();