-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
76 lines (66 loc) · 2.24 KB
/
schema.sql
File metadata and controls
76 lines (66 loc) · 2.24 KB
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
drop table if exists articles_categories;
drop table if exists categories;
drop table if exists comments;
drop table if exists articles;
drop table if exists users;
create table users(
id integer not null primary key generated always as identity,
"createdAt" timestamp default current_timestamp,
"updatedAt" timestamp default current_timestamp,
"avatarUrl" varchar(255) not null,
"firstName" varchar(255) not null,
"lastName" varchar(255) not null,
email varchar(255) not null,
"passwordHash" varchar(255) not null
);
create table articles(
id integer not null primary key generated always as identity,
"userId" integer not null,
annotation varchar(255) not null,
"fullText" varchar(1005),
"createdAt" timestamp default current_timestamp,
"updatedAt" timestamp default current_timestamp,
"imgUrl" varchar(255),
title varchar(255) not null,
foreign key("userId") references users(id)
on delete cascade
on update cascade
);
create table comments(
id integer not null primary key generated always as identity,
"userId" integer not null,
"articleId" integer not null,
text varchar(1000) not null,
"createdAt" timestamp default current_timestamp,
foreign key("userId") references users(id)
on delete cascade
on update cascade,
foreign key("articleId") references articles(id)
on delete cascade
on update cascade
);
create table categories(
id integer not null primary key generated always as identity,
name varchar(35) not null
);
create table articles_categories(
"articleId" integer not null,
"categoryId" integer not null,
"createdAt" timestamp default current_timestamp,
"updatedAt" timestamp default current_timestamp,
constraint articles_categories_id primary key ("articleId", "categoryId"),
foreign key("articleId") references articles(id)
on delete cascade
on update cascade,
foreign key("categoryId") references categories(id)
on delete cascade
on update cascade
);
create index on articles(title);
create index on articles("userId");
create index on comments("userId");
create index on comments("articleId");
create index on articles_categories("articleId");
create index on articles_categories("categoryId");
create unique index on users(email);
create unique index on categories(name);