What is difference between "Primary key" and "Unique key"?

Showing Answers 1 - 63 of 63 Answers

Culver_lake

  • Mar 18th, 2006
 

A unique key has the property that no two values in the range are the same.  A primary key is chosen from among all the unique keys of a table to participate in referential integrity constraints. Primary keys are unique by definition.

  Was this answer useful?  Yes

prema

  • Mar 20th, 2006
 

Primary key and unique are Entity integrity constraints

Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist and no null values are entered.

Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values. (In oracle, one null is not equal to another null).

Rana Irfan

  • Mar 24th, 2006
 

Primary key can't be null but unique key can be null.

  Was this answer useful?  Yes

Culver_lake

  • Mar 24th, 2006
 

sorry rana, unique constraints do not include null.  Null is not a value it's a state where the value is unknown. In DB2 a unique constraint MUST include the NOT NULL clause.

Even if allowed, the index used to guarantee uniqueness could at most have one entry for NULL. This would assume that NULL was treated as a value. DB2 has a special property for an index namely: UNIQUE WHERE NOT NULL, which allows multiple instances of NULL but forces unique values. This is good for M:1 relations where the many side is optional in the relationship. DB2's index is a very special case. Don't go looking for it in other RDBMS products.

  Was this answer useful?  Yes

pramod kumar

  • Apr 6th, 2006
 

1)unique key can be null but primariy key cant be null.

2)primariy key can be refrenced to other table as FK.

3)we

  Was this answer useful?  Yes

pramod kumar

  • Apr 6th, 2006
 

1)unique key can be null but primariy key cant be null.

2)primariy key can be refrenced to other table as FK.

3)we can have

  Was this answer useful?  Yes

pramod kumar

  • Apr 6th, 2006
 

1)unique key can be null but primariy key cant be null.

2)primariy key can be refrenced to other table as FK.

3)we can have multiple unique key in a table but PK is one and only one.

4)PK in itself is unique key.

Karthikeyan

  • Apr 6th, 2006
 

UNIQUE KEY accepts null value whereas primary key does not accept null value and

a table can have more then one unique key column ,but a table can have only one primary key column

primary key creates clustered index ,but unique key creates non clustered index.

  Was this answer useful?  Yes

dhk

  • Apr 20th, 2006
 

Primary which allows only unique+not null values

where has unique allow unique and null values because  a null value is not equal to a null value          

  Was this answer useful?  Yes

Giridhar Ks

  • May 7th, 2006
 

Primary Key:- Primary Key is an attribute or a set of attributes in a table which uniquely identifies a record (row) in a table and no two records (rows) in the table can have the same values for all the columns comprising the primary key.

Unique Key:- A unique key is a key which stores unique values (no duplicates) for that particular column.

Difference between Primary Key & Unique Key:-

1. A Unique key can store a null value and primary key cannot store any null value.

2. A primary key can be references to another table as a Foreign Key.

  Was this answer useful?  Yes

Vinay Kumar

  • May 29th, 2007
 


one more difference is:-

Primary key creates the Clustered index, but unique key creates the Non clustered index.

  Was this answer useful?  Yes

Ajitabh Kumar

  • Jun 8th, 2007
 

Primary key accepts only non duplicate value and not accept null value and only one primary key may have in table


Unique key can have more than one null value , there may be more than one unique key in a table

  Was this answer useful?  Yes

Primary key: -
                     1) Can only one in a table
                     2) We can't insert duplicate value or same value
                     3) Can use as a foreighen key for other table
                     4) It never allow Null values.
Unique key: -
                     1) There can be more than one unique key in one table
                     2) Unique key can have Null Values
                     3) It can't be a candidate key

  Was this answer useful?  Yes

sravee123

  • Jun 9th, 2008
 

Primary can't be null but unique key allows one null value..
&
PRIMARY KEY:In a table we can create only one primarykey 
UNIQUE KEY: we can create no. of unique keys in a table..

  Was this answer useful?  Yes

shakilag

  • Jun 30th, 2009
 

Following are the differences between Primary and Unique constraints:

1) The columns that are primary keys cannot contain NULL values as primary key is meant to uniquely identify a record in a table. The columns that make a unique key can be nullable as unique key just restricts a column not to hold duplicates .

2) There can be only one primary key per table while there can be N number of unique keys defined in a table.

3) By default a clustered index is created on a primary key while a non clustered index is created on a unique key.

dj_dj_dj

  • Jul 1st, 2009
 

Primary Key is Key which don't allows any duplicate values or null values to be inserted. Whereas, Unique key also don't allows duplicate values to be inserted but it allows null values to be inserted because null values cannot be compared to anything.

Regards
Dharmendra Jaiswal
CMS.

  Was this answer useful?  Yes

b.ganeshna

  • Feb 17th, 2010
 

The main difference of primary key and the unique key is:

Primary key does not allow null values as well as duplicate values.

But incase of unique key it does not allow duplicate values but it allows null values.

  Was this answer useful?  Yes

girlgeek1

  • Mar 2nd, 2010
 

A primary key is both unique and not null.  There can only be one primary key per row.  It is what defines the row.  The primary key is the 'name' of the row.

  Was this answer useful?  Yes

sktest

  • Jul 11th, 2010
 

Primary key is used to identify row which is unique and not null
Unique key is used to identify distinct row, it can contian null value

  Was this answer useful?  Yes

machomanic

  • Jul 12th, 2010
 

Primay Key: Unique field in the table which cannot be duplicated and cannot be NULL.
Unique Key: Uniqueu field in the table but can be NULL

  Was this answer useful?  Yes

ajayraj

  • Dec 30th, 2010
 

The Difference between Primary Key and Unique is as follows :

1. Primary Key does not allow null value but Unique key allow.

2. In a Table There will be only one primary key but Unique key cam be more than  one.

3. In case of creating a master - detail relation between two tables
If You are referencing Primary key in another table then no need to specify column name but in case of unique you must have to specify column name.

4. Primary Key creates clustered index where as unique creates non clustered index.


Example for Primary Key:-


CREATE TABLE aa (a1 NUMBER(5) PRIMARY KEY,b1 VARCHAR2 (20));


CREATE TABLE bb (a2 NUMBER(5),b2 VARCHAR2 (20), FOREIGN KEY(a2) REFERENCES aa);


Example for Unique Key :-

CREATE TABLE cc (a3 NUMBER(5) UNIQUE,b1 VARCHAR2 (20));

CREATE TABLE dd (a4 NUMBER(5),b4 VARCHAR2 (20), FOREIGN KEY(a4) REFERENCES cc(a3));


  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions