Skip to content
Advertisement

Which the best way to use inheritance of tables in postgreSQL?

i want to use inheritance in my database (PostgreSQL) so i wrote this code but finally i found there are inherits in PostgreSQL

CREATE SEQUENCE public.user_account_id_seq;
CREATE TABLE public.user_account (
                id BIGINT NOT NULL DEFAULT nextval('public.user_account_id_seq'),
                login VARCHAR(50) NOT NULL,
                f_name VARCHAR(50),
                l_name VARCHAR(50),
                email VARCHAR(50) ,
                CONSTRAINT user_account_pk PRIMARY KEY (id)
);

CREATE SEQUENCE public.simple_user_account_id_seq;
CREATE TABLE public.simple_user_account (
                id BIGINT NOT NULL DEFAULT nextval('public.simple_user_account_id_seq'),
                CONSTRAINT simple_user_account_pk PRIMARY KEY (id)
);

CREATE SEQUENCE public.supervisor_account_id_seq;
CREATE TABLE public.supervisor_account (
                id BIGINT NOT NULL DEFAULT nextval('public.supervisor_account_id_seq'),
                CONSTRAINT supervisor_account_pk PRIMARY KEY (id)
);

with

ALTER TABLE public.simple_user_account ADD CONSTRAINT simple_user_account_fk
FOREIGN KEY (id)
REFERENCES public.user_account (id)
ON DELETE CASCADE
ON UPDATE NO ACTION
NOT DEFERRABLE;

ALTER TABLE public.supervisor_account ADD CONSTRAINT supervisor_account_fk
FOREIGN KEY (id)
REFERENCES public.user_account (id)
ON DELETE CASCADE
ON UPDATE NO ACTION
NOT DEFERRABLE;

or i use INHERITS of postgreSQL

Advertisement

Answer

With inheritance you cannot have globally unique constraints. If that doesn’t bother you, you can use the feature.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement