Yes! Oracle automatically creates a UNIQUE INDEX to enforce a PRIMARY KEY constraint if a suitable existing index is not available.
A Primary Key has two important properties:
- It does not allow duplicate values.
- It does not allow NULL values.
Oracle uses an index to efficiently enforce the uniqueness requirement.
Let's understand with practical examples.
1. Create a Table with a Primary Key
Consider the following CREATE TABLE statement:
CREATE TABLE EMPLOYEE
(
EMP_ID NUMBER PRIMARY KEY,
EMP_NAME VARCHAR2(50),
SALARY NUMBER
);
What Happens Internally?
- Oracle creates the EMPLOYEE table.
- Oracle creates a Primary Key constraint on EMP_ID.
- Oracle automatically creates a unique index on EMP_ID if no suitable index already exists.
Oracle uses index support to enforce uniqueness.
2. How to Verify Whether Oracle Created an Index?
Execute the following query:
SELECT
INDEX_NAME,
TABLE_NAME,
UNIQUENESS
FROM USER_INDEXES
WHERE TABLE_NAME = 'EMPLOYEE';
Example output:
| INDEX_NAME | TABLE_NAME | UNIQUENESS |
|---|---|---|
| SYS_C008123 | EMPLOYEE | UNIQUE |
The index name may be system-generated.
Check the Primary Key Constraint
Execute:
SELECT
CONSTRAINT_NAME,
CONSTRAINT_TYPE,
TABLE_NAME
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'EMPLOYEE';
Example output:
| CONSTRAINT_NAME | CONSTRAINT_TYPE | TABLE_NAME |
|---|---|---|
| SYS_C008123 | P | EMPLOYEE |
Here:
- P means Primary Key.
- U means Unique Constraint.
3. What Happens When We Insert Duplicate Values?
Let's insert some records:
INSERT INTO EMPLOYEE
VALUES (101, 'Ravi', 30000);
INSERT INTO EMPLOYEE
VALUES (102, 'Kumar', 40000);
COMMIT;
Now try inserting a duplicate EMP_ID:
INSERT INTO EMPLOYEE
VALUES (101, 'Sita', 50000);
Oracle returns:
ORA-00001: unique constraint violated
Why?
Because EMP_ID = 101 already exists, and the Primary Key constraint does not allow duplicate values.
4. Does Oracle Always Create a New Index for a Primary Key?
No! This is a very important Oracle interview concept.
If a suitable existing index is available, Oracle can use that index to enforce the Primary Key instead of creating another index.
Example: Create a Unique Index Manually
First, create a table:
CREATE TABLE EMPLOYEE
(
EMP_ID NUMBER,
EMP_NAME VARCHAR2(50),
SALARY NUMBER
);
Now create a unique index manually:
CREATE UNIQUE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID);
Next, add a Primary Key constraint:
ALTER TABLE EMPLOYEE
ADD CONSTRAINT PK_EMP_ID
PRIMARY KEY (EMP_ID);
What Happens?
- Oracle uses the existing unique index.
- Oracle does not need to create another index.
- The existing index helps enforce the Primary Key constraint.
5. Can We Create a Primary Key Using a Non-Unique Index?
Yes! Oracle can use an existing non-unique index to enforce a Primary Key constraint.
Example:
CREATE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID);
Then add the Primary Key:
ALTER TABLE EMPLOYEE
ADD CONSTRAINT PK_EMP_ID
PRIMARY KEY (EMP_ID);
Oracle can use the existing non-unique index to enforce uniqueness through the constraint.
This is an important distinction:
- A Primary Key constraint does not always require a physically UNIQUE index.
- Oracle can enforce uniqueness using a non-unique index as well.
- The constraint itself guarantees that duplicate Primary Key values are not allowed.
6. Difference Between Primary Key and Unique Index
| Feature | Primary Key | Unique Index |
|---|---|---|
| Purpose | Enforces a business rule | Enforces unique index keys |
| Duplicate values | Not allowed | Not allowed |
| NULL values | Not allowed | Multiple NULLs allowed in a single-column index |
| Automatically created? | Oracle may create an index when needed | Created explicitly using CREATE UNIQUE INDEX |
| Foreign Key reference | Can be referenced | Cannot be referenced directly |
| Database object | Constraint | Index |
7. Important Oracle Interview Questions
Yes. Oracle automatically creates a unique index if a suitable existing index is not available.
Not necessarily. Oracle can use a suitable existing index.
Yes. Oracle can use a non-unique index to enforce Primary Key uniqueness.
Not directly while it is being used to enforce the constraint. You generally need to disable or drop the constraint first, depending on the situation.
No. Oracle does not automatically create an index on Foreign Key columns. You generally create one when appropriate for query performance and concurrency.
8. Easy Way to Remember
Oracle creates a suitable index if needed or reuses an existing one.
- Primary Key guarantees UNIQUE and NOT NULL.
- Oracle automatically creates a suitable index if needed.
- An existing suitable index can be reused.
- A Foreign Key does not automatically get an index.
One-Line Interview Answer
"Yes, Oracle automatically creates a unique index when we create a Primary Key constraint, provided a suitable existing index is not available. However, Oracle can also use an existing unique or non-unique index to enforce the Primary Key constraint."
Next Important Oracle Interview Topic
Does Oracle Automatically Create Indexes for Foreign Keys?
Understanding Primary Key indexes versus Foreign Key indexes is very useful for Oracle DBA and SQL Developer interviews.
No comments:
Post a Comment