Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6899609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:26:35+00:00 2026-05-27T07:26:35+00:00

There is an existing table with three columns which all form the primary key.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:26:35+00:00Added an answer on May 27, 2026 at 7:26 am

    You can add an additional column to a table with an ALTER TABLE

    ALTER TABLE table_name
      ADD( new_column_name NUMBER UNIQUE );
    

    You can create a new sequence and then create a trigger that populates the new column using that sequence

    CREATE SEQUENCE sequence_name;
    
    CREATE TRIGGER trigger_name
      BEFORE INSERT ON table_name
      FOR EACH ROW
    BEGIN
      :new.new_column_name := sequence_name.nextval;
    END;
    

    If you are using a version of Oracle prior to 11g, your trigger would need to do a SELECT from DUAL in order to populate the :new.new_column_name column rather than doing a direct assignment

    CREATE TRIGGER trigger_name
      BEFORE INSERT ON table_name
      FOR EACH ROW
    BEGIN
      SELECT sequence_name.nextval
        INTO :new.new_column_name 
        FROM dual;
    END;
    

    Presumably, you’d also want to initialize all the existing rows using the sequence value before you started inserting new rows

    UPDATE table_name
       SET new_column_name = sequence_name.nextval
     WHERE new_column_name IS NULL
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to produce a diagram showing existing tables and their relationships
I have an existing database with tables which each have 4 partitions (there are
Is there an existing application or library in Java which will allow me to
Is there an existing PL/SQL method which takes a string and returns the same
In SharePoint 2003 and 2007, there was a table called AllLists which had a
Just a question about best-practices when upgrading an existing database. Assuming there will be
I have counter for unique column in my table. Now I have list where
There are existing options for doing bulk insert into a single table with EF
Are there existing template extract libraries in either python or php? Perl has Template::Extract
Is there existing software for discriminative reranking, such as that used by the Charniak

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.