· 7 years ago · Jan 18, 2019, 07:22 PM
1create table if not exists messages(
2 id serial primary key,
3 max_length smallint not null check (max_length > 0),
4 default_text text check (length(default_text) <= max_length),
5 created_on timestamp with time zone not null
6);
7
8create table if not exists customized_messages(
9 message_id serial references messages(id) not null,
10 message text not null,
11 created_on timestamp with time zone not null
12);
13
14postgres=# create table if not exists messages(
15postgres(# id serial primary key,
16postgres(# max_length smallint not null check (max_length > 0),
17postgres(# default_text text check (length(default_text) <= max_length),
18postgres(# created_on timestamp with time zone not null
19postgres(# );
20CREATE TABLE
21postgres=#
22postgres=# create table if not exists customized_messages(
23postgres(# message_id serial references messages(id) not null,
24postgres(# message text not null,
25postgres(# created_on timestamp with time zone not null
26postgres(# );
27CREATE TABLE
28postgres=#
29postgres=# insert into messages (id, max_length, default_text, created_on) values (1, 5, null, current_timestamp);
30INSERT 0 1
31postgres=# insert into customized_messages (message_id, message, created_on) values (1, 'longer than 5', current_timestamp);
32INSERT 0 1
33postgres=# select * from messages;
34 id | max_length | default_text | created_on
35----+------------+--------------+-------------------------------
36 1 | 5 | | 2019-01-18 14:12:59.692075-05
37(1 row)
38postgres=# select * from customized_messages;
39 message_id | message | created_on
40------------+---------------+-------------------------------
41 1 | longer than 5 | 2019-01-18 14:13:38.226987-05
42(1 row)