Rules for using SQL statements on the MySQL command line

Source: Internet
Author: User
Tags mysql command line

The rule 1:sql statement must end with a semicolon (;) or (\g)

The semicolon (;) is the end flag for the SQL statement. If you forget the semicolon and press ENTER directly, the following will appear on the MySQL client

    1. Mysql> SELECT * FROM Customer
    2. -

Because the client does not end up with a semicolon, it shows that the SQL statement does not end, and the [->] waits for the user to continue entering the command until the semicolon ends. In some databases, the case of omitting the last semicolon is supported.

Rule 2: Reserved keywords are case insensitive

Reserved keywords are pre-defined keywords in SQL, such as SELECT, from, and so on in the search statement above, which belong to the reserved keyword. These reserved keywords are case insensitive in SQL. In other words, the following statements are correct to
Be executed.

    1. Mysql> SELECT * from customer;
    2. Mysql> select * from Customer;
    3. Mysql> SeLecT * from customer;

However, in general, when you write SQL, you want to keep the size of the keyword as uniform as possible. For example, if you write a reserved keyword in uppercase letters, write a table or column name in lowercase letters, the SQL statement will also look a glance. In addition, depending on the database used, some databases are case-sensitive for table or column names.

Rule 3: Free to add blanks or line breaks

In the middle of the SQL statement, you are free to add spaces or line breaks, such as the following languages can be executed correctly.

    1. Mysql> SELECT *
    2. from customer;
    3. Mysql> SELECT
    4. ->*
    5. ->from
    6. ->customer;

However, it is not legal to add a space or a newline character to the middle of a keyword.

An SQL statement can be written as a single line, but for those long words, you can add the appropriate newline character to it so that it is easy to read.

Wrapping in a command statement is a standard that can be referenced, for example, to list the retrieved object column names in a SELECT statement, and the next line's from command to list the retrieved object table names so that the entire SQL statement looks hierarchical.

In addition, the column name or table name can be wrapped, for example, a single row of the Select column, followed by the retrieved object column name before adding an indentation (tab), all the column name one by one rows are listed separately.

Rule 4: Add comments using [--] or [/* ... */]

Comments can be added to the SQL statement. Comments are information that is not interpreted by the DBMS. Comments are also divided into single-line comments and multiline comments. A single-line comment begins with two [-] until the end of a line is considered a comment. A multiline comment consists of a string consisting of [/*] and [/*].

    1. Mysql> SELECT * from customer; --this is COMMENTS
    2. Mysql>/*this
    3. /*>is
    4. /*>comments*/
The original link in the MySQL command line using the rules of SQL statements, reproduced please specify the source: http://uphtm.com/database/149.html

Rules for using SQL statements on the MySQL command line

Related Article

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.