1.

What is a UNIQUE constraint?

Answer»

A UNIQUE CONSTRAINT ensures that all values in a column are different. This provides uniqueness for the column(s) and helps identify each row uniquely. Unlike primary key, there can be multiple unique constraints defined PER table. The code syntax for UNIQUE is quite SIMILAR to that of PRIMARY KEY and can be used interchangeably.

CREATE TABLE Students ( /* Create table with a single field as unique */ ID INT NOT NULL UNIQUE Name VARCHAR(255));CREATE TABLE Students ( /* Create table with multiple fields as unique */ ID INT NOT NULL LastName VARCHAR(255) FirstName VARCHAR(255) NOT NULL CONSTRAINT PK_Student UNIQUE (ID, FirstName));ALTER TABLE Students /* SET a column as unique */ADD UNIQUE (ID);ALTER TABLE Students /* Set multiple COLUMNS as unique */ADD CONSTRAINT PK_Student /* Naming a unique constraint */UNIQUE (ID, FirstName);


Discussion

No Comment Found