How to create a Foreign Key with "ON UPDATE CASCADE" on Oracle? - oracle

In MS SQL Server it is possible to create a foreign key with ON UPDATE CASCADE option, so whenever you update one of the columns in the primary key, the foreign keys in other tables will also be update by the DBMS.
So, how to do it in Oracle?

Oracle does not allow a Foreign Key constraint with “ON UPDATE CASCADE”.
Here are a couple of options you have.
Create the Foreign Key, and create an “On Update” trigger.
Make use of the package below (needs to be installed in the db).
http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteupdatecascade.html
Let me know if you have additional questions or need more information.

Would a database trigger do the job for you ?
Here is the Oracle doc on the subject of Data Integrity for 11g (just incase you were interested).

You can't use on update cascade, but you can create a trigger that will resolve the issue:
create table tab1(
pk int PRIMARY KEY,
aa int);
create table tab2(
pk int PRIMARY KEY,
tab1_pk int,
FOREIGN KEY(tab1_pk) REFERENCES tab1(pk));
------------------------------------------
create or replace trigger tab1_pkUpdate
after update of pk on tab1
for each row
begin
update tab2 s
set s.tab1_pk = :new.pk
where s.tab1_pk = :old.pk;
end;
/

Related

In which order should we apply primary key, foreign key constraints and create index if the Oracle table has data?

