c# (Csharp) and SQL interview question :- What is the difference between unique key and primary key?

preview_player
Показать описание

See our other Step by Step video series below :-

Unique key can have nulls
Primary key cannot have nulls.
In a single table we can create multiple unique keys.In a single table we can have only one primary key.
=============================================
Unique key creates a non-clustered index by default.Primary Key created a clustered index by default.
================================================
Both unique keys and primary keys can be referenced by foreign key.

Рекомендации по теме
Комментарии
Автор

Do not miss our Interview Question video series

dnfvideo
Автор

Hey you are doing a great job, very clear explanations. Only a few minor mistakes in this video that might confuse the viewers, such as "unique key allows NullS". The reason it is called unique key is because it doesn't accept duplicate values and if you have multiple nulls you will end up with duplicates. Anyways it was a minor mistake, great job! :)

faisalmollick
Автор

Thanks, ,I was stucked on these diffrence which is cleared ..Thank You

ankitatripathi
Автор

Sir - very good explanation on difference between unique key and primary key

satishnagotkar
Автор

it's really powerful 
Thanks for This Update always .

uptrainlovers
Автор

A table can have only one primary key which may consist of single or multiple fields (columns). When multiple fields are used as a primary key, they are called a composite key.

jj
Автор

Nice one, Subscribed.Please keep it up...

ShinyArjunSingh
Автор

@yacirkhan That's not necessarily true. Some (Most) DBMSs don't consider NULL values when determining uniqueness.

breaktheshell
Автор

Good catch waqas, yes you are right. But if you create it will allow to do the same. Thanks for the same. Difficult to go back and change the whole video... ;-)

shivkoirala
Автор

Hi, there is mistake in this video. When you explain point no 2 i.e. multiple unique key in a single table (3:24 Minute), you mistakenly not defined then 2nd column as a unique. It remains as a normal index. Please rectify, if possible.. Anyway thanks for sharing and your great effort....

amitava
Автор

What are unique keys created/used for typically?

damonp
Автор

@breaktheshell
I know .. but this video is for SQL server .. and SQL server does not allow more than one NULL if unique constraint is enforced.

yacirkhan
Автор

Unique Key cannot have nullS. it can only contain a single "NULL" value. Another null will not be a unique value :)

yacirkhan
Автор

Didnt explain why unique keys and primary keys use non and clustered indices. it depends on the way the rows of that table or any table generally needs to be accessed by queries or queried since clustered indices sort and store data rows in table based on their key values i.e. primary keys (one column or many) they require only one defined primary key constraint on the table.

ahsansiddiqui
Автор

Sir please help me how to connect between Microsoft SQL Server and Microsoft Visual Basic.

bikashsharma
Автор

I guess we can multiple Primary key as a composite key - Primary Key ( emp_id, emp_DOB) shouldn't this work?

avinash
Автор

on id column you didn't select unique2 option as 'is unique = yes'. 3:31.

saqibaslamganatra
Автор

I usually like ur short videos and thank you for that. But????the third point is not clear! The column property says NO clustered index for unique key.
So the table may has two forms: 1, heap
2, table with nonclustred index
And a nonclustred index is created on a different data page not on the actual data page. It's not about sorting the data or allowing nulls on the actual datapages.
No 'clustered index' doesn't necessarily mean it has nonclustred index

sisayg
Автор

Okay, what is clustered index? Can i say that the unique key as an alternative key? ._. I kinda confuse there.

TheRoxasth
Автор

In the second point when you tried to show multiple unique keys.. You actually created first as unique key but second as index(uniqu1 - unique key.. uniqu2 - index). Even though the concept is right, you can correct the demonstration.

rajeshkumar-qwlx