2014-02-05 40 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。 –