MySQL basis (1) | Data Types

MySQL basis (1) | Data Types


  1. Numeric types
TINYINT #小整数值,1 字节,有符号(-128,127),无符号(0,255)
SMALLINT #大整数值,2 字节
MEDIUMINT #大整数值,3 字节
INT或INTEGER #大整数值,4 字节,有符号(-2147483648,2147483647),无符号(0,4294967295)
BIGINT #极大整数值,8 字节

FLOAT #单精度浮点数值,4 字节
DOUBLE #双精度浮点数值,8 字节
DECIMAL #小数值,定点数,DECIMAL(M,D),其中M称为精度,表示总共的位数;D称为标度,表示小数的位数;默认D值为0、M值为10
  1. Time Type
DATE #日期值,YYYY-MM-DD,1000-01-01/9999-12-31
TIME #时间值或持续时间,HH:MM:SS,'-838:59:59'/'838:59:59'
YEAR #年份值,YYYY,1901/2155
DATETIME #混合日期和时间值,8字节,YYYY-MM-DD HH:MM:SS,1000-01-01 00:00:00/9999-12-31 23:59:59
TIMESTAMP #混合日期和时间值,时间戳,4字节,'1970-01-01 00:00:01.000000' 到 '2038-01-19 03:14:07.999999'
  1. String type
# 字节型字符串【二进制字节型字符串,没有字符集概念】
BINARY #定长二进制字节型字符串
VARBINARY #变长二进制字节型字符串

TINYBLOB #不超过 255 个字符的二进制字符串,0-255字节    
BLOB #二进制形式的长文本数据,0-65 535字节    => ~64kb
MEDIUMBLOB #二进制形式的中等长度文本数据,0-16 777 215字节   => ~16MB
LONGBLOB #二进制形式的极大文本数据,0-4 294 967 295字节    => ~4GB

# 字节型字符串 end 

# 字符型字符串

CHAR #定长字符串,0-255字节
VARCHAR #变长字符串, 0-65535 字节

TINYTEXT #短文本字符串,0-255字节    
TEXT #长文本数据,0-65535字节   => ~64kb
MEDIUMTEXT  #中等长度文本数据,0-16 777 215字节    => ~16MB
LONGTEXT #极大文本数据,0-4 294 967 295字节  => ~4GB

# 字符型字符串 end

Resolve

① distinguishing character byte and

  • BINARY(N)And VARBINARY(N)the N refers to the length in bytes.

  • CHAR(N)And VARCHAR(N)the N refer to the character length.

  • For BINARY(10), which may be fixed byte 10 is stored, and for CHAR(10), depending on the case of byte character set which may be stored may be.

② distinguish between different mechanisms BINARY, VARBINARY, CHAR, VARCHAR four types of insertion and query

  • char (N) for storing non-binary strings, when inserted, with less than N characters will be automatically added to the tail box, query, trailing spaces will be discarded [i.e. trailing spaces will be removed when inserted ]

  • vachar (N) for storing non-binary strings, the insertion, for not less than N characters to fill the spaces, query, trailing spaces will not be discarded as output] [i.e.

  • binary (N) stored binary string, inserted into the less than N bytes of 0x00 automatically added at the end, when removed, all of the bytes are reserved, returns the byte length of defined length, when comparing all bytes are valid [tail will fill \ 0]

  • inserting varbinary not going to fill the 0x00 byte, query, they also will not drop any bytes, when comparing all the bytes are valid [also] is output

    Example:

    create table test(
      a VARCHAR(3),
      b CHAR(3),
      c VARBINARY(3),
      d BINARY(3)
    )engine=innodb charset=gbk;
    
    insert into test select 'a ','a ','a ','a ';
    
    select hex(a),hex(b),hex(c),hex(d) from test;
    select concat('$',a,'$'),concat('$',b,'$'),concat('$',c,'$'),concat('$',d,'$')
    from test;
    SELECT a = 'a ', b = 'a', c = 'a ', d = 'a \0'from test;

    1 result set:

    1 result set

    The results set 2:

Set 3 results:

  1. Spread
  • VARCHAR:

    Mysql must meet the maximum line width limit, i.e. 65535 (64k) bytes, and the number of string varchar itself is defined by [i.e. varchar (xxx) xxx string by number in parentheses defined]; uft-8 character set used in mysql a character occupies three bytes, so the single table varchar is actually occupied by the maximum length (65535-2) / 3 = 21,844 characters (255 bytes 2 bytes have additional space overhead, minus 2; if it is 255 or less, minus 1).

Example:

#VARCHAR单表单字段最长不能超过21844
CREATE TABLE test(
    va VARCHAR(21845)
)DEFAULT CHARSET=utf8;
[Err] 1118 - Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
#这样就可以了
CREATE TABLE test(
    va VARCHAR(21844)
)DEFAULT CHARSET=utf8;
受影响的行: 0
时间: 0.155s
  • TEXT:

    The maximum limit is 64k bytes, but the essence is the overflow storage . innodb default before storing only 768 bytes in the data page, and the remaining data will be stored in the overflow section, although a single table 65535 by a maximum width limit line, but each table mysql BLOB and TEXT columns only practical wherein the 5-9 bytes, the rest of the store overflow. So the actual occupation table maximum line width of 9 + 2 bytes, plus the extra overhead, nothing to do with the actual width of the table.

  • Get the current time approach

SELECT CURRENT_TIMESTAMP;
SELECT CURRENT_TIMESTAMP();
SELECT NOW();
SELECT LOCALTIME;
SELECT LOCALTIME();
SELECT LOCALTIMESTAMP;
SELECT LOCALTIMESTAMP();
  • String transfer time
select str_to_date('2017-10-16 15:30:28','%Y-%m-%d %H:%i:%s'); 
  • Time to String
select date_format(now(), '%Y-%m-%d %H:%i:%s') ;
  • General type conversion
#字符串转数字
方法一:SELECT CAST('123' AS SIGNED);
方法二:SELECT CONVERT('123', SIGNED);
方法三:SELECT '123'+0;

二进制,同带binary前缀的效果 : BINARY 
字符型,可带参数 : CHAR() 
日期 : DATE 
时间: TIME 
日期时间型 : DATETIME 
浮点数 : DECIMAL 
整数 : SIGNED 
无符号整数 : UNSIGNED

Guess you like

Origin www.cnblogs.com/iwsx/p/12348929.html