2014-02-05 41 views
0

有什么方法可以将复合类型列的子列设置为外键吗?PostgreSQL中复合类型列的子列的外键约束

我曾尝试是:

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

这对我

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

+0

'category'将在两个'info_typ'和'categories'? – Shevliaskovic

+0

您不需要info_typ_中的类别。 – Parado

+0

这实际上是一种替代表格设计的解决方法。在外键中引用复合类型的元素*不可能* AFAIC。 –