• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why use a Unique Key when you can use a Primary Key?

 
Anderson Goldman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this is my first post, and I'm not sure if it should go in this topic or not, but here it goes. I was reading about unique keys and primary keys in Wikipedia, and it seems that a primary key is a special case of a unique key, in that a primary key cannot hold a NULL value.

I was wondering:
1) Why are unique keys called "unique keys"?
2) Why use a unique key when you can use a primary key?

Thanks in advance.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A PK is considered to be an unique identifier of the row. It should never be subject to changes. For example the ID of the User.

An UK is considered to be unique thoroughout the whole column. It is not necessarily an identifier of the row as it may be subject to changes. For example the username or email address of the User.
 
Anderson Goldman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thank you!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!