-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbak.sql
148 lines (125 loc) · 3.58 KB
/
bak.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
141
142
143
144
145
146
147
-- Active: 1670243150740@@127.0.0.1@5432@web-project@web_project
create table test
(
test integer,
test_time timestamp,
test2 serial
);
alter table test
owner to rolemee;
create table rights
(
"rightsId" serial
primary key,
"rightsName" varchar(20) default 'user'::character varying not null
);
alter table rights
owner to rolemee;
create table "user"
(
"userId" varchar(100) not null
constraint user_pk
primary key,
username varchar(100) not null
unique,
password varchar(60) not null,
rights integer default 0 not null
constraint user_rights_rightsid_fk
references rights
);
alter table "user"
owner to rolemee;
create unique index user_userid_uindex
on "user" ("userId");
create table quiz
(
qid serial
constraint quiz_pk
primary key,
"userId" varchar(20) not null
constraint quiz_user_userid_fk
references "user",
time timestamp default now(),
title varchar(255) not null,
content text,
"keyWords" text[] default '{}'::text[],
"like" integer default 0,
dislike integer default 0,
max_like_reply_id integer default 0 not null,
ans_num integer default 0 not null,
like_id text[] default '{}'::text[],
star_id text[] default '{}'::text[] not null
);
alter table quiz
owner to rolemee;
create unique index quiz_qid_uindex
on quiz (qid);
create table answer
(
id serial
primary key,
"userId" varchar(20) not null
constraint answer_user_userid_fk
references "user",
qid integer not null
constraint answer_quiz_qid_fk
references quiz,
time timestamp default now(),
content text not null,
"like" integer default 0,
dislike integer default 0,
like_id text[] default '{}'::text[]
);
alter table answer
owner to rolemee;
create unique index answer_id_uindex
on answer (id);
create function max_like_reply_id_fun() returns trigger
language plpgsql
as
$$
BEGIN
update web_project.quiz set max_like_reply_id=(select "id" from web_project.answer where qid=new.qid order by "like" DESC limit 1 ) where qid=new.qid;
return new;
END
$$;
alter function max_like_reply_id_fun() owner to rolemee;
create trigger max_like_count
after insert or update
of "like"
on answer
for each row
execute procedure max_like_reply_id_fun();
create function get_max_id_answer() returns trigger
language plpgsql
as
$$
BEGIN
PERFORM (select setval('web_project.answer_id_seq',(select max(id)+1 from web_project.answer)));
return new;
end;$$;
alter function get_max_id_answer() owner to rolemee;
create function get_max_id_quiz() returns trigger
language plpgsql
as
$$
BEGIN
PERFORM (select setval('web_project.quiz_qid_seq',(select max(qid) from web_project.quiz)));
return new;
end;$$;
alter function get_max_id_quiz() owner to rolemee;
create function sum_reply() returns trigger
language plpgsql
as
$$
BEGIN
UPDATE web_project.quiz set ans_num = (select count(id) from web_project.answer where answer.qid=new.qid) where qid=new.qid;
RETURN new;
end;
$$;
alter function sum_reply() owner to rolemee;
create trigger ans_sum_t
after insert
on answer
for each row
execute procedure sum_reply();