Cascade update
Is there a way in oracle to achive cascade update functionality just as cascade delete?
It is possible
http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:5773459616034
However, as Tom indicates, it is a very bad practice. If you envision ever contemplating changing primary keys, you need a new primary key. A sequence-generated synthetic key column is an excellent choice if there is no natural key.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC
Similar Messages
-
how to cascade update?
Sorry everyone for going regional with my Mexican fiend!
In MEXMAN's first case, integrity on related data was already being enforced through a valid foreign key (TIZAYUCA.REGLAFAB_PRODUCTO_FK). However, the user wanted to update data included in the foreign key relationship. To acomplish this, I recommended to change the actual constraint state to defer validation until commit. Using the now familiar column names, the sentences are:
ALTER TABLE TIZAYUCA.REGLA_FABRICACION
DROP CONSTRAINT REGLAFAB_PRODUCTO_FK;
ALTER TABLE TIZAYUCA.REGLA_FABRICACION
ADD CONSTRAINT REGLAFAB_PRODUCTO_FK FOREIGN KEY (GRUPO, PRODUCTO)
REFERENCES TIZAYUCA.PRODUCTO (GRUPO, CLAVE)
INITIALLY DEFERRED;
In MEXMAN's second case, a wholly new integrity needs to be enforced between two tables but some actual data present in both tables do not validate the desired constraint. The offending rows from the child table (TIZAYUCA.PRESENTACION) may be obtained executing
SELECT *
FROM tizayuca.presentacion t
WHERE (t.grupo, t.producto) NOT IN
(SELECT p.grupo, p.clave
FROM tizayuca.producto p);
MEXMAN has 3 choices:
1. deleting the offending rows in the child table (TIZAYUCA.PRESENTACION) before executing
alter table TIZAYUCA.PRESENTACION enable constraint PRES_PROD_FK;
2. inserting the missing rows in the parent table (TIZAYUCA.PRODUCTO) before executing
alter table TIZAYUCA.PRESENTACION enable constraint PRES_PROD_FK;
3. making the new constraint ignore the present data and enforcing the relationship starting with new data:
alter table TIZAYUCA.PRESENTACION
add constraint PRES_PROD_FK foreign key (GRUPO, PRODUCTO)
references TIZAYUCA.PRODUCTO (GRUPO, CLAVE)
deferrable initially deferred
enable novalidate;
(drop the present PRES_PROD_FK constraint first)
Please find more on the constraint topic here:
http://download-east.oracle.com/docs/cd/B14117_01/server.101/b10759/clauses002.htm#CJAFFBAA
and here:
http://download-east.oracle.com/docs/cd/B14117_01/server.101/b10759/clauses002.htm#i1002273
Best regards,
Luis Morales,
ConsiteNicaragua.com -
Hi,
I know that oracle apps coding standard prohibits cascade delete feature in the database. Hence it should be implemented at application level.
However my question is regarding cascade update. Need is to update the header's WHO columns, when an update is made to any of the lines. Both the tables are custom tables. I was thinking to write a trigger, instead of putting this logic in application.
What should be the better solution and why? Writing trigger or implementing this functionality in application (OAF).
Abdul WahidHi Sandeep.
Thanks for the answer.
However its still half.
Answer is considering putting the logic at application level,fine. but why?
Question is about its benefits over putting the trigger on the lines table.
Why I am favouring trigger is due to following reasons..
1. Lesser maintenance. (Just one trigger, no code jungle)
2. Update of header with line' WHO is always expected (from application or any concurrent program)
3. And putting things in application, brings challenge of maintaining integrity constrains....
This third point is main reason I am seeing behind oracle's statndard for cascade delete, cause they are seeded tables and putting a trigger on header table may delete lines rows always (Very specific to that screen), which may not be desirable in a lot cases.Huge integrity risk.
This case is of update desirable always, where I am seeing using trigger as help to maintain integrity, instead of risk to integrity.
Is there any doc about such situation( coding and design guidelines). I searched net and metalink, but yet did not find.
(my application is OAF, and its implemented also by the grace of Allah.)
Abdul Wahid -
Best practice when doing large cascading updates
Hello all
I am looking for some help with tackling a fairly large cascading update.
I have an object tree that needs to be merged using JPA and Toplink.
Each update consists of 5-10000 objects with a decent depth as well.
Can anyone give me some pointers/hints towards a Best practice for doing this? Looping though each object with JPA's merge takes minutes to complete, so i would rather not do that.
I have never actually used TopLinks own API before, so i am especially interested if TopLink has an effective way of handling this, preferably with a link to some related reading material?
Note that i have a somewhat duplicate question on (Noting for good forum practice)
http://stackoverflow.com/questions/14235577/how-to-execute-a-cascading-jpa-toplink-batch-updateNot certain what you think you can't do. Take a long clip and open it in the Viewer. Set In and Out points. Drop that into the Timeline. Now you can move along in the Viewer clip and set new Ins and Outs and drop that into the Timeline. Clips in the Timeline are created from the Ins and Outs you set in the Viewer.
Is that what you want to do? If it is, I don't where making copies of the clip would work for you
Later, if you want to match up a clip in the Timeline to that master clip, just use Match Clip (find) in the timeline to find where it correaltes to your main clip
You can have FCE automatically create subclips at camera cut points by using DV Stop/Start Detect if that is what you're looking for -
Cascade Update in Oracle Constraint ?
How can I create a Constraint in Oracle 9i with Cascade Update Option like On Delete Cascade.
It means, if I change the primary key of master table, the foreign key of all the relevant child tables should be updated automatically.
Any idea please ?
Best Regards,
LuqmanOracle doesn't intrinsically have the concept of a CASCADE UPDATE.
From a data modeling standpoint, primary keys should never, ever change under any circumstances. If a column can ever change, it should not be used as a primary key.
Tom Kyte has a package and some code to generate triggers that would do this sort of thing
http://asktom.oracle.com/~tkyte/update_cascade/index.html
but I would strongly suggest you change the data model rather than going down that path.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Cascade Update (Urgent)
Dear Friends,
I need to update the master records have records in details tables. Can anyone suggest me the feature of CASCADE UPDATE. So that moment i changed the master record it should reflect to detail records.
I DON'T WANT TO USE CURSOR TO UPDATE RECORDS IN DETAILS.
Thanks.Hi,
You can try to use the pre-update, on-update, post-update trigger for the master-block. In that you can parse an update statement for the detail block. This will 'simulate' a cascade delete
Hope this helps. -
ON CASCADE UPDATE in ODI ?
Hello,
Does any one know if this works with Oracle as ODI shows this option in Model --> datastores --> constraints --> foreign key reference.
Thanks
GreenwichOracle doesn't intrinsically have the concept of a CASCADE UPDATE.
From a data modeling standpoint, primary keys should never, ever change under any circumstances. If a column can ever change, it should not be used as a primary key.
Tom Kyte has a package and some code to generate triggers that would do this sort of thing
http://asktom.oracle.com/~tkyte/update_cascade/index.html
but I would strongly suggest you change the data model rather than going down that path.
Justin
Distributed Database Consulting, Inc.
http://www.ddbcinc.com/askDDBC -
Updating PK with same value - effect on CASCADE UPDATE
Hello,
I would like to understand how sql server 2008 deals with cascade updates
For example I have
Parent table: Employee with column Id as varchar(20) primary key
Child table with IdEmployee as varchar(20) foreign key
I set up Cascade Update for those two tables, meaning any change to primary key in Employee table will cause update in child table rows that match affecting Id
Scenario 1:
Update Employee
set Id = 'ABC',
Name = 'something new'
where Id = 'CCC'
Result of child table: all rows with foreign key IdEmployee and value of 'CCC' are updated. Expected behavior.
Scenario 2:
Update Employee
set Id = 'ABC',
Name = 'something new 2'
where Id = 'ABC'
This time, i am doing something different. I am beside update of column Name with new value, also update primary key but
with SAME value
Question is: what is going to happen to child rows? Are they ALL going to UPDATE due to CASCADE UPDATE
So far, what i did in order to find solution is:
1. I put an timestamp column in child table that should update each time row gets updated
2. I put a trigger for update event on child table that will write something to some log table
*After I set up those two I ran example like above just to be sure timestamp gets changed as well trigger is being fired
Results of updating PK with same value:
1. Timestamp didnt change
2. Trigger didnt fire
Is this enough to make conclusion that updating primary key with same value ALONG with updating some other columns won't
affect child tables with UPDATE CASCADE ON
Update:
Database is CI AS collation
If i do following
Update Employee
set Id = 'abc',
Name = 'something new'
where Id = 'ABC'
1. Timestamp will change
2. Trigger will fire
Conclusion: Case sensitive is important here!
Thank you very much in advance
Milos>> would like to understand how sql server 2008 deals with cascade updates <<
Your posting has a number of conceptual errors.
1. The terms “parent” and “child” are not RDBMS; they are used in network databases. We have “referenced” and “referencing” tables; they can be the same table.
2. A table models a SET of things, so there is no “Employee” table unless you truly have a one-man company. We want a collective or plural name for the SET/table. A better name is “Personnel” for this table.
3. Her is no such thing as a generic “id” in RDBMS; it has to be “<something in particular>_id” to be valid. Identifiers are usually fixed length
4. It is very, very rude not to post DDL on a forum. You also do not know the ISO-11179 Rules for data element names. They do not change names from table to table! Does your name change whenever you use it in a new place?? NO! Same principle with data.
5. The ISO standard uses “<property>_<attribute property>” syntax, no the old PascalCase.
6. Why did you post a useless narrative? How do we compile “I SET up Cascade UPDATE for those two tables,..” to test it??
CREATE TABLE Personnel
(emp_id CHAR(20) NOT NULL PRIMARY KEY,
emp_name VARCHAR(25) NOT NULL,
CREATE TABLE Health_Plan
(health_plan_acct CHAR(20) NOT NULL PRIMARY KEY,
emp_id CHAR(20) NOT NULL
REFERENCES Personnel(emp_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
Scenario 1:
UPDATE Personnel
SET emp_id = 'ABC',
emp_name = 'something new'
WHERE emp_id = 'CCC';
Result of child table: all rows with foreign key emp_id and value of 'CCC' are updated. Expected behavior.
Scenario 2:
UPDATE Personnel
SET emp_id = 'ABC',
emp_name = 'something new 2'
WHERE emp_id = 'ABC';
This time, I am doing something different. I am beside UPDATE of column emp_name with new value, also UPDATE PRIMARY KEY but
with SAME value.
>> Question is: what is going to happen to child [sic: referencing] rows? Are they ALL going to UPDATE due to CASCADE UPDATE. <<
SQL uses a set-oriented model, so the whole table is updated as a unit of work in theory.
So far, what I did in order to find solution is:
>> I put an timestamp column in child [sic: referencing] table that should UPDATE each time row gets updated <<
Why? It is not in the SET clause list; it cannot change. As an aside, The T-SQL TIMESTAMP is not the ANSI/ISO TIMESTAMP; it is DATETIME2(n) in T-SQL. The old TIMESTAMP is being deprecated because it stinks both in concept and implementation.
>> I put a trigger for UPDATE event on child [sic: referencing] table that will write something to some log table.<<
TRIGGERs are fired by what is called a “database event” shown in the ON [DELETE | UPDATE] clause. T-SQL adds INSERT as an event. An update to any value or to no value at all is still an update. Depending on the collation, case may or may not matter in the final
outcome.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Hi,
I am using Oracle 10 XE, and using Oracle ADF with JDeveloper. I have some basic database tables that users fill in using forms from Oracle ADF. However, there are more complicated database table that rely on these basic tables. My question is when a user makes changes to these basic database tables, how do I cascade the users updates to the more complicated databases tables in Oracle 10 XE?depending on the specific of what you are going to need you should read about composite associations
http://docs.oracle.com/cd/E16162_01/web.1112/e16182/bcentities.htm#sm0150
Which does handle some cascading issues for you.
And also about 4.10.9 How to Synchronize with Trigger-Assigned Values
http://docs.oracle.com/cd/E16162_01/web.1112/e16182/bcentities.htm#BABBHCHI -
Hello,
Let's say I have these 2 classes:
public Class Pessoa {
@Id
@Column(name = "ID_PESSOA", nullable = false)
private Long idPessoa;
private String nmPessoa;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "pessoa", fetch=FetchType.EAGER)
private Collection<Telefone> fones;
public Class Telefone {
@Id
@Column(name = "ID_FONE", nullable = false)
private int idFone
private String nrFone;
private int tpFone;
@JoinColumn(name = "ID_PESSOA", referencedColumnName = "ID_PESSOA")
@ManyToOne
private Pessoa pessoa;
And then I try tu update the Pessoa object:
Pessoa p = pessoaDAO.getPessoa(cdPessoa);
p.setNmPessoa("Maria");
p.getFones().removeAll(p.getFones());
p.setFones(newFoneCollection);
pessoaDAO.atualizar(p); // merge(p)
After this I can see the change on the nmPessoa, but it does not happen on fones.
I can have some changes if I add new fones, but the ones I already have stay in DB.
Could someone tell me how I can do it?
tks!Hi Mike,
thanks for the reply!
I did a test using the Derby DB (from netbeans 5.5) which has some tables. I took 2 tables ORDERS and CUSTOMER that has the same relation as PESSOA and TELEFONE.
In this test netbeans generated the entities automatically and I just created the TestClass like this:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("testaCascadePU");
Persistencia persis = new Persistencia(emf); // has EntityManager, persist/merge/remove/find
Customer c = (Customer)persis.find(entity.Customer.class, new Integer(1));
Customer cTemp = new Customer(c.getCustomerId());
Orders o = new Orders();
o.setQuantity(new Short("30"));
o.setShippingDate(new Date());
o.setCustomerId(cTemp);
o.setOrderNum(new Integer(10398011));
o.setProductId(new Product(new Integer(980001)));
Collection<Orders> ordersL = new ArrayList();
ordersL.add(o);
c.setName("Robert Plant Test");
for (Orders os: c.getOrdersCollection()) {
os.setCustomerId(null);
os.setProductId(null);
c.getOrdersCollection().removeAll(c.getOrdersCollection());
c.setOrdersCollection(ordersL);
persis.update(c);
And this test still have the same problem that I had before.
Even when I "unset" the orders, I keep getting the same orders in the DB plus the new order instead of getting just the new order as I wish.
Am I doing something wrong?
This scenario is quite common but I really don't know how to deal with it.
Thanks again! -
Hi,
If I want to update a Primary Key to a new value and there are existing references to it in a Foreign table, and I do not have On UPDATE CASCADE, how can I accomplish this?
do I have to delete all the child records, update the parent's primary key, and then insert the child records again w/ the updated primary key?
Thanks!Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should
use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
>> If I want to update a PRIMARY KEY to a new value and there are existing references to it in a Foreign [sic: referencing] table, and I do not have ON UPDATE CASCADE, how can I accomplish this? <<
Alter your table and add the constraint. If you missed this one, look for more design errors and commissions.
>> do I have to delete all the child [sic: referencing] records [sic: rows], update the parent's [sic: referenced] primary key, and then insert the child [sic] records [sic]again w/ the updated primary key?<<
You need to learn basic terms and concepts! And t kick the guys that forgot this in the first place.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
I have a master-detail block in my form. My question is When I update one record in master block and commit the corresponding child table records should be updated. I know there exists an integrity constraint between the tables and I am getting an error of "Integrity constraint exists and child record found". Is there any way we can do a work around for this in forms 10g. Your help is appreciated.
thanks
marcbharWhen one record is updated in Master table the corresponding records in child table should also get updated.
For example,
Master Table: Tbl_Mast has colums m_id, s_code, name, sub_name
Records : m_id s_code name sub_name
1 M008.01 sales valid
2 K009.02 marketing invalid
Detail Table: Tbl_Detail has colums detail_id, s_code, name, subname, sale_no
Records : detail_id s_code name subname sale_no
48 M008.01 sales valid 1111
49 M008.01 sales valid 238
50 M008.01 sales valid 400
Now my question is, If I update sub_name = "invalid" where s_code="M008.01" in Tbl_Mast then all the records in detail table Tbl_Detail should be updated as follows
48 M008.01 sales invalid 1111
49 M008.01 sales invalid 238
50 M008.01 sales invalid 400
Now I am getting this following error.
ORA-02292: integrity constraint (A.S_SDC_FK) violated - child record found -
Cascade update of primary key in child tables
Hi,
We have a typical scenario in which the primary key has to propage to the child table into 3 levels. We are getting the no parent key exception if we create new data into the three tables using 3 level master detail relationship and execute commit operation. The following is the structure of the tables.
Parent_table (sno number primary key, name varchar2(100));
Child_table (fk_sno number primary key, name varchar2(100), foreign key (fk_sno) references parent_table(sno));
GrandChild_table (sno number primary key, fk_sno number, name varchar2(100), foreign key (fk_sno) references Child_table(fk_sno));
The requirement is to update fk_sno in child_table and grandchild_table with the trigger generated primary key sno in the parent_table.
The application module is having the following structure
parenttable
|
----------childtable
|
-----------grandchild_table
If we create new records into parent_table and child_table it is working fine when we commit the data. If we create another record into grandchild_table and commit data we are getting the foreign key violation exception. We are using composition in the association to update the modified values to refresh the data.
Is there any problem in the data model to use fk_sno as primary key and foreign key in child_table.
Thanks and Regards,
S R PrasadHmm, another example is CSKS, this logic was probably designed already in the 1980s...maybe it was seen beneficial to be able to select the currently valid record by using the primary key only (the first record where DATBI is larger than system date, not possible when DATAB is the key instead).
Maybe there are better explanations. Why do you need to know this, just curious?
Thomas -
Problem with cascade delete and remove bean
I am working with two entity beans that map to two tables that have a foreign key relationship. Table B has a foreign key to A and in the database that foreign key is set for cascaded updates and cascaded deletes.
The problem occurs when the the sytem first tries to remove bean 1 (mapped to table A) and then remove bean 2 (mapped to B) where bean 2 is associated with bean 1 with a foreign key relationship. The first remove works but then when it tries to remove bean 2 it throws a very ugly "CORBA TRANSACTION_ROLLEDBACK 9998" Maybe exception. My guess is that the reason is because bean 2's reocrd in the database was deleted when bean 1 was removed but the 'bean object' was not removed from the container.
When I go into our Application Server and look at how it see's the tables, it show the wrong relationship. It shows a restrict delete and a restrict update realationship.
My question is, am I wrong to think that this is a application server problem or a configuration problem? It seems to me that attempting to remove a non-existant record should not cause an error. It won't cause any SQL exceptions. Is this a flawed viewpoint? As a work around I made sure that the dependent records are deleted first but it kind of defeats the point of cascaded deletes.
We have a limited number of support calls, should I use one or am I at fault here?If the database removes the record from the second
table, why is the system trying to remove it once
again? You should try to remove an entity from a
single place, should it be the database or the
application. Don't try to remove it twice.
Regards,
DimitarI could do this but it is a huge pain in my ass. The problem is that you might want to remove the dependent bean without removing it's parent. The object structure is a little questionable, I'll admit that. It is, as they say, the eleventh hour and I can't really change that now.
The way this work is that the server gets a list of objects marked either as new, modified, or deleted. It then relates those changes back to the database.
In this case we have two lists(which makes me realize where the class structure sucks.) In order to do what you suggest I would have to get all the deleted parent objects and then search all the deleted child objects for ones that have the parent's key in them.
It would be prefferable to fix the class structure but again this is not an option.
Anyone want to answer the question I asked? -
Dynamic Change event getting error with cascading LOV's
Hi all,
In my application (v4.0.0), I have two sets of cascading drop-down select lists, these each make up a date (year, month, day) plus a time value (not cascading).
I also have a dynamic action that gets the hours difference between the two dates set above. This fires on the change event.
When I change, say the month drop-down, the day list is refreshed. But before it is refreshed, it looks to be emptied and the change event fires. I then get an error from the dynamic action because the day value passed to the query is empty (confirmed when looking at POST data).
Can anyone think of a workaround idea for this? e.g. a way to set the dynamic action to conditional?
Thanks!Hi Peter
Thanks for the tip, I have tried this and it has helped, although not solved the problem. I was originally getting two errors appear ("AJAX call returned server error ORA-01843: not a valid month for Set Value.") and this has removed one of them...
When looking in Firebug, it seems to me that although the dynamic actions (cascading update + set value) are running one before the other, the values aren't updated in order. e.g. the cascading update is not applied before the set value action is run.
Could this be a bug with dynamic actions?
Maybe you are looking for
-
HELP! How to recover lost data for a 7 month period?
I was doing my usual daily syncing (via USB cable) between my Zire 21 to my iMac OSX ver. 10.4.11 and then I noticed that my data disappeared from Feb 18 to Sept 19 2008.... I have my PalmDesktop ver 4.1 data on my iMac since 1998 - it is my diary of
-
Internal Hard Drive Max?
One of my friends just purchased a new M9592LL/A G5 PowerMac. Its the top of the line last model G5 two Dual 2.5 GHz. I was wondering what is, if there is, a max internal hard drive size. I remember that some older computers have issues with being ab
-
Having trouble with connecting my guitar.
Hey guys I just bought logic express 8 wanting to record with my guitars. Im really new to recording and don't know how to record from my instruments. I bought a Griffin instrument guitar jack adapter for my audio-in for my macbook because the apple
-
Background job in shell script?
I run these three commands on the command line to start my Linux gnome session. But when I embed them in a script, as follows, it is as if control is never returned from starting the first command in the background. Obviously I am ignorant of some ba
-
How do we request a new forum?
I'd like to request a new forum for discussing programming on the iPhone. Now that the SDK came out, I'm sure that there's going to be a LOT of traffic on that. It would be nice to keep it all in one place. How do we go about making that happen?