473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to change a NOT NULL column to allow NULLs?

Either I missed something, or ALTER TABLE does not have this
capability. Is there any way of doing it except
DROPping all constraints which mention this table,
EXPORTing the data,
DROPping the table,
reCREATEing the table without the 'NOT NULL property,
reCREATEing the INDEXes,
reloading the data,
redefining all of DROPped constraints
reCREATE the view which were marked inactive by the above.

I think is all the steps, in order.

BTW, is there a way to reactivate a view after it has been marked
inactive? How about an SQL function or procedure?

Nov 12 '05 #1
5 50577
ALTER TABLE does not seem to have this ability. A column that allows
NULL is one byte wider than a NOT NULL column, therefore you will need a
new page layout. So even if there was a command like ALTER TABLE...ALLOW
NULL, DB2 would have to do all the steps you described.

To reactivate the view, you have to CREATE it again, there is no other
way I am aware of.

PS: You will be faster if you load the data before creating the indexes.
Nov 12 '05 #2
DB2 V8.2 has a GUI tool that drived the process through, including
"reactivating" your views.
If you don't like GUI great care as been given in ensuring there is a
regular intreface. Take a look at the altobj() procedure.

Cheers
Serge
Nov 12 '05 #3
Changing a column from NOT NULL to nullable requires adding a "null
indicator" for the column in each existing row. This also changes the
row size! ALTER won't do this.

An alternative to export/import is to create a new, temporary, table to
hold the contents of your original one. Create it with the "NOT LOGGED
INITIALLY" clause and populate it with "INSERT INTO ... SELECT * FROM
....". This will avoid logging when populating the temporary table.

If you need to minimize the table's downtime; you can use the table copy
above, dropping the NOT NULL in the create. Lock the original table in
exclusive mode before you start then copy the table. After you drop the
original table; use the RENAME TABLE to change the name of the temporary
table. If you can't use RENAME TABLE because your tablespace setup won't
allow keeping two copies of the data then you'll have to do a second
table copy to get the data back where it belongs.

Your steps appear to be in a good order. Don't forget about triggers and
stored procedures that are dependent on the table.

Phil Sherman
Robert Stearns wrote:
Either I missed something, or ALTER TABLE does not have this
capability. Is there any way of doing it except
DROPping all constraints which mention this table,
EXPORTing the data,
DROPping the table,
reCREATEing the table without the 'NOT NULL property,
reCREATEing the INDEXes,
reloading the data,
redefining all of DROPped constraints
reCREATE the view which were marked inactive by the above.

I think is all the steps, in order.

BTW, is there a way to reactivate a view after it has been marked
inactive? How about an SQL function or procedure?


Nov 12 '05 #4
Is DB2 UDB v8.1.6 clever enough to have a swap tables which does exactly
that and no more? No deactivation of views, no requirement to drop
integrity constraints (they could be checked, if he felt it necessary),
etc.?

Philip Sherman wrote:
Changing a column from NOT NULL to nullable requires adding a "null
indicator" for the column in each existing row. This also changes the
row size! ALTER won't do this.

An alternative to export/import is to create a new, temporary, table to
hold the contents of your original one. Create it with the "NOT LOGGED
INITIALLY" clause and populate it with "INSERT INTO ... SELECT * FROM
...". This will avoid logging when populating the temporary table.

If you need to minimize the table's downtime; you can use the table copy
above, dropping the NOT NULL in the create. Lock the original table in
exclusive mode before you start then copy the table. After you drop the
original table; use the RENAME TABLE to change the name of the temporary
table. If you can't use RENAME TABLE because your tablespace setup won't
allow keeping two copies of the data then you'll have to do a second
table copy to get the data back where it belongs.

Your steps appear to be in a good order. Don't forget about triggers and
stored procedures that are dependent on the table.

Phil Sherman
Robert Stearns wrote:
Either I missed something, or ALTER TABLE does not have this
capability. Is there any way of doing it except
DROPping all constraints which mention this table,
EXPORTing the data,
DROPping the table,
reCREATEing the table without the 'NOT NULL property,
reCREATEing the INDEXes,
reloading the data,
redefining all of DROPped constraints
reCREATE the view which were marked inactive by the above.

I think is all the steps, in order.

BTW, is there a way to reactivate a view after it has been marked
inactive? How about an SQL function or procedure?


