Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top