A primary key is a field in a table which uniquely identifies each row/record in a database table.
Primary keys must contain unique values.
A primary key column cannot have NULL values.
CREATE TABLE student (stud_id INT PRIMARY KEY, stud_name VARCHAR(60) NOT NULL,)
Primary key enforces the entity integrity of the table.
Primary key always has unique data.
A primary key length cannot be exceeded than 900 bytes.
A primary key cannot have null value.
There can be no duplicate value for a primary key.
A table can contain only one primary key constraint.
The main advantage of this uniqueness is that we get fast access.
The following SQL command creates a PRIMARY KEY on the "s_id" column when the "students" table is created.
CREATE TABLE student ( s_id int NOT NULL, name VARCHAR(255) NOT NULL, address VARCHAR(255), PRIMARY KEY (s_id) )
CREATE TABLE student ( s_id int NOT NULL PRIMARY KEY, name VARCHAR(255) NOT NULL, address VARCHAR(255), )
CREATE TABLE student ( s_id int NOT NULL, name varchar (255) NOT NULL, address varchar (255), CONSTRAINT pk_stud_id PRIMARY KEY (s_id, name) )
The above example there is only one PRIMARY KEY (pk_stud_id). However it is made up of two columns (S_Id and LastName).
DROP (remove) a primary key constraint.
ALTER TABLE student DROP CONSTRAINT s_id