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

insert/select question

I am doing a simple insert into a table and I would like the data to be
retrieved in the order it is inserted not by what appears to be
alphabetical order.

insert into mytbl values('c')
insert into mytbl values('b')
insert into mytbl values('a')

select * from mytbl yields

a
b
c

I would like the select to yield
c
b
a
(the order in which the data was inserted)

What is db2 doing with the ordering during insert?

Nov 12 '05 #1
8 5127
"K.Fawcett" <k.*******@mccsoftware.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
I am doing a simple insert into a table and I would like the data to be
retrieved in the order it is inserted not by what appears to be
alphabetical order.

insert into mytbl values('c')
insert into mytbl values('b')
insert into mytbl values('a')

select * from mytbl yields

a
b
c

I would like the select to yield
c
b
a
(the order in which the data was inserted)

What is db2 doing with the ordering during insert?

The only way would be to define a timestamp column on the table and let DB2
default to the "current timestamp" when the data is inserted. Then you could
use the ORDER BY clause to retrieve the rows by the timestamp column.

By definition, a relational database does not guarantee the rows will be
returned in any particular order unless you specify the order with ORDER BY.
Nov 12 '05 #2
Thanks for you reply. An 'ORDER BY' clause was never required by this
table until a customer wanted to do something new. I will probably
need to add a new field to support this. I guess what threw me is that
the results of a 'SELECT' query always appear alphabetized (at least
for the queries I performed). From what you are saying I should expect
to see random ordering in my results. I accept this, but is there a
reason that I would never experience it? Here is my query and my
result:
E:\src\univ\bmz>DB2 SELECT DISP_NAME,DISP_SYM FROM MCCTOOLS.DISP_TBL
allout.qry

E:\src\univ\bmz>DB2 SELECT PROG_ID,DISP_NAME,DISP_SYM FROM
MCCTOOLS.DISP_TBL WHERE PROG_ID='RX6099B3'

PROG_ID DISP_NAME
DISP_SYM
-------------------------------- --------------------------------
--------
RX6099B3 00_APASS .
RX6099B3 000_APASS .
RX6099B3 01_BUILD B
RX6099B3 02_LBOPA 1
RX6099B3 03_XARRYD 8
RX6099B3 04_LOGIC 7
RX6099B3 05_LBBYP 6
RX6099B3 06_EIDC 5
RX6099B3 07_IOW N
RX6099B3 08_XARRYA Z
RX6099B3 09_WOP1 2
RX6099B3 10_WOP2 3
RX6099B3 11_OLSSD W
RX6099B3 12_POST 9
RX6099B3 13_FAILSTEP K

15 record(s) selected.

Nov 12 '05 #3
"K.Fawcett" <k.*******@mccsoftware.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Thanks for you reply. An 'ORDER BY' clause was never required by this
table until a customer wanted to do something new. I will probably
need to add a new field to support this. I guess what threw me is that
the results of a 'SELECT' query always appear alphabetized (at least
for the queries I performed). From what you are saying I should expect
to see random ordering in my results. I accept this, but is there a
reason that I would never experience it? Here is my query and my
result:
E:\src\univ\bmz>DB2 SELECT DISP_NAME,DISP_SYM FROM MCCTOOLS.DISP_TBL
allout.qry

E:\src\univ\bmz>DB2 SELECT PROG_ID,DISP_NAME,DISP_SYM FROM
MCCTOOLS.DISP_TBL WHERE PROG_ID='RX6099B3'

PROG_ID DISP_NAME
DISP_SYM
-------------------------------- --------------------------------
--------
RX6099B3 00_APASS .
RX6099B3 000_APASS .
RX6099B3 01_BUILD B
RX6099B3 02_LBOPA 1
RX6099B3 03_XARRYD 8
RX6099B3 04_LOGIC 7
RX6099B3 05_LBBYP 6
RX6099B3 06_EIDC 5
RX6099B3 07_IOW N
RX6099B3 08_XARRYA Z
RX6099B3 09_WOP1 2
RX6099B3 10_WOP2 3
RX6099B3 11_OLSSD W
RX6099B3 12_POST 9
RX6099B3 13_FAILSTEP K

15 record(s) selected.

I would not say that the results would be totally random, but there is no
guarantee of the order unless you use an ORDER BY. If the rows were
physically stored in a certain order, then DB2 "might" return them in that
order. But there is no guarantee.

Also consider that DB2 sometimes inserts new rows in the middle of a table,
and sometimes at the end, depending on whether there is a clustering index
defined and where space is available (although if append option is used, it
always inserts new rows at the end).

This definition of a relational database is that the user does not know or
care about how the data is physically stored. If the user wants the rows
returned in a particular order, then the user MUST request that order.

