In SQL, a primary key is a constraint that uniquely identifies each record (row) in a table.
It ensures that the values in the specified column or columns are unique and not null.
The primary key constraint is crucial for maintaining data integrity and allows for efficient querying and indexing of the table.
CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50), product_added_date DATE );
The `products` table is created with columns `product_id`, `product_name`, and `product_added_date`.
The `product_id` column is declared as the primary key for the table using the PRIMARY KEY constraint.
The primary key constraint ensures that each `product_id` value must be unique across all rows in the table.
CREATE TABLE orders ( order_id INT, product_id INT, PRIMARY KEY (order_id, product_id) );
Note: the combination of "order_id" and "product_id" forms the composite primary key, and each combination of these two values must be unique in the table.