【发布时间】:2016-05-03 18:56:37
【问题描述】:
给定一个名为 requests 的 PostgreSQL 表,其中包含一个名为 status 的列和一个类似这样的约束:
ALTER TABLE requests ADD CONSTRAINT allowed_status_types
CHECK (status IN (
'pending', -- request has not been attempted
'success', -- request succeeded
'failure' -- request failed
));
在psql 中,我可以像这样提取有关此约束的信息:
example-database=# \d requests
Table "public.example-database"
Column | Type | Modifiers
----------------------+-----------------------------+-------------------------------------------------------------------
id | integer | not null default nextval('requests_id_seq'::regclass)
status | character varying | not null default 'pending'::character varying
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
Indexes:
"requests_pkey" PRIMARY KEY, btree (id)
Check constraints:
"allowed_status_types" CHECK (status::text = ANY (ARRAY['pending'::character varying, 'success'::character varying, 'failure'::character varying]::text[]))
但是是否可以编写一个专门返回待定、成功、失败的allowed_status_types 的查询?
能够在我的应用程序中记住此查询的结果,而不是必须维护副本,这将是很棒的。
【问题讨论】:
标签: sql database postgresql database-design check-constraints