I'm getting ORA-01981: CASCADE CONSTRAINTS must be specified to perform this revoke when executing:
REVOKE REFERENCES ON dbo.ABC FROM XYZ;
How do I address the issue?
try this:
REVOKE REFERENCES ON dbo.ABC FROM XYZ CASCADE CONSTRAINTS;
REFERENCES is the privilege which allows us to create foreign keys across schemas. If the privilege is revoked any foreign keys we created on the referenced object must be dropped. The documentation says
"When a REFERENCES privilege for a table is revoked from a user, any foreign key integrity constraints that are defined by the user and require the dropped REFERENCES privilege are automatically dropped. "
Hence the need for cascade constraints.
revoke references on dbo.abc from xyz cascade constraints;
Related
I'm trying to use a trigger to clean some #OneToOne related entities upon deletion of another (via SQL). Let's suppose I have a Person and an OrgUnit both referencing some Address. That Address is mapped with an ADDRESS_ID and related foreign key constraints.
The following trigger compiles just fine:
CREATE OR REPLACE TRIGGER ON_DELETE_PERSON
AFTER DELETE ON PERSON
FOR EACH ROW
BEGIN
DELETE FROM ADDRESS WHERE ID = :OLD.ADDRESS_ID;
END;
/
But whenever I try to delete a Person the following error is thrown:
DELETE FROM PERSON WHERE ID = 21179
ORA-04091: table MY_SCHEMA.PERSON is mutating, trigger/function may not see it
ORA-06512: in "MY_SCHEMA.ON_DELETE_PERSON", row 2
ORA-04088: error during execution of trigger 'MY_SCHEMA.ON_DELETE_PERSON'
What must be changed to make this trigger work?
Within the Java code I could simply handle this with CascadeStyle.DELETE... but I want to transfer that responsibility to the database to allow proper ON DELETE CASCADE behavior for all the data.
-- CASCADE DELETE on ORGUNIT deletion...
ALTER TABLE PERSON ADD CONSTRAINT PERSON_F01
FOREIGN KEY (ORGUNIT_ID) REFERENCES ORGUNIT (ID)
ON DELETE CASCADE ENABLE VALIDATE;
-- SET NULL on ADDRESS deletion...
ALTER TABLE PERSON ADD CONSTRAINT PERSON_F02
FOREIGN KEY (ADDRESS_ID) REFERENCES ADDRESS (ID)
ON DELETE SET NULL ENABLE VALIDATE;
So, deleting an ORGUNIT automatically deletes all related PERSON too... but the ADDRESS would remain in the database. The TRIGGER above is meant to handle this case.
So after a few comments the answer pops up:
The FK constraint with ON DELETE SET NULL fires back to the table and causes that exception. Looks like I can't have both...
I'll get rid of the ON DELETE definition and risk the FK constraint error in that case. The cascaded delete via the trigger has the higher value to me.
I have two schemas, and I am trying to create a table with two foreign key constraint. Creating the foreign key constraint does not work regardless of whether I add the constraint separately or in the table creation DDL. Also regardless of which of the two users I try to run it.
The oracle error is ORA-01031: insufficient privileges.
The table is created when I omit the foreign key constraints.
Intended result: create a table with two constraints.
CREATE TABLE "XXX_MONITORING"."COMPOSITE_STATUS"
( "COMPOSITE_STATUS_ID" NUMBER,
"COMPOSITE_ID" NUMBER,
"STATUS" CHAR(1),
CONSTRAINT "COMPOSITE_FK" FOREIGN KEY ("COMPOSITE_ID")
REFERENCES "XXX_MONITORING_CONFIGURATION"."COMPOSITE_KPI_COMPONENTS" ("COMPONENT_ID") ON DELETE CASCADE ENABLE,
CONSTRAINT "COMPOSITE_STATUS_FK" FOREIGN KEY ("STATUS")
REFERENCES "XXX_MONITORING_CONFIGURATION"."INDICATION_COLOR" ("INDICATION_COLOR_ID") ON DELETE CASCADE ENABLE
);
The table COMPOSITE_KPI_COMPONENTS and INDICATION_COLOR are in different schema i.e. XXX_MONITORING_CONFIGURATION.
That user must grant REFERENCES on COMPOSITE_KPI_COMPONENTS and INDICATION_COLOR to XXX_MONITORING.
-- Grant statement
grant REFERENCES on COMPOSITE_KPI_COMPONENTS to XXX_MONITORING;
grant REFERENCES on INDICATION_COLOR to XXX_MONITORING;
I have two oracle users : User1 and User2.
User1 has a table Users_
User2 has a table TestTable
I'm trying to create a foreign key constraint between the two tables as follows :
ALTER TABLE "User2"."TESTTABLE" ADD CONSTRAINT "TESTTABLE_CREATEDBY"
FOREIGN KEY (CREATEDBY) REFERENCES "User1"."USERS_" (ID) ENABLE
User2 has the privilege to select on User1 table Users_ (Grant select to user2 on Users_)
When running the alter table statement I'm having an error : of insufficient privileges.
Does anyone know how to solve that please ?
Cheers,
To create a foreign key against a table in another schema we need to have the REFERENCES privilege on that table. This is a separate privilege because it imposes a burden on the table's owner: they can't delete records from their table if you're referencing them. Find out more
So i need to do a mapping from a Employee table (idEmployee, name, etc..) to a real user with a account created. I decided to add a table Mapping_Employee_User(idEmployee, userName) like below
CREATE TABLE Mapping_Employee_User(
idEmployee NUMBER(6)
CONSTRAINT FK_Mapping_Employee_User1 REFERENCES Employee (idEmployee),
userName VARCHAR2(30 BYTE)
CONSTRAINT FK_Mapping_Employee_User2 REFERENCES ALL_USERS(USERNAME),
CONSTRAINT PK_Mapping_Employee_User PRIMARY KEY (idEmployee, userName)
);
But i am getting a "ORA01031 insufficient privileges Cause: An attempt was made to change the current username or password..." But I am not actually doing that, I just want to make a reference.
As a note: I have full rights with this user
Logged as SYS I can see that the actual table is named "USER$", and I cant find table ALL_USERS...anyway how do I do this kind of reference??
ALL_USERS and USER$ are both system tables/views. They are maintained at a low level by Oracle itself. At a level too low to enforce those constraints. You simply can't do what you're trying to do.
(Think of it this way: what'd happen if you tried to DROP USER bob? Do you expect Oracle to enforce your foreign key constraint? What'd happen if your user tablespace is offline?)
edit: I suggest you just leave off the foreign key on userName. You may want to schedule some job to compare the users in Mapping_Employee_User vs. DBA_USERS to make sure they stay in sync. Alternatively, you may want to manage your Oracle users with, say, LDAP (which I hear is possible).
ALL_USERS is a view and not a table by itself.
grant select on all_users to USERNAME;
should suffice. if you are still getting ORA-01031 it's probably because the user doesn't have the CREATE TABLE privilege:
grant create table to USERNAME;
ALTER TABLE LAB_ADMIN_USER.TEST_TEMPLATE_ABBR ADD (
CONSTRAINT TEST_TEMPLATE_ABBR_R01
FOREIGN KEY (test_template_id)
REFERENCES LAB_ADMIN.TEST_TEMPLATE (test_template_id)
ON DELETE CASCADE)
What is the most likely cause of 'ORA=01031: insufficient privileges' when executing the command above? In other words, what permission does LAB_ADMIN_USER most likely not have?
I already created the table successfully and attempted to add the FK constraint as LAB_ADMIN_USER.
The table with the primary key is owned by a different schema - LAB_ADMIN. That user must grant REFERENCES on TEST_TEMPLATE to LAB_ADMIN_USER.
From the owning schema execute GRANT REFERENCES ON OWNINGSCHEMA.TABLE TO OTHERSCHEMA;