Recipe 1.8. Issuing SQL Statements


Problem

You've started mysql, and now you want to send SQL statements to the MySQL server to be executed.

Solution

Just type them in, but be sure to let mysql know where each one ends.

Discussion

When you invoke mysql, it displays a mysql> prompt to tell you that it's ready for input. To issue a SQL statement at the mysql> prompt, type it in, add a semicolon (;) at the end to signify the end of the statement, and press Enter. An explicit statement terminator is necessary; mysql doesn't interpret Enter as a terminator because you can enter a statement using multiple input lines. The semicolon is the most common terminator, but you can also use \g ("go") as a synonym for the semicolon. Thus, the following examples are equivalent ways of issuing the same statement, even though they are entered differently and terminated differently:

mysql> SELECT NOW(); +---------------------+ | NOW()               | +---------------------+ | 2005-11-15 16:18:10 | +---------------------+ mysql> SELECT     -> NOW()\g +---------------------+ | NOW()               | +---------------------+ | 2005-11-15 16:18:18 | +---------------------+ 

Notice that for the second statement the prompt changes from mysql> to -> on the second input line. mysql changes the prompt this way to let you know that it's still waiting to see the statement terminator.

Be sure to understand that neither the ; character nor the \g sequence that serve as statement terminators are part of the statement itself. They're conventions used by the mysql program, which recognizes these terminators and strips them from the input before sending the statement to the MySQL server. It's important to remember this when you write your own programs that send statements to the server (as we'll begin to do in the next chapter). In such programs, you don't include any terminator characters; the end of the statement string itself signifies the end of the statement. In fact, adding a terminator may well cause the statement to fail with an error.

See Also

mysql also can read statements from a file or from another program. See Recipes Section 1.12 and Section 1.13.




MySQL Cookbook
MySQL Cookbook
ISBN: 059652708X
EAN: 2147483647
Year: 2004
Pages: 375
Authors: Paul DuBois

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net