MySQL Tutorial
A PRIMARY KEY in MySQL is a constraint that uniquely identifies each record in a table. It ensures that no two rows have the same value in the specified column(s) and prevents NULL values.
Key Characteristics:
NULL values.CREATE TABLE table_name ( column1 data_type PRIMARY KEY, column2 data_type, column3 data_type );
CREATE TABLE table_name ( column1 data_type, column2 data_type, column3 data_type, PRIMARY KEY (column1, column2) );
student_id is the Primary Key (Unique and Not Null).name is Not Null, meaning it cannot be empty.email is Unique to avoid duplicates.student_id and course_id together form a Composite Primary Key.Note
In the example above there is only ONE PRIMARY KEY (pk_student). However, the VALUE of the primary key is made up of TWO COLUMNS (student_id + course_id).
To create a PRIMARY KEY constraint on the "student_id" column when the table is already created, use the following SQL:
pk_student is the name of the primary key constraint.To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
Note
If the primary key is a composite key, you will need to remove the entire key using this method.