For a newbie, see "MYSQL _ MySQL ".

Source: Internet
Author: User
A newbie should have read many articles on MYSQL operations and PHP tutorials. today I will introduce several common MYSQL statements.

Display database or table:

Show databases; // You can then use database_name;
Show tables;

Change Table name:

Alter table table_name rename new_t;

Add column:

Alter table table_name add column c_n column attributes;

Delete column:

Alter table table_name drop column c_n;

Create an index:

Alter table c_table add index (c_n1, c_n2 );
Alter table c_table add unique index_name (c_n );
Alt has a lot of articles on PHP tutorials. today we will introduce several commonly used MYSQL statements.

Display database or table:

Show databases; // You can then use database_name;
Show tables;

Change Table name:

Alter table table_name rename new_t;

Add column:

Alter table table_name add column c_n column attributes;

Delete column:

Alter table table_name drop column c_n;

Create an index:

Alter table c_table add index (c_n1, c_n2 );
Alter table c_table add unique index_name (c_n );
Alter table c_table add primary key (sid );

Delete an index:

Alter table c_table drop index c_n1;

Change column information:

Alter table t_table change c_1 c_1 varchar (200 );
Alter table t_table modify 1 c_1 varchar (200 );

Insert statement:

Insert into table_name (c_1, c_2)
Values ('X1', 1 );

Update statement:

Update table_name set c_1 = 1 where c_2 = 3;

Delete a database or table:

Drop table table_name;
Drop database database_name; // data that can be deleted using mysql_drop_db.
If MYSQL runs on the server and you need to connect to the client, you need to connect as follows:

Shell> mysql-h host-u user-p

Enter password :************

Host and user indicate the host name and MySQL account name running on the MySQL server respectively. enter the name of the corresponding machine and the MySQL user name. ******** indicates your password.

If valid, you will see the following prompt:

Welcome to the MySQL monitor. Commands end with; or/g.

Your MySQL connection id is 7 to server version: 5.0.18-nt

Type 'help; 'or'/h' for help. type'/C' to clear the buffer.

Mysql>

Mysql> the prompt tells you to enter the command.

Sometimes MySQL allows you to log on to the server anonymously, and you can directly enter mysql.

Shell> mysql

After successful connection, you can enter quit at the mysql> prompt to exit at any time:

Mysql> quit

Bye

The following shows a simple query instance. this is a simple command that requires the server version number and current date:

Mysql> select version (), current_date;

+ ----------- + -------------- +

| Version () | current_date |

+ ----------- + -------------- +

| 5.0.18-nt | 2006-07-29 |

+ ----------- + -------------- +

1 row in set (0.00 sec)

Mysql>

Here we need to understand:

A command is usually composed of SQL statements followed by a semicolon.

MySQL displays results in tables (rows and columns. the label of the first Behavior column and the result of the subsequent behavior query. the column label is the name of the database table you query. However, if you search for an expression rather than a column value (in the preceding example), the column is usually marked by the expression itself.

Then it will show how many rows are returned and the query time. It provides a rough estimate of the server performance. it indicates that the clock time is not the CPU or machine time.

The keyword can be entered in upper or lower case, that is, it is equivalent in MySQL, but in a query statement, the case must be unified.

The following is another query:

Mysql> select sin (PI ()/4), (4 + 1) * 5;

+ ------------------ + --------- +

| SIN (PI ()/4) | (4 + 1) * 5 |

+ ------------------ + --------- +

| 1, 0.70710678118655 | 25 |

+ ------------------ + --------- +

1 row in set (0.02 sec)

Mysql>

You can enter multiple statements on one line, such:

Mysql> select version (); select now ();

+ ----------------- +

| VERSION () |

+ ----------------- +

| 5.1.2-alpha-log |

+ ----------------- +

1 row in set (0.00 sec)

+ --------------------- +

| NOW () |

+ --------------------- +

| 15:15:00 |

+ --------------------- +

1 row in set (0.00 sec)

Mysql>

A long command can be input in multiple lines. MySQL uses a semicolon to determine whether the statement ends, rather than line feed.

The following is an example of a simple multi-line statement:

Mysql> select

-> User (),

-> Current_date;

+ --------------- + -------------- +

| USER () | CURRENT_DATE |

+ --------------- + -------------- +

| Jon @ localhost | 2007-10-11 |

+ --------------- + -------------- +

Mysql>

In this example, after entering the first line of multi-line query, you can see the prompt changes to->, which means that MySQL does not find the statement end mark and is waiting for the remaining input part. if you do not want to execute a command in the input process, enter/c to cancel it:

Mysql> select

-> User ()

->/C

Mysql>

The following shows the various prompts and the MySQL states they represent:

Prompt description

Mysql> prepare to accept new commands.

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

'> Wait for the next line, and wait for the end of the string starting with a single quotation mark.

"> Wait for the next line, and wait for the end of the string starting with double quotation marks.

'> Wait for the next line, and wait for the end of the identifier starting with the reverse oblique point.

/*> Wait for the next line and wait for the end of comments starting.
Er table c_table add primary key (sid );

Delete an index:

Alter table c_table drop index c_n1;

Change column information:

Alter table t_table change c_1 c_1 varchar (200 );
Alter table t_table modify 1 c_1 varchar (200 );

Insert statement:

Insert into table_name (c_1, c_2)
Values ('X1', 1 );

Update statement:

Update table_name set c_1 = 1 where c_2 = 3;

Delete a database or table:

Drop table table_name;
Drop database database_name; // data that can be deleted using mysql_drop_db.

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.