A database product is considered to be relational, to the extent to which it
isolates the users from the way the data is physically stored. No database
is completely isolated (and no database is completely relational), but the
physical ordering of rows in an answer set is one aspect where DB2 is fully
relational.
Nov 12 '05 #4
>> The only way would be to define a timestamp column on the table and
let DB2
default to the "current timestamp" when the data is inserted. Then you
could
...<<

might not work on a powerful server - many rows could have one and the
same timestamp. using a sequence or an identity column is another
alternative

Nov 12 '05 #5
ak************@yahoo.com wrote:
The only way would be to define a timestamp column on the table and


let DB2
default to the "current timestamp" when the data is inserted. Then you
could
..<<

might not work on a powerful server - many rows could have one and the
same timestamp. using a sequence or an identity column is another
alternative

Use TIMESTAMP(GENERATE_UNIQUE()) or use an IDENTITY column.
Also note that CURRENT TIMESTAMP is constant per statement.
That is dups will occur for multi-row inserts.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #6
Use SELECT ... FROM FINAL TABLE that (according to the DB2 doc, AFAIK),
returns row in the order the table was inserted.

db2 => CREATE TABLE mytbl(c CHAR(1))
DB20000I The SQL command completed successfully.
db2 => SELECT c FROM FINAL TABLE( INSERT INTO mytbl(c) VALUES
('c'),('b'),('a') )

C
-
c
b
a

3 record(s) selected.

-Eugene

Nov 12 '05 #7
ef******@gmail.com wrote:
Use SELECT ... FROM FINAL TABLE that (according to the DB2 doc, AFAIK),
returns row in the order the table was inserted.

db2 => CREATE TABLE mytbl(c CHAR(1))
DB20000I The SQL command completed successfully.
db2 => SELECT c FROM FINAL TABLE( INSERT INTO mytbl(c) VALUES
('c'),('b'),('a') )

C
-
c
b
a

3 record(s) selected.

-Eugene

Actually it doesn't.

SELECT c
FROM FINAL TABLE( INSERT INTO mytbl(c)
VALUES ('c'),('b'),('a') )
ORDER BY INPUT SEQUENCE

Returns the data in the order in which it was provided.
Without the ORDER BY clause all bets are of.
Either way DB2 could decide to use any form of physical or time order to
store the data.
E.g. if mytbl is partitioned the 'a' may be stored first because it will
live on the local node while teh node holding 'c' is on teh other end of
a 50 Baut string-telephone ;-)

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #8
K.Fawcett wrote:
Thanks for you reply. An 'ORDER BY' clause was never required by this
table until a customer wanted to do something new. I will probably
need to add a new field to support this. I guess what threw me is that
the results of a 'SELECT' query always appear alphabetized (at least
for the queries I performed). From what you are saying I should expect
to see random ordering in my results. I accept this, but is there a
reason that I would never experience it? Here is my query and my
result:

<SNIP>
The return order of rows from an RDBMS is not 'random' but 'undefined' and
'indeterminate'. That is because the RDBMS is free to return rows by
scanning the table and reading allrows, by scanning an index on a filter
column or any other column or set of columns which the optimizer determines
might improve efficiency, or be creating a hash or other temporary table as
an intermediary. In this case it looks like the optimizer selected to query
via an index that apparently includes the disp_sym and disp_name columns so
they seem to be returned ordered by disp_name. The engine likely did that
because the filter value of the index for reducing IOs to find rows with the
disp_sym value 'RX6099B3' was high enough that the index read cose was less
than the IO costs saved by not scanning every data page.

Art S. Kagel
Nov 12 '05 #9

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

Similar topics

8
by: Sans Spam | last post by:
Greetings! I have a table that contains all of the function permissions within a given application. These functions are different sections of a site and each has its own permissions (READ, WRITE,...
4
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more...
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 )
11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
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...
2
by: Stéphane Cazeaux | last post by:
Hi I currently use pgsql 7.2.4 (but the following has also been seen on pgsql 7.3.3) with a transaction level set to "read committed". It do a lot of little tests to understand how concurrency...
25
by: Andreas Fromm | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, Im building an user database with many tables keeping the data for the Address, Phone numbers, etc which are referenced by a table where I...
0
by: ImraneA | last post by:
Hi there I had pleasure of upsizing Access v97 db to Access v2K/SQL 2K. Wish to provide some knowledge gained back to community - hopefully help others. 1.Question how do you test stored...
1
by: solomon_13000 | last post by:
connection.asp: <% Sub RunQueryString (pSQL,parms) on error resume next Set conn = Server.CreateObject("ADODB.Connection") conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &...
6
by: lenygold via DBMonster.com | last post by:
Hi everybody: What is the best way to I have 10 tables with similar INSERT requiremnts. INSERT INTO ACSB.VAATAFAE WITH AA(AA_TIN, AA_FILE_SOURCE_CD, .AA_TIN_TYP) AS ( SELECT AA_TIN,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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
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...

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.