当前位置:网站首页>MySQL (II) - MySQL data type

MySQL (II) - MySQL data type

2022-06-23 07:16:00 The bad guy is stupid

One 、INT type

type byte minimum value ( A signed / Unsigned ) Maximum ( A signed / Unsigned )

TINYINT

1-128  /  0127  /  255

SMALLINT

2-32768  /  0

32767  /  65535

MEDIUMINT

3-8388608  /  0

8388607  /  16777215

INT

4

-2147483648  /  0

2147483647  /  4294967295

BIGINT

8

-9223372036854775808  /  0

9223372036854775807  /  18446744073709551615

notes : Usual use BIGINT Are signed .

1、 Create a table with unsigned fields 
    CREATE TABLE test_unsigned (a INT UNSIGNED,b INT UNSIGNED);
    insert into test_unsigned values(1,2);
    select a-b from test_unsigned; #  Due to the use of unsigned types ,a-b=-1, Beyond the int Type precision , So this SQL Report errors 
2、INT(N) What is it? ?
    create table test_int_n(a int(4) zerofill);
    insert into test_int_n values(1);
    insert into test_int_n values(123456);

    mysql> select * from test_int_n;
    +--------+
    | a      |
    +--------+
    |   0001 |
    | 123456 |
    +--------+
#######################
int(N) Medium  N  It's the display width ,  Does not mean   Stored digital   length   Upper limit .
zerofill  Represents when the stored number   length  < N  when , use   Numbers 0  Fill left , Until the full length  N
 When storing the length of a number   exceed N when  , according to   Actual storage   The digital display of 

3、 Self increasing 

 Are there any mistakes in the following grammar ?
 create table test_auto_increment(a int auto_increment); # error , Auto increment must be added to the primary key 
 create table test_auto_increment(a int auto_increment primary key);#  correct 

 following SQL What is the result of the operation ?
insert into test_auto_increment values(NULL); # 1
insert into test_auto_increment values(0);
insert into test_auto_increment values(-1); #-1
insert into test_auto_increment values(null),(100),(null),(10),(null);

#####
 If the insert value is 0 or null Is self increasing , If it is a negative number, you can insert .

  Two 、 Character

type explain N The meaning of Whether there is a character set Maximum length
CHAR(N) Fixed length characters

character

yes

255

VARCHAR(N)

Variable length characters

character

yes

16384

BINARY(N)

Fixed length binary bytes

byte

no

255

VARBINARY(N)

Variable length binary bytes

byte

no

16384

TINYBLOB(N)

Binary big object

byte

no

256

BLOB(N)

Binary big object

byte

no

16K

MEDIUMBLOB(N)

Binary big object

byte

no

16M

LONGBLOB(N)

Binary big object

byte

no

4G

TINYTEXT(N)

Big object

byte

yes

256

TEXT(N)

Big object

byte

yes

16K

MEDIUMTEXT(N)

Big object

byte

yes

16M

LONGTEXT(N)

Big object

byte

yes

4G

1、 Sort rule ,bin Store every character in the string in binary data , Case sensitive ,cici Case insensitive ,ci by case insensitive Abbreviation , That is, the case is not sensitive ;
    select 'a' ='A';

    create table test_ci(a varchar(10),key(a));
    insert into test_ci values('a');
    insert into test_ci values('A');

    select * from test_ci where a = 'a';   -- What's the result ?

    set names utf8mb4 collate utf8mb4_bin
     
    select * from test_ci where a = 'a';   -- What's the result ?

  3、 ... and 、 The date type

The date type Occupancy space Scope of representation

DATETIME

8

1000-01-01 00:00:00 ~ 9999-12-31 23:59:59

DATE

3

1000-01-01 ~ 9999-12-31

TIMESTAMP

4

1970-01-01 00:00:00UTC ~ 2038-01-19 03:14:07UTC

YEAR

1

YEAR(2):1970-2070, YEAR(4):1901-2155

TIME

3

-838:59:59 ~ 838:59:59

notes :datetime No time zone ,timestamp Time zone

create table test_time(a timestamp,b datetime);
insert into test_time values(now(),now());
select * from test_time;
set time_zone='+00:00';
select * from test_time;

Four 、json type

1、JSON Use of type  
    create table json_user (uid int auto_increment,data json,primary key(uid));
    insert into json_user values (null, '{"name":"li","age":18,"address":"beijing"}' );
    insert into json_user values (null,'{"name":"zhang","age":28,"mail":"[email protected]"}');

2、JSON Type dependent functions 
    json_extract: extract 
        select json_extract(data, '$.name'),json_extract(data, '$.address') from json_user;
        +------------------------------+---------------------------------+
        | json_extract(data, '$.name') | json_extract(data, '$.address') |
        +------------------------------+---------------------------------+
        | "li"                         | "beijing"                       |
        | "zhang"                      | NULL                            |
        +------------------------------+---------------------------------+

    JSON_OBJECT:  Convert object to json
        insert into json_user values ( null,json_object("name", "wangwu", "email", "[email protected]", "age",35) );

    json_insert:  insert data 
    json_merge:   Merge the data and return 
     Other functions :https://dev.mysql.com/doc/refman/5.7/en/json-function-reference.html

3、JSON Index of type 
    JSON  Type data itself cannot be indexed directly , Need to index JSON Data regenerates virtual columns (Virtual Columns) after , Index the column .
     create table test_inex_1(
         data json,
         gen_col varchar(10) generated always as (json_extract(data, '$.name')), 
         index idx (gen_col) 
     );

原网站

版权声明
本文为[The bad guy is stupid]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230623048637.html