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

UPDATE statement and unique constraints.

Hi,

We have been having many issues with unique constraints and we seem to
have boiled it down to the equivalent of the following very simple
scenario...

given a table with a unqiue constraint on col1 and populated as
follows

col1 col2
---- ----
bob 1
fred 2

We now want to update/swap both rows without violating the unique
constraint

i.e
update test1 set col1='fred' where col2=1;
update test1 set col1='bob' where col2=2;

The first statement ALWAYS violates the unique constraint
irrespective of whether we do either of the following

1. Turn autocommit off, i.e do both in the same Txn.
2. Wrap the two statements in BEGIN ATOMIC ... END

How should/could this be done ?

i.e the constraint should not be checked until end of statement or Txn
?

Thanks.

Paul.
Nov 12 '05 #1
8 6106
Paul,

Neither will work in DB2 for Multiplatforms. Constraints are checked per
statement and not per transaction.
You must perform the swap in one single update statement.
Depending on the nature of the swap you may be able to use OLAP
functions or correlated queries refering back to the table.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #2
"Paul Reddin" <pa**@abacus.co.uk> wrote in message
news:1f**************************@posting.google.c om...
Hi,

We have been having many issues with unique constraints and we seem to
have boiled it down to the equivalent of the following very simple
scenario...

given a table with a unqiue constraint on col1 and populated as
follows

col1 col2
---- ----
bob 1
fred 2

We now want to update/swap both rows without violating the unique
constraint

i.e
update test1 set col1='fred' where col2=1;
update test1 set col1='bob' where col2=2;

UPDATE test1
set col1 = CASE WHEN col2 =1 then 'fred' WHEN col2=2 then 'bob' END
WHERE col2 = 1 or col2 = 2
;
The first statement ALWAYS violates the unique constraint
irrespective of whether we do either of the following

1. Turn autocommit off, i.e do both in the same Txn.
2. Wrap the two statements in BEGIN ATOMIC ... END

How should/could this be done ?

i.e the constraint should not be checked until end of statement or Txn
?


Regards
Paul Vernon
Business Intelligence, IBM Global Services
Nov 12 '05 #3
Serge/Paul

Thanks for your answers, not what I wanted to hear, but thanks...

Unfortunately, rewriting in such a way is probably not an option
as the updates are formulated by Toplink (OO/ER layer).

I thought BEGIN ATOMIC...END caused the multiple statements to behave as
a single statement? or is this just an exception to that rule?

Thanks.

Paul.

"Paul Vernon" <pa*********@ukk.ibmm.comm> wrote in message news:<bq***********@gazette.almaden.ibm.com>...
"Paul Reddin" <pa**@abacus.co.uk> wrote in message
news:1f**************************@posting.google.c om...
Hi,

We have been having many issues with unique constraints and we seem to
have boiled it down to the equivalent of the following very simple
scenario...

given a table with a unqiue constraint on col1 and populated as
follows

col1 col2
---- ----
bob 1
fred 2

We now want to update/swap both rows without violating the unique
constraint

i.e
update test1 set col1='fred' where col2=1;
update test1 set col1='bob' where col2=2;


UPDATE test1
set col1 = CASE WHEN col2 =1 then 'fred' WHEN col2=2 then 'bob' END
WHERE col2 = 1 or col2 = 2
;
The first statement ALWAYS violates the unique constraint
irrespective of whether we do either of the following

1. Turn autocommit off, i.e do both in the same Txn.
2. Wrap the two statements in BEGIN ATOMIC ... END

How should/could this be done ?

i.e the constraint should not be checked until end of statement or Txn
?


Regards
Paul Vernon
Business Intelligence, IBM Global Services

Nov 12 '05 #4
Paul,

BEGIN ATOMIC .. END
Compiles the multiple statements into one plan.
It does not change the statement level semantics.
Semantically BEGIN ATOMIC .. END also introduces a safepoint (hence ATOMIC)

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #5
AK
Hi Paul,

if you happen to knoiw 2 values that are definitely not present in the
table then you could do like this:

update test1 set col1='not_a_value1' where col2=1;
update test1 set col1='not_a_value2' where col2=2;
update test1 set col1='fred' where col2=1;
update test1 set col1='bob' where col2=2;

and yes, you could wrap that in BEGIN ATOMIC block

Theoretically, there is another option: if you cannot swap col1
values, then you could swap all the other columns in these two
records, I mean

update test1 set col2=value2_fromrec2, col3=value3_fromrec2 ... where
col2=1;
update test1 set col2=value2_fromrec1, col3=value3_fromrec1 ... where
col2=2;

