3
Types of Keys in DBMS According to Codd , Date , and all other experts, a key has only one meaning in relational theory: it is a set of one or more columns whose combined values are unique among all occurrences in a given table. A key is the relational means of specifying uniqueness. 1. Unique Key 2. Primary Key 3. Super Key 4.Candidate Key 5. Alternate Key 6. Composite Key 7. Foreign Key Primary keys are used to uniquely identify rows in a relational database design . It usually comprises of a single table column, but may consist of a multiple columns as well. Each row in a table must have a distinct value (or a set of values) in the column marked as the primary key. It is possible for a table to have more than one column with unique values in the table, however only one primary key can be defined. Each column with distinct values is called a unique key. It should be kept in mind that a unique key must have distinct values in all possible rows, not just the currently existing ones. The Social Security Number, ISBN on books etc are good examples of unique keys. A primary key can be regarded as a special unique key. No row can have an empty value (called the NULL) in the primary key column, where as a unique key is allowed to hold a NULL. Primary keys can be defined at the time of table creation or can be added in after the table has been created. Difference B/w Unique & P.Key A. Primary key is Combination of Unique, Not Null & has the irreducible property. Super Key If we add additional attribute to a primary key the resulting combination would still uniquely identify as instance of entity set, this augmented key are called super key. Eg. Roll no and name ie Super key can still be reducible to have form a primary key. Candidate Key There can be several key's in a given relation, that can be identified uniquely. Those can be called Candidate keys Minimal Super key is called as candidate key An attribute or set of attributes that can identify uniquely in a given relation - SUPER KEY. An attribute alone that can identify uniquely in a relation is called Primary Key. There can be several key's in a given relation, that can be identified uniquely. Those can be called Candidate keys.

Types of Keys in DBMS

Embed Size (px)

Citation preview

Page 1: Types of Keys in DBMS

Types of Keys in DBMS

According to Codd, Date, and all other experts, a key has only one meaning in relational theory: it is a set of one or more columns whose combined values are unique among all occurrences in a given table. A key is the relational means of specifying uniqueness.1. Unique Key 2. Primary Key 3. Super Key 4.Candidate Key 5. Alternate Key 6. Composite Key7. Foreign Key

Primary keys are used to uniquely identify rows in a relational database   design . It usually comprises of a single table column, but may consist of a multiple columns as well. Each row in a table must have a distinct value (or a set of values) in the column marked as the primary key. It is possible for a table to have more than one column with unique values in the table, however only one primary key can be defined. Each column with distinct values is called a unique key.It should be kept in mind that a unique key must have distinct values in all possible rows, not just the currently existing ones. The Social Security Number, ISBN on books etc are good examples of unique keys.  

A primary key can be regarded as a special unique key. No row can have an empty value (called the NULL) in the primary key column, where as a unique key is allowed to hold a NULL.  

Primary keys can be defined at the time of table creation or can be added in after the table has been created. Difference B/w Unique & P.KeyA. Primary key is Combination of Unique, Not Null & has the irreducible property.

Super KeyIf we add additional attribute to a primary key the resulting combination would still uniquely identify as instance of entity set, this augmented key are called super key.Eg. Roll no and nameie Super key can still be reducible to have form a primary key.

Candidate KeyThere can be several key's in a given relation, that can be identified uniquely. Those can be called Candidate keysMinimal Super key is called as candidate key

A superkey is defined as a set of attributes of a relation for which it holds that in all relations assigned to that variable there are no two distinct tuples (rows) that have the same values for the attributes in this set.As an exampleCode:

+-------------+------------+------------+| Roll Number | First Name | Last Name |+-------------+------------+------------+| CSU0001 | Shabbir | Bhimani || CSU0002 | SomeName | SurName || CSU0003 | Larry | page |+-------------+------------+------------+

Now here we have the following as super keys

1. Roll Number | First Name - SK1

2. Roll Number | First Name | Last Name  - SK2

An attribute or set of attributes that can identify uniquely in a given relation - SUPER KEY.An attribute alone that can identify uniquely in a relation is called Primary Key.There can be several key's in a given relation, that can be identified uniquely. Those can be called Candidate keys.

Page 2: Types of Keys in DBMS

Super Key = Combination of (Unique Key + Non Unique key)

A candidate key of a relationship is a set of attributes of that relationship such that there are no two distinct tuples with the same values for these attributes.

In simple example candidate key is a minimal superkey, i.e. a superkey of which no proper subset is also a superkey.Roll No is the Candidate key for SK1 & SK2

Since a relation is a set(no duplicate elements), it holds that every relation will have at least one candidate key (because the entire heading is always a superkey). For practical reasons RDBMS usually require that for each relation one of its candidate keys is declared as the primary key

Alternate keyFor example, a relational database with a table "employee" could have attributes like "employee_id", "national_insurance_number", and so on. In this case, both "employee_id" and "national_insurance_number" serve as unique identifiers for a given employee, and could thus arguably be used for a primary key. Hence, both of them are called "candidate keys". If, for example, "national_insurance_number" was chosen as the primary key, "employee_id" would become the alternate key.

Composite keyA primary key that consists of two or more attributes is known as composite key

Foreign keyA foreign key is a referential constraint between two tables.[1] The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. The columns in the referencing table must be the primary key or other candidate key in the referenced table. The values in one row of the referencing columns must occur in a single row in the referenced table

A table may have multiple foreign keys, and each foreign key can have a different referenced table. Each foreign key is enforced independently by the database system. Therefore, cascading relationships between tables can be established using foreign keys.