To explain the meaning of m in MySQL data type int (m) _python

Source: Internet
Author: User
Tags mysql manual

Introduced

The integer types in the MySQL data type is a bit odd. You may see int data types such as int (3), int (4), int (8). When I first contacted MySQL, I thought that int (3) occupies less storage space than int (4), and int (4) occupies less storage space than int (8).

Later, see the MySQL manual, found that they understand the wrong.

Int (m): M indicates the maximum display width for the integer types.

In the integer data type, M represents the maximum display width.

Originally, in Int (m), the value of M has nothing to do with how much storage space the Int (m) occupies. Int (3), int (4), int (8) are occupied by 4 Btyes of storage space on the disk. In other terms, int (M) is the same as the int data type, except that the way it is displayed to the user is somewhat different.

In addition, int (M) can only be combined with Zerofill to make us see the difference clearly.

Mysql> drop table if exists t;
Mysql> CREATE TABLE t (id int zerofill);
mysql> INSERT into T (ID) values (a);
Mysql> select * from T;
+------------+
| ID   |
+------------+
| 0000000010 |
+------------+
mysql> ALTER TABLE t change column ID ID int (3) Zerofill;
Mysql> select * from T;
+------+
| id |
+------+
| 010 |
+------+
mysql>
mysql> ALTER TABLE t change column ID ID int (4) Zerofill;
Mysql> select * from T;
+------+
| id |
+------+
| 0010 |
+------+
mysql>
mysql> insert INTO T (ID) values (1000000);
Mysql> select * from T;
+---------+
| ID  |
+---------+
| 0010 |
| 1000000 |
+---------+

As you can see from the test above, "(M)" Specifies the width of the int type value display. If the field data type is int (4), then: When the value 10 o'clock is displayed, "00" is to be added to the left, and "0" on the left when the number 100 is displayed, and when the number 1000000 is displayed, The specified width "(4)" has been exceeded, so it is output as is.

When using the integer type in the MySQL data type (tinyint, smallint, mediumint, Int/integer, bigint), I can't think of any meaning when you add a "(M)" to the data type after the special requirement.

Add the following data types

1. Integral type

MySQL data type meaning (signed)
tinyint (m) 1 byte range ( -128~127)
smallint (m) 2 byte range ( -32768~32767)
Mediumint (m) 3 byte range (- 8388608~8388607
Int (m) 4 byte range ( -2147483648~2147483647)
bigint (m) 8 byte range (18 of +-9.22*10)

The value range, if added unsigned, doubles the maximum value, such as tinyint unsigned (0~256).
The M in Int (m) represents the width of the display in the Select query result set, does not affect the actual range of values, does not affect the width of the display, and does not know what is the use of this m.

2. Floating-point type (float and double)

MySQL data type meaning
float (m,d) single-precision floating-point 8-bit precision (4 bytes)  m total number, D decimal place double
(m,d) double-precision floating-point 16-bit precision (8 bytes) m total number, D decimal digit

Set a field defined as float (5,3), if you insert a number 123.45678, the actual database is 123.457, but the total number is also the actual, that is 6 bits.

3, fixed-point number

A floating-point type holds an approximate value in the database, while a fixed-point type holds an exact value in the database.

The decimal (m,d) parameter m<65 is the total number, d<30 and d<m are decimal places.

4, String (Char,varchar,_text)

MySQL data type meaning
char (n)  fixed length, up to 255 characters
varchar (n) variable length, up to 65,535 characters
tinytext variable length, up to 255 characters
text  variable length, up to 65,535 characters
mediumtext variable length, up to 2 of 24 square-1 characters
longtext variable length, up to 2 32 Times Square-1 characters

5, date and time data types

MySQL data type meaning
date  3 bytes, dates, format: 2014-09-18
time  3 bytes, times, format: 08:42:30
datetime 8 bytes, date time, Format: 2014-09-18 08:42:30
timestamp 4 bytes, automatic store record modification time of  year 1 bytes, years

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.

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.