IV. Databases (data type)

A. MySQL data types supported

1. Value Type

MySQL supports all standard SQL numeric data types. 

These types include strictly numeric data types (INTEGER, SMALLINT, DECIMAL and NUMERIC), and an approximate numeric data types (FLOAT, REAL and DOUBLE PRECISION). 

INT is a synonym for INTEGER keyword, keyword DEC is a synonym for DECIMAL. 

MySQL supports the integer types TINYINT, MEDIUMINT and BIGINT. The following table shows the range of each storage and integer type required. 

For decimal representation, MySQL divided into two ways: floating-point and fixed-point. It comprises a float float (single-precision) and double (double precision), and the fixed decimal point is only one of a string stored in MySQL, more precise than a float, suitable for high-precision data represented currency. 

Stored bit BIT data type field value, and supports MyISAM, MEMORY, InnoDB and BDB tables

                                                   

2. INIT type

# Create a table default int width, a specified width int (. 5) 
MySQL> Create Table t1 (ID1 int, ID2 int (. 5 )); 
Query the OK, 0 rows affected ( 0.02 sec) 

# inserted as t1, data 1,1 
MySQL> INSERT INTO T1 values (1,1 ); 
Query the OK, . 1 Row affected (0.01 sec) 

# can be seen that there is no abnormality in the result and 
MySQL> SELECT * from T1;
 + ------ + + ------ 
| id1 | id2 | 
+ ------ + ------ + 
| 1 | 1 | 
+ ------ + ------ + 
Row in the SET (0.00 sec) 

# then when we insert a value greater than the width, the error will not happen? 
MySQL> INSERT INTO T1 values (111111,111111 ); 
Query the OK,Row affected 1 (0.00 sec) 

# The answer is no, id2 still shows the correct value, the impact has not been limited width 
MySQL> the SELECT * from T1;
 + ------------ + - + ------ 
| id1 | id2 | 
+ ------------ + -------- + 
| 0000000001 | 00001 | 
| 0,000,111,111 | 111111 | 
+ ---- + -------- + -------- 
rows in SET (0.00 sec) 

# modify id1 add a field to field unsigned unsigned    unsigned is of the digital type unsigned, int type, e.g. range: -2 ^ 31 to 2 ^ 31 - 1, and the scope of unsigned int: 0 to 2 ^ 32. Appears to be a good type unsigned 
MySQL> ALTER Modify Table T1 unsigned int ID1; 
Query the OK, 0 rows affected ( 0.01 sec) 
Records: 0 Duplicates: 0 Warnings: 0

MySQL > desc T1;
 + ------------------ + ------- + ------ + ----- + ---- + ------- + ----- 
| Field, | Type | Null | Key | the Default | Extra | 
+ ------- + -------------- + ------ + ----- + ---- + --------- + ------- 
| id1 | int (10) unsigned | YES | | NULL | | 
| id2 | int (5) | YES | | NULL | | 
+ ------------------ + ------- + ------ + + --------- + ------- + ----- 
rows in SET (0.01 sec) 

# when the data to be added is greater than 214,748,364 id1 time, can be smoothly inserted 
mysql> insert into t1 values (2147483648,2147483647 ); 
Query the OK, . 1 Row affected (0.00 sec) 

# when the data to be added is greater than 214,748,364 id2 when being given
mysql> insert into t1 values (2147483647,2147483648);
ERROR 1264 (22003): Out of range value for column 'id2' at row 1

3.FAOALT decimal type

The float type represents a single precision floating point values, double-precision floating point value type represents a double, float and double are floating-point, and decimal fixed-point type;

 DECIMAL data type for the exact numerical values ​​stored in the database. We often reserved for the DECIMAL data type columns accurate precision, such as currency accounting system data

 
 

 mysql is a double floating point data type, double (6,2) value indicating a predetermined display does not exceed six digits, with two digits after the decimal point

 MySQL floating-point type and type name can be added after the (M, D) is represented, M is a total length, D denotes the length of the value after the decimal point, M and D, also known as precision and scale, such as float ( 5,2) the
               will be rounded five o'clock can be displayed as 999.99, MySQL stored value


