Bug #81491 delete then insert multiple values produces data in wrong order.
Submitted: 18 May 2016 15:05 Modified: 18 May 2016 18:13
Reporter: tom mlay Email Updates:
Status: Not a Bug Impact on me:
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.5.49-c11 OS:Any
Assigned to: CPU Architecture:Any
Tags: insert data wrong order

[18 May 2016 15:05] tom mlay
delete then insert multiple values produces data in wrong order. The behavior is different between 5.5.6 on windows (correct) and 5.5.49-c11 on Linux/Centos (incorrect).

For a variety of reasons, I delete a set of records from a table and then insert new ones. As a minimal demonstration see the sql commands below.

I create a table.
Insert multiple values.
Select shows the values in the proper order (id's asc).
Delete those values.
Insert the same values.
Select shows the values in the wrong order (new id's DESC).

It appears mysql is getting the correct id with the correct set of values but I would epxect the data to be written in the order the values are provided.

The work-around is to select with "order by id" but I fear this may be a sign of some larger problem.

When I execute these commands under Windows wampp (mysql 5.5.6) I get the results I expect.

This occurs in both the command line and through php.

How to repeat:
drop table if exists x1;
create table x1 (
	report		int default 0,
	file_id		int default 0,
	field_id	int default 0,

insert into x1 (report,file_id,field_id) values(99,4,69),(99,4,86),

select * from x1;

delete from x1 where report = 99;

insert into x1 (report,file_id,field_id) values(99,4,69),(99,4,86),

select * from x1;
[18 May 2016 15:50] MySQL Verification Team
Thank you for your bug report. However, it is not a bug ...

One of the basic premises of the relational model is that data sets do not have an ordering. It thus applies to all SQL databases. It was stipulated first time by the great C.J.Date, who was co-inventor of SQL.

Hence, when the language was first time standardized, that was clearly stipulated. Hence, on page 373 of the SQL-92 standard it writes loud and clear: "If an <order by clause> is not specified, then the table specified by the <cursor specification> is T and the ordering of rows in T is implementation-dependent."

Hence, no guarantee ....
[18 May 2016 18:13] tom mlay
Thanks for your quick reply.

I understand there is no intrinsic ordering of data in an sql table. However, in every previous version of mysql and in every other sql product I have worked with (MS Sql, IBN/SQL,Postgress) I have been able to rely on the data being written in the order in which I insert it, i.e. the select * from table returns the records in the order inserted.

Your interpretation leads to two significant issues:
1) If I insert 10 records in one insert command I get a different result than if I do 10 insert commands.
2) I now have to do an order by id in order to retrieve the data in the order I inserted it. This will be a MAJOR performance issue on large data sets.

-Tom Mlay