MySQL Official manual learning notes 1 mysql simple hands-on _mysql

Source: Internet
Author: User
Tags sin

Connecting and disconnecting servers

Connecting to a server usually requires providing a MysqlUser name and most likely requires a password. If the server is running on a machine other than the logon server, you also need to specify the host name:
shell>    mysql-h host - u user - p  
Enter Password:    ********

Host on behalf of the MySQL server running the hostname, userrepresents the MySQL account username,******** represents your password.

If it works, you should see some introductory information at the mysql> prompt:

shell>    mysql-h host - u user - p  
Enter Password:    ********
Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 25338 to server Version:5.1.2-alpha-standard
Type ' help, ' or ' \h ' for help. Type ' \c ' to clear the buffer.
Mysql>

The mysql> prompt tells you that MySQL is ready to enter commands for you.

Some MySQL installations allow users to connect to a server running on a local host with Anonymous (unnamed) users. If your machine is in this situation, you should be able to invoke MySQL without any options to connect to the server:

shell>    MySQL

After successfully connecting, you can enter QUIT (or \q) at any time at mysql> prompt to exit:

mysql>    QUIT
Bye

In Unix, you can also disconnect the server by pressing the control-d key.

A simple familiarity

The following is a simple command that asks the server to tell it the version number and the current date. At mysql> prompt, enter the following command and press ENTER:

mysql>  SELECT VERSION (), current_date;    a command usually consists of an  SQL statement followed by a semicolon. 
+-----------------+--------------+
| VERSION () | current_date |
+-----------------+--------------+
| 5.1.2-alpha-log | 2005-10-11 |
+-----------------+--------------+
1 row in Set (0.01 sec)   
Mysql>

In addition,mysql can enter keywords in case of capitalization. The following query is equivalent:

mysql>    SELECT VERSION (), current_date;
mysql>    Select version (), current_date;
mysql>    SeLeCt vErSiOn (), current_date;

This is another query that says you can use MySQL as a simple calculator:

mysql>    SELECT SIN (PI ()/4), (4+1) *5;
+------------------+---------+
| SIN (PI ()/4) | (4+1) *5 |
+------------------+---------+
|      0.70710678118655 | 25 |
+------------------+---------+
1 row in Set (0.02 sec)

The command shown here is a fairly short single-line statement. You can enter more than one statement on a single line, and you need to open each statement at a semicolon interval:

mysql>    SELECT VERSION (); SELECT now ();
+-----------------+
| VERSION () |
+-----------------+
| 5.1.2-alpha-log |
+-----------------+
1 row in Set (0.00 sec)
+---------------------+
| Now () |
+---------------------+
| 2005-10-11 15:15:00 |
+---------------------+
1 row in Set (0.00 sec)

You don't have to give a single command in one line, and longer commands can be entered in multiple rows. MySQL determines where the statement ends by looking for a terminating semicolon. (in other words,mysql collects input rows but does not execute until you see a semicolon.) )

Here is an example of a simple multiline statement:

mysql>    SELECT
->    USER ()
->    ,
->    current_date;
+---------------+--------------+
| USER () | current_date |
+---------------+--------------+
| Jon@localhost | 2005-10-11 |
+---------------+--------------+

In this example, after you enter the first line of a multiline query, note that the prompt changes from mysql> to , which is exactly what MySQL says is not seeing the full statement and is waiting for the rest. The prompt is your friend because it provides valuable feedback, and if you use that feedback, you will always know what MySQL is waiting for.

If you decide not to perform a command in the input process, enter \c to cancel it:

mysql>    SELECT
->    USER ()
->    \c
Mysql>

Also note the prompt, which switches back to Mysql> after you enter \c, and provides feedback to indicate that MySQL is ready to accept a new command.

The following table shows the various prompts you can see and describes the status of the MySQL they represent:

Wait for the next line of multiline commands.

) begins the end of the string.

"490" > "

prompt

meaning

mysql>

Ready to accept the new command.

wait for the next line, waiting in single quotes

("
Width= "490" > wait for next line, wait for double The end of the string that begins with the number ("" ).

wait for next line, wait for reverse ramp The end of the identifier at which the point (' ' ) begins.

/*>

wait for next line, wait The end of the comment that span>/* started.

The ' > and ' > Prompt will appear during string collection (prompting MySQL to wait for the end of the string). In MySQL, you can writestrings enclosed by ' or ' 'characters (for example,' Hello ' or ' goodbye '), and MySQL allows you to enter strings that span multiple lines. When you see a ' > or ' > prompt, this means that you have entered a line containing astring starting with the ' or ' 'bracket character, but have not entered a matching quotation mark for the terminating string. This shows you carelessly omitted a quote character. For example:

mysql>    SELECT * from my_table WHERE name = ' Smith and age <;
' >

If you enter a SELECT statement and then press enter and wait for the result, nothing appears. Don't be surprised, "why is the query so long?" ", note " The clue provided by the > prompt. It tells you that MySQL expects to see the remainder of a string that is not terminated. (Do you see the error in the statement?) String "Smith lost the second quotation mark. )

What are you going to do at this point? The simplest is to cancel the command. In this case, however, you can't just type the \c because MySQL interprets it as part of the string it is collecting! Instead, you should enter the closing quote character (so MySQL knows you finished the string ), and then enter \c:

mysql>    SELECT * from my_table WHERE name = ' Smith and age <;
' >    ' \c
Mysql>

The prompt returns to Mysql>, showing that MySQL is ready to accept a new command.

The ' > prompt is similar to the ' > and ' > prompt, but indicates that you have started without ending the ' > start ' identifier.

It is important to know the meaning of the ' > and ' > Prompt, because if you enter an unsigned string incorrectly, any subsequent rows entered will be ignored by MySQL -including the line containing quit! This can be quite confusing, especially if you do not know you need to provide a closing quote before canceling the current command.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.