-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_setup.sql
82 lines (75 loc) · 2.91 KB
/
db_setup.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
DROP TABLE IF EXISTS users CASCADE;
CREATE TABLE users (
id SERIAL PRIMARY KEY, -- 使用 SERIAL 自增主键
username VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
sex CHAR(1) CHECK (sex IN ('M', 'F', 'O')),
age INTEGER CHECK (age >= 0),
is_admin BOOLEAN DEFAULT FALSE
);
DROP TABLE IF EXISTS books CASCADE;
CREATE TABLE books (
id SERIAL PRIMARY KEY,
isbn VARCHAR(255) UNIQUE NOT NULL,
title VARCHAR(255) NOT NULL,
author VARCHAR(255) NOT NULL,
press VARCHAR(255) NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity >= 0), -- 确保数量非负
sale_price NUMERIC(10, 2) CHECK (sale_price >= 0) -- 允许销售价格为 NULL
);
DROP TABLE IF EXISTS imports CASCADE;
CREATE TABLE imports (
id SERIAL PRIMARY KEY,
book_id INTEGER NOT NULL,
cost_price NUMERIC(10, 2) NOT NULL CHECK (cost_price >= 0), -- 使用 cost_price 代替 purchase_price,确保成本价非负
quantity INTEGER NOT NULL CHECK (quantity >= 0), -- 确保数量非负
is_paid BOOLEAN DEFAULT FALSE,
FOREIGN KEY (book_id) REFERENCES books(id)
);
DROP TABLE IF EXISTS transactions CASCADE;
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
book_id INTEGER NOT NULL,
transaction_time TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW(), -- 自动记录交易时间
quantity INTEGER NOT NULL CHECK (quantity > 0), -- 交易数量必须为正
balance_change NUMERIC(10, 2) NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id), -- 假设已有 users 表
FOREIGN KEY (book_id) REFERENCES books(id) -- 假设已有 books 表
);
CREATE OR REPLACE FUNCTION fn_book_sale()
RETURNS TRIGGER AS $$
DECLARE
current_user_id INTEGER;
BEGIN
SELECT current_setting('myapp.user_id') INTO current_user_id;
INSERT INTO transactions (user_id, book_id, transaction_time, quantity, balance_change)
VALUES (current_user_id, NEW.id, NOW(), OLD.quantity - NEW.quantity, (OLD.quantity - NEW.quantity) * NEW.sale_price);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_book_sale ON books;
CREATE TRIGGER trg_book_sale
AFTER UPDATE OF quantity ON books
FOR EACH ROW
WHEN (OLD.quantity > NEW.quantity)
EXECUTE PROCEDURE fn_book_sale();
CREATE OR REPLACE FUNCTION fn_book_import()
RETURNS TRIGGER AS $$
DECLARE
current_user_id INTEGER;
BEGIN
-- 获取当前设置的用户 ID
current_user_id := current_setting('myapp.user_id')::INTEGER;
INSERT INTO transactions (user_id, book_id, transaction_time, quantity, balance_change)
VALUES (current_user_id, NEW.book_id, NOW(), NEW.quantity, -NEW.quantity * NEW.cost_price);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_book_import ON imports;
CREATE TRIGGER trg_book_import
AFTER UPDATE OF is_paid ON imports
FOR EACH ROW
WHEN (OLD.is_paid IS FALSE AND NEW.is_paid IS TRUE)
EXECUTE PROCEDURE fn_book_import();