开发者

row order when inserting multplie rows in MySQL

开发者 https://www.devze.com 2022-12-14 13:15 出处:网络
In PHP, I create and exe开发者_StackOverflow中文版cute SQL queries like the following. INSERT INTO Table (Column1, Column2, Column3) VALUES

In PHP, I create and exe开发者_StackOverflow中文版cute 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.


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.


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


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


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


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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消