What are the common application skills in MySQL databases?

Source: Internet
Author: User

The following articles mainly introduce some special practical application skills that we all know in the MySQL database. If you are interested in the related practical application skills, you can click here to view the following articles.

View query results in XML format

By using the traditional-xml option to call the MySQL command line client program, you can view it in XML format (instead of the traditional list form.

MySQL database query results

This technique is useful if you plan to integrate the query output with other programs. Here is an example:

Table

 
 
  1. shell> mysql --xml  
  2. mysql> SELECT * FROM test.stories;  
  3. 1  
  4. This is a test  
  5. 2  
  6. This is the second test  
  7. 2rows in set (0.11 sec)  

Fast index Reconstruction

Generally, if you want to change the server's full-text search variable, you need to re-create a full-text index in the table to ensure that your updates are mapped. This operation will take a lot of time, especially if you need to process a lot of data. A quick solution.

The repair table command is used to demonstrate the process:

Table B

 
 
  1. mysql> REPAIR TABLE content QUICK;  
  2. +-----------+--------+----------+----------+  
  3. | Table| Op| Msg_type | Msg_text |  
  4. +-----------+--------+----------+----------+  
  5. | content| repair | status| OK|  
  6. +-----------+--------+----------+----------+  
  7. 1 row in set (0.05 sec)  

Compress certain table types

If you are processing a read-only MyISAM table, MySQL database allows you to compress it to save disk space. You can use myisampack as follows:

Table C

 
 
  1. shell> myisampackmovies.MYI  
  2. Compressing movies.MYD: (146 records)  
  3. - Calculating statistics  
  4. - Compressing file  
  5. 41.05% 

Use traditional SQL

MySQL supports traditional SQL queries and IF and CASE structures. The following is a simple example:

Table D

 
 
  1. mysql> SELECT IF (priv=1, 'admin', 'guest')  
  2. As usertype FROM privs WHERE username = 'joe';  
  3. +----------+  
  4. | usertype |  
  5. +----------+  
  6. | admin|  
  7. +----------+  
  8. 1 row in set (0.00 sec) 

Output table data in CSV format

The MySQL output file contains a list of all SQL commands. If you want to import the output file to the MySQL database, this function is very practical, but this method will not work if the target program (such as Excel) cannot communicate with SQL. In this case, you can tell MySQL

Create an output file in CSV format, which is easy to import to most programs. The operation process of mysqldump is demonstrated here:

 
 
  1. shell> mysqldump -T .  
  2. --fields-terminated-by=", " mydbmytable  

This will generate a text file in the current directory, containing records from mydb. mytable list with comma as the delimiter.

Use the strict mode to reduce the appearance of "bad" Data

The MySQL server can run in a variety of different modes, and each of them is optimized for specific purposes. By default, no mode is set. However, by adding the following options to the server command line, you can easily change the mode settings and run MySQL in "strict" Mode:

 
 
  1. shell> mysqld --sql_mode="STRICT_ALL_TABLES" &  

In "strict" mode, the query is aborted through MySQL and an error is returned. Many automatic correction functions of the server are invalid. Similarly, a stricter time check will be executed in this mode.

Monitoring Server

You can run the show status command to obtain a server running and statistical report, including the number of connections opened, the number of activation queries, and the normal running time of the server. For example:

Table E

 
 
  1. Emysql> SHOW STATUS;  
  2. +------------------+-------+  
  3. | Variable_name| Value |  
  4. +------------------+-------+  
  5. | Aborted_clients| 0|  
  6. | Aborted_connects | 0|  
  7. ...  
  8. | Uptime| 851|  
  9. +------------------+-------+  
  10. 156 rows in set (0.16 sec)  

The create table code is returned automatically.

MySQL database allows you to automatically obtain SQL commands to recreate a specific table. Simply run the show create table command and view the TABLE creation code, as shown below:

Table F

 
 
  1. mysql> SHOW CREATE TABLE products;  
  2. ---------------------------------------  
  3. | Table| Create Table  
  4. +----------+---------------------------  
  5. | products | CREATE TABLE `products` (  
  6. `id` int(8) NOT NULL auto_increment,  
  7. `name` varchar(255) NOT NULL default '',  
  8. `price` int(10) default NULL,  
  9. PRIMARY KEY(`id`)  
  10. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |  
  11. +----------+----------------------------  
  12. 1 row in set (0.27 sec) 

Create a more useful Command Prompt:

By default, the MySQL database command line client program displays a simple mysql> prompt. However, you can use specific modifications to change the prompt to make it more effective, including the current user name, host name, and the selected database. As follows:

Table G

 
 
  1. mysql> prompt \U:/\d> 
  2. PROMPT set to '\U:/\d>'  
  3. root@localhost:/db1>  

The above content is a little-known introduction to some of the special skills of the MySQL database. I hope you will have some gains.

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.