views:

32

answers:

3

Is it possible set more than one primary key for a table?

If it is possible as composite key - so how it can be used with application??

ADVANTAGES .?

A: 

You can't. There can only be a single primary key on a table (that's why it's called primary..). The only thing you can do is to specify a PRIMARY KEY on multiple columns (but that depends on the database layout:

CREATE TABLE Persons
(
  P_Id int NOT NULL,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)
halfdan
is it composite key..?? how it is used..?/
pvaju896
+4  A: 

No, that's not possible.

However:

You can have more than one field in the primary key.

You can also add unique indexes to fields that are not in the primary key.

Guffa
A: 

No, you cannot have more than one primary key in a table. Yes, a primary key can consist of multiple fields, but, considering your question, I don't think that is what you need. I'm not sure what it is that you need exactly, but a primary key has a few capabilities that you are probably looking for in another field:

Unicity: If you are in need of another field that has a unique value throughout all records, you can apply a UNIQUE constraint:

ALTER TABLE <table identifier> 
  ADD [ CONSTRAINT <constraint identifier> ] 
  UNIQUE ( <column expression> {, <column expression>}... )

Speed: If you have a field that is searched for very often, and need to speed up your query, you can add an index to that field.

CREATE INDEX <index identifier>
   ON <table identifier> (<field name>)
Rob Vermeulen