Introduction
The UNIQUE constraint makes sure that no two rows in a table have the same value in a specific column or group of columns.
Jump into concepts and practice - no test required
CREATE TABLE table_name ( column_name data_type UNIQUE ); -- Or add UNIQUE to existing column: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column_name);
CREATE TABLE Users ( id INT PRIMARY KEY, email VARCHAR(255) UNIQUE );
ALTER TABLE Products ADD CONSTRAINT unique_product_code UNIQUE (product_code);
CREATE TABLE Employees ( id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), UNIQUE (first_name, last_name) );
CREATE TABLE Customers ( customer_id INT PRIMARY KEY, phone_number VARCHAR(20) UNIQUE ); INSERT INTO Customers (customer_id, phone_number) VALUES (1, '123-456-7890'); INSERT INTO Customers (customer_id, phone_number) VALUES (2, '987-654-3210'); -- The next insert will fail because phone_number '123-456-7890' already exists INSERT INTO Customers (customer_id, phone_number) VALUES (3, '123-456-7890');
UNIQUE constraint in SQL?email in a table users?products with a UNIQUE constraint on product_code, what happens when you run this SQL?INSERT INTO products (product_code, name) VALUES ('X123', 'Item A');
INSERT INTO products (product_code, name) VALUES ('X123', 'Item B');username but got an error. What is the most likely cause?username contains duplicate values already" identifies the cause. A non-existent table is unrelated (assuming it exists); UNIQUE can be added later; indexing is irrelevant or automatic.username contains duplicate values already -> Option Bfirst_name and last_name in the employees table is unique, but duplicates are allowed in each column individually. Which SQL statement correctly enforces this?