-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.sql
140 lines (123 loc) · 5.46 KB
/
init.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
CREATE TABLE IF NOT EXISTS role -- 角色表
(
RoleID INT PRIMARY KEY AUTO_INCREMENT,
RoleName char(20) NOT NULL UNIQUE,
WriteBlog bit DEFAULT 0, -- 写博客
WriteComment bit DEFAULT 1, -- 写评论
WriteMsg bit DEFAULT 1, -- 写留言
CreateUser bit DEFAULT 0, -- 创建新用户
ReadBlog bit DEFAULT 1, -- 读博客
ReadComment bit DEFAULT 1, -- 读评论
ReadMsg bit DEFAULT 1, -- 读留言
ReadSecretMsg bit DEFAULT 0, -- 读私密留言
ReadUserInfo bit DEFAULT 0, -- 读取用户信息
DeleteBlog bit DEFAULT 0, -- 删除博客
DeleteComment bit DEFAULT 0, -- 删除评论
DeleteMsg bit DEFAULT 0, -- 删除留言
DeleteUser bit DEFAULT 0, -- 删除用户
ConfigureSystem bit DEFAULT 0, -- 配置系统
ReadSystem bit DEFAULT 0 -- 读系统信息
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS user -- 创建用户表
(
ID INT PRIMARY KEY AUTO_INCREMENT,
Email char(32) NOT NULL UNIQUE,
PasswdHash char(128) NOT NULL,
Role INT NOT NULL DEFAULT 3,
FOREIGN KEY (Role) REFERENCES role (RoleID)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
INSERT INTO role (RoleID,
RoleName,
WriteBlog,
WriteComment,
WriteMsg,
CreateUser,
ReadBlog,
ReadComment,
ReadMsg,
ReadSecretMsg,
ReadUserInfo,
DeleteBlog,
DeleteComment,
DeleteMsg,
DeleteUser,
ConfigureSystem,
ReadSystem)
VALUES (1, 'Admin', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), -- 管理员用户
(2, 'Coordinator', 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1); -- 协管员用户
INSERT INTO role (RoleID, RoleName)
VALUES (3, 'Default'); -- 默认用户
INSERT INTO role (RoleID, RoleName, WriteComment, WriteMsg)
VALUES (4, 'Anonymous', 0, 0); -- 默认用户
CREATE TABLE IF NOT EXISTS blog -- 创建博客表
(
ID INT PRIMARY KEY AUTO_INCREMENT, -- 文章 ID
Auth INT NOT NULL, -- 作者
Title char(20) NOT NULL, -- 标题
SubTitle char(20) NOT NULL, -- 副标题
Content TEXT NOT NULL, -- 内容
CreateTime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建的时间
UpdateTime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建的时间
Top BIT NOT NULL DEFAULT 0, -- 置顶
FOREIGN KEY (Auth) REFERENCES user (ID)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS archive -- 归档表
(
ID INT PRIMARY KEY AUTO_INCREMENT, -- 归档 ID
Name CHAR(30) NOT NULL UNIQUE, -- 归档名称
DescribeText char(100) NOT NULL -- 描述
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS blog_archive -- 归档表
(
BlogID INT, -- 文章ID
ArchiveID INT, -- 归档ID
FOREIGN KEY (BlogID) REFERENCES blog (ID),
FOREIGN KEY (ArchiveID) REFERENCES archive (ID)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE VIEW blog_archive_with_name AS
SELECT BlogID, ArchiveID, archive.Name As ArchiveName, archive.DescribeText AS DescribeText
FROM blog_archive
LEFT JOIN archive on blog_archive.ArchiveID = archive.ID;
CREATE VIEW blog_with_archive AS
SELECT blog.ID AS BlogID,
blog_archive.ArchiveID AS ArchiveID,
Auth,
Title,
SubTitle,
Content,
CreateTime,
UpdateTime,
Top
FROM blog
RIGHT JOIN blog_archive ON blog.ID = blog_archive.BlogID;
CREATE TABLE IF NOT EXISTS comment -- 评论表
(
ID INT PRIMARY KEY AUTO_INCREMENT, -- 评论 ID
BlogID INT NOT NULL, -- 博客 ID
Auth INT NOT NULL, -- 作者
Content TEXT NOT NULL, -- 内容
CreateTime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建的时间
UpdateTime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建的时间
FOREIGN KEY (BlogID) REFERENCES blog (ID),
FOREIGN KEY (Auth) REFERENCES user (ID)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE VIEW comment_user AS
SELECT comment.ID as CommentID, BlogID, Auth, user.Email as Email, Content, CreateTime, UpdateTime
FROM comment
LEFT JOIN user on user.ID = comment.Auth
ORDER BY UpdateTime DESC;
CREATE TABLE IF NOT EXISTS message -- 留言表
(
ID INT PRIMARY KEY AUTO_INCREMENT, -- 留言 ID
Auth INT NOT NULL, -- 作者
Content TEXT NOT NULL, -- 内容
Secret BIT NOT NULL DEFAULT 0, -- 私密内容
CreateTime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建的时间
UpdateTime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, -- 创建的时间
FOREIGN KEY (Auth) REFERENCES user (ID)
) CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE VIEW message_user AS
SELECT message.ID as MsgID, Auth, user.Email as Email, Content, CreateTime, UpdateTime, Secret
FROM message
LEFT JOIN user on user.ID = message.Auth
ORDER BY UpdateTime DESC;