473,398 Members | 2,404 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.

select count then insert

Hi

If I do

Begin transaction
Stmt 1: select count(*) from tableA
If the count is zero, then proceed to the following
Stmt 2: insert "something" to tableA

Stmt 3: insert "something" to tableB
End transaction

If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)

Would an unique index help?

Thanks
LeWindLetter

Jan 16 '07 #1
14 6637
Hi.

On 2007-01-16 10:28, le**********@gmail.com wrote:
If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)
That depends on the transaction isolation level you're using. With the
default setting, "Cursor Stability", you will run into problems.
Have a look at the different isolation levels that are available, and
the "set transaction isolation" command.
Would an unique index help?
No, it wouldn't.

Benjamin
Jan 16 '07 #2
This is sort of tangential, but just in case it's helpful, you may want
to consider (if you haven't already) writing your conditional insert
like so:

BEGIN ATOMIC
IF EXISTS(SELECT 1 FROM TABLEA) THEN
INSERT INTO TABLEA...
ELSE
...
END IF
END;

or, perhaps, use MERGE

--Jeff

le**********@gmail.com wrote:
Hi

If I do

Begin transaction
Stmt 1: select count(*) from tableA
If the count is zero, then proceed to the following
Stmt 2: insert "something" to tableA

Stmt 3: insert "something" to tableB
End transaction

If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)

Would an unique index help?

Thanks
LeWindLetter
Jan 16 '07 #3
To Benjamin,

Do you mean using Repeatable read isolation level? Is it on connection
level? If I am using a JDBC connection pool, would it affect other
transaction?
In this case, would select count lock the whole table?

To Jeff,

Is there a way to do it in Java rather than using PL SQL? cause I
don't want to tied to a particular database

Thanks both of you first

Jan 17 '07 #4
On 2007-01-17 07:33, le**********@gmail.com wrote:
Do you mean using Repeatable read isolation level?
I mean the level that is called "repeatable read" in DB2, but
"serializable" in common.
Is it on connection level?
Yes.
If I am using a JDBC connection pool, would it affect other
transaction?
I don't see any connection between the usage of a connection pool and
the behaviour of transactions. However, every transaction in your system
should use an appropriate isolation level. Not every operation requires
serializable isolation.
In this case, would select count lock the whole table?
I assume the select count(*) will cause a share lock on the whole table
and thus any other transaction running in isolation level serializable
will not be allowed to update or insert data in that table unless the
lock is released.

Benjamin
Jan 17 '07 #5

le**********@gmail.com wrote:
Hi

If I do

Begin transaction
Stmt 1: select count(*) from tableA
If the count is zero, then proceed to the following
Stmt 2: insert "something" to tableA

Stmt 3: insert "something" to tableB
End transaction

If I am using DB2 default setting, would there be data consistency
issue if there are two users
doing the above transaction. (i.e., both select, got count=0, then
proceed to insert)

Would an unique index help?

Thanks
LeWindLetter

Just a note.

Statement 1 and 2 can be combined into one statement:

INSERT INTO tableA SELECT something FROM (VALUES(something)) t
WHERE (SELECT COUNT(*) FROM tableA) = 0

B.

Jan 17 '07 #6
How about this?
(Columns are arbitrally specified.)
CREATE TABLE tableA
(col1 INTEGER
,col2 INTEGER
);

CREATE TABLE tableB
(genkey INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY
,col1 INTEGER
,col2 INTEGER
);

WITH checkA AS (
select count(*) countA from tableA
)
,InsertA(DummyA) AS (
SELECT 0
FROM FINAL TABLE(insert into tableA
VALUES (7, 400) )
WHERE (SELECT countA FROM checkA) = 0
)
SELECT 0
FROM FINAL TABLE(insert into tableB
(col1, col2)
VALUES (7, 350) )
WHERE (SELECT countA FROM checkA) = 0;

Jan 18 '07 #7
Tonkuma wrote:
How about this?
(Columns are arbitrally specified.)
CREATE TABLE tableA
(col1 INTEGER
,col2 INTEGER
);

CREATE TABLE tableB
(genkey INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY
,col1 INTEGER
,col2 INTEGER
);

WITH checkA AS (
select count(*) countA from tableA
)
,InsertA(DummyA) AS (
SELECT 0
FROM FINAL TABLE(insert into tableA
VALUES (7, 400) )
WHERE (SELECT countA FROM checkA) = 0
)
SELECT 0
FROM FINAL TABLE(insert into tableB
(col1, col2)
VALUES (7, 350) )
WHERE (SELECT countA FROM checkA) = 0;
Whoa!

Care to explain this? I do not understand "FINAL TABLE()" or how it can
have an INSERT inside it.

B.

Jan 18 '07 #8
Benjamin Gufler wrote:
On 2007-01-17 07:33, le**********@gmail.com wrote:
Do you mean using Repeatable read isolation level?

I mean the level that is called "repeatable read" in DB2, but
"serializable" in common.
Is it on connection level?

Yes.
You can also do this on a statement level. To do so, append a WITH RR
onto the end of the select statement - it will change the isolation
level for the duration of that query to Repeatable Read.

Jan 18 '07 #9

Brian Tkatch skrev:
[...]
Whoa!

Care to explain this? I do not understand "FINAL TABLE()" or how it can
have an INSERT inside it.

