-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.sql
67 lines (54 loc) · 1.56 KB
/
db.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
USE todo_list;
CREATE TABLE todo_list(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
description TEXT,
status BOOLEAN DEFAULT FALSE,
date_submit DATETIME NOT NULL
);
ALTER TABLE todo_list CHANGE date_submit date_submit DATETIME DEFAULT NOW();
CREATE TABLE completed(
id INTEGER AUTO_INCREMENT PRIMARY KEY,
count INTEGER NOT NULL
);
DROP TRIGGER update_count_completed_add;
CREATE TRIGGER update_count_completed_add
AFTER UPDATE ON todo_list
FOR EACH ROW
BEGIN
IF NEW.status = 1 AND OLD.status != NEW.status THEN
UPDATE completed SET counts = counts + 1;
END IF;
END;
DROP TRIGGER update_count_completed_drop;
CREATE TRIGGER update_count_completed_drop
AFTER UPDATE ON todo_list
FOR EACH ROW
BEGIN
IF NEW.status = 0 AND OLD.status != NEW.status THEN
UPDATE completed SET counts = counts - 1;
END IF;
END;
DROP TRIGGER delete_count_completed_drop;
CREATE TRIGGER delete_count_completed_drop
AFTER DELETE ON todo_list
FOR EACH ROW
BEGIN
IF OLD.status = 1 THEN
UPDATE completed SET counts = counts - 1;
END IF;
END;
DROP TRIGGER update_date_submit;
CREATE TRIGGER update_date_submit
BEFORE UPDATE ON todo_list
FOR EACH ROW
BEGIN
IF (OLD.title != NEW.title OR OLD.description != NEW.description) AND OLD.status = NEW.status THEN
SET NEW.date_submit = NOW();
END IF;
END;
SELECT * FROM completed;
UPDATE completed SET counts = 0;
SELECT * FROM todo_list;
SHOW VARIABLES LIKE 'log_error';
UPDATE todo_list SET title = 'Title', description = 'New Description' WHERE id = 43;