MySql official manual Study Notes 1 MySql easy to get started

Source: Internet
Author: User

Connect to and disconnect the server

A MySQLThe user name and a password may be required. If the server runs on a machine other than the login server, you must specify the Host Name:
shell> mysql -h host -u user -p
Enter password: ********

HostRepresentativeMySQLHost Name of the server,UserRepresentativeMySQLAccount username,********Your password.

If it works, you should seeMysql>Some introduction information after the 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>

Mysql>Prompt to tell youMysqlEnter the Command for you.

SomeMySQLThe installation allows anonymous (untitled) users to connect to the server running on the local host. If your machine is in this situation, you should be able to call it without any options.MysqlConnect to the server:

shell> mysql

After successful connection, you canMysql>EnterQUIT (Or\ Q)Exit at any time:

mysql> QUIT
Bye

InUnix, You can also pressControl-DKey to disconnect the server.

 

Easy to understand

The following is a simple command that requires the server to tell it the version number and current date. InMysql>Enter the following command and press Enter:

Mysql>Select version (), CURRENT_DATE;//A command is usually composedSQLStatement, 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,MysqlKeywords can be entered in upper or lower case. The following queries are equivalent:

mysql> SELECT VERSION(), CURRENT_DATE;
mysql> select version(), current_date;
mysql> SeLeCt vErSiOn(), current_DATE;

This is another query, which indicates that you canMysqlAs 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 displayed here is a very short single-line statement. You can enter multiple statements on one line. You only need to separate the statements with one semicolon:

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 do not need to give a command in one line. A long command can be entered into multiple lines.Mysql isDetermine where the statement ends by looking for an end semicolon. (In other words,MysqlCollect input rows, but do not execute until you see a semicolon .)

Here is an example of a simple multi-line statement:

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

In this example, after entering the first line of multi-line query, note thatMysql>Change->, Which is exactlyMysqlIt indicates that it does not see the complete statement and is waiting for the remaining part. The prompt is your friend, because it provides valuable feedback. If you use this feedback, you will always knowMysqlWhat is waiting.

If you decide not to execute a command in the input process, enter\ CCancel it:

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

Note the prompt.\ CLater, it switches backMysql>, Provide feedback to indicateMysqlPrepare to accept a new command.

The following table shows the various prompts that you can see and briefly describesMysqlStatus:

Prompt

Description

Mysql>

Prepare to accept the new command.

->

Wait for the next line of the multi-line command.

'>

Wait for the next line, and wait in single quotes("'")The end of the Start string.

">

Wait for the next line and double quotation marks(""")The end of the Start string.

'>

Wait for the next line, wait for the reverse oblique point('`')The end of the Start identifier.

/*>

Wait for the next line./*End of the Start comment.

 

Will appear during string collection'>And">Prompt (PromptMySQLWaiting for the end of the string ). InMySQLCan be written'''Or'"'Character string(For example,'Hello'Or"Goodbye")AndMysqlYou can enter a string that spans multiple rows. When you see'>Or">At the prompt, this means that'''Or'"'Line of the string starting with the parentheses, but no matching quotation marks are entered for the ending string. This shows that you accidentally omitted a quotation mark character. For example:

mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
    '>

If you enterSELECTStatement, and then pressEnter(Press ENTER)Key and wait for the result. Nothing appears. Don't be surprised, "Why is the query so long ?", Note:">Clues provided by the prompt. It tells youMysqlExpect to see the rest of an unterminated string. (Do you see errors in statements? String"SmithThe second quotation mark is dropped .)

In this step, what should you do? The simplest is to cancel the command. However, in this case, you cannot just enter\ CBecauseMysqlInterpret it as part of the string it is collecting! On the contrary, enter the characters in the closed quotation marks.(This wayMysqlKnowing that you have completed the string)And then enter\ C:

mysql> SELECT * FROM my_table WHERE name = 'Smith AND age < 30;
    '> '\c
mysql>

Prompt backMysql>, DisplayMysqlYou are ready to accept a new command.

'> The prompt is similar'> And"> Prompt, but it indicates that you have started but have not ended'>Start identifier.

Yes'>And">The meaning of the prompt is very important, because if you mistakenly enter an unterminated string, any line entered later will beMysqlIgnore--Include includeQUITLine! This may be confusing, especially if you do not know Before canceling the current command, you need to provide the ending quotation marks.

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.