473,809 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

maintaining sequence no of rows.

sumittyagi
202 Recognized Expert New Member
Hi! to all!

I have got an issue.
* I maintain serial no. of rows tables in a column serial_no(which is set as primary key).

* while inserting i count the no of rows, "select count(*) from tbl", and increment one to it, and assign that as serial no to new row to be inserted.

is it the right way to achieve this. or is there any other better way to do it.

thanks in advance for your help.
Mar 12 '07 #1
6 3651
sumittyagi
202 Recognized Expert New Member
isn't there anyone having any idea about this???
Mar 15 '07 #2
bartonc
6,596 Recognized Expert Expert
Hi! to all!

I have got an issue.
* I maintain serial no. of rows tables in a column serial_no(which is set as primary key).

* while inserting i count the no of rows, "select count(*) from tbl", and increment one to it, and assign that as serial no to new row to be inserted.

is it the right way to achieve this. or is there any other better way to do it.

thanks in advance for your help.
This is not safe at all. If you delete a row from anywhere but the end, you'll duplicate a sernum. I don't know if DB2 has a field type of AUTOINCREMENT, but it should. Somehow you need to let the DBMS figure out the new ID and then you should be able to query it. In MySQL it's
Expand|Select|Wrap|Line Numbers
  1.  SELECT last_insert_id()
Hope that gets you on the right track.
Mar 16 '07 #3
Snib
24 New Member
isn't there anyone having any idea about this???
Unless your are using a DB with a similar function to the MS Access "AutoNumber " you will need to generate each unique serial number before you add the new row to your main table.

One way to solve this problem is to create a new table that just contains (as a minimum) two columns, for example REF_ID and NEXT_REF_NUMBER . The RE_ID could be a text field which contains a unique name for the reference, say "SERIALNO" and the NEXT_REF_NUMBER is an integer with a starting value of 1, or if you already have data in your main table then use the last serial number issued.

Before you add a new row to the main table you read the "SERIALNO" record from the new table, increment the NEXT_REF_NUMBER by 1 and then update the table immediately. keep the previous value of the NEXT_REF_NUMBER in a working storage fields as this is the serial number you need for your new row.

Depending on how you application works, and whether you have got any other outstanding updates to other tables pending at this point, you could commit the change to the new table. If your system can be accessed by multiple users at the same time this allows another user access to the new table to get the next available serial number.

If the addition of the row to the main table is in the middle of a number of other updates I would advice that you get the next serial number at the start of this process, before any other DB2 changes are made and commit this change to the database.

This helps to ensure that you do not issue the same serial number twice. Even if a later activity fails and the other Db2 changes are rolled back the update to the new table is already committed. It also ensure that other users can get to the new table as soon as possible to get the next serial number and that "roll backs" due to other errors do not again result in duplicate references being created.

Either way you can then add the new record to you main table with the serial number you stored. If a record gets deleted from the main table this will not affect the next serial number to be issued as this is being stored indepedantly on the other table and the next available serial number is not affected by the number of rows on the main table.

You can then use this new table to store as many references as you like, each one just needs it's own name that is unique.

There is a further downside to this process - each serial number produced follows on immediately from the next which means that subsequent inserts will add rows immediately after the last addition. This does not give DB2 a far chance to distribute the additions across the table as you are basically inserting one row after another.

If the system is not multi-user then this is not really a problem, it just results in your data not be very well organised on the table and you may need to re-organise the table more frequently because of this.

The way around this problem is to "cycle" the reference number. This requires an additional column on the new talble, REFLENGTH. This is again an integer and defines how long the generate reference is. So for example the row for "SERIALNO" could have a REFLENGTH of 8. So instead of creating "1" as the first reference you would create "00000001". You then bascially reverse this number so it becomes "10000000". This is then the value used on your main table as SERIALNO. The NEXT_REF_NUMMBE R on the main table is still incremented by 1, so in this example goes to 2 and this is then updated back to the table. When the next reference is created and reversed this gives a value of "20000000". This means that the two reference numbers generated do not follow sequencelly and so DB2 inserts these at difference points on the table and index - resulting in the data inserts being completed with a better distribution.

If you already have data on the table this is not a problem as you just set the NEXT_REF_NUMBER field to be the maximum value of the current rows on the table, but reversed e.g. current max serial number is 12345678, so set the NEXT_REF_NUMBER to 87654321. If you add one to this you get 87654322 which results in a serial number of 22345678 so a duplicate number is never generated.

