Académique Documents
Professionnel Documents
Culture Documents
MySQL has now created your database. You can check this by typing show databases again:
mysql> show databases;
+--------------------+
| Database
+--------------------+
| information_schema |
| bookstore
| mysql
|
|
| performance_schema |
| test
+--------------------+
4 rows in set (0.00 sec)
title
author
price
PRIMARY KEY
(id)
);
Let's take a look at the SQL statements in this file and see what they do:
USE bookstore
This tells MySQL to switch to the bookstore database that you created earlier. MySQL
will then carry out all further operations on this database.
DROP TABLE IF EXISTS books
This deletes any previous books table from the database, since you can't redefine a table if
it already exists.
Be careful when using DROP TABLE. When you delete a table like this, any data in
the table is gone forever!
+---------------------+
Adding records
You've now created a database called bookstore, and added a books table to it. Let's try adding
a record to the books table.
INSERT INTO books ( title, author, price )
VALUES ( "The Grapes of Wrath", "John Steinbeck", 12.99 );
You should see the following output, indicating that MySQL has added the row to the table:
Query OK, 1 row affected (0.06 sec)
Notice that we haven't specified a value for the id field. Since it's an auto_increment field,
MySQL generates the field value automatically.
Let's add another couple of books to the table:
mysql> INSERT INTO books ( title, author, price )
VALUES ( "Nineteen Eighty-Four", "George Orwell", 8.99 ),
( "The Wind-Up Bird Chronicle", "Haruki Murakami", 7.99 );
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
As you can see, you can insert multiple rows at once by supplying multiple sets of field values,
enclosed in parentheses and separated by commas.
Retrieving records
Now that we've added some records to the table, how can we retrieve them? This is where SQL
queries come in. The SQL SELECT statement lets you retrieve one or more records from a table
or even multiple tables at once based on criteria that you supply. The basic syntax is:
SELECT fieldNames FROM tableName [WHERE criteria]
There's a lot more to the SELECT statement than this, but we'll keep things simple in this tutorial!
Let's try a basic SELECT query on our books table using the MySQL Monitor:
mysql> SELECT * FROM books;
+----+----------------------------+-----------------+-------+
| id | title
| author
| price |
+----+----------------------------+-----------------+-------+
| 1 | The Grapes of Wrath
| John Steinbeck | 12.99 |
| 2 | Nineteen Eighty-Four
| George Orwell
| 8.99 |
| 3 | The Wind-Up Bird Chronicle | Haruki Murakami | 7.99 |
+----+----------------------------+-----------------+-------+
This SELECT query retrieves all fields (*) from the books table. Since we haven't supplied any
additional criteria, the query retrieves all the records in the table, and displays the field values in the
MySQL monitor.
As you can see, MySQL has auto-generated the values for the id field, beginning with 1.
What if we want to retrieve just one record from the table, such as the book "Nineteen EightyFour"? To narrow down the selection, we can add a WHERE clause, like this:
mysql> SELECT * FROM books WHERE id = 2;
+----+----------------------+---------------+-------+
| id | title
| author
| price |
+----+----------------------+---------------+-------+
| 2 | Nineteen Eighty-Four | George Orwell | 8.99 |
+----+----------------------+---------------+-------+
1 row in set (0.00 sec)
As well as selecting by the id field, we can select by any other field we like:
mysql> SELECT * FROM books WHERE title = "Nineteen Eighty-Four";
+----+----------------------+---------------+-------+
| id | title
| author
| price |
+----+----------------------+---------------+-------+
| 2 | Nineteen Eighty-Four | George Orwell | 8.99 |
+----+----------------------+---------------+-------+
1 row in set (0.01 sec)
We can also use other operators, such as < (less than), > (greater than), and the boolean AND
operator, to retrieve a range of records:
mysql> SELECT * FROM books WHERE price < 10 AND price > 5;
+----+----------------------------+-----------------+-------+
| id | title
| author
| price |
+----+----------------------------+-----------------+-------+
| 2 | Nineteen Eighty-Four
| George Orwell
| 8.99 |
| 3 | The Wind-Up Bird Chronicle | Haruki Murakami | 7.99 |
+----+----------------------------+-----------------+-------+
2 rows in set (0.00 sec)
Finally, instead of retrieving all fields using *, we can specify just the field or fields we want to
retrieve. Here's an example:
mysql> SELECT title, author FROM books;
+----------------------------+-----------------+
| title
| author
|
+----------------------------+-----------------+
| The Grapes of Wrath
| John Steinbeck |
| Nineteen Eighty-Four
| George Orwell
|
| The Wind-Up Bird Chronicle | Haruki Murakami |
+----------------------------+-----------------+
3 rows in set (0.00 sec)
As you can see, SELECT queries make it easy to retrieve just the records and fields you want from
your table.