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

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


Views: 34327 | Community Opinion: 33

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.




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.




Ravindrasinha Said..

primary key is not be null ...
unique key will be null , this is major diffrent




Pankaj Kumar Rajput Said..

It seems to me that a primary key is really just a clustered index, unique constraint, and not null constraint. Do primary keys add any real value.
A table can have at most one primary key, but more than one unique key. A primary key is a combination of columns which uniquely specify a row. It is a special case of unique keys. One difference is that primary keys have an implicit NOT NULL constraint while unique keys do not. Thus




S.S.Hemant Said..

Primary Key: primary key is uniquely identifies each row in database.
*NOT NULL
*Must be unique value
*each table shoud have one primary key
-----------------------------------------------
Unique key: Unique Key is doesn't allow NULL Value.
*May ba NULL
*Must be Unique value




Jhilom Haldar Said..

One more attribute you missed which is not so big point but it is very major point sometimes to define value in table that

Primary key Supports Auto Increment value
but
Unique doesn't support auto Increment value

So the differences are mainly

1) Primary Key can't not be Null where as Unique may be Null some times if declared.
2) Maximum one Primary Key can be declared in a table where we can declare many Unique keys.
3) Primary key can be associated with another table's Foreign Key whereas It is impossible for Unique field.
4) And with Auto Increment field we can generate ID Automatically where we are unable to do that with Unique.




kiran mishra Said..

unique and primary both primary key and unique enforce uniqueness of the column on which they are defined.there is one primary key in the table and many unique key.primary key can't be null value where as unique may be null.one primary key and many unique key in a single table .which uniquely identifie the table




kiran mishra Said..

unique and primary both primary key and unique enforce uniqueness of the column on which they are defined.there is one primary key in the table and many unique key.primary key can't be null value where as unique may be null.one primary key and many unique key in a single table .which uniquely identifie the table




sangeeth Said..

primary key is not be null ................
unique key will be null , this is major diffrent...............................................................................................................................................................................................




mohan,atul Said..

major difference is
primary key doesnt allow null value,and supports auto increament where as unique key allow null value and doesnt support autoincreament...............................................................................................................................................................................................




Pradip Said..

unique and primary both primary key and unique enforce uniqueness of the column on which they are defined.there is one primary key in the table and many unique key.primary key can't be null value where as unique may be null.one primary key and many unique key in a single table .which uniquely identifie the table





Please enter your name.
*your opinion/answer:
Please enter your name.
*your opinion/answer: Please enter your name.
*your opinion/answer: Please enter your name.
*your opinion/answer: Please enter your name.
*your opinion/answer: Please enter your name.
*your opinion/answer:




khan Asim Said..

unique and primary both primary key and unique enforce uniqueness of the column on which they are defined.
Primary key doesnot allow null value but unique key allows
null value. We can declare only one primary key in a table
but a table can have multiple unique key(column assign).




vishal Adhau Said..

there is following difference between primary key and unique


primary key:
*apply on only one column in single table
*not allow null values
*create clusterd index on column
Unique Key:
*apply on multiple column in single table
*allow onle one null value
*create non clusterd index on cloumn




Dipak Bhangale Said..

primary key doesnt allow null value.........................................................................................................................................................................
unique key will be null.......................




Rahul Singh Said..

This is one of the best answer so far, I have read online. Just useful information. Very well presented. I had found another good collection of sql keys over internet.
please check out this link...

http://mindstick.com/Articles/d837d16c-d483-4948-ad18-41b405c011bd/?Important%20SQL%20Keys




Bharti Said..

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 nonclustered index by default. to Know more Click Here




VIVEK.V Said..

primary key cannot have null value and has uniqueness.a table can have only one primary key.

unique key has uniqueness but can have null values.
a table can have one or more unique key. ...............




VIVEK.V Said..

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

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)
)Note: In the example above there is only ONE PRIMARY KEY (pk_PersonID). However, the value of the pk_PersonID is made up of two columns (P_Id and LastName).



--------------------------------------------------------------------------------




Indianapolis Colts Jersey Said..

A wonderful game is definitely astounding effectiveness,Cheap Colts Jersey unfortunately benefits a people and artists as well as the TV audiences.Manning Jersey all not only thrills a people,any excitement as well overpowers Coach Outlet who check out games plus physical activities.While the viewers pick and choose a common hobby,Coach Factory Outlet will choose a common company in addition to player,and can even spend millions of dollars on the popular Coach Online Outlet and teams simply by doing prophecies on what groups should profit.To enable Cheap Coach Bags to make money every time they watch the experience at the same time.Sporting events picks now have develop into incredibly methodical. Coach Outlet Stores is not loosely sorted out to any extent further.UGG Boots Outlet are several web pages this let the zealous activities potential customers and the wonderful keen on wagering turned into paid members located at a free or even at a small price.North Face Outlet is just like a better way connected with gambling and also bets regarding activities together with large agrees with about whatever online game is called sporting activities picking.




Neha Said..

Primary key automatically create Clustered index when it is created.
A table created with Primary key restricts null values.

Unique key automatically create Non clustered index when it is created.
A table created with Unique key zllows insertion of null values.




Naeem Rao 0300-6737481 Said..

Primary key cannot contain null value
unique key contains at least one null value;
...........................................................
..............................................................
............................................................
......................................................




Rana Usman Said..

primary key by default create clustered index
unique key by default create non-clustered index,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
................................................................
.......................................................




Anil Said..

Primary key does not accept null values
unique key accept null values.
..........................................................
............................................................
..........................................................
..........................................................




Sharad Said..

There will be one primary key table.
More than one unique key in a table.
.............................................
................................................
................................................
..............................................




jeet Said..

kuch bhi bol doge kya???????????
ye sb jhuth hai gumrah kar rae h tmhe?????????/ mauj kato

kaha pade ho chakkar me jb koi nai h takkar samjheo.........................................................................................................................................................................




foxin Said..

canada goose jacket have a wonderful amount of purchasers. They may be manufactured having a fluffy inner coating to assist keep ones body warm as well as have deep pockets and in addition hardwearing.To satisfy the winter season of 2011, canada goose sale shops hold an activity of discount rates at the earliest. We offer comprehensive collections of Canada Goose apparel, this kind of as Canada Goose coats, Canada Goose Men Vest and Canada Goose guys jackets, etcetera. Welcome to Canada Goose Outlet on the net shop, the very best and least expensive Canada goose jacketsgoods factory shop, free shipping to all globe. In addition, True Religion supplies such huge quantity youll uncover jeans for each occasion. Aside from you acquire a person set of two and uncomplicated True Religion jeans, Canada Goose Men Down Jackets and in addition the developer jeans which will perfectly complement your glimpse and model.




Coach Outlet Said..

Cheap Coach Bags purses are so popular in United States that no body has not heard it.Now it seems sucess.As a designer handbag, one Coach Outlet Canada may cost you hundreds of dollars.It is quite costy for most people.But comparing with other top brands in the world, the price of Cheap Colts Jersey is still acceptable.And the price of coach is also quite reasonable.You do not need to save several months to buy a genuine Coach Factory Outlet.The quality of genuine Coach Online Outlet is so amazing.Some of them are made of top end genuine leather.So a Supra TK Society really worth you spending hundreds of dollars.In fact, women wil do this because they know Coach Outlet Stores has a great looking and amazing quality.There are some designer UGG Boots Outlet which are sold at some outlets.Also, you can find many cheap coach bags at many coach outlet across United States.At coach outlet stores, you will discover that a designer North Face Outlet is not as expensive as you think.






What do you think? Add your opinion/answer
*Name
*your opinion/answer: