开发者

Difference between Primary Key, Unique Key and candidate key

开发者 https://www.devze.com 2023-01-13 19:35 出处:网络
What is difference between P开发者_如何学编程rimary Key, Unique Key and candidate key?Super Key is the set of one or more column (ie attributes) which uniquely identifies a record.

What is difference between P开发者_如何学编程rimary Key, Unique Key and candidate key?


Super Key is the set of one or more column (ie attributes) which uniquely identifies a record.

Candidate key is a minimal Super key.(it mean we cant remove any attributes from it otherwise it will not remain Superkey anymore).

Primary Key is a arbitrary selected Candidate key. There must be only and only One primary key. We can choose any candidate key as a Primary key. Other candidate keys which are not chosen as Primary are called Alternate Keys.

If Primary Key have more then one column (or attributes) ,it is called Composite Key.


Candidate key is a minimal (i.e. irreducible) key, unique key is a pleonasm, and primary key is an obsoleted concept still surviving from the days when people thought it possible for one key to be "more unique" than any of the others.


Candidate keys - It is a column that can uniquely identify all columns for a specific row or instance of your database table. Your passport ID is a good example of a Candidate key and when using this ID you can retrieve a persons name, surname and other details related to that specific person.

Primary key - There may be a few Candidate keys on a specific database table which is unique and can be used to identify a specific instance in a table e.g. Vehicle registration number, Chassis number, engine serial number etc... but remember there can only be one candidate key that will be used as a primary key. Difference between Candidate Key vs Primary Key:- ) Both Primary and Candidate keys can uniquely identify records in a table on database.

2) Both Primary and Candidate keys are has constraints UNIQUE and NOT NULL.

3) Primary key or Candidate keys can be either single column or combination of multiple columns in a table.


Google does not work anymore?

http://en.wikipedia.org/wiki/Candidate_key

http://en.wikipedia.org/wiki/Unique_key


Candidate Key – A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.

Primary Key – A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key. One needs to be very careful in selecting the Primary Key as an incorrect selection can adversely impact the database architect and future normalization. For a Candidate Key to qualify as a Primary Key, it should be Non-NULL and unique in any domain. I have observed quite often that Primary Keys are seldom changed. I would like to have your feedback on not changing a Primary Key.

For more articles


All the answers are missing physical representation and some of them are incomplete. So here is the difference by graphical representation

Difference between Primary Key, Unique Key and candidate key


Explanation

Super Key

Super key is a set of one or more than one keys that can be used to identify a record uniquely in a table.

Example : Primary key, Unique key, Alternate key are subset of Super Keys.

Candidate Key

A Candidate Key is a set of one or more fields/columns that can identify a record uniquely in a table. There can be multiple Candidate Keys in one table. Each Candidate Key can work as Primary Key.

Example: In above diagram ID, RollNo and EnrollNo are Candidate Keys since all these three fields can be work as Primary Key.

Primary Key

Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table. It can not accept null, duplicate values. Only one Candidate Key can be Primary Key.

Alternate key

Alternate key is a key that can be used as a primary key. Basically it is a candidate key that is currently not being used as the primary key.

Example: In above diagram RollNo and EnrollNo becomes Alternate Keys when we define ID as Primary Key.

Composite/Compound Key

Composite Key is a combination of more than one fields/columns of a table. It can be a Candidate key, Primary key.

Unique Key

Uniquekey is a set of one or more fields/columns of a table that uniquely identify a record in database table. It is like Primary key but it can accept only one null value and it can not have duplicate values. For more help refer the article Difference between primary key and unique key.

Foreign Key

Foreign Key is a field in database table that is Primary key in another table. It can accept multiple null, duplicate values. For more help refer the article Difference between primary key and foreign key.

Example : We can have a DeptID column in the Employee table which is pointing to DeptID column in a department table where it a primary key.

source


A composite key is a primary key which has more than 1 column. For example consider the many to many relation between student and courses. Here we need another table , lets say studentcourses.

This studentcourses table will have student_id and course_id as its columns. This will form your composite key.

Candidate key is a set of columns which can uniquely identify the values in a table and can act as a unique key. One of these candidate keys will become the primary key and the rest will become alternate keys.

Unique key as the name suggests , is used to uniquely identify a value in a table. For example Emp_id etc. Every candidate key will act as a unique key. Unique key can never be a NULL value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号