One table can have one Primary Key constrain and this is used to uniquely identify the row. Primary Key can’t have Null data.
Creating and deleting Primary Key constraint of Table and difference between Unique Key in MySQL
Creating a Primary Key
For a New table
CREATE TABLE `student` (
`id` int(2) NOT NULL,
`name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
`class` varchar(10) CHARACTER SET utf8 NOT NULL DEFAULT '',
`mark` int(3) NOT NULL DEFAULT '0',
`gender` varchar(6) CHARACTER SET utf8 NOT NULL DEFAULT 'male',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Adding Primary Key to an existing table for single column.
The error we got depends on setting of STRICT mode.
Example: Primary Key & Unique Key constraint
In our student table we will have student roll number (or student id) , similarly in one more column we are storing Mobile phone number of each student.
Here we have to use student id as Primary key but phone column can have Unique constraint. Some student may not have submitted phone number so we can have Null data for that row phone column, but we can’t keep student id as null.