Creating a table of two types in PostgreSQL

I created two types:

Create  Type info_typ_1 AS (
Prod_id integer, 
category integer);

Create Type movie_typ AS(
title varchar(50),
actor varchar(50),
price float);

And I want to create a table consisting of these two types. I know that for a table consisting of one type, this is:

CREATE TABLE Table1 of type1
(
  primary key(prod_id)
);

Is there a way to do this for the two types that I created above?

What I tried to do (this is wrong) creates a third type containing the first two:

Create Type info_ AS (
info info_typ_1,
movie movie_typ);

and then creating the table:

CREATE TABLE table1 of info_
(
  primary key(Prod_id)
);

But that will not work. I get this error:

ERROR:  column "prod_id" named in key does not exist
LINE 3:   primary key(Prod_id)
          ^
********** Error **********

ERROR: column "prod_id" named in key does not exist
SQL state: 42703
Character: 43
+1
source share
1 answer

You cannot make it the prod_idprimary key table1, because the only columns are the two composite types infoand movie. You cannot access the basic types of these composite types in a sentence PRIMARY KEY.

pk info movie.
, , , , , .

- ...

( ). :

CREATE TABLE info (
  prod_id integer
 ,category integer
);

CREATE TABLE movie (
   title text
  ,actor text
  ,price float
);

CREATE  TABLE movie_info (
   PRIMARY KEY(prod_id)             -- now we can use the base column!
)
INHERITS (info, movie);

INSERT INTO movie_info (prod_id, category, title, actor, price)
VALUES (1, 2, 'who donnit?', 'James Dean', '15.90');

SELECT * FROM movie_info;

- > SQLfiddle, .

.

+3

Source: https://habr.com/ru/post/1622135/


All Articles