Nov 12 '05 #5
That's a great idea to add to a futures wish list. There's a lot of work
needed in the catalog to do a swap. Every one of the objects you've
mentioned has internal catalog relationships leading back to the table.
Performing a "table swap" would require concurrent modification of all
of these. At a minimum; this would lock up significant portions of the
catalog during the update and should also lock all dependent objects.
Many of today's shops, running web-initiated dynamic queries probably
wouldn't want the system-wide service interruption this would cause.

The existing mechanism for doing this forces the DBA to examine the full
impact of the change.

Database design principles expect that both current and future data
requirements are examined before defining physical attributes. Changing
a column from NOT NULL to nullable indicates that something was missed
in the original design. Nullable columns require additional programming
to process and require additional physical space to store. I've seen
many cases of a CHAR(1) column defined as nullable. This occurs even
when business rules allow a default, non-meaningful, value.

Variable length character data is another place where I've often raised
questions about nullable data. If you can specify that the length is
zero and limit the stored data to two bytes for the length field; why
define an additional byte for the nullable indicator? For a 100 row
table with 50% null values in a column; the null indicators will add 100
bytes to the data and will save 100 bytes of length fields. Every case
needs to be examined for alternatives and their impact.

Phil Sherman

Robert Stearns wrote:
Is DB2 UDB v8.1.6 clever enough to have a swap tables which does exactly
that and no more? No deactivation of views, no requirement to drop
integrity constraints (they could be checked, if he felt it necessary),
etc.?

Philip Sherman wrote:
Changing a column from NOT NULL to nullable requires adding a "null
indicator" for the column in each existing row. This also changes the
row size! ALTER won't do this.

An alternative to export/import is to create a new, temporary, table
to hold the contents of your original one. Create it with the "NOT
LOGGED INITIALLY" clause and populate it with "INSERT INTO ... SELECT
* FROM ...". This will avoid logging when populating the temporary table.

If you need to minimize the table's downtime; you can use the table
copy above, dropping the NOT NULL in the create. Lock the original
table in exclusive mode before you start then copy the table. After
you drop the original table; use the RENAME TABLE to change the name
of the temporary table. If you can't use RENAME TABLE because your
tablespace setup won't allow keeping two copies of the data then
you'll have to do a second table copy to get the data back where it
belongs.

Your steps appear to be in a good order. Don't forget about triggers
and stored procedures that are dependent on the table.

Phil Sherman
Robert Stearns wrote:
Either I missed something, or ALTER TABLE does not have this
capability. Is there any way of doing it except
DROPping all constraints which mention this table,
EXPORTing the data,
DROPping the table,
reCREATEing the table without the 'NOT NULL property,
reCREATEing the INDEXes,
reloading the data,
redefining all of DROPped constraints
reCREATE the view which were marked inactive by the above.

I think is all the steps, in order.

BTW, is there a way to reactivate a view after it has been marked
inactive? How about an SQL function or procedure?


Nov 12 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
3
by: A.M. de Jong | last post by:
Reading a lot about Nulls right now I still can't find a Technical reason to use it or not. For what I've understand is this: In an Ingres database a Null column has a standard extra storage...
10
by: Bodza Bodza | last post by:
I'm having an argument with an incumbent self-taught programmer that it is OK to use null foreign keys in database design. My take is the whole point of a foreign key is that it's not supposed...
3
by: Trevor Best | last post by:
Is there a *simple* way to change a collumn from allowing null to not null? I just unchecked "allow nulls" in EM and the SQL it generates to do this one thing is astonishing, create table, drop...
3
by: serge | last post by:
Someone was explaining me the following: 1- NULL values is NOT good for a column that has an index 2- Therefore, all columns have default values of : - 0 for numbers - " " for strings I...
3
by: binder | last post by:
I am designing a new table with a few columns that may or may not have a value on each row that is inserted. What issues determine whether to allow a NULL value to be inserted for that column or...
4
by: Eric Layman | last post by:
Hi everyone, Im puzzled by a NULL behaviour in SQL 2000 server. There is a column in the table that does not allow NULL. During data mining, the staff noted that, for that particular column,...
9
by: =?Utf-8?B?UGV0ZXJX?= | last post by:
I have a TabControl on a Windows form in which I have various tab pages each with a DataGridView, the first column of which is a DataGridViewCheckBoxColumn and subsequent columns being...
6
by: BobLewiston | last post by:
When I try to save a new (inserted) record in an SQL database, I get the following System.Runtime.InteropServices.ExternalException message: I have to either find out how to insert an...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.