Difference between a primary key and a unique key in SQL?

Difference between a primary key and a unique key in SQL?



Views: 4956 | Community Opinion: 9

Tags..  SQL Server  DBA  Asp.Net Interview  Database

Bookmark this page..



Ask a New Question Go to Home

Community Opinion/Answers
 
Sam Smith Said..

In SQL both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a non-clustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.




Lakshmi Said..

There can be more than one unique values for the columns in a table.

There is only one primary key for a column and primary key can be associated with other tables with foreign key relationship.

This is not possible with Unique key




Lakshmi Said..

There is a correction

There is only one primary key for a table.




swathi Said..

primary key doesnot allow duplicate values and doesnot allow null.
unique key also doesnot allow duplicate values but allows one null value.




; Said..

k




Rajesh Said..

There are more then one primary key in a table.Primary Key do not allow any null value.but unique key allow one null value.




kiran.d Said..

when we compare both primary key and unique key,they appear to be similar but their functionalities may differ,bcoz primary key is to serve as identity b/w the two tables as unique key may not have.




vivek k Said..

Unique Key: Identify the row by uniquely, means if you are inserting a records into the table suppose you already inserted a record with ID 101 and another record you are inserting with the same ID then it will give an error(unique key voilation).

Prmary Key:Once you define a primay key on a particular column then a column become a mandatory field.That means you need to insert something on it, you can not leave as a blank otherwise it will give an error as primary key voilation.




suresh Said..

what is actual defination of Dot Net??






Register or Login to Post Your Opinion