Re: Modifying check constraints

From: Jason Earl <jason(dot)earl(at)simplot(dot)com>
To: Paulo Jan <admin(at)mail(dot)ddnet(dot)es>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Modifying check constraints
Date: 2001-11-14 19:07:05
Message-ID: 87u1vxi33a.fsf@npa01zz001.simplot.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


You have to reload the table if you want to change check constraints.
See:

http://www.postgresql.org/idocs/index.php?sql-altertable.html

However, an easy way to get around this is to not use CHECK
constraints in these situations, and instead use a FOREIGN KEY
constraint like this:

-- BEGIN EXAMPLE

CREATE TABLE tipos (
tipo varchar(8) PRIMARY KEY
);

INSERT INTO tipos (tipo) VALUES ('tipo1');
INSERT INTO tipos (tipo) VALUES ('tipo2');
INSERT INTO tipos (tipo) VALUES ('tipo3');

CREATE TABLE blabla (
id int,
tipo varchar(8) REFERENCES tipos(tipo)
);

-- END EXAMPLE

Yes, this does require a little more typing. However, you only build
your schema once. Using this method your application will be much
more flexible. Adding new tipos is as simple as:

INSERT INTO tipos (tipo) VALUES ('tipo_neuvo');

I hope this is helpful,

Jason Earl

Paulo Jan <admin(at)mail(dot)ddnet(dot)es> writes:

> Hi all:
>
> Let's suppose I create a table with a CHECK constraint, like:
>
> CREATE TABLE blabla (id int, tipo varchar(8),
> CHECK tipo IN ('tipo1', 'tipo2', 'tipo3'))
>
> Can I change the CHECK constraint afterwards? For example, can I
> add another value that "tipo" can have, or I'd have to recreate the
> table?
>
>
>
> Paulo Jan.
> DDnet.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stephan Szabo 2001-11-14 19:28:00 Re: Modifying check constraints
Previous Message Thalis A. Kalfigopoulos 2001-11-14 18:47:32 Re: Modifying check constraints