在创建表之后,您需要填充它。LOAD DATA 和 INSERT 语句对此非常有用。After creating your table, you need to populate it. The
LOAD DATA and
INSERT statements are useful for
this.
假设您的宠物记录可以按如下方式描述。(请注意,MySQL 期望日期格式为 ';这可能与您习惯的不同。)Suppose that your pet records can be described as shown here.
(Observe that MySQL expects dates in
YYYY-MM-DD''
format; this may differ from what you are used to.)YYYY-MM-DD'
| 名字name | 主人owner | 物种species | 性别sex | 出生birth | 死亡death |
|---|---|---|---|---|---|
| 蓬松的Fluffy | 哈罗德Harold | 猫cat | ff | 1993-02-04 | |
| 爪子Claws | 格温Gwen | 猫cat | mm | 1994-03-17 | |
| 巴菲Buffy | 哈罗德Harold | 狗dog | ff | 1989-05-13 | |
| 牙牙Fang | 本尼Benny | 狗dog | mm | 1990-08-27 | |
| 鲍泽Bowser | 黛安Diane | 狗dog | mm | 1979-08-31 | 1995-07-29 |
| 欢唱Chirpy | 格温Gwen | 鸟bird | ff | 1998-09-11 | |
| 口哨Whistler | 格温Gwen | 鸟bird | 1997-12-09 | ||
| 瘦Slim | 本尼Benny | 蛇snake | mm | 1996-04-29 |
因为你是从一个空表开始的,一个简单的方法来填充它是创建一个包含每个动物一行的文本文件,然后用一条语句将文件内容加载到表中。Because you are beginning with an empty table, an easy way to populate it is to create a text file containing a row for each of your animals, then load the contents of the file into the table with a single statement.
你可以创建一个文本文件pet.txt每行包含一条记录,值用制表符分隔,列的顺序是 CREATE TABLE句。对于缺失值(例如未知性别或仍在活的动物的死亡日期),可以使用 NULL 值。要在文本文件中表示这些,可以用 \N(反斜杠,大写 N)。例如,Whistler the bird 的记录会是这样(其中值之间的空白是一个制表符字符):You could create a text file pet.txt
containing one record per line, with values separated by tabs,
and given in the order in which the columns were listed in the
CREATE TABLE statement. For
missing values (such as unknown sexes or death dates for animals
that are still living), you can use NULL
values. To represent these in your text file, use
\N (backslash, capital-N). For example, the
record for Whistler the bird would look like this (where the
whitespace between values is a single tab character):
Whistler Gwen bird \N 1997-12-09 \N
要将文本文件 pet.txt到 pet 表中,请使用以下语句:To load the text file pet.txt into the
pet table, use this statement:
mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;
如果你在 Windows 上使用一个以 \r\n 作为行终止符的编辑器创建了这个文件,你应该改用以下语句:If you created the file on Windows with an editor that uses
\r\n as a line terminator, you should use
this statement instead:
mysql>LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE petLINES TERMINATED BY '\r\n';
(在运行 macOS 的苹果机器上,你可能想使用 LINES TERMINATED BY '\r'。)(On an Apple machine running macOS, you would likely want to use
LINES TERMINATED BY '\r'.)
如果愿意,你可以在 LOAD
DATA 语句中明确指定列值分隔符和行结束标记,但默认值是制表符和换行符。这些默认设置足以让语句正确读取文件 pet.txt。You can specify the column value separator and end of line
marker explicitly in the LOAD
DATA statement if you wish, but the defaults are tab
and linefeed. These are sufficient for the statement to read the
file pet.txt properly.
如果该语句执行失败,则很可能是因为您的 MySQL 安装默认没有启用本地文件功能。请参阅 第 8.1.6 节,“使用 LOAD DATA LOCAL 的安全注意事项”,了解如何更改此设置的信息。If the statement fails, it is likely that your MySQL installation does not have local file capability enabled by default. See Section 8.1.6, “Security Considerations for LOAD DATA LOCAL”, for information on how to change this.
当你想一次添加一条新记录时,INSERT 语句非常有用。在最简单的形式中,你需要为每一列提供值,顺序与 CREATE TABLE 语句中列出的顺序相同。假设 Diane 得到了一只新仓鼠,名叫 “Puffball.” 你可以使用如下的 INSERT 语句添加一条新记录:When you want to add new records one at a time, the
INSERT statement is useful. In
its simplest form, you supply values for each column, in the
order in which the columns were listed in the
CREATE TABLE statement. Suppose
that Diane gets a new hamster named “Puffball.” You
could add a new record using an
INSERT statement like this:
mysql>INSERT INTO petVALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
字符串和日期值在这里以引用的字符串形式指定。此外,使用 INSERT,你可以直接插入 NULL 来表示缺失值。你不会像使用 LOAD DATA 时那样使用 \N。String and date values are specified as quoted strings here.
Also, with INSERT, you can insert
NULL directly to represent a missing value.
You do not use \N like you do with
LOAD DATA.
从这个例子中,你应该能够看出,如果使用几个 INSERT 语句来初始化加载记录,而不是使用单个 LOAD DATA 语句,需要输入的内容将会多得多。From this example, you should be able to see that there would be
a lot more typing involved to load your records initially using
several INSERT statements rather
than a single LOAD DATA
statement.