How to Add a Primary Key  to the existing table

Posted on 2011-10-30
Last Modified: 2012-05-12
Hi all

I am having table,already in that table iam having a Primary key  column , For that  now i want to add another primary Key to the Other Column , Please suggest!!

Thanks in advance
Question by:nrajasekhar7

    Expert Comment

    right click and set Primary key
    LVL 3

    Expert Comment


    You can't have 2 primary keys in one table. That's contradictory to the relational theory - please chech the page:

    You can though add another Unique inex (unique key) to your table using following syntax:

    The syntax for creating a index is:

        CREATE [UNIQUE] INDEX index_name
          ON table_name (column1, column2, . column_n)

    LVL 23

    Accepted Solution

    - do you require a combination of both column to be as primary key? then you can drop the existing primary key and create a new one with combination of both column:

    - drop prmarykey
    ALTER TABLE tablename DROP CONSTRAINT primarykeyname_pk;

    - re-create primary key with 2 column:
    ALTER TABLE tablename ADD CONSTRAINT primarykeyname_pk PRIMARY KEY (column1, column2);
    LVL 23

    Expert Comment

    - or if you need that column to be index for search performance, use the following:

    CREATE INDEX indexname_idx ON tablename (column1);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
    Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now