content_image/829065.jpg
## 3.3.3 Loading Data into a Table 

For more information about MySQL data types, see Chapter 11, Data Types . 

After creating your table, you need to populate it. The LOAD DATA  and INSERT  statements are useful for this. 

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.) 

<img src='content_image/829065.jpg'>

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. 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 

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; 

If you created the file on Windows with an editor that uses \r
  as a line terminator, you should use this statement instead: 

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

(On an Apple machine running macOS, you would likely want to use 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. These are sufficient for the statement to read the file pet.txt  properly. 

If the statement fails, it is likely that your MySQL installation does not have local file capability enabled by default. See Section 6.1.6, “Security Considerations for LOAD DATA LOCAL” , for information on how to change this. 

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 

