문제

Is there any way to set a sub-column of a composite type column as a foreign key?

What I have tried is:

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)
);

but it doesn't work.

도움이 되었습니까?

해결책

This work for me

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)
);

I removed line with references, so using type works. SQL Fiddle DEMO

If you need foreign key, try below code:

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top