How to use MySQL built-in functions to convert IP addresses and related numbers

Source: Internet
Author: User
Tags mysql manual

The following articles mainly introduce how to use MySQL built-in functions to convert IP addresses and related numbers. In actual operations, when processing character formats, such as converting 54789, we only need to use: format (12345, 0). The following describes the specific content.

Use MySQL built-in functions to convert IP addresses and numbers

Using two built-in functions

Inet_aton: converts an IP address to a number.

Inet_ntoa: convert a numeric type to an IP address

Make full use of the mysql built-in format Function

Especially when processing character formats, for example, to convert 12345 to 12,345, you only need to use: format (12345, 0), if you use format (12345, 2) 12,345.00 is displayed...

Use MySQL built-in functions to solve timestamp Problems

 
 
  1. eg : select FROM_UnixTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x'); 

Results: 2004 3rd August 03:35:48 2004

Use mysql_convert_table_format to convert the table Type

DBI and DBD mysql-related modules are required for use. Example:

 
 
  1. mysql_convert_table_format --user=root --password='xx' --type=myisam test yejr 

Modify the field name in the mysql table

 
 
  1. alter table tb_name change old_col new_col definition... 

Use temporary variables

 
 
  1. select @var1:=a1+a2 as a_sum,@var2:=b1+b2 as b_sum,@var1+@var2 as total_sum from test_table xxx; 

Store IP addresses of the int type

The original error was deemed to be sufficient only when the bigint type was used. later it was found that the int unsigned type was sufficient. :)

Use the IF function to quickly modify the ENUM Field Value

Example:

 
 
  1. update rule set enable = if('0' = enable,'1','0') where xxx; 

Enable type: enum ('0', '1') not null default '0'

Transactions cannot be nested

Avoid excessive connections caused by long sleep connections

Set the global variables wait_timeout and interactive_timeout to smaller values, for example

10 (s), it will automatically disconnect each sleep connection after 10 s if it has not been queried.

Set the mysql built-in function client prompt (prompt)

 
 
  1. export MYSQL_PS1="(\u:\h:)\d> " 

After you log on with mysql-hlocalhost-uroot-pxx db_name, the prompt is changed:

 
 
  1. (root:localhost:)db_name> 

Easy to use :), always remind you on which server to avoid misoperation

For more details, see the MySQL manual.

Sort out MyISAM fragments

1) Periodically run the optimize table command.

2) use mysqldump to output data and re-import it back. This is obviously easier for large tables to use the first method.

Organize Innodb fragments

1) run the NULL command, alter table xxx engine = Innodb;

2.) Same as mysqldump

If MySQL considers that the number of retrieved records exceeds 30% of the total number of records, select full table scan instead of Index

After MySQL 5.0.3, spaces after the VARCHAR field will not be deleted.

After upgrading to 4.1.1 or higher, it is difficult to downgrade back to 4.0 or 4.1, because InnoDB uses multiple tablespaces.

After MySQL 4.1, MySQL defines the length of a string field as a character length rather than a byte length.

When MySQL 4.1 => 5.0, a new startup option innodb_table_locks is added, which causes InnoDB to be requested during table lock.

Table lock. This option is enabled by default, but it may cause deadlocks in AUTOCOMMIT = 1 and lock tables applications.

Starting from 5.0.3, precise mathematics is used when DECIMAL values and precise rounding values are calculated. DECIMAL is stored in a more effective format.

Start from 5.0.12 and use USING

All connections, including derivative forms of external connections, are processed according to the SQL: 2003 standard; this change causes less natural connections and the use of USING

The number of result fields generated by the JOIN, and these fields will be displayed in a more reasonable order. The priority of the comma comparison operator is the same as that in JOIN and left join.

In the past, the lock waiting for timeout would cause InnoDB to roll back all the current transactions. From 5.0.13 onwards, only the most recent SQL statements will be rolled back.

The index order of TEXT fields ending with spaces in InnoDB and MyISAM tables has changed. Therefore, you must run the "check table" statement to repair the data TABLE. If an error occurs, run

Fix the "optimize table" or "repair table" statement, or even redump (use mysqldump)

After the DECIMAL field created in MyISAM and InnoDB tables between MySQL 5.0.3 and 5.0.5 is upgraded to 5.0.6, a crash occurs.

The above content is an introduction to using MySQL built-in functions to convert IP addresses and numbers. I hope you will get some benefits.

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.