3.3.3 Loading Data into a Table将数据加载到表中

After creating your table, you need to populate it. 创建表后,需要填充它。The LOAD DATA and INSERT statements are useful for this.LOAD DATA语句和INSERT语句对此很有用。

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.)(注意MySQL希望日期为'YYYY-MM-DD'格式;这可能与您习惯的情况不同。)

nameownerspeciessexbirthdeath
FluffyHaroldcatf1993-02-04
ClawsGwencatm1994-03-17
BuffyHarolddogf1989-05-13
FangBennydogm1990-08-27
BowserDianedogm1979-08-311995-07-29
ChirpyGwenbirdf1998-09-11
WhistlerGwenbird1997-12-09
SlimBennysnakem1996-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.因为您是从一个空表开始的,所以填充它的一种简单方法是为每个动物创建一个包含一行的文本文件,然后用一条语句将文件内容加载到表中。

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. 您可以创建一个文本文件pet.txt,每行包含一条记录,其值由制表符分隔,并按照CREATE TABLE语句中列的顺序给出。For missing values (such as unknown sexes or death dates for animals that are still living), you can use NULL values. 对于缺少的值(例如未知性别或仍然活着的动物的死亡日期),可以使用NULL值。To represent these in your text file, use \N (backslash, capital-N). 要在文本文件中表示这些内容,请使用\N(反斜杠,大写-N)。For example, the record for Whistler the bird would look like this (where the whitespace between values is a single tab character):例如,Whistler the bird的记录如下所示(其中值之间的空格是单个制表符):

Whistler        Gwen    bird    \N      1997-12-09      \N

To load the text file pet.txt into the pet table, use this statement:要将文本文件pet.txt加载到pet表中,请使用以下语句:

mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet;

If you created the file on Windows with an editor that uses \r\n as a line terminator, you should use this statement instead:如果在Windows上创建文件时使用的编辑器将\r\n用作行终止符,则应改用以下语句:

mysql> LOAD DATA LOCAL INFILE '/path/pet.txt' INTO TABLE pet
LINES TERMINATED BY '\r\n';

(On an Apple machine running macOS, you would likely want to use LINES TERMINATED BY '\r'.)(在运行macOS的Apple计算机上,您可能希望使用LINES TERMINATED BY '\r'。)

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. 如果愿意,可以在LOAD DATA语句中显式指定列值分隔符和行尾标记,但默认值为tab和linefeed。These are sufficient for the statement to read the file pet.txt properly.这些足够语句正确读取文件pet.txt

If the statement fails, it is likely that your MySQL installation does not have local file capability enabled by default. 如果该语句失败,则您的MySQL安装在默认情况下可能没有启用本地文件功能。See Section 6.1.6, “Security Considerations for LOAD DATA LOCAL”, for information on how to change this.有关如何更改的信息,请参阅第6.1.6节,“本地负载数据的安全注意事项”

When you want to add new records one at a time, the INSERT statement is useful. 当您希望一次添加一条新记录时,INSERT语句非常有用。In its simplest form, you supply values for each column, in the order in which the columns were listed in the CREATE TABLE statement. 最简单的形式是,按照CREATE TABLE语句中列的顺序为每列提供值。Suppose that Diane gets a new hamster named Puffball. 假设Diane得到了一个名为“Puffball”的新仓鼠。You could add a new record using an INSERT statement like this:您可以使用如下INSERT语句添加一条新记录:

mysql> INSERT INTO pet
VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);

String and date values are specified as quoted strings here. 字符串和日期值在此处指定为带引号的字符串。Also, with INSERT, you can insert NULL directly to represent a missing value. 此外,使用INSERT,您可以直接插入NULL来表示缺少的值。You do not use \N like you do with LOAD DATA.您使用\N的方式与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.从本例中,您应该能够看到,在最初使用几个INSERT语句而不是单个LOAD DATA语句加载记录时,需要进行更多的键入。