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 (注意MySQL希望日期为'
format; this may differ from what you are used to.)YYYY-MM-DD
''YYYY-MM-DD'
格式;这可能与您习惯的情况不同。)
name | owner | species | sex | birth | death |
---|---|---|---|---|---|
Fluffy | Harold | cat | f | 1993-02-04 | |
Claws | Gwen | cat | m | 1994-03-17 | |
Buffy | Harold | dog | f | 1989-05-13 | |
Fang | Benny | dog | m | 1990-08-27 | |
Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
Chirpy | Gwen | bird | f | 1998-09-11 | |
Whistler | Gwen | bird | 1997-12-09 | ||
Slim | Benny | snake | m | 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.因为您是从一个空表开始的,所以填充它的一种简单方法是为每个动物创建一个包含一行的文本文件,然后用一条语句将文件内容加载到表中。
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 如果在Windows上创建文件时使用的编辑器将\r\n
as a line terminator, you should use this statement instead:\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 (在运行macOS的Apple计算机上,您可能希望使用LINES TERMINATED BY '\r'
.)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
语句加载记录时,需要进行更多的键入。