dbTalk Databases Forums  

Changing from NOT NULL to NULL

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Changing from NOT NULL to NULL in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rob Mosher
 
Posts: n/a

Default Changing from NOT NULL to NULL - 02-15-2004 , 01:57 PM






Is there anyway I can change a field to allowing nulls without dumping
the table, dropping it, recreating it as desired, and filling all the
data back in?

Thanks,
Rob Mosher

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: Changing from NOT NULL to NULL - 02-15-2004 , 05:08 PM






Rob Mosher <mosher (AT) andrews (DOT) edu> writes:
Quote:
Is there anyway I can change a field to allowing nulls without dumping
the table, dropping it, recreating it as desired, and filling all the
data back in?
ALTER TABLE ... DROP NOT NULL does the trick in recent releases. Before
that, you could resort to manually poking the attnotnull flag in
the pg_attribute row for the field.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
  #3  
Old   
Nabil Sayegh
 
Posts: n/a

Default Re: Changing from NOT NULL to NULL - 02-15-2004 , 05:20 PM



Rob Mosher wrote:
Quote:
Is there anyway I can change a field to allowing nulls without dumping
the table, dropping it, recreating it as desired, and filling all the
data back in?
ALTER TABLE tab ALTER COLUMN col DROP NOT NULL;

HTH
--
e-Trolley Sayegh & John, Nabil Sayegh
Tel.: 0700 etrolley /// 0700 38765539
Fax.: +49 69 8299381-8
PGP : http://www.e-trolley.de

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #4  
Old   
Michael Glaesemann
 
Posts: n/a

Default Re: Changing from NOT NULL to NULL - 02-15-2004 , 05:33 PM




On Feb 16, 2004, at 4:57 AM, Rob Mosher wrote:

Quote:
Is there anyway I can change a field to allowing nulls without dumping
the table, dropping it, recreating it as desired, and filling all the
data back in?
<http://www.postgresql.org/docs/current/static/sql-altertable.html>

Michael Glaesemann
grzm myrealbox com


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
  #5  
Old   
joseph speigle
 
Posts: n/a

Default Re: Changing from NOT NULL to NULL - 02-15-2004 , 05:56 PM



Quote:
Is there anyway I can change a field to allowing nulls without dumping the table, dropping it,
recreating it as desired, and filling all the data back in?
I hope this gets indexed correctly as I hosed the original and tried a cut-n-paste of the subject line,

For that, I have this which I didn't write myself but snagged from somewhere. Maybe pgsql-general???


-- 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
RETURN NULL;
END IF;

-- Lock table with standard ALTER TABLE locks
EXECUTE ''LOCK TABLE '' || quote_ident(tablename) || '' IN ACCESS EXCLUSIVE MODE'';

-- Update the system catalogs
EXECUTE ''UPDATE pg_attribute SET attnotnull = false WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = '' || quote_literal(tablename) || '') AND attname = '' || quote_literal(colname);

-- Get number of rows modified
GET DIAGNOSTICS rec_affected = ROW_COUNT;

-- Return number of rows modified
RETURN (rec_affected = 1);

END;
' LANGUAGE 'plpgsql'
WITH (isstrict);
--
joe speigle
www.sirfsup.com

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



Reply With Quote
  #6  
Old   
Rob Mosher
 
Posts: n/a

Default Re: Changing from NOT NULL to NULL - 02-16-2004 , 10:03 AM



I will try this out as soon as I can. Thanks to all who replied.

Rob Mosher

Tom Lane wrote:

Quote:
Rob Mosher <mosher (AT) andrews (DOT) edu> writes:


Is there anyway I can change a field to allowing nulls without dumping
the table, dropping it, recreating it as desired, and filling all the
data back in?



ALTER TABLE ... DROP NOT NULL does the trick in recent releases. Before
that, you could resort to manually poking the attnotnull flag in
the pg_attribute row for the field.

regards, tom lane


---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.