MYSQL – What is a primary key?

Mar 12, 2012   //   by phpfreelancer.biz   //   Blog, MySQL Tutorial  //  No Comments

A primary key is a column that is defined as uniquely identifying each row.

 CREATE TABLE Persons
(
    P_Id int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255),
    PRIMARY KEY (P_Id)          //primary key is on this line
)

A primary key is a unique identifier for the row. It is automatically assigned by the database management system (auto-increment for that value). So if you have a database of people in an organisation, their primary key may be their employee number. Every time you add an employee, they receive a unique employee number that is usually the previous employees number + 1.

Leave a comment

Share This Post

RSS Wordpress News