Version: PostgreSQL 9.4.2
Column | Type | Modifiers
------------+---------+----------------------------------------------------------------
id | integer | not null default nextval('T1_id_seq'::regclass)
name | text |
value | text |
parent_id | integer |
Indexes:
"T1_pkey" PRIMARY KEY, btree (id)
"T1_id_idx" btree (id)
I have two tables like this in Postgresql, say T1 and T2 with tree like data structure referencing data from own table.
I need to modify some rows in T1 and insert it to T2 in the exact order as the rows appeared in T1. What I have done thus far is copy the relevant rows from table T1 to a temporary table T3 for data modification and insert everything from T3 to T2 when changes' made.
T3 is created using
CREATE TABLE T3 (LIKE T1 INCLUDING ALL)
INSERT * INTO T3 SELECT * FROM T1
The end result is rather strange. All the data from T3 were copied to T2, but the order of the ids seems to be random.
However the result is correct if I invoke the same script to copy data from T1 to T3 directly. What is even more bizarre is it's also correct if if I split the above script into two separate script to
- Create T3 from T1 and copy data from T1 to T3
- Copy T3 to T2 using INSERT method.
Any clues?
order byin aselectstatement