EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial Oracle UNIQUE Index
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

Oracle UNIQUE Index

By Priya PedamkarPriya Pedamkar

Oracle UNIQUE Index

Introduction to Oracle UNIQUE Index

Oracle Unique Index (Index creates an entry in the database for each value that has been stored in the indexed column of a table to allow faster retrieval of data) can be defined as a unique index which does not allow the entry of the duplicate values in that particular column which has been created with unique constraint as whenever we create a unique constraint on a column or create a primary key, the Oracle database automatically creates a unique index.

Syntax

Let us now look into the SYNTAX of the Oracle UNIQUE INDEX. The syntax is similar and almost similar to create index syntax.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

CREATE UNIQUE INDEX indexName ON
tableName (column1, column2, …);

Parameters

Below are parameters of Oracle UNIQUE Index:

indexName: It refers to the name of the Index the user wants to create.

tableName: It refers to the name of the table on which the user wants to create a unique index.

column1, column2, ..: It refers to the column names of the table on which unique index will be created.

How does UNIQUE Index work in Oracle?

In the previous section of the article, we discussed the definition of the UNIQUE index. In this section, we are going to discuss how the Unique Index works in Oracle. A unique index is automatically created when we create a table that has a unique constraint but in case we want to explicitly create a UNIQUE index, we have to run the CREATE INDEX query. Once we execute the CREATE INDEX query with the particular column entry is provided in the database for all values assigned in the column or in other words indexes are created. When a UNIQUE INDEX is created in a column of a table, the index does not allow any other duplicate index to be created for that column because since the column of the table already has a Unique constraint associated with it, the unique constraint does not allow any duplicate values to be inserted for that column.

Examples to Implement Oracle UNIQUE Index

In this section, we are going to discuss how we can create and use UNIQUE Index with the help of examples.

Example #1

ORACLE AUTOMATICALLY CREATES UNIQUE INDEX: In general, whenever we associate a column or columns with a primary key or unique constraint, the oracle database automatically creates a unique index on the primary key or the unique key columns. In this example, we will create a table with two columns and assign unique constraints to one of them and then check whether UNIQUE INDEX has been created on it or not. Let us first create a table with a unique constraint.

Code:

CREATE TABLE table1 (
uK1 INT UNIQUE,
c2 INT
);

Output:

Oracle UNIQUE Index1

We have added a unique constraint on one of the columns. So, let us prepare a SELECT query to check whether the unique index has been created or not.

Code:

SELECT
index_name,
index_type,
visibility,
status
FROM
all_indexes
WHERE
table_name = 'TABLE1';

Output:

Oracle UNIQUE Index2

Explanation: As we can see in the above screenshot the unique index SYS_C0020292 has been created automatically.

Example #2

ORACLE UNIQUE INDEX WHEN ONLY ONE COLUMN: In this section, we will discuss we can create a unique index on one column in a table with the help of an example. We have already gone through the syntax earlier. In this example, we will create a UNIQUE INDEX on the column customer name which is present inside the table customers, and then we will try to insert duplicate values and check whether it is allowing or not. So, let us first create the index.

Code:

CREATE UNIQUE INDEX cust_unique
ON customers(customer_name);

Output:

Oracle UNIQUE Index3

Explanation: The column is customer_name for which a unique index will be created. As we can see in the above screenshot that the unique index has been created.

Now let us look at the existing values present in the table.

Code:

SELECT * from customers;

Output:

existing values

As we can see there are six rows in the table. Let us now check the unique index by trying to enter a duplicate value. Let us prepare an INSERT query for the same.

Code:

INSERT INTO customers
VALUES('AD002','Nilanjan','New Delhi');

Output:

Oracle UNIQUE Index5

Explanation: As we can see that it throws ORA-00001 Unique Constraint error.

Example #3

ORACLE UNIQUE INDEX WHEN MORE THAN ONE COLUMN: In this section, we will discuss about how we can create a unique index on more than one column in a table with the help of an example. We have already gone through the syntax earlier. In this example, we will create a UNIQUE INDEX on two columns city and vehicle name which is present inside the table vehicle, and then we will try to insert duplicate values and check whether it is allowing or not. So, let us first create the index.

Code:

CREATE UNIQUE INDEX vehicle_unique
ON vehicle(vehicle_name, city);

Output:

create the index

Let us now execute the query in SQL Developer and check the result. Let us now check the contents of the table vehicle.

Code:

SELECT * from vehicle;

Output:

contents of the table

We will now try to insert duplicate values into the column’s vehicle_name and city. Let us now prepare an INSERT query and try to insert duplicate values.

Code:

INSERT INTO vehicle
VALUES('VH013','Honda','100','DELHI','','9000');

Output:

INSERT query

Explanation: As we can see that it throws ORA-00001 Unique Constraint error.

Conclusion

In this article, we discussed the UNIQUE INDEX. We started the article with a discussion about the definition of the UNIQUE INDEX. Later on in the article, we discussed the working of the UNIQUE INDEX with the help of examples.

Recommended Articles

This is a guide to Oracle UNIQUE Index. Here we discuss an introduction to Oracle UNIQUE Index, syntax, how does it work with programming examples. You can also go through our other related articles to learn more –

  1. Oracle Clauses
  2. Unique Key in MySQL
  3. Indexes in PostgreSQL
  4. Table in MySQL
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more