-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.sql
72 lines (66 loc) · 1.89 KB
/
app.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
CREATE TABLE categoies ( -- 分类
id SERIAL PRIMARY KEY, -- 自增主键
name VARCHAR(20) NOT NULL UNIQUE, -- 分类名称
is_del BOOLEAN NOT NULL DEFAULT FALSE -- 是否删除
);
CREATE TABLE articles ( -- 文章
id SERIAL PRIMARY KEY, -- 自增主键
category_id INT NOT NULL REFERENCES categoies(id), -- 文章所属分类的ID,外键
title VARCHAR(255) NOT NULL, -- 文章标题
content TEXT NOT NULL, -- 文章内容
dateline TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 添加时间
is_del BOOLEAN NOT NULL DEFAULT FALSE -- 是否删除
);
-- 插入示例数据
INSERT INTO categoies (id,name) VALUES
(1,'Rust'), (2,'Go'), (3,'Javascript');
-- 插入更多示例数据
ALTER SEQUENCE categoies_id_seq RESTART WITH 4;
INSERT INTO categoies (name) VALUES
('U3A0CsWdiy'),
('SWACTQFa0Y'),
('GYqfhaKJ6J'),
('0sjsXVArdZ'),
('MiN8lR1g9B'),
('oBorPeyIvH'),
('cqS4jGnmxG'),
('dc0qqvbDNP'),
('jq8K6LgUFy'),
('K1tKtlvzgf'),
('Z5kEYZYEdp'),
('y3K6ryqRMF'),
('hwPu60bq1u'),
('2Idzt9CmAV'),
('vbLGfMJNHz'),
('6tTPkRtpWB'),
('sWBfrpOAIB'),
('zgmXGcYsGt'),
('WH2EBpojIS'),
('m1rsNTknqS');
-- 插入文章的示例数据
INSERT INTO articles (category_id, title,content) VALUES
(1, '标题-GLKUSroPOR', '内容-GLKUSroPOR'),
(1, '标题-hFQRulHJAk', '内容-hFQRulHJAk'),
(2, '标题-pM0TURxhwC', '内容-pM0TURxhwC'),
(1, '标题-svNJmWaqRo', '内容-svNJmWaqRo'),
(3, '标题-8XWiTUSfhB', '内容-8XWiTUSfhB'),
(2, '标题-yvwE32TLkg', '内容-yvwE32TLkg');
CREATE TABLE tags ( -- 标签
id SERIAL PRIMARY KEY, -- 自增主键
name VARCHAR(20) NOT NULL UNIQUE -- 标签名称
);
INSERT INTO tags (name) VALUES
('标签1'),
('标签2'),
('标签3');
CREATE TABLE article_tags ( -- 文章标签
id SERIAL PRIMARY KEY, -- 自增主键
article_id INT NOT NULL REFERENCES articles(id),
tag_id INT NOT NULL REFERENCES tags(id)
);
INSERT INTO article_tags(article_id,tag_id) VALUES
(1, 1),
(2, 1),
(2, 2),
(3, 1),
(1, 3);