pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedCHECK CONSTRAINT blunder(s)
2+ messages / 2 participants
[nested] [flat]
* CHECK CONSTRAINT blunder(s)
@ 2002-05-14 20:50 Thomas Good <tomg@sqlclinic.net>
0 siblings, 1 reply; 2+ messages in thread
From: Thomas Good @ 2002-05-14 20:50 UTC (permalink / raw)
To: pgsql-sql
Hi,
Can someone clue me in as to proper syntax for adding a not null
check constraint? I've fumbled around a bit without much luck
and I don't see this covered in the alter table section of the
interactive docs.
My pseudo code is:
ALTER TABLE doof ADD CONSTRAINT bleibt_doof NOT NULL (record_id);
But obviously this fails.
TIA - Cheers
-----------------------------------------------------------------------
Thomas Good e-mail: tomg@sqlclinic.net
Programmer/Analyst phone: (+1) 718.818.5528
Residential Services fax: (+1) 718.818.5056
Behavioral Health Services, SVCMC-NY mobile: (+1) 917.282.7359
-- --
SQL Clinic - An Open Source Clinical Record www.sqlclinic.net
------------------------------------------------------------------------
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: CHECK CONSTRAINT blunder(s)
@ 2002-05-15 05:23 Christopher Kings-Lynne <chriskl@familyhealth.com.au>
parent: Thomas Good <tomg@sqlclinic.net>
0 siblings, 0 replies; 2+ messages in thread
From: Christopher Kings-Lynne @ 2002-05-15 05:23 UTC (permalink / raw)
To: Thomas Good <tomg@sqlclinic.net>; pgsql-sql
> Can someone clue me in as to proper syntax for adding a not null
> check constraint? I've fumbled around a bit without much luck
> and I don't see this covered in the alter table section of the
> interactive docs.
>
> My pseudo code is:
> ALTER TABLE doof ADD CONSTRAINT bleibt_doof NOT NULL (record_id);
> But obviously this fails.
OK, Postgres doesn't currently have an SQL command for changing an
attributes NOT NULL property. There are a few options.
1) Add a CHECK constraint:
ATLER TABLE doof ADD CHECK (record_id IS NOT NULL);
This will work, but the column will still be of type 'null'.
2) Edit the catalogs
This isn't too hard:
UPDATE pg_attribute SET attnotnull = true WHERE attrelid = (SELECT oid FROM
pg_class WHERE relname = 'doof') AND attname = 'record_id';
You probably want to check that there's no NULL values in the column before
you do this. You can do it all in a transaction and LOCK the table as well
to make sure no-one adds NULL values while you're doing it.
3) I've attached two functions. Load them into your postgres. Just go:
SELECT kl_setnotnull('doof', 'record_id');
SELECT kl_setnull('doof', 'record_id');
These functions do all the proper locking and checking for you.
(These funcs are in the public domain BTW)
4) Wait for 7.3
The postgres CVS has a new command:
ALTER TABLE doof ALTER record_id SET NOT NULL;
ALTER TABLE doof ALTER record_id DROP NOT NULL;
FWIW, I recommend option (2) or (3) at the moment.
Cheers,
Chris
Attachments:
kl_setnotnull.sql (application/octet-stream, 1.2K) download
kl_setnull.sql (application/octet-stream, 984B) download
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2002-05-15 05:23 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-05-14 20:50 CHECK CONSTRAINT blunder(s) Thomas Good <tomg@sqlclinic.net>
2002-05-15 05:23 ` Christopher Kings-Lynne <chriskl@familyhealth.com.au>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox