Adding generated key columns to existing tables seems to be complicated in DB2. The following code adds a new generated primary key
id to the table
tablename. Granted, the code is ugly, but it works. I was not able to come up with a better solution in time.
ALTER TABLE tablename ADD COLUMN id INTEGER NOT NULL WITH DEFAULT 0 ALTER TABLE tablename ALTER COLUMN id GENERATED BY DEFAULT AS IDENTITY REORG TABLE tablename UPDATE tablename SET id = DEFAULT ALTER TABLE tablename ALTER COLUMN id SET PRIMARY KEY
EDIT: I tested this code with DB2 9.7, but it should work on earlier versions of DB2.