TOP CATEGORIES

×

How to create a unique constraint in SQL?

N

Nisha

A UNIQUE constraint ensures that all values in a column are distinct (no duplicates are allowed). A table can have multiple UNIQUE constraints, unlike a PRIMARY KEY, and NULL values are permitted.


UNIQUE Constraint Example for Creation & Existing Table

CREATE TABLE student (

      student_id INT PRIMARY KEY,

      email VARCHAR(100) UNIQUE,

      phone_number VARCHAR(20),

      CONSTRAINT uq_phone UNIQUE (phone_number)

);

ALTER TABLE student ADD CONSTRAINT uq_email UNIQUE (email);

0 votes

Your Answer