forked from Realhedin/topjava02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitDB_hsql.sql
More file actions
36 lines (32 loc) · 1002 Bytes
/
Copy pathinitDB_hsql.sql
File metadata and controls
36 lines (32 loc) · 1002 Bytes
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
DROP TABLE user_roles IF EXISTS ;
DROP TABLE meals IF EXISTS ;
DROP TABLE users IF EXISTS;
DROP SEQUENCE global_seq IF EXISTS;
CREATE SEQUENCE GLOBAL_SEQ AS INTEGER START WITH 100000;
CREATE TABLE users
(
id INTEGER GENERATED BY DEFAULT AS SEQUENCE GLOBAL_SEQ PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
registered TIMESTAMP DEFAULT now(),
enabled BOOLEAN DEFAULT TRUE,
calories_per_day INTEGER DEFAULT 2000 NOT NULL
);
CREATE UNIQUE INDEX user_email_key ON USERS ( email );
CREATE TABLE user_roles
(
user_id INTEGER NOT NULL,
role VARCHAR(255),
CONSTRAINT user_roles_idx UNIQUE (user_id, role),
FOREIGN KEY ( user_id ) REFERENCES USERS ( id ) ON DELETE CASCADE
);
CREATE TABLE meals
(
id INTEGER GENERATED BY DEFAULT AS SEQUENCE GLOBAL_SEQ PRIMARY KEY,
datetime TIMESTAMP,
description VARCHAR(255),
calories INT,
user_id INTEGER NOT NULL,
FOREIGN KEY ( user_id ) REFERENCES USERS ( id ) ON DELETE CASCADE
);