Retrieving Information from a Table
201
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\n
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\n';
(On an Apple machine running OS X, 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 Issues with
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
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 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. You do not use
\N
like you do with
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.
3.3.4. Retrieving Information from a Table
The
SELECT
statement is used to pull information from a table. The general form of the statement is:
SELECT
what_to_select
FROM
which_table
WHERE
conditions_to_satisfy
;
what_to_select
indicates what you want to see. This can be a list of columns, or
*
to indicate “all
columns.”
which_table
indicates the table from which you want to retrieve data. The
WHERE
clause
is optional. If it is present,
conditions_to_satisfy
specifies one or more conditions that rows must
satisfy to qualify for retrieval.
3.3.4.1. Selecting All Data
The simplest form of
SELECT
retrieves everything from a table:
mysql>
SELECT * FROM pet;
+----------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...