Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting into table in order

Tags:

sql

mysql

How can I insert values into a table (MySQL) in the following manner:
On all the rows of a table, in order of ID column (PK), insert incrementing number in column 'num'?
For example if the table had 3 rows , with Ids 1,5,2, I want ID 1 to get num=1, ID 2 to get num=2 and ID 5 to get num=3.

EDIT I will explain why I (think I) need this:
I am trying to split a column off a table into a separate table with a 1-to-1 relation. I thought I would get all the values in order of ID and insert them into the new table, with an auto-incrementing PK. then I know that, in order of ID, the values for the new reference column in the original table will be auto-incrementing numbers. So I want to insert them in that order. I hope this is clear.

like image 712
Baruch Avatar asked Feb 23 '12 14:02

Baruch


People also ask

Does insert into have to be in order?

If you are specifying the column names, the order doesn't matter. For example: INSERT INTO TABLE_NAME VALUES ('','','') // Here the values needs to be in order of columns present in your table.

Does SQL insert in order?

The only thing that order by on an insert is guaranteed to do is assign the values of an identity column if one exists. Your select has no order by, hence SQL is in no way required to return the data in any particular order.

How do you insert data into descending order in SQL?

The SQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.


3 Answers

i am currently not in front of sql database engine and cannot therefore submit fully verified sql code. however if your num field is not an autoincrement field than do something like this:

CREATE TEMPORARY TABLE temp_table_x (
    num int auto_increment primary key,
    reference_id int
);

INSERT temp_table_x (reference_id)
SELECT id FROM source_table ORDER BY id;

UPDATE source_table st 
    SET st.num = x.num
FROM temp_table_x x
WHERE reference_id = id;
like image 104
Julien May Avatar answered Nov 10 '22 00:11

Julien May


As long as the num field is an autoincrement field it should be as simple as:

INSERT INTO
  yourTable (
    field1,
    field2,
    field3,
    etc
  )
SELECT
  field1,
  field2,
  field3
FROM
  yourSourceTable
ORDER BY
  originalIdField
like image 43
MatBailie Avatar answered Nov 09 '22 23:11

MatBailie


I would NOT make a field that references a column in another table an auto-increment column.

Even if the column that it references is an auto-increment, I wouldn't make the column auto-increment. It will be difficult to keep the columns in sync. If an insert is rolled back in one table but not the other, you'll be out of sync until you reset the auto_increment value.

If it's a 1 to 1 relationship, feel free to make the column a primary key. That way it will be ordered by the column, and it will ensure unique values. However, if any two columns must match, they should not both be auto-increment, though, they should be of the same type (eg. INTEGER).

For example, here's our original table, where the first column is an auto-increment integer column:

id  customer_name  email_address
---------------------------
1   jsmith         [email protected]
2   bwilliams      [email protected]

If you wanted to split the email_address off to its own table, in a 1 to 1 relationship:

id  email_address
---------------------------
1   [email protected]
2   [email protected]

I would make the first column an integer field and make it the primary key, but it would NOT be an auto-increment column.

To insert values into such a table, you could simply do this:

INSERT INTO table2
  (id, email_address)
SELECT id, email_address
  FROM table1
  ORDER BY id
like image 36
Marcus Adams Avatar answered Nov 10 '22 00:11

Marcus Adams