In MySQL, the PRIMARY KEY constraint is used to uniquely identify each record in a table. It enforces the uniqueness of values in one or more columns and ensures that these columns cannot contain NULL values.
Here's how you can use the PRIMARY KEY constraint while creating a table:
CREATE TABLE table_name (
column_name data_type PRIMARY KEY,
...
);
You can also apply the PRIMARY KEY constraint to multiple columns:
CREATE TABLE table_name (
column1_name data_type,
column2_name data_type,
...
PRIMARY KEY(column1_name, column2_name)
);
For example, suppose you have a table named employees and you want to ensure that the id column uniquely identifies each employee:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
);
In this example, the id column is defined with the PRIMARY KEY constraint, which means that each value in the id column must be unique, and it cannot contain NULL values.
Using the PRIMARY KEY constraint is crucial for maintaining data integrity and ensuring that your table's records can be uniquely identified. It's typically used with columns like id or customer_id, which serve as unique identifiers for the records in the table.