5

I have a table in my database.

+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| rollno | int(11)     | NO   |     | NULL    |                |
| name   | varchar(20) | NO   |     | NULL    |                |
| marks  | int(11)     | NO   |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+

By default if I query

select * from students;

Shows result sorted by id INT (auto-increment).

+----+--------+------------+-------+
| id | rollno | name       | marks |
+----+--------+------------+-------+
|  1 |     65 | John Doe   |    89 |
|  2 |     62 | John Skeet |    76 |
|  3 |     33 | Mike Ross  |    78 |
+----+--------+------------+-------+
3 rows in set (0.00 sec)

I want to change default sorting behaviour and make rollno the default sorting field, how do I do this?

7
  • 1
    Just ORDER BY the thing you want to ORDER BY Commented Apr 2, 2014 at 16:06
  • In my situation I need to sort it by rollno most of the time so I want if it is stored in that order by default I won't have to sort in each query. Commented Apr 2, 2014 at 16:07
  • What you are asking for is essentially not possible. This would require the table to be constantly resorted on each update, which any SQL engine simply will not do for obvious reasons. There is a lot more complexity in doing that than there is in simply adding an ORDER BY onto your query. Commented Apr 2, 2014 at 16:10
  • In my condition inserts/updates are quite less as compared to selects that's why I want to persist modified sorting. Commented Apr 2, 2014 at 16:13
  • 1
    There's no point to it. Mysql (nor any other DBMS) will not persist sorting on a table. You could technically run ALTER TABLE students ORDER BY rollno ASC; after every insert/update, but you would take a huge performance hit as opposed to just sorting the results of a select. Which is more expensive, sorting an entire table, or sorting only the result of a select statement? What is stopping you from just adding order by to your select statement? Commented Apr 2, 2014 at 16:16

3 Answers 3

5

There is no default sort order!

The DB returns the data in the fastest way possible. If this happen to be the order in which it is stored or a key is defined then this is up to the system. You can't rely on that.

Think about it: Why would the DB use performace to order something by default if you don't need it ordered. DBs are optimised for speed.

If you want it being ordered then you have to specify that in an order by clause.

Sign up to request clarification or add additional context in comments.

2 Comments

In my situation I need to sort it by rollno most of the time so I want if it is stored in that order by default I won't have to sort in each query.
You don't understand. You can't rely on any default order. If you want it ordered - add an order by clause. Otherwise don't. That is the only way of ordering relyable.
1

Run this

ALTER TABLE students ORDER BY rollno ASC;

3 Comments

This will only sort existing data in the table; it will not maintain the sort order in the table if rows are added or changed. You should never rely on the assumption that data in a table is already presorted.
This is exactly what I want but as @ElGavilan said what about future inserts/updates?
Databases are optimized for speed not for order. It is much faster to only have to sort the result of a select than it is to consistently sort the entire contents of a table.
0

select * from students order by rollno asc; will return your results sorted by that column. It should be noted that there is no default sorting behavior as far as data is actually stored in the database (aside from identities and indexes); you should never depend on your results being sorted a certain way unless you explicitly sort them (using order by).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.