My Oracle Support Banner

ORA-1031 Trying to Create PK/FK on Another User's Table (Doc ID 94092.1)

Last updated on MARCH 14, 2019

Applies to:

Oracle Database - Enterprise Edition - Version 7.0.16.0 to 11.2.0.1.0 [Release 7.0 to 11.2]
Oracle Database Cloud Schema Service - Version N/A and later
Oracle Database Exadata Cloud Machine - Version N/A and later
Oracle Database Exadata Express Cloud Service - Version N/A and later
Oracle Cloud Infrastructure - Database Service - Version N/A and later
Information in this document applies to any platform.

Purpose

NOTE: In the images and/or the document content below, the user information and data used represents fictitious data from the Oracle sample schema(s) or Public Documentation delivered with an Oracle database product. Any similarity to actual persons, living or dead, is purely coincidental and not intended in any manner.

To clarify unclear documentation regarding the behavior of primary key constraints.

Scope

DBAs who manage constraints and support personnel.


In order to create a primary/foreign key on another schema's table you must have
the create any index privilege or create the index seperately or grant a
REFERENCES privilege to the user who is executing the ALTER TABLE command
Granting index and alter privileges on the table is not sufficient.
Here is why:

Given two schemas, user1 and user2, with user1 owning table FOO and granting
ALL on FOO to user2, if user2 issues CREATE INDEX foo_pk on FOO(BAR)the result
will be the creation of an index user2.foo_pk on FOO.

However, the statement ALTER TABLE FOO ADD CONSTRAINT foo_pk PRIMARY KEY(bar)
will result in a recursive SQL statement CREATE INDEX user1.
which will fail with ora-1031 insufficient privileges as user2 does not have
privileges to create an index in the user1 schema.
Note that granting index privilege on an object in user1 schema does NOT grant
the privilege to create indexes in user1 schema.

Details

To view full details, sign in with your My Oracle Support account.

Don't have a My Oracle Support account? Click to get started!


My Oracle Support provides customers with access to over a million knowledge articles and a vibrant support community of peers and Oracle experts.