Database Reference
In-Depth Information
database name — unless you want to execute an SQL statement for a table in another
database. After entering the
USE
command, you can re-enter the earlier SQL statement to
list the tables in the database without specifying that you want
test
. It's taken for gran-
ted:
SHOW TABLES;
+----------------+
| Tables_in_test |
+----------------+
| books |
+----------------+
Now that we've peeked at a database, which is not much more than a grouping of tables
(in this example, only one table), and created a table, let's look inside the table that we
created. To do that, we'll use the SQLstatement
DESCRIBE
, like so:
DESCRIBE books;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| book_id | int(11) | YES | | NULL | |
| title | text | YES | | NULL | |
| status | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
In these results you can see that we created three fields for entering data, named
book_id
,
title
, and
status
. That's pretty limited, but we're keeping things simple
in this chapter. The first and third fields,
book_id
and
status
, are integer types, mean-
ing they can contain only numbers. We stipulated that when we created the table by
addingthe
INT
keyword when specifying those columns. The other field,
title
, can
contain text, which includes anything you can type at the keyboard. We set that earlier
with the
TEXT
keyword. Don't worry about remembering any of this now. We're just
looking around to get a feel for the system andthe
mysql
client.
Inserting and Manipulating Data
Let's put somedata in this table. Enter the following three SQL statements within the
mysql
client:
INSERT INTO
books
VALUES
(
100
,
'Heart of Darkness'
,
0
);
INSERT INTO
books
VALUES
(
101
,
'The Catcher of the Rye'
,
1
);
INSERT INTO
books
VALUES
(
102
,
'My Antonia'
,
0
);