Learn DBMS and MySQL

Our tutorial provides basic and advance concepts of database management and MySQL commands with detailed examples

DBMS Introduction
MySQL Database
MySQL Tables
MySQL Queries
MySQL Clauses
MySQL Conditions
MySQL Keys

MySQL Primary Key

MySQL primary key is a single or combination of the field, which is used to identify each record in a table uniquely. If the column contains primary key constraints, then it cannot be null or empty. A table may have duplicate columns, but it can contain only one primary key. It always contains unique value into a column.

When you insert a new row into the table, the primary key column can also use the AUTO_INCREMENT attribute to generate a sequential number for that row automatically. MySQL automatically creates an index named "Primary" after defining a primary key into the table. Since it has an associated index, we can say that the primary key makes the query performance fast.

Rules for Primary key

Following are the rules for the primary key:

  1. The primary key column value must be unique.

  2. Each table can contain only one primary key.

  3. The primary key column cannot be null or empty.

  4. MySQL does not allow us to insert a new row with the existing primary key.

  5. It is recommended to use INT or BIGINT data type for the primary key column.

We can create a primary key in two ways:

For example

Let's move on to Foreign Key in MySQL


Share this page on :