PostgreSQL中复合类型列子列的外键约束

时间:2014-02-05 09:18:49

标签: sql postgresql foreign-keys compositetype

有没有办法将复合类型列的子列设置为外键?

我试过的是:

Create Type info_typ_ AS (
  category integer ,
  title text ,
  actor text ,
  price double precision );

Create Table Products_typobj (
prod_id integer,
info info_typ_,
primary key(prod_id),
Category references Categories(Category)
);

但它不起作用。

1 个答案:

答案 0 :(得分:1)

这项工作对我来说

Create Type info_typ_ AS (
  category integer ,
  title text ,
  actor text ,
  price double precision );

Create Table Products_typobj (
prod_id integer,
info info_typ_,
primary key(prod_id)
);

我删除了references行,因此使用type有效。 的 SQL Fiddle DEMO

如果您需要外键,请尝试以下代码:

Create Type info_typ_ AS (
  category integer ,
  title text ,
  actor text ,
  price double precision );

Create Table Categories (
  Category int,
  primary key(Category)
);  

Create Table Products_typobj (
prod_id integer,
info info_typ_,
Category int references Categories(Category),  
primary key(prod_id),
FOREIGN KEY (Category) REFERENCES Categories (Category)
);

<强> SQL Fiddle DEMO