MySQL basic data types

Foreword

Originally I feel no need to write this blog, because MySQL official described in great detail. But recently encountered some problems are not simply look at the official presentation can be resolved in a design table, the So, here would like everyone to look at.

type of data

Integer

First, we look at storage space and a range of values ​​for each integer type MySQL 5.7 official given need:

It should be noted here that, from the practical point of view, we must choose the most appropriate data type according to the actual situation. E.g:

  • It represents a boolean 0 and 1 column, select TINYINT (1) is sufficient, but if you are using a memory byte count is greater than TINYINT data type is poorly designed.

Different databases and memory to Java, for example, where you can use byte type used not long type of problem, because most objects are short-lived objects in the program, the method is finished this memory area was released, 7 bytes do not actually exist waves do not waste one said. However, as a database storage system, space BIGINT 8-byte data is real.

Integer (N) in the form of

Look at the official presentation MySQL 5.7

For integer types, M is the maximum width of the display. Regardless of the value of the maximum width of the display range of the display 255. The width of the types comprising

So for integer (N) in this form, as long as we understand two things:

  • No matter how much N is independent of the type and number of Bytes

  • N denotes the display width, insufficient with zeros, ignoring over the entire length of the direct digital display, but is set to an integer valid unsigned zerofill

Below is an example:

DROP TABLE IF EXISTS test;
CREATE TABLE test (
   a INT(5),
   b INT(5) UNSIGNED,
   c INT(5) UNSIGNED ZEROFILL,
   d INT(8) UNSIGNED ZEROFILL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO test VALUES(1,1,1,1111111111);

SELECT * FROM test;

Guess you like

Origin www.cnblogs.com/tkzL/p/11366948.html