创建数据库是最简单的部分,但此时它是空的,正如 SHOW TABLES 所显示的:Creating the database is the easy part, but at this point it is
empty, as 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.
你想要一个包含你每只宠物记录的表格。这个表格可以称为pet表,至少应该包含每只动物的名字。因为名字本身不是很有趣,所以表格中应该包含其他信息。例如,如果你家中有不止一个人养宠物,你可能想列出每只动物的主人。你还可能想记录一些基本的描述性信息,如物种和性别。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. 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.
年龄呢?这可能会引起兴趣,但在数据库中存储年龄并不是一个好做法。年龄会随着时间变化,这意味着你需要经常更新记录。相反,最好存储一个固定值,比如出生日期。然后,每当你需要计算年龄时,可以将当前日期与出生日期的差值计算出来。MySQL 提供了处理日期计算的函数,所以这并不难。存储出生日期而不是年龄还有其他好处: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. Storing birth date rather than age has other advantages, too:
你可以使用数据库执行一些任务,比如生成即将到来的宠物生日提醒。(如果你觉得这种查询有点搞笑,请注意,这与在商业数据库中为确定需要在本周或本月发送生日问候的客户而提出的问题是相同的,以实现计算机辅助的个性化服务。)You can use the database for tasks such as generating reminders for upcoming pet birthdays. (If you think this type of query is somewhat silly, note that it is the same question you might ask in the context of a business database to identify clients to whom you need to send out birthday greetings in the current week or month, for that computer-assisted personal touch.)
您可以计算相对于除当前日期以外的日期的年龄。例如,如果您在数据库中存储了死亡日期,您就可以轻松计算宠物去世时的年龄。You can calculate age in relation to dates other than the current date. For example, if you store death date in the database, you can easily calculate how old a pet was when it died.
您可能还能想到其他在pet表中有用的信息类型,但到目前为止确定的这些就足够了:姓名、主人、种类、性别、出生日期和死亡日期。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.
使用CREATE TABLE语句来指定表的布局: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 是 name、owner 和 species 列的一个不错的选择,因为这些列的值长度各不相同。那些列定义中的长度不需要全部相同,也不必是 20。通常,你可以选择从 1 到 65535 中的任意长度,只要你认为合理即可。如果选择不当,后来发现需要更长的字段,MySQL 提供了 ALTER
TABLE 语句。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
normally pick any length from 1 to
65535, 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.
在动物记录中,可以选择几种类型的值来表示性别,例如'm'和'f',或者可能是'male'和'female'。使用单个字符'm'和'f'是最简单的。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'.
对于birth和death列,使用DATE数据类型是一个相当明显的选择。The use of the DATE data type for
the birth and death
columns is a fairly obvious choice.
一旦创建了表,SHOW
TABLES应该会产生一些输出:Once you have created a table, SHOW
TABLES should produce some output:
mysql> SHOW TABLES;
+---------------------+
| Tables in menagerie |
+---------------------+
| pet |
+---------------------+
要验证您的表是否按预期创建,请使用DESCRIBE语句: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 | |
+---------+-------------+------+-----+---------+-------+
您可以随时使用DESCRIBE,例如,如果您忘记了表中列的名称或它们的类型。You can use DESCRIBE any time,
for example, if you forget the names of the columns in your
table or what types they have.
有关 MySQL 数据类型的更多信息,请参见第 13 章,数据类型。For more information about MySQL data types, see Chapter 13, Data Types.