B.
It is called transition tables (I think) and they are somewhat similar
to what you can do in a trigger (referencing ... as ...). From what I
understand they are not part of sql standard (yet), but I find them
extremely handy. For example, when deleting from a table you can select
from the effected rows, and if you see something that shouldn't be
deleted, rollback. I.e.

db2 +c "select * from old table (delete from T where ...) X"

-- oops, two many

db2 rollback

See example 7 at:

http://publib.boulder.ibm.com/infoce...65%20%28%22%20
/Lennart

Jan 18 '07 #10
Brian:

Take a peek at http://www.vldb.org/conf/2004/IND1P1.PDF. Pretty cool
stuff :-).

--Jeff

Brian Tkatch wrote:
Tonkuma wrote:
How about this?
(Columns are arbitrally specified.)
CREATE TABLE tableA
(col1 INTEGER
,col2 INTEGER
);

CREATE TABLE tableB
(genkey INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY
,col1 INTEGER
,col2 INTEGER
);

WITH checkA AS (
select count(*) countA from tableA
)
,InsertA(DummyA) AS (
SELECT 0
FROM FINAL TABLE(insert into tableA
VALUES (7, 400) )
WHERE (SELECT countA FROM checkA) = 0
)
SELECT 0
FROM FINAL TABLE(insert into tableB
(col1, col2)
VALUES (7, 350) )
WHERE (SELECT countA FROM checkA) = 0;

Whoa!

Care to explain this? I do not understand "FINAL TABLE()" or how it can
have an INSERT inside it.

B.
Jan 18 '07 #11
Benjamin Gufler wrote:
On 2007-01-17 07:33, le**********@gmail.com wrote:
Do you mean using Repeatable read isolation level?

I mean the level that is called "repeatable read" in DB2, but
"serializable" in common.
Not knowing much about DB2, but is "repeatable read" really the same as
"serializable"? Aren't they are slightly different?
>From what I understand, serializable is the most secure isolation
level, which guarantees that the execution of the operations of
concurrently executing transactions produces the same effect as some
serial execution of those same transactions (i.e. an execution where
one transaction executes to completion before the next begins).

AFAIK repeatable read offers the same consistency guarantee as
serializable, except that phantoms may be encountered. (I.e. a
transaction reads a set of rows that satisfy a search condition.
Another transaction then performs an update which generates one or more
new rows that satisfy the search condition. If the original query is
repeated (using the same search condition), extra rows appear in the
result-set that where previously not found.)
/Jarl

Jan 19 '07 #12
On 2007-01-19 11:45, ja**@mimer.com wrote:
Benjamin Gufler wrote:
Not knowing much about DB2, but is "repeatable read" really the same as
"serializable"? Aren't they are slightly different?
No, they are not different. It's just the DB2 naming of the levels that
may cause some confusion:

"common" name | DB2 name
-----------------+----------------------
read uncommitted | uncommitted read
read committed | cursor stability
repeatable read | read stability
serializable | repeatable read

Benjamin
Jan 19 '07 #13
Lennart wrote:
Brian Tkatch skrev:
[...]
Whoa!

Care to explain this? I do not understand "FINAL TABLE()" or how it can
have an INSERT inside it.

B.

It is called transition tables (I think) and they are somewhat similar
to what you can do in a trigger (referencing ... as ...). From what I
understand they are not part of sql standard (yet), but I find them
extremely handy. For example, when deleting from a table you can select
from the effected rows, and if you see something that shouldn't be
deleted, rollback. I.e.

db2 +c "select * from old table (delete from T where ...) X"

-- oops, two many

db2 rollback

See example 7 at:

http://publib.boulder.ibm.com/infoce...65%20%28%22%20
/Lennart
Thanx for pointing it out. I'll have to read more about this later.

B.

Jan 19 '07 #14

jefftyzzer wrote:
Brian:

Take a peek at http://www.vldb.org/conf/2004/IND1P1.PDF. Pretty cool
stuff :-).

--Jeff
Thanx for the link. I'm going to have to read that.

B.

Jan 19 '07 #15

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

Similar topics

7
by: JP Belanger | last post by:
I have a question on select count(), which may betray my lack of database knowledge. Here goes. I have java code driving a transaction which goes like this: select count(*) from table ...
0
by: Lars Rasmussen | last post by:
I tried that, but i dont work either. I need to insert a way that mysql doese'nt complain when i copy some records that have the same id (or that it just gives it an id according to the...
0
by: Mike Chirico | last post by:
I found the following interesting and wanted to pass it along Reference (TIP 12): http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download Connect and Select from Java //...
9
by: Mike R | last post by:
Hi, I cant figure out how to do this.... for example: Select name from mytab order by col1 could return Mike
3
by: dumbledad | last post by:
Hi All, I'm confused by how to replace a SELECT statement in a SQL statement with a specific value. The table I'm working on is a list of words (a column called "word") with an index int...
4
by: Nick Barr | last post by:
Hi, I am trying to gather stats about how many times a resource in our web app is viewed, i.e. just a COUNT. There are potentially millions of resources within the system. I thought of two...
3
by: imrantbd | last post by:
I need array type name like "destList" must use for my destlist select box,not a single name.Or need a solution to capture multiple value of "destList" select box and send all selected value in php...
2
by: unabogie | last post by:
I have a table with entries tied to a membership database. The problem is that I want to select a limit of sixteen entries per member, per day, where some members have 16+ entries per day. I...
5
by: Lennart | last post by:
I really like the construction: select * from new table (update ....) X but I noticed that it cant be used as: insert into T select * from new table (update ....) X because of:
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.