473,394 Members | 1,944 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,394 software developers and data experts.

update...where not exists .... on same table -- 390 v7.1.1

i need to update a column which is a member of the PK on this
table. there are some thousands of rows to be updated, many more
thousand already in the table.

so, i get a constraint violation doing a simple

set col1 = 'x' where col1 = 'y'

attempted (in general terms; col1 & col2 are the PK)

update table1 a
set a.col1 = 'x' where a.col1 = 'y' and not exists (
select * from table1 z where
a.col2 = z.col2 and
z.col1 = 'x')

get a SQL0104N error. the v7 SQL Reference doesn't say the
syntax is wrong, but doesn't have an example like this. can this
be done??

thanks,
robert
Nov 12 '05 #1
7 19371

"robert" <gn*****@rcn.com> wrote in message
news:da**************************@posting.google.c om...
i need to update a column which is a member of the PK on this
table. there are some thousands of rows to be updated, many more
thousand already in the table.

so, i get a constraint violation doing a simple

set col1 = 'x' where col1 = 'y'

attempted (in general terms; col1 & col2 are the PK)

update table1 a
set a.col1 = 'x' where a.col1 = 'y' and not exists (
select * from table1 z where
a.col2 = z.col2 and
z.col1 = 'x')

get a SQL0104N error. the v7 SQL Reference doesn't say the
syntax is wrong, but doesn't have an example like this. can this
be done??

You can't update the primary key of a table directly. This is due to the
possibility that your update will affect potentially millions of rows in
dependent tables whose foreign key is the same as the primary key you are
changing.

To change a primary key, you need to:
1. INSERT a new row that has the desired key
2. UPDATE all the foreign keys that refer to the old primary key value to
the new value
3. DELETE the old primary key

Rhino
Nov 12 '05 #2
"Rhino" <rh****@NOSPAM.sympatico.ca> wrote in message news:<cS*********************@news20.bellglobal.co m>...
"robert" <gn*****@rcn.com> wrote in message
news:da**************************@posting.google.c om...
i need to update a column which is a member of the PK on this
table. there are some thousands of rows to be updated, many more
thousand already in the table.

so, i get a constraint violation doing a simple

set col1 = 'x' where col1 = 'y'

attempted (in general terms; col1 & col2 are the PK)

update table1 a
set a.col1 = 'x' where a.col1 = 'y' and not exists (
select * from table1 z where
a.col2 = z.col2 and
z.col1 = 'x')

get a SQL0104N error. the v7 SQL Reference doesn't say the
syntax is wrong, but doesn't have an example like this. can this
be done??

You can't update the primary key of a table directly. This is due to the
possibility that your update will affect potentially millions of rows in
dependent tables whose foreign key is the same as the primary key you are
changing.

To change a primary key, you need to:
1. INSERT a new row that has the desired key
2. UPDATE all the foreign keys that refer to the old primary key value to
the new value
3. DELETE the old primary key

Rhino


well, i was happily updating columns in the unique index (we don't
define PKs as such here in Gopher Gultch; nor FKs, but i guess that
follows), so it's not that. the EXISTS clause is meant to not
execute the UPDATE if there is already a row which has the same values
as would happen if the UPDATE did execute. from what i read in the UDB
UPDATE docs, this is permissible syntax. the 390 docs don't say one way
or the other. the APAR listing has a bunch of entries about problems
with UPDATE and subqueries, but none i saw which mention self-references.

so, it seems the answer is: don't do that.
Nov 12 '05 #3
Notice that no correlation name is allowed in the UPDATE clause in
Standard SQL; this is to avoid some self-referencing problems that
could occur in the various improperly implemented dialects. But it
also follows the data model in Standard SQL. When you give a table
expression a correlation name, it is to act as if a materialized table
with that correlation name has been created in the database. That
table then is dropped at the end of the statement. If you allowed
correlation names in the UPDATE clause, you would be updating the
materialized table, which would then disappear and leave the base
table untouched.

Look at what you wrote in the subquery predicate; I think you meant to
see if the updated row already exists, not if the old row exists.

UPDATE Table1
SET col1 = 'x'
WHERE col1 = 'y'
AND NOT EXISTS -- to see if you are about to create a duplicate
(SELECT *
FROM Table1 AS Z
WHERE Table1.col2 = z.col2
AND z.col1 = 'y');

Don't forget to have ON UPDATE CASCADE actions on all the FOREIGN KEY
references, too!
Nov 12 '05 #4
Rhino wrote:
You can't update the primary key of a table directly. This is due to the
possibility that your update will affect potentially millions of rows in
dependent tables whose foreign key is the same as the primary key you are
changing.

To change a primary key, you need to:
1. INSERT a new row that has the desired key
2. UPDATE all the foreign keys that refer to the old primary key value to
the new value
3. DELETE the old primary key


