cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Why does ALTER TABLE only allow for one added non-nullable column?

VolkerBarth
Contributor
3,835

When I add a non-nullable column to a non-empty table, I certainly have to add a default.

IIRC, with older versions, I had to use a two-step approach because adding a non-nullable column on non-empty tables was not allowed at all):

ALTER TABLE MyTable ADD MyCol1 bit NULL DEFAULT 0;
ALTER TABLE MyTable MODIFY MyCol1 NOT NULL;

With newer versions (say, v12), I can do that as desired in one step:

ALTER TABLE MyTable ADD MyCol1 bit NOT NULL DEFAULT 0;

However, this fails with an -116 SQL CODE error ("Table must be empty") when trying to add more than one such column:

ALTER TABLE MyTable
   ADD MyCol1 bit NOT NULL DEFAULT 0,
   ADD MyCol2 bit NOT NULL DEFAULT 0;

(The same is true for adding table contraints, AFAIK.)

Why is that limitation - and is it documented anywhere?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member

The limitation was not documented, and has been partially relaxed for SQLA 16 with performance improvements to ALTER TABLE statement. The limitation itself was related to implementation.

VolkerBarth
Contributor
0 Kudos

Yes, v16.0.0.2158 does allow several such additions in one single statement, so thanks for the clarification.

(Whereas altering two table check constraints in one statement still does lead to an "ALTER TABLE conflict" in v16, too - well, I can live with that:)

Answers (0)