If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > adding a column with default NULL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-01-09, 05:33
ppande ppande is offline
Registered User
 
Join Date: Jul 2008
Posts: 7
adding a column with default NULL

Hello Gurus,

One quick question.

How to define a column to accept null values in db2? I guess columns accept null values when it is created.

Also is there any specific syntax to define a column with default null, also is there any difference in syntax if db2 is residing on windows and mainframe?

Syntax
ALTER TABLE SchemaName.TableName ADD ColumnName DataType WITH DEFAULT NULL

The above command works fine when executed in DB2 on Windows.

Please let me know whether above syntax will work fine on DB2 in Mainframe OS (Z/OS)
Please revert back at the earliest.

Thanks and Regards,
Priyesh V Pande
Reply With Quote
  #2 (permalink)  
Old 09-01-09, 09:27
rdutton rdutton is offline
Registered User
 
Join Date: Dec 2008
Posts: 76
This is the format for adding a nullable column:

alter table schemaname.tablename add column columnname integer;

This is true for DB2 on all platforms. By adding WITH DEFAULT, you are giving the column a default value: an empty string for CHAR columns, 0 for numeric columns, current date or time or timestamp for date/time columns, etc.. The default value will be used instead of null if a row is inserted without providing a value for the column.
__________________
RD
Reply With Quote
  #3 (permalink)  
Old 09-01-09, 09:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If you don't specify a WITH DEFAULT clause, then DB2 will implicitly use WITH DEFAULT NULL as default.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On