INTRODUCTION
In this tutorial, I am going to explain about keys in MySQL with examples. Without wasting time, let’s start.
In MySQL, a key is a data item that identifies a record exclusively. In other terms, the key is a group of columns used to uniquely define a record in a table. It is used to retrieve or extract rows from a table as needed. Keys have many forms of constraint, such as columns, which cannot hold repeated values or null values.
Types of Keys
MySQL supports the following types of keys. Let’s see.
- Super Key
- Candidate Key
- Primary Key
- Unique Key
- Foreign Key
Primary Key
A primary key is a column or a group of columns that represent each row in the table in
an unique manner. As per Wikipedia, “A primary key is a specific choice of a minimal set of attributes that uniquely identify a tuple (row) in a relation“.
Note:
A table can only have one primary key and must contain unique values in it.
A) Define a PRIMARY KEY Constraint using create table
In a create table statement, you can define primary key for a table.
Syntax:
CREATE TABLE <table_name>
(
Column_name1 datatype(),
Column_name2 datatype(),
PRIMARY KEY (Column_name1)
);
For Example: The following table definition has student_id which is PRIMARY KEY.
- CREATE TABLE Student_details
- (
- student_id INT AUTO_INCREMENT,
- student_name varchar(50),
- student_address varchar(200) PRIMARY KEY(student_id)
- );
B) Define PRIMARY KEY Constraint Using Alter Table
In MySQL, we can also add a primary key on the column of the existing table.
Syntax:
ALTER TABLE <table_name>
ADD PRIMARY KEY (column_name);
For Example: First of all we have to create a table with the specified fields.
Create a table:
- CREATE TABLE friend(
- friend_id int,
- friend_name varchar(200),
- date DATETIME
- );

Insert the values into the table:
- INSERT INTO friend VALUES(1,'Ram','2020-08-27');
- INSERT INTO friend VALUES(2,'Shayam','2020-08-27');
Result:
- SELECT * from friend;

Command to view data type of Table named employees
Syntax: DESCRIBE friend;
Output

Command to add primary key into the Table name friend
The Alter Query is used to redefine the table 'friend' and add keyword is used to add primary
key on column 'friend_id'.
Query:
- ALTER TABLE friend
- ADD PRIMARY KEY (friend_id);
- Again type, DESCRIBE friend;
OUTPUT:

UNIQUE KEY
The alter unique key is to be used to changed the structure of the table and to add a unique key of the specified column in the table. The unique key allows one of the null values to be inserted in the column and the unique key table can contain more than one unique key. It is a column constraint.
A) Define a UNIQUE KEY Constraint using create table
1) When you want to define unique key for only one column
Syntax:
CREATE TABLE <table_name>
(
Column_name1 datatype() UNIQUE,
Column_name2 datatype(),
);
2) When you want to define more than one unique key on a table
Syntax:
CREATE TABLE <table_name>
(
Column_name1 datatype(),
Column_name2 datatype(),…
Column_namen datatype(),
UNIQUE (column_name1, column_name2)
);
For Example: Now, we are going to create a table with some unique key constraints.
- CREATE TABLE VATSA(
- ID INT AUTO_INCREMENT PRIMARY KEY,
- Company_name varchar(100) UNIQUE,
- Address varchar(250) UNIQUE
- );

Now, insert some rows into it.









Onkar SharmaPosted Aug 28, 2020, 12:47 PM
Nice article, Very well explained...
Gowtham RajamanickamPosted Apr 25, 2015, 6:38 AM
good