Try inserting two rows with the same StudentID
to see what happens.
When you execute this query, the database will throw an error similar to:
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
This happens because the primary key enforces the rule that each value in the StudentID
column must be unique. By trying to insert a duplicate StudentID
, the database prevents the operation, ensuring data integrity.
Example 2: Adding a primary key to an existing table
If the table is already created without a primary key, we can add it later: