Composite type, DEFAULT, NOT NULL, REFERENCES

Поиск
Список
Период
Сортировка
Искать

Composite type, DEFAULT, NOT NULL, REFERENCES

От:
erhaminus <erhaminus@o2.pl>
Дата:

Hi,

Is a way to define DEFAULT, NOT NULL and REFERENCES for members of composite type?

For example:

-- type
CREATE TYPE bibl.bibliography AS 
(
	edition TEXT,
	publisher_id BIGINT			
);

-- table def
create table bibl.monograph
(
	id BIGSERIAL PRIMARY KEY NOT NULL,
	bibl bibl.bibliography		
);

-- how to do that, assuming that:
--    we want edition NOT NULL, DEFAULT with value "first"
--    we want publisher_id reffering to bibl.publisher(id) 

This does not work for me:

ALTER TABLE bibl.monograph ALTER COLUMN (bibl).is_bibliography SET NOT NULL;
ALTER TABLE bibl.monograph ALTER COLUMN (bibl).is_bibliography SET DEFAULT false;

Thanks in advance,
Best regards

Re: Composite type, DEFAULT, NOT NULL, REFERENCES

От:
"David Johnston" <polobo@yahoo.com>
Дата:
-----Original Message-----
From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of erhaminus
Sent: Wednesday, October 19, 2011 10:33 AM
To: pgsql-general@postgresql.org
Subject: [GENERAL] Composite type, DEFAULT, NOT NULL, REFERENCES


Hi,

Is a way to define DEFAULT, NOT NULL and REFERENCES for members of composite type?

--------------------/Original Message---------------

The only way to constrain a Type is to use a DOMAIN but whether you can domain a composite type I do not know.

There is no concept of "Foreign Key" when it comes to Types, only Tables.

David J.


Re: Composite type, DEFAULT, NOT NULL, REFERENCES

От:
Merlin Moncure <mmoncure@gmail.com>
Дата:
On Wed, Oct 19, 2011 at 9:32 AM, erhaminus  wrote:
>
> Hi,
>
> Is a way to define DEFAULT, NOT NULL and REFERENCES for members of composite type?
>
> For example:
>
> -- type
> CREATE TYPE bibl.bibliography AS
> (
>        edition TEXT,
>        publisher_id BIGINT
> );
>
> -- table def
> create table bibl.monograph
> (
>        id BIGSERIAL PRIMARY KEY NOT NULL,
>        bibl bibl.bibliography
> );
>
> -- how to do that, assuming that:
> --    we want edition NOT NULL, DEFAULT with value "first"
> --    we want publisher_id reffering to bibl.publisher(id)
>
> This does not work for me:
>
> ALTER TABLE bibl.monograph ALTER COLUMN (bibl).is_bibliography SET NOT NULL;
> ALTER TABLE bibl.monograph ALTER COLUMN (bibl).is_bibliography SET DEFAULT false;

sure:

create table bibl.monograph
(
       id BIGSERIAL PRIMARY KEY NOT NULL,
       bibl bibl.bibliography check((bibl).is_bibliography is not
null) DEFAULT row(false, 'something');
);

create unique index on bibl.monograph(bibl);

create table a_table
(
  bibl bibl.bibliography references bibl.monograph(bibl)
);

Not saying this is a good idea -- but it can be done.  The only thing
you can't do is DOMAIN a composite type so you have to manually add
the check constraint to every table containing the type of you want to
enforce it.

merlin
FAQ