Create a table of two types in PostgreSQL

后端 未结 1 1142
花落未央
花落未央 2020-12-11 12:32

I have 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),
         


        
相关标签:
1条回答
  • 2020-12-11 12:58

    You cannot make prod_id the primary key of table1 because the only columns are the two composite types info and movie. You cannot access the base types of these composite types in a PRIMARY KEY clause.

    What you were trying to do works with a pk constraint on info or movie.
    Except, it's probably not what you were looking for, which is not possible this way.

    You could implement something like this with ...

    Inheritance

    Here you can inherit from multiple parent tables (substitute for your types). Example:

    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 demonstrating both.

    Be sure to read about limitations of inheritance in the manual.

    0 讨论(0)
提交回复
热议问题