Example 1
Require each product SKU to appear only once
The existing SKUs are distinct, so the unique index can be created. After that, attempting to insert another row with SKU-001 would fail instead of creating an ambiguous product record.
CREATE TABLE products (id INT, sku VARCHAR(20), name VARCHAR(50));
INSERT INTO
products (id, sku, name)
VALUES
(1, 'SKU-001', 'Widget'),
(2, 'SKU-002', 'Gadget');CREATE UNIQUE INDEX ux_products_sku ON products (sku);
SELECT
sku,
name
FROM
products
ORDER BY
sku;| sku | name |
|---|---|
| SKU-001 | Widget |
| SKU-002 | Gadget |
The unique-index creation changes future write behavior, not the selected rows.