DBMS Keys
DBMS Keys
Example:
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
Super Key
Primary Key
Candidate Key
Alternate Key
Foreign Key
Compound Key
Composite Key
Surrogate Key
Example:
Example:
Example:
In this table, StudID, Roll No, Email are qualified to become a primary key. But
since StudID is the primary key, Roll No, Email becomes the alternative key.
Example: In the given table Stud ID, Roll No, and email are candidate keys which
help us to uniquely identify the student record in the table.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
In this example, we have two table, teach and department in a school. However,
there is no way to see which search work in which department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can
create a relationship between the two tables.
Example:
In this example, OrderNo and ProductID can't be a primary key as it does not
uniquely identify a record. However, a compound key of Order ID and Product ID
could be used as it uniquely identified each record.
The difference between compound and the composite key is that any part of the
compound key can be a foreign key, but the composite key may or maybe not a
part of the foreign key.
What is a Surrogate Key?
An artificial key which aims to uniquely identify each record is called a surrogate
key. These kind of key are unique because they are created when you don't have
any natural primary key. They do not lend any meaning to the data in the table.
Surrogate key is usually an integer.
Above, given example, shown shift timings of the different employee. In this
example, a surrogate key is needed to uniquely identify each employee.
Helps you to uniquely identify a record in the It is a field in the table that is the primary key of
table. another table.
Primary Key never accept null values. A foreign key may accept multiple null values.
Primary key is a clustered index and data in A foreign key cannot automatically create an index,
the DBMS table are physically organized in clustered or non-clustered. However, you can
the sequence of the clustered index. manually create an index on the foreign key.
You can have the single Primary key in a You can have multiple foreign keys in a table.
table.
Summary
A DBMS key is an attribute or set of an attribute which helps you to identify
a row(tuple) in a relation(table)
DBMS keys allow you to establish a relationship between and identify the
relation between tables
Seven Types of DBMS keys are Super, Primary, Candidate, Alternate,
Foreign, Compound, Composite, and Surrogate Key.
A super key is a group of single or multiple keys which identifies rows in a
table.
A column or group of columns in a table which helps us to uniquely
identifies every row in that table is called a primary key
All the keys which are not primary key are called an alternate key
A super key with no repeated attribute is called candidate key
A compound key is a key which has many fields which allow you to
uniquely recognize a specific record
A key which has multiple attributes to uniquely identify rows in a table is
called a composite key
An artificial key which aims to uniquely identify each record is called a
surrogate key
Primary Key never accept null values while a foreign key may accept
multiple null values.