good luck!
Nov 12 '05 #6
Hi,

did you try a unique query that does the work ?

Like this one (I make it on SQL Server) :

CREATE TABLE A_TEST
(col1 varchar(16) ,
col2 int not null unique)

INSERT INTO A_TEST VALUES ('bob', 1)
INSERT INTO A_TEST VALUES ('fred', 2)

UPDATE A_TEST
SET col2 = (SELECT col2
FROM A_TEST T
WHERE T.col2 + A_TEST.col2 = 3)
WHERE col2 <= 2

SELECT * FROM A_TEST

col1 col2
---------------- -----------
bob 2
fred 1

Please mail me if this do the right work on DB2. I am interested in such
cases !
A +

--
Frédéric BROUARD - expert SQL, spécialiste : SQL Server / Delphi / web
Livre SQL - col. Référence : http://sqlpro.developpez.com/bookSQL.html
Le site du SQL, pour débutants et pros : http://sqlpro.developpez.com
****************** mailto:br******@club-internet.fr ******************

Paul Reddin a écrit:
Hi,

We have been having many issues with unique constraints and we seem to
have boiled it down to the equivalent of the following very simple
scenario...

given a table with a unqiue constraint on col1 and populated as
follows

col1 col2
---- ----
bob 1
fred 2

We now want to update/swap both rows without violating the unique
constraint

i.e
update test1 set col1='fred' where col2=1;
update test1 set col1='bob' where col2=2;

The first statement ALWAYS violates the unique constraint
irrespective of whether we do either of the following

1. Turn autocommit off, i.e do both in the same Txn.
2. Wrap the two statements in BEGIN ATOMIC ... END

How should/could this be done ?

i.e the constraint should not be checked until end of statement or Txn
?

Thanks.

Paul.

Nov 12 '05 #7
Frédéric BROUARD <br******@club-internet.fr> wrote in message
Hi,

did you try a unique query that does the work ?

Like this one (I make it on SQL Server) :

CREATE TABLE A_TEST
(col1 varchar(16) ,
col2 int not null unique)

INSERT INTO A_TEST VALUES ('bob', 1)
INSERT INTO A_TEST VALUES ('fred', 2)

UPDATE A_TEST
SET col2 = (SELECT col2
FROM A_TEST T
WHERE T.col2 + A_TEST.col2 = 3)
WHERE col2 <= 2

SELECT * FROM A_TEST

col1 col2
---------------- -----------
bob 2
fred 1

Please mail me if this do the right work on DB2. I am interested in such
cases !

Excellent!
I tested this idea in DB2 and it worked fine.
One thing I want to point out is the following.
Original requirement is swap/update unique column col1.
So, should you use these CREATE and UPDATE SQL?
CREATE TABLE Test1
(col1 VARCHAR(10) NOT NULL UNIQUE
,col2 INTEGER NOT NULL
);

INSERT INTO Test1
VALUES ('bob', 1) , ('fred', 2);
UPDATE test1 U
SET col1 = (SELECT col1
FROM test1 T
WHERE T.col2 + U.col2 = 3);

SELECT * FROM test1;
-----------------------------------------

COL1 COL2
---------- -----------
fred 1
bob 2

2 record(s) selected.
Nov 12 '05 #8
AK
another option would be to delete both rows, then insert their new versions
Nov 12 '05 #9

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

Similar topics

5
by: S.Patten | last post by:
Hi, I have a problem with updating a datetime column, When I try to change the Column from VB I get "Incorrect syntax near '942'" returned from '942' is the unique key column value ...
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...
2
by: MAS | last post by:
Below is a simple UPDATE that I have to perform on a table that has about 2.5 million rows (about 4 million in production) This query runs for an enourmous amount of time (over 1 hour). Both the...
5
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
8
by: Jan van Veldhuizen | last post by:
The UPDATE table FROM syntax is not supported by Oracle. I am looking for a syntax that is understood by both Oracle and SqlServer. Example: Table1: id name city ...
2
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an...
1
by: Bryan | last post by:
Hi, I have two tables. I want to update two columns in my first table, and with two values and held in my #temp table but based on a RUN_DATE from my first table. Can anyone point me in...
6
by: John J. Hughes II | last post by:
Normally when I use the Adaptor update function and it work fine but sometimes I get a "duplicate entry" error after the merge. I believe the problem is because the primary key is modified and the...
1
by: Grant McLean | last post by:
Hi First a simple question ... I have a table "access_log" that has foreign keys "app_id" and "app_user_id" that reference the "application_type" and "app_user" tables. When I insert into...
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: 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?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.