-
Notifications
You must be signed in to change notification settings - Fork 0
/
period.sql
95 lines (72 loc) · 2.4 KB
/
period.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
93
94
95
CREATE OR REPLACE SCHEMA period;
USE period;
-- create an application-period table
CREATE OR REPLACE TABLE reservation (
uuid UUID DEFAULT UUID(),
bungalow_name VARCHAR(100) NOT NULL,
client_name VARCHAR(100) NOT NULL,
start_date DATE,
end_date DATE,
PRIMARY KEY (uuid, start_date),
PERIOD FOR reservation (start_date, end_date)
);
ALTER TABLE reservation
ADD UNIQUE unq_reservation (bungalow_name, reservation WITHOUT OVERLAPS)
;
-- make an existing table application-period
CREATE OR REPLACE TABLE reservation (
uuid UUID DEFAULT UUID(),
bungalow_name VARCHAR(100) NOT NULL,
client_name VARCHAR(100) NOT NULL,
start_date DATE,
end_date DATE,
PRIMARY KEY (uuid)
);
ALTER TABLE reservation
ADD PERIOD FOR reservation (start_date, end_date),
ADD UNIQUE unq_reservation (bungalow_name, reservation WITHOUT OVERLAPS)
;
-- demo
INSERT INTO reservation (bungalow_name, client_name, start_date, end_date)
VALUES
('Yellow House', 'John Coltrane', '2023-08-01', '2023-08-15'),
('Yellow House', 'Charles Mingus', '2023-08-16', '2023-08-30'),
('Green House', 'Ornette Coleman', '2023-08-01', '2023-08-30')
;
-- this should fail
INSERT INTO reservation (bungalow_name, client_name, start_date, end_date)
VALUES
('Green House', 'Thelonius Monk', '2023-08-29', '2023-09-30')
;
-- delete the first day of a reservation
DELETE FROM reservation
FOR PORTION OF reservation
FROM '2023-08-01' TO '2023-08-02'
WHERE bungalow_name = 'Yellow House'
;
SELECT * FROM reservation;
-- delete the first 3 days of a resercation
-- by specifying an interval
DELETE FROM reservation
FOR PORTION OF reservation
FROM '2023-08-01' TO ('2023-08-01' + INTERVAL 3 DAY)
WHERE bungalow_name = 'Yellow House'
;
SELECT * FROM reservation;
-- delete 1 dqy in the middle of a reservation
-- NOTE: this is where we get an error if we don't include
-- one of the dates in the primary key
DELETE FROM reservation
FOR PORTION OF reservation
FROM '2023-08-10' TO ('2023-08-10' + INTERVAL 2 DAY)
WHERE bungalow_name = 'Yellow House'
;
SELECT * FROM reservation;
-- update a portion of a reservation
UPDATE reservation
FOR PORTION OF reservation
FROM '2023-08-10' TO ('2023-08-10' + INTERVAL 11 DAY)
SET client_name = 'John Cage'
WHERE bungalow_name = 'Green House'
;
SELECT * FROM reservation WHERE bungalow_name = 'Green House';