What is a Primary Key Index in Oracle?

A Primary Key Index in Oracle Database is an index that Oracle uses to enforce the uniqueness of a Primary Key and help retrieve records efficiently using the Primary Key column.

In simple words, when we create a Primary Key on a table, Oracle normally creates a Unique Index automatically to ensure that duplicate Primary Key values are not allowed.

A Primary Key also does not allow NULL values.

Simple Definition:
A Primary Key Index helps Oracle enforce Primary Key uniqueness and retrieve records efficiently using Primary Key columns.

1. Example of a Primary Key Index

Suppose we have an EMPLOYEE table.

EMP_ID EMP_NAME SALARY
101 Ravi 30000
102 Kumar 40000
103 Priya 35000
104 Arun 45000

Here, EMP_ID uniquely identifies each employee.

Step 1: Create a Table with a Primary Key

CREATE TABLE employee
(
    emp_id NUMBER PRIMARY KEY,
    emp_name VARCHAR2(50),
    salary NUMBER
);

Explanation:

  • EMP_ID is the Primary Key.
  • Duplicate EMP_ID values are not allowed.
  • NULL values are not allowed.
Output: Table created.

Step 2: What Happens Internally?

When we create a Primary Key, Oracle normally creates a Unique Index automatically if a suitable existing index is not available.

For example, Oracle may create an index similar to:

CREATE UNIQUE INDEX SYS_C001234
ON employee(emp_id);
Note: This is only an illustration. Oracle generates its own index name when it automatically creates the index. The actual index name can be different.

2. What Happens When We Insert Duplicate Values?

Let's insert employee records into the EMPLOYEE table.

INSERT INTO employee
VALUES (101, 'Ravi', 30000);

INSERT INTO employee
VALUES (102, 'Kumar', 40000);

Both records are inserted successfully.

Now try inserting a duplicate EMP_ID:

INSERT INTO employee
VALUES (101, 'Arun', 45000);
Oracle Error:
ORA-00001: unique constraint violated

Why?

Because EMP_ID 101 already exists in the EMPLOYEE table.

The Primary Key constraint prevents duplicate values, with Oracle using a suitable index to enforce uniqueness.

3. How Does a Primary Key Index Improve Performance?

Consider the following query:

SELECT *
FROM employee
WHERE emp_id = 102;

Oracle can use the Primary Key Index to locate the employee record efficiently.

  • Without an index: Oracle may perform a Full Table Scan.
  • With an index: Oracle can potentially perform an Index Unique Scan because EMP_ID is unique.
Important:
An Index Unique Scan is an execution plan operation that Oracle can use when accessing a unique index with equality conditions on all indexed key columns.

4. How to Check Primary Key Indexes in Oracle?

We can use Oracle Data Dictionary views to find Primary Key constraints and their associated indexes.

Step 1: Find the Primary Key Constraint

SELECT constraint_name,
       table_name,
       constraint_type
FROM user_constraints
WHERE table_name = 'EMPLOYEE'
AND constraint_type = 'P';

Here, P indicates a Primary Key constraint.

Step 2: Find the Index Associated with the Primary Key

SELECT constraint_name,
       index_name
FROM user_constraints
WHERE table_name = 'EMPLOYEE'
AND constraint_type = 'P';

This query displays the index associated with the Primary Key constraint, when one is associated.

Step 3: Check Index Details

SELECT index_name,
       table_name,
       uniqueness
FROM user_indexes
WHERE table_name = 'EMPLOYEE';

The UNIQUENESS column indicates whether the index is UNIQUE or NONUNIQUE.

5. Difference Between Primary Key and Unique Index

Primary Key Unique Index
A database constraint. A database index.
Prevents duplicate values. Prevents duplicate indexed keys.
Does not allow NULL values. Can allow multiple NULL values in a single-column index.
Only one Primary Key constraint per table. Multiple Unique Indexes can exist.
Normally creates or uses a suitable index. Created using CREATE UNIQUE INDEX.
Important Interview Point:
A Primary Key is a constraint, whereas a Unique Index is an index structure. They are related but are not the same thing.

6. Important Oracle Interview Questions

Q1. Does Oracle automatically create an index when we create a Primary Key?

Yes. Oracle normally creates a Unique Index if a suitable existing index is not available.

Q2. Is a Primary Key Index Unique or Non-Unique?

Oracle normally uses a Unique Index to enforce a Primary Key.

However, if a suitable pre-existing Non-Unique Index is used to enforce the Primary Key, Oracle can use that instead.

Q3. Can a table have multiple Primary Keys?

No. A table can have only one Primary Key constraint, but it can contain multiple columns. This is called a Composite Primary Key.

Q4. Can a Primary Key contain NULL values?

No. Primary Key columns cannot contain NULL values.

Q5. Can we create a Primary Key without creating an index manually?

Yes. Oracle normally creates the required index automatically if a suitable index does not already exist.

7. Interview Answer – What is a Primary Key Index in Oracle?

A Primary Key Index in Oracle is an index used to enforce the uniqueness of Primary Key values and improve data retrieval.

When we create a Primary Key constraint, Oracle normally creates a Unique Index automatically if a suitable index does not already exist.

Easy way to remember:

  • Primary Key → Uniquely identifies each row.
  • Primary Key Index → Helps enforce uniqueness and retrieve records efficiently.
  • Primary Key → Does not allow duplicate or NULL values.

Important interview point: A Primary Key is a constraint, not an index. Oracle normally uses a Unique Index to enforce that constraint.

Conclusion

A Primary Key Index is an important concept in Oracle Database. It helps Oracle enforce Primary Key uniqueness and retrieve records efficiently.

Understanding the difference between a Primary Key constraint and a Unique Index is important for Oracle Developers and DBAs, especially when preparing for Oracle SQL and DBA interviews.

Key Points to Remember:
  • Primary Key uniquely identifies each row.
  • Primary Key does not allow duplicate or NULL values.
  • Oracle normally creates a Unique Index automatically.
  • A Primary Key is a constraint, not an index.
  • Oracle can use a suitable existing Non-Unique Index.
  • Use USER_CONSTRAINTS and USER_INDEXES to check Primary Key and index details.

No comments:

Post a Comment