Saturday, July 23, 2005

What’s the difference between Primary Key and Unique Key?

Both Primary & Unique keys are Indexes.

Index - An index is a structure in a table that orders the data. It allows the database to access data quickly (In MySQL its implemented using B-tree algorithms).

Primary Key - This is an index that cannot be NULL, Primary Keys are used in building relationships between tables in a database. (an index is automatically created on the primary key). The difference between primary and ordinary keys is that there can be multiple keys, but only one primary key.

Unique Key - Unique and Index are same, the difference is, in Unique, duplicate are not allowed in any circumstances and that is enforced by database server. Primary key(s) qualify to be Unique on basis of their uniqueness.
In case, your table has 2 primary keys means that the 2 fields together form one unique key. Each field by itself may have repeating values, but both primary keys combined together must be unique.


Anonymous said...

I found this site using [url=][/url] And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!

Sorry for offtopic

Anonymous said...

Nicely explained, I guess the same would stand out for DB2 as well...

Refer following

href="">Difference between primary and unique key

Java programmer said...

That's add value to original post. worth including in my list of differences between primary and unique key.

Anonymous said...

I have look for this explanation all around the Internet.
However not new to me, more than 30 years ago using relational database tables, let me tell to whom reads this.
Primary Key and Unique Keys in the context of the table are equal. Primary keys were created for relational tables, where you relate one table with the other without the need to link fields on each table.


Popular Posts (Last 30 days)

This is a personal blog and I do not speak for my employer.