Creating and Using a Database
197
example,
'hello'
or
"goodbye"
), and
mysql
lets you enter strings that span multiple lines. When
you see a
'>
or
">
prompt, it means that you have entered a line containing a string that begins with a
“
'
” or “
"
” quote character, but have not yet entered the matching quote that terminates the string. This
often indicates that you have inadvertently left out a quote character. For example:
mysql>
SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
'>
If you enter this
SELECT
statement, then press Enter and wait for the result, nothing happens. Instead
of wondering why this query takes so long, notice the clue provided by the
'>
prompt. It tells you that
mysql
expects to see the rest of an unterminated string. (Do you see the error in the statement? The
string
'Smith
is missing the second single quotation mark.)
At this point, what do you do? The simplest thing is to cancel the command. However, you cannot just
type
\c
in this case, because
mysql
interprets it as part of the string that it is collecting. Instead, enter
the closing quote character (so
mysql
knows you've finished the string), then type
\c
:
mysql>
SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
'>
'\c
mysql>
The prompt changes back to
mysql>
, indicating that
mysql
is ready for a new command.
The
`>
prompt is similar to the
'>
and
">
prompts, but indicates that you have begun but not
completed a backtick-quoted identifier.
It is important to know what the
'>
,
">
, and
`>
prompts signify, because if you mistakenly enter
an unterminated string, any further lines you type appear to be ignored by
mysql
—including a line
containing
QUIT
. This can be quite confusing, especially if you do not know that you need to supply the
terminating quote before you can cancel the current command.
3.3. Creating and Using a Database
Once you know how to enter commands, you are ready to access a database.
Suppose that you have several pets in your home (your menagerie) and you would like to keep track
of various types of information about them. You can do so by creating tables to hold your data and
loading them with the desired information. Then you can answer different sorts of questions about
your animals by retrieving data from the tables. This section shows you how to perform the following
operations:
• Create a database
• Create a table
• Load data into the table
• Retrieve data from the table in various ways
• Use multiple tables
The menagerie database is simple (deliberately), but it is not difficult to think of real-world situations
in which a similar type of database might be used. For example, a database like this could be used by
a farmer to keep track of livestock, or by a veterinarian to keep track of patient records. A menagerie
distribution containing some of the queries and sample data used in the following sections can be
obtained from the MySQL Web site. It is available in both compressed
tar
file and Zip formats at
http://
dev.mysql.com/doc/
.
Use the
SHOW
statement to find out what databases currently exist on the server:
mysql>
SHOW DATABASES;
+----------+
Содержание 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 ...