473,398 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Constraints

Hi -

I would like to know how the following task is accomplished:

Updating the Primary Key in the parent table when there are other tables
that reference this Key(Foreign Key)
Is there a way to have DB2 handle this task for you..

-Dave
Nov 12 '05 #1
4 2161
"Dave D" <da*******@yahoo.com> wrote in message news:<qo******************@fe10.lga>...
Hi -

I would like to know how the following task is accomplished:

Updating the Primary Key in the parent table when there are other tables
that reference this Key(Foreign Key)
Is there a way to have DB2 handle this task for you..

-Dave


Hi Dave,

This isn't possible in DB2. I know it's possible in a few other
RDBMS's but not in DB2. There is no "update cascade". As long as there
are no "child" rows, you can do an update of the pk..from the moment
there are child rows, DB2 prohibits this, for your own protection.

with best regards,

Kurt Struyf
Senior database consultant

Competence Partners, Belgium
Nov 12 '05 #2

"Dave D" <da*******@yahoo.com> wrote in message
news:qo******************@fe10.lga...
Hi -

I would like to know how the following task is accomplished:

Updating the Primary Key in the parent table when there are other tables
that reference this Key(Foreign Key)
Is there a way to have DB2 handle this task for you..

I beg to differ with Kurt Struyf: there *is* a way to handle updates of
primary keys that have foreign keys. It's just not a particularly DIRECT
way.

Rather than simply updating the primary key directly, which is not possible
due to the Referential Integrity rules and the absence of cascading updates
of primary keys in DB2, you have to go about things in a more roundabout
way:
1. Create a new row containing the desired primary key value in the parent
table. It will have no dependent rows at this point.
2. Find all the rows that are dependents of the old primary key in all of
the dependent tables. Change the foreign keys of those rows to the newly
created value from Step 1. This can be a lot of work if there are many
dependencies; remember, you have to change the dependents of the dependents
and the dependents of the dependents of the dependents, and so on until all
of them are done.
3. Delete the old primary key row.

A simple example to illustrate. I'll adapt from the sample tables that come
with DB2.

Let's say you have two tables, EMP and DEPT. DEPT contains one row for every
department in a company and looks like this:

DEPTNO DEPTNAME
A00 ADMIN
B01 SALES
C01 EDUC
D01 MANUF

The primary key of DEPT is DEPTNO.

EMP contains one row for each employee in the company and contains columns
EMPNO, LASTNAME, and WORKDEPT. It looks this way:

EMPNO LASTNAME WORKDEPT
001 HAAS A00
002 SMITH D01
003 BROWN C01
004 GREEN C01
005 WHITE B01

The primary key of this table is EMPNO and WORKDEPT is a foreign key
referring back to the DEPTNO column of the DEPT table.

You want to change the department number for the SALES department from B01
to S01. You try to do this:

update dept
set deptno = 'S01'
where deptno = 'B01'

This fails due to referential integrity; you can't change a primary key that
has dependent rows and department B01 has a dependent row in the EMP table,
specifically WHITE, employee number 005.

Here is how you accomplish what you want to do INDIRECTLY.

First, create the new primary key row. You want the SALES department to have
the primary key S01 so create that row:

INSERT INTO DEPT VALUES('S01', 'SALES');

Second, change all foreign keys that are dependent on the old value, B01, to
the new value, S01. In this case, that is one row in the EMP table:

UPDATE EMP
SET WORKDEPT = 'S01'
WHERE WORKDEPT = 'B01'

Third, delete the old primary key row:

DELETE FROM DEPT
WHERE DEPTNO = 'B01'

That's it: your primary key has the new desired value, all of your dependent
rows now have the new value in their foreign keys, and all references to the
old value are gone.

For what it's worth, the process of generating all the SQL to do this work -
which is usually a lot more work in real life due to much more complex table
relationships - could almost certainly be automated if you wanted to take
the time to do it. All of the relationships and information necessary to do
so are stored in the DB2 catalog and a tool *could* be written that figures
out all of the statements that would be necessary given a particular change
to a specific primary key. If you didn't mind doing that work, it might be a
worthwhile investment if you regularly have to change primary key values for
some reason.

Rhino




Nov 12 '05 #3
Dave D wrote:
Hi -

I would like to know how the following task is accomplished:

Updating the Primary Key in the parent table when there are other tables
that reference this Key(Foreign Key)
Is there a way to have DB2 handle this task for you..

-Dave

Use triggers instead (You can still use RI in a NOT ENFORCED mode to
keep the performance characteristics).
The need for UPDATE CASCADE revolves around the need to update primary
keys, which revoles in turn around the question whether one should use
"natural" keys or not.
Gets very religeous very quickly :-)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
Kurt Struyf wrote:
"Dave D" <da*******@yahoo.com> wrote in message
news:<qo******************@fe10.lga>...
Hi -

I would like to know how the following task is accomplished:

Updating the Primary Key in the parent table when there are other tables
that reference this Key(Foreign Key)
Is there a way to have DB2 handle this task for you..

-Dave


Hi Dave,

This isn't possible in DB2. I know it's possible in a few other
RDBMS's but not in DB2. There is no "update cascade". As long as there
are no "child" rows, you can do an update of the pk..from the moment
there are child rows, DB2 prohibits this, for your own protection.


That's not true. You can specify different referential actions:

RESTRICT - forbid the operation
NO ACTION - allow the operation and check the results afterwards for
consistency (Rhino gave an example for that)

The referential actions for DELETE operations also support

SET NULL - set the referencing value to NULL
SET DEFAULT - set the referencing value to the default values specified for
that column

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Paul | last post by:
Hi: I am not sure whether somebody can sort it out for me. I am doing data transfer from one oracle database to another. Both of them has the same structure (like same tables etc).the only...
0
by: Rajesh Kapur | last post by:
We use InnoDB tables and foreign key constraints extensively. The mysqldump backs up the database tables in alphabetical order with foreign key constraints defined in the create statement of each...
1
by: Robin Tucker | last post by:
I'm considering adding domain integrity checks to some of my database table items. How does adding such constraints affect SQL Server performance? For example, I have a simple constraint that...
4
by: Dmitri | last post by:
I just looked at a coworker's stored procedure and this person is dropping 4 Foreign key constraints and then re-adding them after processing the required logic (updating rows in the 4 tables in...
2
by: DW | last post by:
Greetings: I have to do a one-off forceful change of some data in a database. I need to disable some FK constraints, make the data change, and then re-enable the constraints. My process will...
10
by: serge | last post by:
I am doing a little research on Google about this topic and I ran into this thread: ...
3
by: Tim | last post by:
I have spent the last 2-3 hours trying to find a way to just list the constraints for a given table (this includes referential - foriegn keys, not just check constraints). I know how to create...
0
by: BobTheDatabaseBoy | last post by:
i've Googled some this morning, but to my surprise, i don't find any offering (for fee or open source), which would integrate with, say Jakarta Struts, to provide the UI edits from cataloged...
3
by: Marek Berkan | last post by:
Hi, I have a problem with deffering constraints with db2. It was explained five year ago at this same group...
4
by: Bobby Edward | last post by:
I have an xsd dataset. I created a simple query called GetDataByUserId. I can preview the data fine! I created a very simple BLL function that calls it and returns a datatable. When I run...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.