1.In which order should we apply primary key, foreign key constraints and create index if the Oracle table has millions of data and does not have prior constraints?
2.Can we use 'NOLOGGING PARALLEL' while applying primary key and foreign key constraints like we do while applying(creating) indexes? Or any other method so that primary key and foreign key constraints could be applied faster?
Note: I'll use bullets so that it is easier to read, as it is easy to get lost in long sentences.
My thoughts on the subject; see if anything of this helps.
Well,
as you can't create a foreign key constraint if column(s) it references aren't part of primary or unique key
you'll obviously first have to create primary key constraints
and then foreign key constraints
When you
create a primary key constraint,
Oracle automatically creates index that supports it, unless there's already an index you can use (with the USING INDEX clause)
which means that you can "skip" some indexes (those for primary key constraints as they'll already exist) and virtually save some time
and create "other" indexes
On the other hand,
if you first create unique index on future primary key columns and
later add primary key constraint with the USING INDEX clause, Oracle will "skip" check for possible duplicate values because unique index won't allow them
The same goes for
NOT NULL constraint on future primary key columns; primary key doesn't allow NULLs so - if a column already is NOT NULL, enforcing primary key constraint can skip NULL check as well
I don't know
which columns you'll additionally index, but - as you're on Oracle 11g -
don't forget to index all foreign key constraint columns
because you might encounter unexpected table locks if you
update primary key column in parent table, or
delete parent record
Can you do it with no logging and in parallel? Yes:
SQL> create table test (id number, name varchar2(20));
Table created.
SQL> create unique index ui1_test_id on test (id) nologging parallel 20;
Index created.
SQL> alter table test add constraint pk_test primary key (id) using index ui1_test_id nologging parallel 20;
Table altered.
SQL>
But you'll probably want to change the index and table back to NOPARALLEL and LOGGING when the initial creation is done.
SQL> alter index ui1_test_id noparallel;
Index altered.
SQL> alter table test logging noparallel;
Table altered.

How i can solve ORACLE problem in foreign key

I have a problem in oracle and I need help. I have the following query:
1 CREATE TABLE TEST1 (
2 NAME VARCHAR(20)
3 ID VAR(9)
4 PRIMARY KEY(ID)
5 FOREIGN KEY(NAME) References TEST2(ANAME)
6 ON DELETE CASCADE ON UPDATE SET NULL );
If I want to delete line #6 what should i do?
"How I can change the value of primary key and based of that the foreign keys of this pk will change too?"
First, you should never need to do that. Primary keys like this are really just numbers that identify a row, they have no meaning in themselves. It's like asking how you would change the ROWID of a row.
If you must, you could:
Find the foreign keys pointing to this table and disable them with ALTER CONSTRAINT myconstraint DISABLE
Update your primary table and catch the new id value with UPDATE test1 SET id = mysequence.NEXTVAL WHERE id = :oldid RETURNING id INTO :newid, assuming it's set by a sequence.
Update the ids in your other tables with the new id.
Reenable your constraints.
Note that altering constraints is DDL and will do an implicit commit and this approach will leave your tables unprotected by the foreign key constraints.
A second approach would be to:
Insert a new row in the primary table and catch the new id.
Update the id in the foreign tables with the new id.
Delete the old row in the primary table.
Now that I think about it, that second approach seems better to me. No DDL and it just seems cleaner.

Foreign key did not update with primary key in oracle

i'm using oracle 11g r2 Express Edition and SQL Command line. i'm getting problem when i update the primary key the foreign key did not update. is there any solution?
this is the command for my 2 table
CREATE TABLE staff(staff_id number(5) not null primary key);
CREATE TABLE customer(customer_id number(5) not null primary key,staff_id number(5) not null references staff);
What exactly did you do?
What is your SQL statement?
Did you updated the primary key to a value that is consistent with some row in the customer table? Are you sure there was a corresponding row in the customer table?

Oracle - TOM's package for "ON CASCADE UPDATE" [duplicate]

In MS SQL Server it is possible to create a foreign key with ON UPDATE CASCADE option, so whenever you update one of the columns in the primary key, the foreign keys in other tables will also be update by the DBMS.
So, how to do it in Oracle?
Oracle does not allow a Foreign Key constraint with “ON UPDATE CASCADE”.
Here are a couple of options you have.
Create the Foreign Key, and create an “On Update” trigger.
Make use of the package below (needs to be installed in the db).
http://tkyte.blogspot.com/2009/10/httpasktomoraclecomtkyteupdatecascade.html
Let me know if you have additional questions or need more information.
Would a database trigger do the job for you ?
Here is the Oracle doc on the subject of Data Integrity for 11g (just incase you were interested).
You can't use on update cascade, but you can create a trigger that will resolve the issue:
create table tab1(
pk int PRIMARY KEY,
aa int);
create table tab2(
pk int PRIMARY KEY,
tab1_pk int,
FOREIGN KEY(tab1_pk) REFERENCES tab1(pk));
------------------------------------------
create or replace trigger tab1_pkUpdate
after update of pk on tab1
for each row
begin
update tab2 s
set s.tab1_pk = :new.pk
where s.tab1_pk = :old.pk;
end;
/

Foreign key constraint error 1452 in MySQL - Magento import

i am trying to import a sql dump of magento along with some product data and i get this foreign key constraint error:
`ERROR 1452 (23000) at line 231680: Cannot add or update a child row: a foreign key constraint fails:
`magento`.`#sql-b33_27`, CONSTRAINT `FK_CATALOG_COMPARE_ITEM_CUSTOMER_ID_CUSTOMER_ENTITY_ENTITY_ID` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE CASCADE ON )`
This is the sql code which is causing the error :
--
-- Constraints for table `catalog_eav_attribute`
--
ALTER TABLE `catalog_eav_attribute`
ADD CONSTRAINT `FK_CATALOG_EAV_ATTRIBUTE_ATTRIBUTE_ID_EAV_ATTRIBUTE_ATTRIBUTE_ID` FOREIGN KEY (`attribute_id`) REFERENCES `eav_attribute` (`attribute_id`) ON DELETE CASCADE ON UPDATE CASCADE;
I am not very comfortable with sql queries. Could some one please explain me what this query does and guide me to resolve this? Thanks.
You are trying to add a record into catalog_eav_attribute, but you do not have a corresponding record in eav_attribute that matches on attribute_id
If you are also inserting bulk data into eav_attribute, I would recommend doing that first, and then the data would be in the table before the foreign key on catalog_eav_attribute needed to reference it.
This article discusses how you can use:
SET FOREIGN_KEY_CHECKS = 0;
--Do your update here
SET FOREIGN_KEY_CHECKS = 1;
If you cannot change the order that you are inserting data. You just have to make sure your data follows the Foreign Keys once everything has been inserted into the database, before you can re-enable the FOREIGN_KEY_CHECKS
I used a database repair tool, after that did this in SQL:
DROP TABLE catalog_product_flat_1 ,
catalog_product_flat_2 ,
catalog_product_flat_3 ;
Now the index is built succesfully.

Resources