-
Notifications
You must be signed in to change notification settings - Fork 21
/
create_data_model.sql
64 lines (57 loc) · 2.05 KB
/
create_data_model.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
-- Executing the schema creation in a batch will improve execution speed.
start batch ddl;
create table if not exists singers (
id varchar not null primary key,
version_id int not null,
first_name varchar,
last_name varchar not null,
full_name varchar generated always as (coalesce(concat(first_name, ' '::varchar, last_name), last_name)) stored,
active boolean,
created_at timestamptz,
updated_at timestamptz
);
create table if not exists albums (
id varchar not null primary key,
version_id int not null,
title varchar not null,
marketing_budget numeric,
release_date date,
cover_picture bytea,
singer_id varchar not null,
created_at timestamptz,
updated_at timestamptz,
constraint fk_albums_singers foreign key (singer_id) references singers (id)
);
create table if not exists tracks (
id varchar not null,
track_number bigint not null,
version_id int not null,
title varchar not null,
sample_rate float8 not null,
created_at timestamptz,
updated_at timestamptz,
primary key (id, track_number)
) interleave in parent albums on delete cascade;
create table if not exists venues (
id varchar not null primary key,
version_id int not null,
name varchar not null,
description jsonb not null,
created_at timestamptz,
updated_at timestamptz
);
create table if not exists concerts (
id varchar not null primary key,
version_id int not null,
venue_id varchar not null,
singer_id varchar not null,
name varchar not null,
start_time timestamptz not null,
end_time timestamptz not null,
created_at timestamptz,
updated_at timestamptz,
constraint fk_concerts_venues foreign key (venue_id) references venues (id),
constraint fk_concerts_singers foreign key (singer_id) references singers (id),
constraint chk_end_time_after_start_time check (end_time > start_time)
);
run batch;