#
Three fields of the table are created float, double, and decimal parameter indicates the total display 5, with fractions of 2 mysql> create table t2 (id1 float (5,2), id2 double (5,2), id3 decimal (5,2 )); Query the OK, 0 rows affected ( 0.02 sec) # inserted into the table 1.23, the result of normal MySQL> iNSERT iNTO T2 values (1.23,1.23,1.23 ); Query the OK, . 1 Row affected (0.00 sec) MySQL > SELECT * from T2; + ------ + ------ + ------ + | ID1 | ID2 | ID3 | + ------ + ------ + ------ + | 1.23 | 1.23 | 1.23 | + ------ + ------ + ------ + Row in the SET (0.00 sec) # insert into a table 1.234 , you will find 4 are cut off MySQL> INSERT INTO T2 values (1.234,1.234,1.234 ); Query the OK, . 1 Row affected, warning. 1 (0.00 sec) MySQL > SELECT * from T2; + ------ + ------ + - + ----- | id1 | id2 | ID3 | + ------ + ------ + ------ + | 1.23 | 1.23 | 1.23 | | 1.23 | 1.23 | 1.23 | + + ------ + ------ + ------ rows in the SET (0.00 sec) # insert into the table 1.235 found that although the data is truncated, but followed the rules of rounding mysql> insert into values T2 (1.235,1.235,1.235 ); Query the OK, . 1 Row affected, warning. 1 (0.00 sec) MySQL > SELECT * from T2; ------ + ------ + ------ + + | id1 | id2 | ID3 | + ------ + ------ + ------ + | 1.23 | 1.23 | 1.23 | | 1.23 | 1.23 | 1.23 | | 1.24 | 1.24 | 1.24 | + ------ + ------ + ------ + rows in the SET (0.00 sec ) # build a new table to remove the constraint parameter MySQL> Create table T3 (ID1 a float, Double ID2, ID3 decimal); Query the OK, 0 rows affected ( 0.02 sec) # are inserted 1.234 MySQL> iNSERT INTO T3 values (1.234,1.234,1.234 ) ; Query the OK, 1 Row affected, 1 warning (0.00 sec) # found that the default value is decimal (10,0) integer MySQL> the SELECT * from T3; ------- + ------- + ------ + + | id1 | id2 | ID3 | + ------- + ------- + - + ---- | 1.234 | 1.234 | 1 | + ------- + ------- + ------ + Row in the SET (0.00 sec) # when there are no constraints on the number of decimal places when the input long decimal, float and double will find the difference between MySQL> INSERT INTO T3 values (1.2355555555555555555,1.2355555555555555555,1.2355555555555555555555 ); Query the OK, 1 Row affected, 1 warning (0.00 sec) MySQL > the SELECT * from T3; -------------------- + --------- + ------ + + | id1 | id2 | ID3 | + --- + -------------------- + ------ + ------ | 1.234 | 1.234 | 1 | | 1.23556 | 1.2355555555555555 | 1 | +---------+--------------------+------+ rows in set (0.00 sec)

4. The date and time class

It indicates the type of date and time is a time value DATETIME, DATE, TIMESTAMP, TIME, and YEAR. 

Each type has a range of valid values time and a " zero " value is used when the value of the specified unlawful MySQL can not represent a " zero " value. 

TIMESTAMP specific types of automatic update feature, which will be described later.

                                                             

 

 

 

 
 

Date:

       Date Type: The system uses three bytes to store the data corresponding to the format: YYYY-mm-dd, range can be expressed from 1000-01-01 to 9999-12-12, 0000-00-00 initial value

 

Time:

       Time type: it can be represented by a specified time, but the system is also provided to store three bytes, corresponding to the format: HH: ii: ss, but the time mysql type can represent much larger time range,

       It can be expressed from -838: 59: 59 to 838: 59: 59, in particular the use mysql is used to describe a time period .

 

Datetime:
         Date Time type: that is, in front of the date and time merge, the time indicated, 8 bytes of data stored in the format YYYY-mm-dd HH: ii : ss,

          Interval can be expressed 1000-01-01 00:00:00 to 9999-12-12 23:59:59, which may be a 0 value: 0000-00-00 00:00:00


mysql> create table t4 (d date,t time,dt datetime); Query OK, 0 rows affected (0.02 sec) mysql> desc t4; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | d | date | YES | | NULL | | | t | time | YES | | NULL | | | dt | datetime | YES | | NULL | | +-------+----------+------+-----+---------+-------+ rows in set (0.01 sec) mysql> insert into t4 values (now(),now(),now()); Query OK, 1 row affected, 1 warning (0.01 sec) mysql> select * from t4; +------------+----------+---------------------+ | d | t | dt | +------------+----------+---------------------+ | 2018-09-21 | 14:51:51 | 2018-09-21 14:51:51 | +------------+----------+---------------------+ row in set (0.00 sec) mysql> insert into t4 values (null,null,null); Query OK, 1 row affected (0.01 sec) mysql> select * from t4; +------------+----------+---------------------+ | d | t | dt | +------------+----------+---------------------+ | 2018-09-21 | 14:51:51 | 2018-09-21 14:51:51 | | NULL | NULL | NULL | +------------+----------+---------------------+ rows in set (0.00 sec)

 

Guess you like

Origin www.cnblogs.com/Sup-to/p/11226055.html