7

In PHP, I create and execute SQL queries like the following.

INSERT INTO Table (Column1, Column2, Column3) VALUES
('1', '2', '3'),
('A', 'B', 'C'),
('AA', 'BB', 'CC');

However, the order in which they are inserted into the database is different every time I try this. Is there a way that I can ensure that they are inserted in the order they are listed?

Addition: Thanks guys for the help. I have using PHP to create MySQL tables from CSV files for a while. In the past, I have always used created a table and inserted all the rows all at once. In these case, the SQL table always had the same order as my INSERT query. However, now I am creating a MySQL and then adding contents gradually. This is when the database order becomes random.

I have overcome this by using ALTER TABLE ... ORDER BY queries, but I am curious as there was order in the first case and now it seems very random.

1

7 Answers 7

6

The default order is what the order of insert statements executed. Unless there's a hierarchical relationship between the rows, the inserted order is irrelevant. If you want the output in a consistent fashion, you must define an ORDER BY clause in your query.

3
  • Do note that this is in regard to referential integrity only.
    – Leprechaun
    Dec 23, 2009 at 7:55
  • @Leprechuan: True, I was speaking with regard to the example provided.
    – OMG Ponies
    Dec 23, 2009 at 8:04
  • Could someone advice where is info about insertion order? Can not find. Tested insert 1000 rows: Insert into table (Column) values (1),(2),(3), and so on. And in mysql see the same order as i provided. But can not find documentation where is written in what order mysql inserts values.
    – Andris
    Jan 3, 2022 at 8:13
3

If you want to retrieve the rows in a certain order, the only way to be sure is to use an ORDER BY clause.

3

The order they're inserted isn't that important since you will likely be using the ORDER BY clause in your queries when pulling the data back out. Remember, MySQL is for storing the data, not necessarily presenting it.

SELECT col1, col2
FROM table1
ORDER BY col2 ASC // fixes any ordering issues in the native-storage
2

A relation never defines order, so you cannot depend on that. You can use an ORDER BY clause to get the desired results.

2

There is a way you can guarantee the order in which they're inserted:

INSERT INTO Table (Column1, Column2, Column3) VALUES ('1', '2', '3');
INSERT INTO Table (Column1, Column2, Column3) VALUES ('A', 'B', 'C');
INSERT INTO Table (Column1, Column2, Column3) VALUES ('AA', 'BB', 'CC');

:-)

But you need to understand that SQL is a relational algebra, working with result sets, and that the order in which things are inserted does not matter. Unless one of the columns is a time stamp of some sort or you have a auto-increment field that you want set in a specific order for the three rows, it won't matter (and both those are actually not good reasons for wanting order controlled).

You're not guaranteed any particular when you extract the rows unless you specify what order you want them in so you shouldn't be concerned about the order they're created. Without an order by clause, the order can actually change each time you do a select.

3
  • Note that this answer is highly inefficient, since it requires three (or more) separate queries to do what can be accomplished in one. It would be much more prudent for the concerned php programmer to follow the advice in the earlier posts.
    – eykanal
    Dec 23, 2009 at 4:25
  • 1
    Did you not see the smiley, @eykanal? In any case, none of the other answers actually guarantee order of insertion which is what the OP asked for - this answer does that, however unnecessary it may be. Other than that, my advice was the same as the others: it's most likely not necessary due to the fact that data sets should be selected with order-by if you want order and, if you don't, there is absolutely no guarantee that you'll get a specific (or even consistent) order.
    – paxdiablo
    Dec 23, 2009 at 4:58
  • +1 because @paxdiablo answer is the correct answer. Probably inefficient, but it's what Brian asked: "Is there a way that I can ensure that they are inserted in the order they are listed?" Mar 13, 2012 at 21:58
1

Even though the order of insert is not guarantee by the DBMS it is typically FIFO. First In First out.

1
  • Not even close. DBMS' vary wildly in the order they produce unordered data sets, from primary key order, other-index order depending on the where-clauses, order affected by clustered indexes or even random order depending on whether you have to do a full table scan and the history of inserts and deletes.
    – paxdiablo
    Dec 23, 2009 at 5:02
0

There is no way (that I am aware of) to force the INSERT statement to occur in a specific Order. Just doing a bare select, such as:

SELECT * FROM Table

Won't always return the data in the same order either. This is because MySQL (and all other database systems) are meant for storing data, and it is up to the developer to query it appropriately. Luckily, there are many different ways to order, limit, and select the data, such as:

ORDER BY column_name ASC
LIMIT 10
WHERE column_name = "test"
WHERE column_name IN (1, 2, 3)
--etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.