ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

What is the difference between primary key and unique key in SQL Server?

user-image
تم إضافة السؤال من قبل Mohamed Omer , Software .Net Developer , Freelancer
تاريخ النشر: 2014/01/21
Naresh kumar
من قبل Naresh kumar , Sr. Software Engineer , vayamtech

There are three Major Diffrenece.

1. Primary key by default Cluster Index  -  Unique Key By default Non Cluster Index

2. Primary Key Not allow Null  - Unique Allow only1 Null Value

3. Primary Key Only one on a Table field -  Unique Key May be more than1

 

Hamid Anjum
من قبل Hamid Anjum , Team Lead \ Sr. Software Engineer , University of Business and Technology, Jeddah

Both PRIMARY KEY and UNIQUE KEY enforces the Uniqueness of the values on the column on which it is defined. Below i am going to mention major difference between PRIMARY KEY and UNIQUE KEY:

 

 PRIMARY KEY:

Null: It doesn’t allow Null values.

Because of this we refer PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINTINDEX: By default it adds a clustered index.

LIMIT: A table can have only one PRIMARY KEY Column[s]

 UNIQUE KEY:

Null: Allows Null value. But only one Null value.

INDEX: By default it adds a UNIQUE non-clustered index

LIMIT: A table can have more than one UNIQUE Key Column[s]

 

umar qadir
من قبل umar qadir , Senior .Net Developer/Team Leader , Vertika india Private Limited

I will Elobrate primary key and unique key here.

Primary key is also a unique key but unique key cannot be primary key.Because unique key can be null primary key cant be null.we have only one primary key field allowed in table but table can have multiple unique keys.eg telephone number is a unique but some one doesnt have a telephone.rollno of a student is unique and there will be always a roll number to a student that means roll number is unique and cant be null thats called primary key...

Shiraz Ahmad
من قبل Shiraz Ahmad , IT Manager , Habib Bank Limited

Both are same, additionally unique key allow null values.

Muhammad Ahsan Mirza
من قبل Muhammad Ahsan Mirza , Software Development Engineer , Remit Anywhere Solutions

There are some major differences between Primary and Unique key like, there is always one primary key column that uniquely identifies a record but there can be more than one unique key columns in a table. Unique key column(s) can contain one null value but primary key column is never nullable. However, both Unique and Primary keys enforce the uniqness of the data in the respective columns i.e; you cannot have duplicate values in the columns.

مازن كمال الدين حسن عبدالله
من قبل مازن كمال الدين حسن عبدالله , Business Intelligence Analyst , Etisalat UAE

In SQL Server, the primary key doesn't allow null values while the unique key allows only one null value in the recordset (unique). You can only define a single primary key on a table which by default creates a clustered index, while you can define more than one unique key in a table which by default created a non-clustered index.

المزيد من الأسئلة المماثلة

هل تحتاج لمساعدة في كتابة سيرة ذاتية تحتوي على الكلمات الدلالية التي يبحث عنها أصحاب العمل؟