If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Postgres is ordering my table against my wil

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-30-10, 23:53
nanda_justgames nanda_justgames is offline
Registered User
 
Join Date: Oct 2010
Posts: 2
Postgres is ordering my table against my wil

Hi all,

I'm developing a program to put a XML file in tables.

My table's primary key is a string like bellow:

1.1
1.2.1
1.3.2
1.10.2

This granted my XML ordering. I have to do that way. Those have to be my primary keys. Period.

When I create tables the insert statements are on that order to like bellow:

INSERT INTO table (primarykey) VALUES (1.1)
INSERT INTO table (primarykey) VALUES (1.2.1)
INSERT INTO table (primarykey) VALUES (1.3.2)
INSERT INTO table (primarykey) VALUES (1.10.2)

So the output in table could be exactly as I showed it to you above.

BUT postgres is given me the output

1.1.1
1.10.2
1.2.1
1.3.1

The 1.10 should be the last line, but this is not happening!

I can only imagine that it has some ordering method in postgres that are reordering MY TABLES!

Can someone please, tell me whats going on and how to solve it?

This is my CREATE_TABLE statements:

'CREATE TABLE MainTable(ElementID character varying NOT NULL,Value character varying,PathID characdter varying NOT NULL,CONSTRAINT mainTable_pk PRIMARY KEY (elementID));

Where elementID is the string that I've just showned...

Sorry for my bad english... PLEASE HELP!
Reply With Quote
  #2 (permalink)  
Old 10-31-10, 04:46
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,675
Data in a relational table is not sorted.
Think of a table as a bag of apples. Those apples do not have an order as well.

If you want your data to be returned in a specific way you have to use an ORDER BY.

As the data you are inserting does not have a "natural" way of sorting you need to either write a function that will sort 1.10 after 1.2 (because the ASCII sort will sort it the other way round) or you need to add a column (e.g. integer) that will define the sort order.
Reply With Quote
  #3 (permalink)  
Old 10-31-10, 09:33
caracadon caracadon is offline
Registered User
 
Join Date: Oct 2010
Location: Western USA
Posts: 17
In reality the table is in correct order. Your PK is not a number but varying character AKA String or Text and is in correct ASCENDING order as displayed by you.
Maybe you should create an additional field called ORDINAL or something like that and give each primary key a numeric order to sort in the order you expect?
Reply With Quote
  #4 (permalink)  
Old 10-31-10, 09:39
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,675
Quote:
Originally Posted by caracadon View Post
In reality the table is in correct order.
A table does not have an "order"!
Reply With Quote
  #5 (permalink)  
Old 10-31-10, 13:19
nanda_justgames nanda_justgames is offline
Registered User
 
Join Date: Oct 2010
Posts: 2
Tks guys!

I'll make another column with index.... so I could order later with ORDER BY!

Tks!
Reply With Quote
  #6 (permalink)  
Old 10-31-10, 17:23
caracadon caracadon is offline
Registered User
 
Join Date: Oct 2010
Location: Western USA
Posts: 17
Quote:
Originally Posted by shammat View Post
A table does not have an "order"!
Ok, but the results were in ascending order then
Reply With Quote
  #7 (permalink)  
Old 10-31-10, 17:58
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,675
Quote:
Originally Posted by caracadon View Post
Ok, but the results were in ascending order then
Unless you specify an ORDER BY any order you see is purely coincidence.
Reply With Quote
  #8 (permalink)  
Old 10-31-10, 18:08
caracadon caracadon is offline
Registered User
 
Join Date: Oct 2010
Location: Western USA
Posts: 17
LOL. Yes, I know. But my point was and in context with the op's expected outcome, is that the results were already in ascending order being text { a, b, c, ... }, whatever the process was that produced it. Not the numeric { 1, 2, 3, ... } that he was expecting; and for the reason I gave.

You are quite correct though: I should be more explicit with my responses, I suppose.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On