Reputation: 3586
I have a table which already contains some rows and does not have a primary key.
I want to add a primary key now, but for that I need to set unique values for the primary key field of the existing rows. How can this be done, using the newly added generator?
Upvotes: 2
Views: 1860
Reputation: 5481
Primary key does not generate any values by itself. It is constraint. In your example you need do next:
Example:
CREATE TABLE test (a varchar(20));
COMMIT;
INSERT INTO test (a) VALUES ('A');
INSERT INTO test (a) VALUES ('B');
INSERT INTO test (a) VALUES ('C');
COMMIT;
ALTER TABLE test ADD pk INTEGER NOT NULL;
CREATE GENERATOR g_test;
COMMIT;
UPDATE test SET pk = GEN_ID(g_test, 1);
ALTER TABLE test ADD CONSTRAINT test_pk
PRIMARY KEY (pk);
Upvotes: 5