Oracle - TOM's package for "ON CASCADE UPDATE" [duplicate] - 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

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?

how to drop and add multi constraints by 1 sql statement for H2 database

I'm working on H2 database, and I meet this problem -
to drop one constraint is fine, I can use this statement
alter table customer drop constraint if exists fk_customer_order ;
for add one constraint is fine too, I can use this statement.
alter table customer add constraint fk_customer_order foreign key (order_id) references order (id) on delete cascade on update cascade;
but the problems is, in customer table I have more foreign key and I want delete them in one query statement.
Something like this
alter table customer drop constraint fk_customer_order
drop constraint fk_customer_information
drop constraint ....
but this seem can not be done in h2 database, anyone can tell me can or not add or drop multi constraint by 1 sql statment? Any answer are welcome and I appreciate much.
I think it can not be done. Why don't you use multiple statements?

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.

How to create a Foreign Key with "ON UPDATE CASCADE" on 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;
/

Resources