-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInitialize.sql
235 lines (185 loc) · 5.83 KB
/
Initialize.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
DROP TABLE IF EXISTS public.docs;
DROP TABLE IF EXISTS public.buff;
DROP TABLE IF EXISTS public.recs;
DROP TABLE IF EXISTS public.logs;
DROP TABLE IF EXISTS public.user;
DROP TABLE IF EXISTS public.book;
DROP SEQUENCE IF EXISTS public.user_id_seq;
DROP SEQUENCE IF EXISTS public.docs_id_seq;
DROP SEQUENCE IF EXISTS public.buff_id_seq;
DROP SEQUENCE IF EXISTS public.book_id_seq;
DROP SEQUENCE IF EXISTS public.recs_id_seq;
DROP SEQUENCE IF EXISTS public.logs_id_seq;
-- user: Information of users
-- user_id_seq
CREATE SEQUENCE IF NOT EXISTS public.user_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.user_id_seq
OWNER TO postgres;
CREATE TABLE IF NOT EXISTS public.user
(
id bigint NOT NULL DEFAULT nextval('user_id_seq'::regclass),
username character varying(256) COLLATE pg_catalog."default",
password_hash character varying(512) COLLATE pg_catalog."default",
email character varying(256) COLLATE pg_catalog."default",
role smallint,
image text,
CONSTRAINT user_pkey PRIMARY KEY (id),
CONSTRAINT name_unique UNIQUE (username),
CONSTRAINT email_unique UNIQUE (email)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.user
OWNER to postgres;
INSERT INTO public.user(id, username, password_hash, role)
VALUES (0, 'guest', NULL, 0);
INSERT INTO public.user(username, password_hash, role)
VALUES ('admin', 'admin', 0);
INSERT INTO public.user(username, password_hash, role)
VALUES ('user', 'user', 1);
-- docs: Information of documents
-- docs_id_seq
CREATE SEQUENCE IF NOT EXISTS public.docs_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.docs_id_seq
OWNER TO postgres;
CREATE TABLE IF NOT EXISTS public.docs
(
id bigint NOT NULL DEFAULT nextval('docs_id_seq'::regclass),
title character varying(256) COLLATE pg_catalog."default",
file_path text,
uploaded_by bigint,
download_count integer,
upload_time timestamp with time zone,
doc_type character varying(128),
author character varying(128),
publish_date date,
CONSTRAINT docs_pkey PRIMARY KEY (id),
CONSTRAINT docs_unique UNIQUE (file_path),
CONSTRAINT uploaded_by_exist FOREIGN KEY (uploaded_by)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.docs
OWNER to postgres;
-- buff: Buffer of uploaded documents
-- buff_id_seq
CREATE SEQUENCE IF NOT EXISTS public.buff_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.buff_id_seq
OWNER TO postgres;
CREATE TABLE IF NOT EXISTS public.buff
(
id bigint NOT NULL DEFAULT nextval('buff_id_seq'::regclass),
title character varying(256) COLLATE pg_catalog."default",
file_path text,
uploaded_by bigint,
download_count integer,
upload_time timestamp with time zone,
doc_type character varying(128),
author character varying(128),
publish_date date,
CONSTRAINT buff_pkey PRIMARY KEY (id),
CONSTRAINT buff_unique UNIQUE (file_path),
CONSTRAINT uploaded_by_exist FOREIGN KEY (uploaded_by)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
NOT VALID
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.buff
OWNER to postgres;
-- book: Information of books
-- book_id_seq;
CREATE SEQUENCE IF NOT EXISTS public.book_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.book_id_seq
OWNER TO postgres;
CREATE TABLE IF NOT EXISTS public.book
(
id bigint NOT NULL DEFAULT nextval('book_id_seq'::regclass),
title character varying(256) COLLATE pg_catalog."default",
author character varying(128) COLLATE pg_catalog."default",
book_type character varying(128) COLLATE pg_catalog."default",
publish_date date,
available boolean,
CONSTRAINT book_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.book
OWNER to postgres;
-- recs: Information of records of borrowing
-- recs_id_seq;
CREATE SEQUENCE IF NOT EXISTS public.recs_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.recs_id_seq
OWNER TO postgres;
CREATE TABLE IF NOT EXISTS public.recs
(
id bigint NOT NULL DEFAULT nextval('recs_id_seq'::regclass),
user_id bigint NOT NULL,
book_id bigint NOT NULL,
borrowed_at timestamp with time zone,
returned_at timestamp with time zone,
CONSTRAINT recs_pkey PRIMARY KEY (id),
CONSTRAINT book_id_exist FOREIGN KEY (book_id)
REFERENCES public.book (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT user_id_exist FOREIGN KEY (user_id)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.recs
OWNER to postgres;
-- logs: Information of logs
-- logs_id_seq;
CREATE SEQUENCE IF NOT EXISTS public.logs_id_seq
INCREMENT 1
START 1
MINVALUE 1
MAXVALUE 9223372036854775807
CACHE 1;
ALTER SEQUENCE public.logs_id_seq
OWNER TO postgres;
CREATE TABLE IF NOT EXISTS public.logs
(
id bigint NOT NULL DEFAULT nextval('logs_id_seq'::regclass),
user_id bigint,
action character varying(512) COLLATE pg_catalog."default",
log_time timestamp with time zone,
CONSTRAINT logs_pkey PRIMARY KEY (id),
CONSTRAINT user_id_exist FOREIGN KEY (user_id)
REFERENCES public."user" (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.logs
OWNER to postgres;