Month: May 2021

MySQL – DBMS Keys Examples

A key is an attribute or set of an attribute which helps you to identify a row(tuple) in a relation(table). They allow you to find the relation between two tables. Keys help you uniquely identify a row in a table by a combination of one or more columns in that table.

Why we need a Key?

Here, are reasons for using Keys in the DBMS system.

  • Keys help you to identify any row of data in a table. In a real-world application, a table could contain thousands of records. Moreover, the records could be duplicated. Keys ensure that you can uniquely identify a table record despite these challenges.
  • Allows you to establish a relationship between and identify the relation between tables
  • Help you to enforce identity and integrity in the relationship. Keys are used to enforce referential integrity in your database.

Various Keys in Database Management System

DBMS has following seven types of Keys each have their different functionality:

  • Super Key
  • Primary Key
  • Candidate Key
  • Alternate Key
  • Foreign Key
  • Compound Key
  • Composite Key
  • Surrogate Key
  • Unique Key

Super key

SUPER KEY is a group of single or multiple keys which identifies rows in a table. A Super key may have additional attributes that are not needed for unique identification.

Primary Key

PRIMARY KEY is a column or group of columns in a table that uniquely identify every row in that table. The Primary Key can’t be a duplicate meaning the same value can’t appear more than once in the table. A table cannot have more than one primary key.

Rules for defining Primary key:

Two rows can’t have the same primary key value

  • It must for every row to have a primary key value.
  • The primary key field cannot be null.
  • The value in a primary key column can never be modified or updated if any foreign key refers to that primary key.

Example : 

StudID Roll No First Name LastName Mobile No.
1 11 Tom Price 9999999999
2 12 Nick Wright 8787878787
3 13 Dana Natan 7474747474

In the above-given example, StudID is a primary key because it uniquely identifies an employee record. In this table, no other employee can have the same employee ID.

Alternate key

ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that table. A table can have multiple choices for a primary key but only one can be set as the primary key. All the keys which are not primary key are called an Alternate Key.

Example:In this table, StudID, Roll No, Mobile No are qualified to become a primary key. But since StudID is the primary key, Roll No, Mobile No becomes the alternative key.

 

StudID Roll No First Name LastName Mobile No.
1 11 Tom Price 9999999999
2 12 Nick Wright 8787878787
3 13 Dana Natan 7474747474

Foreign key

FOREIGN KEY is a column that creates a relationship between two tables. The purpose of Foreign keys is to maintain data integrity and allow navigation between two different instances of an entity. It acts as a cross-reference between two tables as it references the primary key of another table.

This concept is also known as Referential Integrity.

Example: Refer following two table .

Student Info

StudID Roll No First Name LastName Mobile No.
1 11 Tom Price 9999999999
2 12 Nick Wright 8787878787
3 13 Dana Natan 7474747474

Receipt Info : In this table, adding the foreign key in StudID to the Receipt Info, we can create a relationship between the two tables (Between Student Info and Receipt Info)

ReceiptID StudID Amount
1001 1 100
1002 2 200
1003 3 450

Candidate Key

CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table. Candidate Key is a super key with no repeated attributes. The Primary key should be selected from the candidate keys. Every table must have at least a single candidate key. A table can have multiple candidate keys but only a single primary key.

Properties of Candidate key:

  • It must contain unique values
  • Candidate key may have multiple attributes
  • Must not contain null values
  • It should contain minimum fields to ensure uniqueness
  • Uniquely identify each record in a table

Example: In the given table Stud ID, Roll No, and Mobile No are candidate keys which help us to uniquely identify the student record in the table.

StudID Roll No First Name LastName Mobile No.
1 11 Tom Price 9999999999
2 12 Nick Wright 8787878787
3 13 Dana Natan 7474747474

Candidate Key

Compound key

COMPOUND KEY has two or more attributes that allow you to uniquely recognize a specific record. It is possible that each column may not be unique by itself within the database. However, when combined with the other column or columns the combination of composite keys become unique. The purpose of the compound key in database is to uniquely identify each record in the table.

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.

OrderNo PorductID Product Name Quantity
B005 JAP102459 Mouse 5
B005 DKT321573 USB 10
B005 OMG446789 LCD Monitor 20
B004 DKT321573 USB 15
B002 OMG446789 Laser Printer 3

Composite key

COMPOSITE KEY is a combination of two or more columns that uniquely identify rows in a table. The combination of columns guarantees uniqueness, though individually uniqueness is not guaranteed. Hence, they are combined to uniquely identify records in a table.

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.

Surrogate key

SURROGATE KEYS is An artificial key which aims to uniquely identify each record is called a surrogate key. This kind of partial key in dbms is unique because it is 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. A surrogate key is a value generated right before the record is inserted into a table.

 

Fname Lastname Start Time End Time
Anne Smith 09:00 18:00
Jack Francis 08:00 17:00
Anna McLean 11:00 20:00
Shown Willam 14:00 23:00

Above, given example, shown shift timings of the different employee. In this example, a surrogate key is needed to uniquely identify each employee.

Surrogate keys in sql are allowed when

  • No property has the parameter of the primary key.
  • In the table when the primary key is too big or complicated.

Unique Key

UNIQUE KEY in MySQL is a single field or combination of fields that ensure all values going to store into the column will be unique. It means a column cannot stores duplicate values. For example, the email addresses and roll numbers of students in the “student_info” table or contact number of employees in the “Employee” table should be unique.

MySQL allows us to use more than one column with UNIQUE constraint in a table. It can accept a null value, but MySQL allowed only one null value per column. It ensures the integrity of the column or group of columns to store different values into a table.

Needs of Unique Key

  • It is useful in preventing the two records from storing identical values into the column.
  • It stores only distinct values that maintain the integrity and reliability of the database for accessing the information in an organized way.
  • It also works with a foreign key in preserving the uniqueness of a table.
  • It can contain null value into the table.

Difference Between Primary key & Unique key

Primary Key Unique Key
Unique identifier for rows of a table Unique identifier for rows of a table when primary key is not present
Cannot be NULL Can be NULL
Only one primary key can be present in a table Multiple Unique Keys can be present in a table
present in a table present in a table
Selection using primary key creates clustered index Selection using unique key creates non-clustered index

Summary

  • 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.

//Ex:
CREATE TABLE Tbl_Employee (
    EmployeeID int,
    LastName varchar(255),
    FirstName varchar(255),
    Gender varchar(255),
    City varchar(255), 
    PRIMARY KRY(EmployeeID)
);

INSERT INTO Tbl_Employee VALUES (1,'Smith','James','Male','Mumbai');
INSERT INTO Tbl_Employee VALUES (2,'Johnson','Maria','Female','New York');
INSERT INTO Tbl_Employee VALUES (3,'Miller','David','Male','London');
INSERT INTO Tbl_Employee VALUES (4,'Wilson','Maria','Female','Paris');
INSERT INTO Tbl_Employee VALUES (5,'Thomas','Mary','Female','Singapore');
INSERT INTO Tbl_Employee VALUES (6,'Martin','Steven','Male','Bangkok');
INSERT INTO Tbl_Employee VALUES (7,'Lee','Karen','Female','Dubai');

//SQL to show keys
SHOW KEYS FROM Tbl_Employee WHERE Key_name = 'PRIMARY'