Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 72AC9475905 for ; Wed, 15 May 2002 02:50:42 -0400 (EDT) Received: from ciderbox.stanaway.net (ciderbox.stanaway.net [170.76.50.98]) by postgresql.org (Postfix) with ESMTP id B15C7475E32 for ; Wed, 15 May 2002 02:50:38 -0400 (EDT) Received: by ciderbox.stanaway.net (Postfix, from userid 1000) id 361C893659; Wed, 15 May 2002 01:50:32 -0500 (CDT) Subject: Re: ALTER TABLE for field modify... [Fwd: Re: [SQL] CHECK From: David Stanaway To: PostgreSQL List Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="=-XBrs/UNNyvUFcMBrOMMN" X-Mailer: Ximian Evolution 1.0.3 Date: 15 May 2002 01:50:31 -0500 Message-Id: <1021445431.996.3015.camel@ciderbox> Mime-Version: 1.0 X-Archive-Number: 200205/125 X-Sequence-Number: 4300 --=-XBrs/UNNyvUFcMBrOMMN Content-Type: multipart/mixed; boundary="=-dd2cnO47hCgcDt+h4D5r" --=-dd2cnO47hCgcDt+h4D5r Content-Type: text/plain Content-Transfer-Encoding: quoted-printable This might be helpful.. --=-dd2cnO47hCgcDt+h4D5r Content-Disposition: inline Content-Description: Forwarded message - Re: [SQL] CHECK CONSTRAINT blunder(s) Content-Type: message/rfc822 Return-Path: Delivered-To: david@stanaway.net Received: from postgresql.org (postgresql.org [64.49.215.8]) by ciderbox.stanaway.net (Postfix) with ESMTP id 14FDE935A2 for ; Wed, 15 May 2002 00:24:18 -0500 (CDT) Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 49198475D0C; Wed, 15 May 2002 01:24:06 -0400 (EDT) Received: from postgresql.org (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with SMTP id A5752476322; Wed, 15 May 2002 01:23:22 -0400 (EDT) Received: from localhost.localdomain (postgresql.org [64.49.215.8]) by localhost (Postfix) with ESMTP id 7D9FB475982 for ; Wed, 15 May 2002 01:23:08 -0400 (EDT) Received: from houston.familyhealth.com.au (i231-006.nv.iinet.net.au [203.59.231.6]) by postgresql.org (Postfix) with ESMTP id 8B5FF475910 for ; Wed, 15 May 2002 01:23:03 -0400 (EDT) Received: (from root@localhost) by houston.familyhealth.com.au (8.11.6/8.11.6) id g4F5Mum60175 for pgsql-sql@postgresql.org; Wed, 15 May 2002 13:22:56 +0800 (WST) (envelope-from chriskl@familyhealth.com.au) Received: from mariner (mariner.internal [192.168.0.101]) by houston.familyhealth.com.au (8.11.6/8.9.3) with SMTP id g4F5MsV60078; Wed, 15 May 2002 13:22:54 +0800 (WST) From: "Christopher Kings-Lynne" To: "Thomas Good" , "PostgreSQL SQL List" Subject: Re: [SQL] CHECK CONSTRAINT blunder(s) Date: Wed, 15 May 2002 13:23:04 +0800 Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0098_01C1FC13.A4C1D870" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0) X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Importance: Normal In-Reply-To: X-scanner: scanned by Inflex 0.1.5c - (http://www.inflex.co.za/) Precedence: bulk Sender: pgsql-sql-owner@postgresql.org This is a multi-part message in MIME format. ------=_NextPart_000_0098_01C1FC13.A4C1D870 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable > 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 =3D true WHERE attrelid =3D (SELECT oid = FROM pg_class WHERE relname =3D 'doof') AND attname =3D '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 ------=_NextPart_000_0098_01C1FC13.A4C1D870 Content-Type: application/octet-stream; name="kl_setnotnull.sql" Content-Disposition: attachment; filename="kl_setnotnull.sql" Content-Transfer-Encoding: quoted-printable -- This function takes a table and column and will set the column -- to NOT NULL. -- -- $Id$ -- DROP FUNCTION kl_setnotnull(name, name); CREATE FUNCTION kl_setnotnull(name, name) RETURNS boolean AS ' DECLARE tablename ALIAS FOR $1; colname ALIAS FOR $2; rec_affected int; BEGIN -- If any params are NULL, return NULL - this means function -- can be defined isstrict. IF tablename IS NULL OR colname IS NULL THEN=20 RETURN NULL; END IF; -- Lock table with standard ALTER TABLE locks EXECUTE ''LOCK TABLE '' || quote_ident(tablename) || '' IN ACCESS EXCLUSIV= E MODE''; =09 -- See if there are any NULLs in the column already EXECUTE ''SELECT * FROM '' || quote_ident(tablename) || '' WHERE '' || quo= te_ident(colname) || '' IS NULL LIMIT 1''; IF FOUND THEN RAISE EXCEPTION ''Column contains NULL values.''; RETURN FALSE; END IF; -- Update the system catalogs EXECUTE ''UPDATE pg_attribute SET attnotnull =3D true WHERE attrelid =3D (= SELECT oid FROM pg_class WHERE relname =3D '' || quote_literal(tablename) |= | '') AND attname =3D '' || quote_literal(colname); -- Get number of rows modified GET DIAGNOSTICS rec_affected =3D ROW_COUNT; -- Return success if one row affected RETURN (rec_affected =3D 1); =09 END; ' LANGUAGE 'plpgsql' WITH (isstrict); ------=_NextPart_000_0098_01C1FC13.A4C1D870 Content-Type: application/octet-stream; name="kl_setnull.sql" Content-Disposition: attachment; filename="kl_setnull.sql" Content-Transfer-Encoding: quoted-printable -- This function takes a table and column and will set the column -- to allow NULLs. -- -- $Id$ -- DROP FUNCTION kl_setnull(name, name); CREATE FUNCTION kl_setnull(name, name) RETURNS boolean AS ' DECLARE tablename ALIAS FOR $1; colname ALIAS FOR $2; rec_affected int; BEGIN -- If any params are NULL, return NULL - this means function -- can be defined isstrict. IF tablename IS NULL OR colname IS NULL THEN=20 RETURN NULL; END IF; -- Lock table with standard ALTER TABLE locks EXECUTE ''LOCK TABLE '' || quote_ident(tablename) || '' IN ACCESS EXCLUSIV= E MODE''; -- Update the system catalogs EXECUTE ''UPDATE pg_attribute SET attnotnull =3D false WHERE attrelid =3D = (SELECT oid FROM pg_class WHERE relname =3D '' || quote_literal(tablename) = || '') AND attname =3D '' || quote_literal(colname); -- Get number of rows modified GET DIAGNOSTICS rec_affected =3D ROW_COUNT; -- Return number of rows modified RETURN (rec_affected =3D 1); =09 END; ' LANGUAGE 'plpgsql' WITH (isstrict); ------=_NextPart_000_0098_01C1FC13.A4C1D870 Content-Type: text/plain Content-Disposition: inline MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable ---------------------------(end of broadcast)--------------------------- TIP 3: if posting/reading through Usenet, please send an appropriate subscribe-nomail command to majordomo@postgresql.org so that your message can get through to the mailing list cleanly ------=_NextPart_000_0098_01C1FC13.A4C1D870-- --=-dd2cnO47hCgcDt+h4D5r-- --=-XBrs/UNNyvUFcMBrOMMN Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (GNU/Linux) Comment: For info see http://www.gnupg.org iQEXAwUAPOIFN76yIDFGeSAyFALUDQP+Lp0TjRzVQkIjW4BF2U1w9mkvIY9Kk++k mRbWXiI9gxblU7iwR2D/87Cr4Z7VI+F0eyNMWe6HsxgHmPyag4lTqLIozM6LYlCz bjXT9dtntPZ1DJA8SFXAmQQwe4/2EZLYiVqVHYdKE0TqRptj/pjUPep4N4prkCIG 8xT59VT32z8D/AlirGU8DfnqFXpq6qHwJ/KRr4TNLmK7KgYuvcmu8aK0GV3va+pW zAUp+I4c/zQ+3jg7GC5tjZ0iQ/6Ykj7HCB7GjMvWZ9NVrpgpd3VxeLN4aMr33k3j T0h/YcVstWyv3pjNxHGaiuGY4po8crrXoDkZaIF1Ml8rT6ca4vRJmoBT =3QsN -----END PGP SIGNATURE----- --=-XBrs/UNNyvUFcMBrOMMN--