Well, that's not quite correct. Of course you can update the values of a
primary key column (unless you have an MPP system and the column in
question is part of the partitioning key). What you can't do, however, is
updating a value of a primary or unique key if there are dependent records
for the value to be updated. The reason is that DB2 does not yet support
ON UPDATE CASCADE for foreign key relationships.

If your foreign key (FK) is defined as ON UPDATE NO ACTION, then the update
will succeed if the result of the update does not violate the FK. If the
FX is defined as ON UPDATE RESTRICT, then you can't update the value if
there is any dependent row.

To clarify the difference between NO ACTION and RESTRICT, the following
works in the first case and is rejected in the second:

CREATE TABLE t1 ( c1 INT NOT NULL PRIMARY KEY )
CREATE TABLE t2 ( c1 INT FOREIGN KEY REFERENCES t1 ON UPDATE NO ACTION )
INSERT INTO t1 VALUES (1), (2), (3)
INSERT INTO t2 VALUES (2), (3)

-- this works with NO ACTION and fails with RESTRICT
UPDATE t1 SET c1 = c1+1

Another option is to not use foreign keys at all and to implement a bunch of
triggers for each FK instead. Then you can handle the cascading update in
the trigger. However, I wouldn't really recommend such an approach because
it gets really messy really fast.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #5
Well, that's not quite correct. Of course you can update the values of a
primary key column (unless you have an MPP system and the column in
question is part of the partitioning key).

Updating the partitioning key is allowed since DB2 V7.2.
It was part of the 440.000 TpmC TPC-C result when Win2K was introduced
(good old times....)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #6
Serge Rielau wrote:
Well, that's not quite correct. Of course you can update the values of a
primary key column (unless you have an MPP system and the column in
question is part of the partitioning key).

Updating the partitioning key is allowed since DB2 V7.2.
It was part of the 440.000 TpmC TPC-C result when Win2K was introduced
(good old times....)


That's great! I wasn't aware of that....

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #7
Knut Stolze <st****@de.ibm.com> wrote in message news:<c8**********@fsuj29.rz.uni-jena.de>...
Rhino wrote:
You can't update the primary key of a table directly. This is due to the
possibility that your update will affect potentially millions of rows in
dependent tables whose foreign key is the same as the primary key you are
changing.

To change a primary key, you need to:
1. INSERT a new row that has the desired key
2. UPDATE all the foreign keys that refer to the old primary key value to
the new value
3. DELETE the old primary key


Well, that's not quite correct. Of course you can update the values of a
primary key column (unless you have an MPP system and the column in
question is part of the partitioning key). What you can't do, however, is
updating a value of a primary or unique key if there are dependent records
for the value to be updated. The reason is that DB2 does not yet support
ON UPDATE CASCADE for foreign key relationships.

If your foreign key (FK) is defined as ON UPDATE NO ACTION, then the update
will succeed if the result of the update does not violate the FK. If the
FX is defined as ON UPDATE RESTRICT, then you can't update the value if
there is any dependent row.

To clarify the difference between NO ACTION and RESTRICT, the following
works in the first case and is rejected in the second:

CREATE TABLE t1 ( c1 INT NOT NULL PRIMARY KEY )
CREATE TABLE t2 ( c1 INT FOREIGN KEY REFERENCES t1 ON UPDATE NO ACTION )
INSERT INTO t1 VALUES (1), (2), (3)
INSERT INTO t2 VALUES (2), (3)

-- this works with NO ACTION and fails with RESTRICT
UPDATE t1 SET c1 = c1+1

Another option is to not use foreign keys at all and to implement a bunch of
triggers for each FK instead. Then you can handle the cascading update in
the trigger. However, I wouldn't really recommend such an approach because
it gets really messy really fast.


Google was being cranky, so i couldn't get a reply in. thanks for the
NO ACTION explanation. here in Gopher Gultch, there are not FKs or PKs
as such, only the occasional unique index. but i do have this lower
brain stem memory of making such updates on UDB; i'll need to go
back and confirm that.

thanks,
rboert
Nov 12 '05 #8

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

Similar topics

8
by: Jason | last post by:
I have a table that matches up Securities and Exchanges. Individual securities can belong on multiple exchanges. One of the columns, named PrimaryExchangeFlag, indicates if a particular exchange is...
6
by: Karen Middleton | last post by:
In MS Access I can do in one SQL statement a update if exists else a insert. Assuming my source staging table is called - SOURCE and my target table is called - DEST and both of them have the...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
4
by: Duane Phillips | last post by:
"David Portas" <snipped for brevity> wrote: Example 1: > > UPDATE table_a > SET col = ? /* Unspecified */ > WHERE EXISTS > (SELECT * > FROM table_b > WHERE table_b.key_col =...
3
by: takilroy | last post by:
Hi, Does anyone know of a simple way to do this? I want to create an insert trigger for a table and if the record already exists based on some criteria, I want to update the table with the...
17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
9
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader>...
5
by: keeps21 | last post by:
A little problem I've run into is the following. I have a script that allows a user to edit a story. I have an HTML form for title and main_text which gets it's values by pulling the selected...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.