Databases Reference
In-Depth Information
combination of its
album_id
and the primary key of its owning entity, which is
artist_id
.
You should now feel comfortable about choosing a database, listing its tables, and
retrieving all of the data from a table using the
SELECT
statement. To practice, you might
want to experiment with the
university
or
flight
databases you loaded in Chapter 3
in “Loading the Sample Databases.” Remember that you can use the
SHOW TABLES
state-
ment to find out the table names in these databases.
Choosing Columns
You've so far used the
*
wildcard character to retrieve all columns in a table. If you
don't want to display all the columns, it's easy to be more specific by listing the columns
you want, in the order you want them, separated by commas. For example, if you want
only the
artist_name
column from the
artist
table, you'd type:
mysql>
SELECT artist_name FROM artist;
+---------------------------+
| artist_name |
+---------------------------+
| New Order |
| Nick Cave & The Bad Seeds |
| Miles Davis |
| The Rolling Stones |
| The Stone Roses |
| Kylie Minogue |
+---------------------------+
6 rows in set (0.01 sec)
If you want both the
artist_name
and the
artist_id
, in that order, you'd use:
mysql>
SELECT artist_name,artist_id FROM artist;
+---------------------------+-----------+
| artist_name | artist_id |
+---------------------------+-----------+
| New Order | 1 |
| Nick Cave & The Bad Seeds | 2 |
| Miles Davis | 3 |
| The Rolling Stones | 4 |
| The Stone Roses | 5 |
| Kylie Minogue | 6 |
+---------------------------+-----------+
6 rows in set (0.00 sec)
You can even list columns more than once:
mysql>
SELECT artist_id, artist_id FROM artist;
+-----------+-----------+
| artist_id | artist_id |
+-----------+-----------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |