SHOW TABLES
tells you: mysql> SHOW TABLES;
Empty set (0.00 sec)
The harder part is deciding what the structure of your database should be: what tables you need and what columns should be in each of them.You can probably think of other types of information that would be useful in the
pet
table, but the ones identified so far are sufficient: name, owner, species, sex, birth, and death. Use a
CREATE TABLE
statement to specify the layout of your table: mysql>CREATE TABLE pet (name VARCHAR(20), owner VARCHAR(20),
->species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
VARCHAR
is a good choice for the name
, owner
, and species
columns because the column values vary in length. The lengths in those column definitions need not all be the same, and need not be 20
. You can pick any length from 1
to 255
, whatever seems most reasonable to you. (If you make a poor choice and it turns out later that you need a longer field, MySQL provides an ALTER TABLE
statement.) Several types of values can be chosen to represent sex in animal records, such as
'm'
and 'f'
, or perhaps 'male'
and 'female'
. It is simplest to use the single characters 'm'
and 'f'
. The use of the
DATE
data type for the birth
and death
columns is a fairly obvious choice. Once you have created a table,
SHOW TABLES
should produce some output: mysql> SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| pet |
+---------------------+
To verify that your table was created the way you expected, use a DESCRIBE
statement: mysql> DESCRIBE pet;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(20) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
You can use DESCRIBE
any time, for example, if you forget the names of the columns in your table or what types they have.
0 comments:
Post a Comment