A PL/SQL Unique Index is a powerful database object used to ensure the uniqueness of values in one or more columns of a table. In Oracle databases, indexes play an important role in enhancing query performance by enabling quick retrieval of rows.
The unique index specifically enforces a rule that no two rows in a table can have the same values for the indexed column(s). In this article, we will explain the PL/SQL Unique Index in detail with syntax and examples.
PL/SQL Unique Index
By creating a unique index, the database automatically maintains data uniqueness, rejecting any attempts to insert or update records that violate this constraint. This feature makes unique indexes essential for maintaining data integrity, especially where duplicate entries must be prevented, such as email addresses or employee IDs.
Syntax:
CREATE UNIQUE INDEX index_name
ON table_name (column1, column2, ...);
key terms
- index_name: The name of the unique index.
- table_name: The name of the table, which is the unique index is being created.
- column1, column2, ...: The columns that can be unique index will be enforce the uniqueness on. We can specify that the more than one column for the composite uniqueness.
Example 1: Unique Index on email
In this example, we will create the unique index on email column to ensure that the no two users can have same email address. After creation of unique index, we will try to insert the statements into the users table
Query:
CREATE UNIQUE INDEX idx_users_email
ON users (email);
-- Insert a user with a unique email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (1, 'Alice', 'Smith', '[email protected]');
-- Insert another user with a different email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (2, 'Bob', 'Johnson', '[email protected]');
-- Insert a user with the same email (will fail)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (3, 'Charlie', 'Brown', '[email protected]');
Output:
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.IDX_USERS_EMAIL) violated
Explanation:
- The first two insert statements will succeed because the email values are unique.
- The third insert statement is failed with the above error, because the email '[email protected]' already exist in the users table, so that it violates the unique index constraint.
Example 2: Unique Composite Index on first_name and email
In this example, we will create composite unique index on combination of the first_name and email. This ensures that the individual columns may have the duplicate values, the combination of both must be unique. After creating the index, let us try to insert the rows into the users table:
Query:
CREATE UNIQUE INDEX idx_users_firstname_email
ON users (first_name, email);
-- Insert a user with a unique combination of first_name and email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (1, 'John', 'Doe', '[email protected]');
-- Insert another user with the same first_name but a different email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (2, 'John', 'Doe', '[email protected]');
-- Insert a user with a different first_name and the same email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (3, 'Jane', 'Doe', '[email protected]');
-- Insert a user with the same first_name and email as an existing user (will fail)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (4, 'John', 'Smith', '[email protected]');
Output:
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.IDX_USERS_FIRSTNAME_EMAIL) violated
Explanation:
- In the above query, the first three insert statements will be succeeded because each has the unique combination of first_name and email.
- The fourth insert statement will be failed with the above error, because the combination of first_name = 'John' and email = '[email protected]' already exists in users table and violating the composite unique index.
Example 3: Handling NULL Values with Unique Indexes
In Oracle, unique indexes allow multiple NULL values. However, non-NULL values must follow the uniqueness rule. After creation of unique index, let us try to insert the values into the users table using below given query.
Query:
CREATE UNIQUE INDEX users_email
ON users (email);
-- Insert a user with NULL email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (1, 'Alice', 'Smith', NULL);
-- Insert another user with NULL email (valid, as NULL is allowed)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (2, 'Bob', 'Johnson', NULL);
-- Insert a user with a non-NULL unique email (valid)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (3, 'Charlie', 'Brown', '[email protected]');
-- Insert another user with the same non-NULL email (will fail)
INSERT INTO users (user_id, first_name, last_name, email)
VALUES (4, 'David', 'Clark', '[email protected]');
Output:
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.USERS_EMAIL) violated
Explanation:
- The first two inserts with NULL values succeed because Oracle allows multiple NULL values in columns with a unique index
- The third insert statement will be succeed, as '[email protected]' is a unique the non-NULL value.
- The fourth insert statement will be failed with the above error, because the email '[email protected]' is already present in table and the unique index prevent a insertion of the duplicate non-NULL values.
Conclusion
In conclusion, A PL/SQL Unique Index is the essential feature for the maintaining the data integrity in the Oracle database by ensuring that the specific columns or the combination of the columns contains the unique values.
By preventing duplicate entries, unique indexes help enforce the business rules like unique email addresses, product codes or other critical identifiers. They can created on the single columns or across the multiple columns.
Similar Reads
SQL Unique Index
A SQL Unique Index is a constraint that ensures no two rows in the indexed columns of a table have the same values, thereby preventing duplicate entries. Unique indexes are crucial for maintaining data integrity, particularly when you need specific columns or combinations of columns to hold only uni
6 min read
MySQL Unique Index
In MySQL, a unique index is a crucial tool for ensuring that values in a specific column or a group of columns within a table are unique. This feature is essential to maintain data integrity by preventing duplicate entries where uniqueness is required. In this guide, we'll explore MySQL's unique ind
6 min read
PL/SQL Unique Key
In Oracle databases, ensuring data integrity and uniqueness within the table is a crucial aspect of the database design. One key feature that assists in this is the Unique Key Constraint. In this article, we will learn about Unique Key in PL/SQL, including its types, syntax, use cases, and examples.
6 min read
PostgreSQL - UNIQUE Index
In PostgreSQL, a UNIQUE index is used to ensure that the values in one or more columns are unique across the rows in a table. This is essential for maintaining data integrity and avoiding duplicate entries.This article will provide a detailed overview of UNIQUE indexes, including syntax, examples, a
4 min read
Index in PL/SQL
PL/SQL, Oracle's extension to SQL, combines SQL with procedural programming features like loops, conditionals, and exception handling. It enables developers to create stored procedures, functions, triggers, and other database applications. As a block-structured language, PL/SQL allows seamless integ
5 min read
MariaDB Unique Index
MariaDB is a fast, scalable, open-source community-supported relational database management system thatâs also an enhanced version of MySQL. Content management systems (CMS) are a key application of MariaDB. A CMS is a publication system through which web creators can push and manage large quantitie
5 min read
SQLite Indexes
SQLite is an embedded database that doesn't use a database like Oracle in the background to operate. It is written in C language and is used by developers who embed a lightweight database over the existing application, browser, or embedded systems. The main features of SQLite are that it is a quick,
8 min read
PL/SQL Drop Index
In PL/SQL an index is a database object that improves the speed of the data retrieval operations on the table. However, there are situations where we might want to remove an index either to optimize performance or to retrieve disk space. In this article, we will provide a comprehensive guide on how
4 min read
PL/SQL | User Input
Prerequisite - PL/SQL Introduction In PL/SQL, user can be prompted to input a value using & character. & can be used to prompt input for different data types. Consider, following table: Table: GFG id author likes 1 sam 10 2 maria 30 3 ria 40 Following queries will create a new table named GF
2 min read
SQL | UNIQUE Constraint
In SQL, constraints play a vital role in maintaining the integrity and accuracy of the data stored in a database. One such constraint is the UNIQUE constraint, which ensures that all values in a column (or a combination of columns) are distinct, preventing duplicate entries. This constraint is espec
4 min read