Code has been added to clipboard!

How to Use SQL UNIQUE in Oracle

Reading time 1 min
Published Aug 9, 2017
Updated Oct 3, 2019

SQL UNIQUE Constraint: Main Tips

  • You can use this constraint to classify each data cell in a table of a specific database in Oracle.
  • It is described in a PRIMARY KEY by default.
  • There can be only one PRIMARY KEY and a lot of UNIQUE constraints per table.
  • The SQL SELECT UNIQUE construct only works in Oracle. For other management systems, use SQL SELECT DISTINCT.

SQL UNIQUE Syntax Needed to CREATE TABLE

In the example below, we produce the UNIQUE constraint while creating the table:

Example
CREATE TABLE People (
    ID int NOT NULL UNIQUE,
    NameLast varchar(255) NOT NULL,
    NameFirst varchar(255),
    Year int
);

The code example below permits naming the constraints:

Example
CREATE TABLE People (
    ID int NOT NULL,
    LName varchar(255) NOT NULL,
    FName varchar(255),
    Year int,
    CONSTRAINT UC_Person UNIQUE (ID,LName)
);

SQL UNIQUE on ALTER TABLE: Example

In the example below, we produce the UNIQUE constraint after the table creation:

Example
ALTER TABLE People
ADD UNIQUE (ID);

The code example below permits naming the constraints:

Example
ALTER TABLE People
ADD CONSTRAINT UC_Person UNIQUE (ID,LName);

Note: SQL SELECT UNIQUE construct is non-standard and only supported by Oracle. For other database systems, use SQL SELECT DISTINCT.

Learn SQL
Introduction
Syntax
Data Types
Server Data Types
Commands
Commands List
Wildcards
Constraints
Aggregate Functions
Date Functions
Date Format
Injection
SQL Server Hosting
Views
Auto-incrementation
SQL Operators
AS
AND & OR
IN
BETWEEN
WHERE
GROUP BY
HAVING
ORDER BY
LIKE
NOT
NOT EQUAL
UNION
NULL
NOT NULL
DEFAULT
UNIQUE
FOREIGN KEY
PRIMARY KEY
CHECK
Indexes
ALTER TABLE
CREATE DATABASE
CREATE TABLE
DELETE
DROP
INSERT INTO SELECT
INSERT INTO
SELECT
SELECT DISTINCT
SELECT INTO
SELECT TOP
UPDATE
FULL OUTER JOIN
INNER JOIN
JOIN
LEFT JOIN
RIGHT JOIN
AVG()
COUNT
FIRST
LAST
MAX
MIN()
SUM()
LEN
UCASE
MID
NOW
ROUND
FORMAT
LOWER
CONVERT
ISNULL