从复合TYPE创建表时如何设置默认值?

时间:2017-03-02 19:58:45

标签: postgresql

我想为从复合类型创建的表的字段设置默认值,但是我收到错误:

PLSQL $ cat ../animal2.sql 
DROP TYPE IF EXISTS zoo_t CASCADE;
DROP TYPE IF EXISTS animal_t;
CREATE TYPE animal_t AS (
    animal_id           integer,
    animal_color        varchar,
    animal_name         varchar
);
CREATE TYPE zoo_t AS (
    wolf_object         animal_t,
    bear_object         animal_t    
);
CREATE TABLE zoopark OF zoo_t (
    (wolf_object).animal_color  WITH OPTIONS DEFAULT 'black'
);
 PLSQL $ psql dev < ../animal2.sql 
DROP TYPE
DROP TYPE
CREATE TYPE
CREATE TYPE
ERROR:  syntax error at or near "("
LINE 2:  (wolf_object).animal_color  WITH OPTIONS DEFAULT 'black'
         ^
PLSQL $ 

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

您无法通过DEFAULT直接设置字段。但是你可以设置整个字段:

CREATE TABLE zoopark OF zoo_t (
    wolf_object  WITH OPTIONS DEFAULT (NULL, 'black', NULL)
);

或者你可以创建TRIGGER并设置animal_color如果它未设置但是字段wolf_object不为null,或者如果它为null则创建整个字段