There is an existing table with three columns which all form the primary key.
What is the best way to add a unique column to it? Prefferably creating a sequence for it while Im at it.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can add an additional column to a table with an
ALTER TABLEYou can create a new sequence and then create a trigger that populates the new column using that sequence
If you are using a version of Oracle prior to 11g, your trigger would need to do a
SELECTfromDUALin order to populate the:new.new_column_namecolumn rather than doing a direct assignmentPresumably, you’d also want to initialize all the existing rows using the sequence value before you started inserting new rows
But it seems very odd to add a new sequence-generated column to an existing table with a composite primary key unless the goal was to use that new column as the primary key. The whole point of having a sequence generated column is so that you have a stable, synthetic primary key that doesn’t depend on the actual business data. So it would seem to make much more sense to drop the existing primary key, add the new column, populate the data, declare the new column as the new primary key, and then define a unique constraint on the three columns that comprised the old primary key.