Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 250
  • Last Modified:

How to Add a Primary Key to the existing table

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
Raj
0
nrajasekhar7
Asked:
nrajasekhar7
  • 2
1 Solution
 
cnerminCommented:
right click and set Primary key
0
 
JPrzybyszewskiCommented:
Hi

You can't have 2 primary keys in one table. That's contradictory to the relational theory - please chech the page: http://databases.about.com/cs/administration/g/primarykey.htm

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)
      [ COMPUTE STATISTICS ];

Regards
Jarek
0
 
OP_ZaharinCommented:
- 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);
0
 
OP_ZaharinCommented:
- or if you need that column to be index for search performance, use the following:

CREATE INDEX indexname_idx ON tablename (column1);
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now