Can we insert duplicate values into different partitions of an Oracle partitioned table when using LOCAL and GLOBAL UNIQUE INDEXES?
This is an important Oracle interview question. The behavior is different depending on whether we use a LOCAL or GLOBAL unique index.
Let's understand this concept with practical SQL INSERT examples.
1. Create a Partitioned Employee Table
First, create an EMPLOYEE table partitioned by JOIN_DATE.
CREATE TABLE employee (
emp_id NUMBER,
emp_name VARCHAR2(100),
salary NUMBER,
join_date DATE
)
PARTITION BY RANGE (join_date)
(
PARTITION p2024
VALUES LESS THAN (DATE '2025-01-01'),
PARTITION p2025
VALUES LESS THAN (DATE '2026-01-01'),
PARTITION pmax
VALUES LESS THAN (MAXVALUE)
);
Here are the table details:
- Table name: EMPLOYEE
- Partitioning column: JOIN_DATE
- Partitioning method: RANGE
- Partitions: P2024, P2025 and PMAX
2. Create a GLOBAL UNIQUE INDEX
Suppose we want EMP_ID to be unique across the entire EMPLOYEE table.
CREATE UNIQUE INDEX emp_uidx
ON employee (emp_id)
GLOBAL;
Oracle allows this because a GLOBAL unique index can enforce uniqueness across all table partitions without including the partitioning column.
3. Insert Initial Records
Let's insert two employees into different partitions.
INSERT INTO employee
VALUES (101, 'John', 50000,
DATE '2024-06-15');
INSERT INTO employee
VALUES (102, 'David', 60000,
DATE '2025-03-10');
COMMIT;
These records are stored in different partitions.
| EMP_ID | EMP_NAME | JOIN_DATE | Partition |
|---|---|---|---|
| 101 | John | 15-JUN-2024 | P2024 |
| 102 | David | 10-MAR-2025 | P2025 |
4. Try to Insert a Duplicate EMP_ID
Now, try inserting EMP_ID = 101 into another partition.
INSERT INTO employee
VALUES (101, 'Peter', 70000,
DATE '2025-05-15');
Oracle raises a unique constraint violation because EMP_ID = 101 already exists in another partition.
ORA-00001: unique constraint violated
Why Does This Happen?
- EMP_ID is the unique index column.
- The index is GLOBAL.
- Global uniqueness is checked across all partitions.
- Therefore, duplicate EMP_ID values are not allowed anywhere in the table.
A GLOBAL UNIQUE INDEX guarantees uniqueness across the complete partitioned table, not just within an individual partition.
5. Create a LOCAL UNIQUE INDEX
Now let's understand the behavior of a LOCAL UNIQUE INDEX.
First, drop the previous global index.
DROP INDEX emp_uidx;
Create a LOCAL unique index on EMP_ID and JOIN_DATE.
CREATE UNIQUE INDEX emp_uidx_local
ON employee (emp_id, join_date)
LOCAL;
This index is allowed because JOIN_DATE, the table partitioning column, is included in the unique index key.
6. Insert Duplicate EMP_ID Values with Different JOIN_DATE Values
Let's insert the following records.
INSERT INTO employee
VALUES (101, 'John', 50000,
DATE '2024-06-15');
INSERT INTO employee
VALUES (101, 'Peter', 70000,
DATE '2025-05-15');
COMMIT;
Why? Because the complete unique key combinations are different.
| EMP_ID | EMP_NAME | JOIN_DATE | Partition |
|---|---|---|---|
| 101 | John | 15-JUN-2024 | P2024 |
| 101 | Peter | 15-MAY-2025 | P2025 |
Why Are Duplicate EMP_ID Values Allowed?
The LOCAL unique index is defined on:
(EMP_ID, JOIN_DATE)
Therefore, Oracle checks the complete combination rather than EMP_ID alone.
The two combinations are:
- (101, 15-JUN-2024)
- (101, 15-MAY-2025)
Since the complete combinations are different, both records are allowed.
A composite unique index guarantees uniqueness of the complete combination. It does not guarantee that every individual column is unique.
7. Try to Insert the Same Composite Key Again
Now try inserting exactly the same EMP_ID and JOIN_DATE combination.
INSERT INTO employee
VALUES (101, 'Smith', 80000,
DATE '2025-05-15');
Oracle raises ORA-00001 because the combination (101, 15-MAY-2025) already exists.
Even though the table is partitioned, a LOCAL unique index prevents duplicate complete key combinations within the relevant partition.
8. What Happens If We Create a LOCAL UNIQUE INDEX on EMP_ID Alone?
Consider the following statement:
CREATE UNIQUE INDEX emp_uidx_local
ON employee (emp_id)
LOCAL;
Oracle rejects this statement because the LOCAL unique index does not include the table partitioning column JOIN_DATE.
A LOCAL unique index must include all partitioning key columns in its unique key.
9. LOCAL vs GLOBAL UNIQUE INDEX: INSERT Behavior
| Scenario | LOCAL UNIQUE | GLOBAL UNIQUE |
|---|---|---|
| Duplicate EMP_ID in different partitions | Allowed if JOIN_DATE differs | Rejected |
| Duplicate EMP_ID and JOIN_DATE in same partition | Rejected | Rejected when indexed columns match |
| Unique key excludes partitioning column | Not allowed | Allowed |
| Unique key includes partitioning column | Allowed | Allowed |
10. Important Oracle Interview Questions
Answer: No. A GLOBAL unique index enforces uniqueness across the entire table.
Question 2: Can duplicate EMP_ID values exist in different partitions with a LOCAL UNIQUE INDEX?
Answer: Yes, provided the complete unique key combinations are different. For example, (101, 2024-06-15) and (101, 2025-05-15).
Question 3: Can we create a LOCAL UNIQUE INDEX on EMP_ID alone when the table is partitioned by JOIN_DATE?
Answer: No. The LOCAL unique index must include JOIN_DATE.
Question 4: Does a composite LOCAL UNIQUE INDEX guarantee EMP_ID uniqueness?
Answer: No. It guarantees uniqueness of the complete combination of EMP_ID and JOIN_DATE.
Question 5: Which index should we use if EMP_ID must be globally unique?
Answer: Use a GLOBAL unique index on EMP_ID, or another suitable global uniqueness design.
11. Test Your Understanding
An EMPLOYEE table is partitioned by JOIN_DATE. A LOCAL UNIQUE INDEX is created on (EMP_ID, JOIN_DATE).
Two records are inserted:
INSERT INTO employee
VALUES (101, 'John', 50000,
DATE '2024-06-15');
INSERT INTO employee
VALUES (101, 'Peter', 70000,
DATE '2025-05-15');
What will happen?
12. Final Conclusion
Remember These Important Rules
1. A GLOBAL UNIQUE INDEX enforces uniqueness across all partitions.
2. A LOCAL UNIQUE INDEX must include all table partitioning key columns.
3. Duplicate individual EMP_ID values can exist with a composite LOCAL UNIQUE INDEX if the complete key combinations differ.
4. Duplicate complete key combinations are rejected by both LOCAL and GLOBAL unique indexes.
Understanding these INSERT examples will help you answer Oracle partitioning and indexing interview questions confidently.
Next Topic: What Happens to LOCAL and GLOBAL Indexes When We DROP or TRUNCATE a Partition in Oracle?