Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL - What is a primary key?

I'm in the process of learning Mysql, and I'm creating databases. So, after looking at several websites, the definition for a primary key is:

The PRIMARY KEY constraint uniquely identifies each record in a database table.

and is used like this:

    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
)

However, I still don't know what it's used for and why we need it. So my question is.

Can someone explain to me what a primary key is (in basic english) and why we need one and what is it used for?

Thank-you.

like image 434
r1nzler Avatar asked Mar 04 '12 00:03

r1nzler


1 Answers

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

Also, by defining a column as PRIMARY KEY, it may be referenced as a foreign key in other tables when defining referential integrity constraints.

like image 129
Bohemian Avatar answered Oct 04 '22 20:10

Bohemian