- Introduction To MySQL
 - MySQL RDBMS
 - MySQL SQL
 - MySQL SELECT Statement
 - MySQL WHERE Clause
 - MySQL AND, OR And NOT Operators
 - MySQL ORDER BY Keyword
 - MySQL INSERT INTO Statement
 - MySQL NULL Values
 - MySQL UPDATE Statement
 - MySQL DELETE Statement
 - MySQL LIMIT Clause
 - MySQL MIN() And MAX() Functions
 - MySQL COUNT(), AVG() And SUM() Functions
 - MySQL LIKE Operator
 - MySQL Wildcards
 - MySQL IN Operator
 - MySQL BETWEEN
 - MySQL Aliases
 - MySQL Joins
 - MySQL INNER JOIN Keyword
 - MySQL LEFT JOIN Keyword
 - MySQL RIGHT JOIN Keyword
 - MySQL CROSS JOIN Keyword
 - MySQL Self Join
 - MySQL UNION Operator
 - MySQL GROUP BY Statement
 - MySQL HAVING Clause
 - MySQL EXISTS Operator
 - MySQL ANY And ALL Operators
 - MySQL INSERT INTO SELECT Statement
 - MySQL CASE Statement
 - MySQL NULL Functions
 - MySQL Comments
 - MySQL Operators
 - MySQL CREATE DATABASE Statement
 - MySQL DROP DATABASE Statement
 - MySQL CREATE TABLE Statement
 - MySQL DROP TABLE Statement
 - MySQL ALTER TABLE Statement
 - MySQL Constraints
 - MySQL NOT NULL Constraint
 - MySQL UNIQUE Constraint
 - MySQL PRIMARY KEY Constraint
 - MySQL FOREIGN KEY Constraint
 - MySQL CHECK Constraint
 - MySQL DEFAULT Constraint
 - MySQL CREATE INDEX Statement
 - MySQL AUTO INCREMENT Field
 - MySQL Working With Dates
 - MySQL Views
 - MySQL Data Types
 - MySQL Functions
 
MySQL PRIMARY KEY Constraint
MySQL PRIMARY KEY Constraint
The PRIMARY KEY constraint uniquely identifies each record in a table.
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
Practice Excercise Practice now
PRIMARY KEY On CREATE TABLE
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:
 
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
In the example above there is only ONE PRIMARY KEY (PK_Person). However, the VALUE of the primary key is made up of TWO COLUMNS (ID + LastName).
Practice Excercise Practice now
PRIMARY KEY On ALTER TABLE
To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use the following SQL:
 
ADD PRIMARY KEY (ID);
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);
If you use ALTER TABLE to add a primary key, the primary key column(s) must have been declared to not contain NULL values (when the table was first created).
Practice Excercise Practice now
DROP A PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint, use the following SQL:
 
DROP PRIMARY KEY;
Practice Excercise Practice now
Products
Partner
Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP