4

I have a MySQL table with 3 columns for simplicity sake:

Datename     |Date    |Name
-------------+--------+-----
30/06/15test |30/06/15|test
30/06/15other|30/06/15|other

Note: Column Datename is unique

I would like all new data to be inserted into the table based on its value.

Example:

  • If the Date is 29/06/15, it would be inserted at bottom of table.
  • If the Date is 01/07/15, it would be inserted at the top of the table

Or preferably sorted by Datename column so it will sort by date then name.

Is this easily achievable, or do I need to just create a page that SELECT and ORDER BY?

0

2 Answers 2

7

A table is, by definition, an unordered bag of rows. There is no guarantee that if you say SELECT * FROM table you will get the rows back in the same order you inserted. Think about throwing a bunch of popsicle sticks on the ground while blindfolded; now take off the blindfold and tell me which one hit the floor last.

No ORDER BY is essentially telling the database you don't care about order.

If you do care about order, you need two things:

  1. At least one column that can dictate order (an auto increment column, a date/time column populated with now(), or something you manually specify).

  2. An actual ORDER BY expression on the outermost part of the query that presents the data (and don't forget to include a tie-breaker, if the first column might not be unique).

1

Check Codd's rules - there's no mention of "ordering". You can order by a PRIMARY KEY (in the SELECT statement) but you aren't forced to.

If you use ORDER BY in your SELECT for your INSERT and use MySQL's AUTO_INCREMENT to INSERT into your target table, then you should have the target table's PK be "ordered" by your criteria.

You should be aware that the SQL standard doesn't specify an ordering unless the SELECT query specifies it. So, you could do SELECT * FROM My_Table (no order by) and get different results from two queries in a row (however, it does appear to me that this is implicitly ordered by PK by the RDBMS).

4
  • It's not implicitly ordered. It often is - but not always. Too many exceptions, to rely on it. Commented Jun 29, 2015 at 23:16
  • @ypercube - could you give a counterexample in any of the servers I mentioned? As I said, I've never seen it not happen - but I accept that it can - as it's not specified/guaranteed by the standard.
    – Vérace
    Commented Jun 29, 2015 at 23:20
  • 2
    I'm pretty sure I had a SQLfiddle example. If I couldn't find the link, so here is another one: SQLfiddle Commented Jun 29, 2015 at 23:22
  • I thought that it only worked because the UNIQUE(b, a) was contrived - since by definition, PK(a) UNIQUE. Tried with your SQLFiddle - but then changed that to UNIQUE(b) and got same result - the data was not in PK order. I'll be ! Even if you add the UNIQUE(b) after table creation **and adding the data and doing a preliminary SELECT *... - it still returns the data out of PK order. Even more puzzling - create table, insert data select - in order of a. Then add Unique(b). In b order. Add PK(a) - still in b order - I thought it was the last one added, but apparently not. MySQL! :-)
    – Vérace
    Commented Jun 30, 2015 at 1:07

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.