Re: Composite type

From: Kevin Grittner <kgrittn(at)ymail(dot)com>
To: antono124 <g(dot)antonopoulos000(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Composite type
Date: 2014-01-28 20:35:34
Message-ID: 1390941334.46164.YahooMailNeo@web122304.mail.ne1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

antono124 <g(dot)antonopoulos000(at)gmail(dot)com> wrote:

> Lets say that we have 2 tables.
> Create Table "table1" Of "type1"
> Create Table "table2" Of "type2"
>
> I want to refer the first table in the second. I want to
> reference the whole table not only one field, so something like
> that:
>
> CREATE TYPE type2 AS OBJECT (
>   var1  NUMBER,
>   var2    REF type1
>   )
>
> CREATE TABLE table2 OF type2 (
>   PRIMARY KEY (Pk),
>   FOREIGN KEY (fk) REFERENCES table1)
>
> Can i do something like this in Postgre?

First, it's PostgreSQL or Postgres for short; not Postgre.

It's pretty hard to see what you want here.  You might be looking
for something like this:

CREATE TYPE type1 AS (
  k1  bigint,
  v1  text
  );
CREATE TYPE type2 AS (
  k2  bigint,
  v2  text,
  k1  bigint
  );
CREATE TABLE table1 (
  LIKE type1,
  PRIMARY KEY (k1)
  );
CREATE TABLE table2 (
  LIKE type2,
  PRIMARY KEY (k2),
  FOREIGN KEY (k1) REFERENCES table1
  );

To reference data from both together you might want a view:

CREATE VIEW view1 AS
  SELECT * FROM table1 JOIN table2 USING (k1);

If that's not quite what you're after you might want to look at the
INHERITS clause of CREATE TABLE.

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2014-01-28 21:13:18 Re: PostgreSQL specific datatypes very confusing for beginners who use wrappers around JDBC
Previous Message Merlin Göttlinger 2014-01-28 20:11:21 PostgreSQL specific datatypes very confusing for beginners who use wrappers around JDBC