Database Reference
In-Depth Information
ALTER TABLE
country_codes
ORDER BY
country_code
;
That should have been processed quickly. Let's run the
SELECT
statement again to see
what the first five rows in the table now contain:
SELECT * FROM
country_codes LIMIT 5;
+--------------+----------------------+
| country_code | country_name |
+--------------+----------------------+
| ac | Ascension Island |
| ad | Andorra |
| ae | United Arab Emirates |
| af | Afghanistan |
| ag | Antigua and Barbuda |
+--------------+----------------------+
Notice that the results are different and that the rows are now sorted on the
coun-
try_code
columns without having to specify that order in the
SELECT
statement. To
put the rows back in order by
country_name
, enter the
ALTER TABLE
statement, but
with the
country_name
column instead of the
country_code
column.
Again, reordering a table is rarely necessary. You can order the results of a
SELECT
state-
ment with the
ORDER BY
clause like so:
SELECT
*
FROM
country_codes
ORDER BY
country_name
LIMIT
5
;
The results of this SQL statement are the same as the previous
SELECT
statement, and
the difference in speed is usuallyindiscernible.