· 8 years ago · Feb 08, 2018, 04:00 PM
1
2DROP TABLE IF EXISTS users;
3DROP TABLE IF EXISTS blogs;
4DROP TABLE IF EXISTS blogposts;
5DROP TABLE IF EXISTS answers;
6DROP SEQUENCE IF EXISTS users_id_seq;
7DROP SEQUENCE IF EXISTS blogs_id_seq;
8DROP SEQUENCE IF EXISTS blogposts_id_seq;
9DROP SEQUENCE IF EXISTS answers_id_seq;
10
11
12CREATE TABLE users (
13 id integer NOT NULL,
14 username text,
15 password text
16);
17
18CREATE TABLE blogs (
19 id integer NOT NULL,
20 name text,
21 owner_id integer,
22 about text,
23 contact text,
24 links text
25);
26
27CREATE TABLE blogposts (
28 id integer NOT NULL,
29 blog_id integer,
30 title text,
31 message text,
32 img text
33);
34
35CREATE TABLE answers (
36 id integer NOT NULL,
37 blogpost_id integer,
38 owner_id integer,
39 username text,
40 message text
41);
42
43
44CREATE SEQUENCE users_id_seq
45 START WITH 1
46 INCREMENT BY 1
47 NO MINVALUE
48 NO MAXVALUE
49 CACHE 1;
50
51
52CREATE SEQUENCE blogs_id_seq
53 START WITH 1
54 INCREMENT BY 1
55 NO MINVALUE
56 NO MAXVALUE
57 CACHE 1;
58
59
60CREATE SEQUENCE blogposts_id_seq
61 START WITH 1
62 INCREMENT BY 1
63 NO MINVALUE
64 NO MAXVALUE
65 CACHE 1;
66
67CREATE SEQUENCE answers_id_seq
68 START WITH 1
69 INCREMENT BY 1
70 NO MINVALUE
71 NO MAXVALUE
72 CACHE 1;
73
74
75ALTER TABLE ONLY users ALTER COLUMN id SET DEFAULT nextval('users_id_seq'::regclass);
76ALTER TABLE ONLY blogs ALTER COLUMN id SET DEFAULT nextval('blogs_id_seq'::regclass);
77ALTER TABLE ONLY blogposts ALTER COLUMN id SET DEFAULT nextval('blogposts_id_seq'::regclass);
78ALTER TABLE ONLY answers ALTER COLUMN id SET DEFAULT nextval('answer_id_seq'::regclass);
79
80
81ALTER TABLE ONLY users
82 ADD CONSTRAINT user_pk PRIMARY KEY (id);
83
84ALTER TABLE ONLY blogs
85 ADD CONSTRAINT blog_pk PRIMARY KEY (id);
86
87ALTER TABLE ONLY blogposts
88 ADD CONSTRAINT blogpost_pk PRIMARY KEY (id);
89
90ALTER TABLE ONLY answers
91 ADD CONSTRAINT answer_pk PRIMARY KEY (id);