Issue Details (XML | Word | Printable)

Key: DERBY-1645
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Bryan Pendleton
Reporter: Alan Baldwin
Votes: 1
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Derby

ALTER TABLE ... SET INCREMENT BY X... Turns off the "Generated By Default" identity column constraint

Created: 04/Aug/06 05:39 PM   Updated: 30/Jun/09 04:12 PM
Return to search
Component/s: SQL
Affects Version/s: 10.1.3.1
Fix Version/s: 10.2.2.0, 10.3.1.4

Time Tracking:
Not Specified

Environment: Both Ubuntu Linux, Windows XP... Java 1.4.2_x and Java 1.5
Issue Links:
Duplicate
 

Issue & fix info: Release Note Needed
Resolution Date: 13/Nov/06 08:41 PM


 Description  « Hide
I have a table which has an auto-generated key:

create table MyTable (
   TableId INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL,
   StringValue VARCHAR(20) not null,
   constraint PK_MyTable primary key (TableId)
)

I verify that GENERATED BY DEFAULT is set:

SELECT * FROM
sys.syscolumns col
INNER JOIN sys.systables tab ON col.referenceId = tab.tableid
WHERE tab.tableName = 'MYTABLE' AND ColumnName = 'TABLEID'

I'm pulling in data for which I need to preserve the ID's:

INSERT INTO MYTABLE (TableId, StringValue) VALUES (1, 'test1')
INSERT INTO MYTABLE (TableId, StringValue) VALUES (2, 'test2')
INSERT INTO MYTABLE (TableId, StringValue) VALUES (3, 'test3')

In the absense of the Derby 10.2 feature (ALTER TABLE WITH RESTART X), I try to just change the INCREMENT BY value and insert a row so that I can reset the "next" key value:

ALTER TABLE MyTable ALTER TableId SET INCREMENT BY 50

Then I insert a "dummy" record (which I will delete later...) to move the key upwards:

INSERT INTO MYTABLE (StringValue) VALUES ('test53')

However, I can now no longer insert explicit values into the primary key like this:

INSERT INTO MYTABLE (TableId, StringValue) VALUES (-999, 'test3')

I get this error: SQL Exception: Attempt to modify an identity column 'TABLEID'.

Upon checking the sys.syscolumns table again, it verifies that the table no longer has an auto-generated key, but the TableId is still an identity column.

 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Repository Revision Date User Message
ASF #474502 Mon Nov 13 20:36:51 UTC 2006 bpendleton DERBY-1495: Error modifying an identity column after altering the column
DERBY-1645: ALTER TABLE SET INCREMENT turns off "Generated By Default"

This patch changes ModifyColumnNode.bindAndValidateDefault so that it
detects the case(s) where the user is altering aspects of an identity column,
and ensures that the other aspects of that identity column are preserved and
not lost. The crucial issue is that if the column is Generated By Default,
then the DefaultInfoImpl column in the SYSCOLUMNS table needs to get
retained when the user uses ALTER TABLE to change either the start value
or the increment value; otherwise the behavior effectively switches from
Generated By Default to Generated Always.
Files Changed
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/autoincrement.out
MODIFY /db/derby/code/trunk/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java
MODIFY /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/autoincrement.sql

Repository Revision Date User Message
ASF #475081 Wed Nov 15 00:14:16 UTC 2006 bpendleton DERBY-1495: Error modifying an identity column after altering the column
DERBY-1645: ALTER TABLE SET INCREMENT turns off "Generated By Default"

Merged the fix from the trunk by svn merge -r 474501:474502 ../trunk/
Files Changed
MODIFY /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/master/autoincrement.out
MODIFY /db/derby/code/branches/10.2/java/engine/org/apache/derby/impl/sql/compile/ModifyColumnNode.java
MODIFY /db/derby/code/branches/10.2/java/testing/org/apache/derbyTesting/functionTests/tests/lang/autoincrement.sql