3.3.2 Creating a Table创建表

Creating the database is the easy part, but at this point it is empty, as SHOW TABLES tells you:创建数据库很容易,但此时它是空的,正如SHOW TABLES告诉您的:

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 want a table that contains a record for each of your pets. 您需要一个包含每个宠物的记录的表。This can be called the pet table, and it should contain, as a bare minimum, each animal's name. 这可以称为pet表,它至少应该包含每种动物的名字。Because the name by itself is not very interesting, the table should contain other information. 因为名称本身不是很有趣,所以表应该包含其他信息。For example, if more than one person in your family keeps pets, you might want to list each animal's owner. 例如,如果您的家庭中有多人饲养宠物,您可能希望列出每种动物的主人。You might also want to record some basic descriptive information such as species and sex.您可能还需要记录一些基本的描述性信息,例如物种和性别。

How about age? 年龄呢?That might be of interest, but it is not a good thing to store in a database. 这可能很有趣,但存储在数据库中不是一件好事。Age changes as time passes, which means you'd have to update your records often. 年龄随着时间的推移而变化,这意味着你必须经常更新你的记录。Instead, it is better to store a fixed value such as date of birth. 相反,最好存储一个固定值,例如出生日期。Then, whenever you need age, you can calculate it as the difference between the current date and the birth date. 然后,无论何时需要年龄,都可以将其计算为当前日期和出生日期之间的差值。MySQL provides functions for doing date arithmetic, so this is not difficult. MySQL提供了进行日期运算的函数,所以这并不困难。Storing birth date rather than age has other advantages, too:存储出生日期而不是年龄还有其他好处:

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.您可能会想到在pet表中有用的其他类型的信息,但到目前为止确定的信息已经足够了:姓名、所有者、物种、性别、出生和死亡。

Use a CREATE TABLE statement to specify the layout of your table:使用CREATE 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. VARCHAR对于nameownerspecies列是一个很好的选择,因为列值的长度不同。The lengths in those column definitions need not all be the same, and need not be 20. 这些列定义中的长度不必都相同,也不必为20You can normally pick any length from 1 to 65535, whatever seems most reasonable to you. 您通常可以选择165535之间的任意长度,任何您认为最合理的长度。If you make a poor choice and it turns out later that you need a longer field, MySQL provides an ALTER TABLE statement.如果您做了一个糟糕的选择,后来发现您需要一个更长的字段,MySQL会提供一个ALTER TABLE语句。

Several types of values can be chosen to represent sex in animal records, such as 'm' and 'f', or perhaps 'male' and 'female'. 可以选择几种类型的值来表示动物记录中的性别,例如'm''m',或者'male''female'It is simplest to use the single characters 'm' and 'f'.使用单个字符'm''f'最简单。

The use of the DATE data type for the birth and death columns is a fairly obvious choice.birthdeath列使用DATE数据类型是一个相当明显的选择。

Once you have created a table, SHOW TABLES should produce some output:创建表后,SHOW TABLES应生成一些输出:

mysql> SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| pet                 |
+---------------------+

To verify that your table was created the way you expected, use a DESCRIBE statement:要验证表是否按预期方式创建,请使用DESCRIBE语句:

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.您可以随时使用DESCRIBE,例如,如果您忘记了表中列的名称或它们的类型。

For more information about MySQL data types, see Chapter 11, Data Types.有关MySQL数据类型的更多信息,请参阅第11章,“数据类型”