Hope this helps, possibly a "little" more than you asked for!
Mar 16 '07 #4
sumittyagi
202 Recognized Expert New Member
Thanks bartonc and snib.
I got the idea!

Special thanks to snib for such a descriptive explaination.
Mar 19 '07 #5
Snib
24 New Member
I found something else that you may want to look into, Not sure exactly what version of DB2 this came in with, also not sure of performance overhead etc.

http://publib.boulder. ibm.com/infocenter/db2luw/v8/index.jsp?topic =/com.ibm.db2.udb .doc/admin/t0004990.htm

I would definitely advice using the "GENERATED ALWAYS" option to ensure that the applications cannot set the values.

Apparently once you have completed an insert you can then call and SQL SET with the INDENTIT_VAL_LO CAL() function to return the number generated by the last INSERT command.

Only problem I can see if that you cannot set the initial value - apparently this may have been resolved with a change to this in DB2 V8 or V9 but I could not find anything on a quick Google search. Maybe one of the DBAs that looks after this site would know a reference you could use.

Regards

Snib
Mar 19 '07 #6
Snib
24 New Member
I just found a reference for the DB2 SEQUENCE type, the next thing in UDB after "IDENTIY COLUMNS"! Have a look at this URL for further details:

http://www-128.ibm.com/developerworks/db2/library/techarticle/0205pilaka/0205pilaka2.htm l

Seems you have a few options if you have DB2 Version 9 - you learn something new every day!

Regards

Snib
Mar 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2011
by: Axier | last post by:
Hi, I wonder how this can happen. A "not null" field is set to "" even if a sequence should set this to next sequence number. Ok, I know it is not set to null, it is set to '' which is proven by the below Select statement but it is not set to the next sequence number and the inserts
0
1192
by: Axier | last post by:
Hi, I wonder how this can happen. A "not null" field is set to "" even if a sequence should set this to next sequence number. Ok, I know it is not set to null, it is set to '' which is proven by the below Select statement but it is not set to the next sequence number and the inserts
4
538
by: Eric E | last post by:
Hi, I have a question about sequences. I need a field to have values with no holes in the sequence. However, the values do not need to be in order. My users will draw a number or numbers from the sequence and write to the field. Sometimes, however, these sequence numbers will be discarded (after a transaction is complete), and thus available for use. During the transaction, however, any drawn numbers need to be unavailable. I would...
4
6334
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a large amount of data to sort e.g > 200 then there is a slight delay in the table sort. To cater for this I want to provide some feedback to the user to inform them that the sort function is processing. My idea is to add a "sorting...." message...
11
3703
by: Wilfried Mestdagh | last post by:
Hi, does foreach guarantees the sequence order starting from index 0 ? -- rgds, Wilfried http://www.mestdagh.biz
4
10086
by: Szymon Dembek | last post by:
Hi Recently I did some DB2 and ODBC coding in Visual FoxPro. I bumped on a problem I cannot resolve. When I issue a delete statement that deletes no rows (no rows qualify for the WHERE conditions), DB2 raises SQL0100W warning with SQLSTATE=02000. The same happens when issuing an insert with select clause (insert into .... select from ...) when no rows are fetched by the select clause.
7
2154
by: Nathan Sokalski | last post by:
I have a page which I dynamically add several usercontrols (*.ascx files) to using the following code: Public Sub Refresh() For Each section As DataRow In Me.GetSections().Rows CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ", "")), adminsection2).RefreshSection() Next End Sub
0
1726
by: jsreddy | last post by:
Hi All, I am having 4 text boxes in a Gridview Template Column. The Tab Sequence is set to 0 - 4. the tab sequence works fine when i have only one Row. if i am adding one more row to grid then tab sequence jumps to different rows in 0-4 order. but i need to set the sequence in first row & then it should go to second row after 4th text box. can any one please help how to set the tab sequence in row by row. Thanks in advance. Sridhar
8
11253
by: arachno | last post by:
My Oracle sequences seem to be auto-incrementing themselves "over time". My row ID's are sequenced like this: 1, 4, 5, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 41, etc I'm using "sequencename.nextval" when inserting new rows into my tables, so one would think they'd come in a straight sequence? Example: INSERT INTO hprequests (requestid, username, flagtype, reqstatus, workstatus, requesttitle, statuschangedby)
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10378
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10121
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.