In MySQL, Unique key constraint ensures that the values in a specified column or a set of columns are unique across all rows in a table.
Unlike a primary key, a unique key allows for one null value.
Each unique key creates a unique index on the specified columns, speeding up the retrieval of data when searching based on those columns.
CREATE TABLE products ( product_id INT PRIMARY KEY, product_name VARCHAR(50) NOT NULL, product_code VARCHAR(100) UNIQUE );
The `products` table is created with columns `product_id`, `product_name`, and `product_code`.
The `product_id` column is the primary key for the table.
The `product_code` column has a unique key constraint using the UNIQUE keyword, ensuring that each `product_code` in the table must be unique.
CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, customer_name VARCHAR(50), UNIQUE (customer_id, order_id) );
The "orders" table has columns "order_id", "customer_id", and "customer_name".
The combination of "customer_id" and "order_id" is specified as a composite unique key using the UNIQUE keyword.