• mysql数值类型


    类型表

    int的存储宽度是4个Bytes,即32个bit,即2**32
    
    无符号最大值为:4294967296-1
    
    有符号最大值:2147483648-1
    
    有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,
    所以int类型默认的显示宽度为11是非常合理的 最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok

    整型:默认有符号(-)

    insert into t8 values(-1);  可用看到 -1;

    insert into t8 values(128) :超出范围 为127

    inset into  t8  values(-129); 超出范围为128

    设置为无符号,超出范围时和上面类似

    create table t9(n tinyint unsigned);  

    insert into t9 values(-1),(256)

    整型的宽度代表?

    注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下
    
    其实我们完全没必要为整数类型指定显示宽度,使用默认的就可以了

    create table t10(n int(1) unsigned); //里面的 1 表示显示宽度,一般都不用设置 即是 int

    例如:

    create  table t11(n int(5) unsigned zerofill); //不够0补充
    
    insert into t11 values(1);
    
    select * from t11;  //0001
    
    insert into t11 values(111111111111); 
    select * from t11; // 4294967295

    浮点型

    定点数类型  DEC等同于DECIMAL  
    
    浮点类型:FLOAT DOUBLE
    
    作用:存储薪资、身高、体重、体质参数等
    ======================================
    #FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
    
    定义:
            单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30
    
    有符号:
               -3.402823466E+38 to -1.175494351E-38,
               1.175494351E-38 to 3.402823466E+38
    无符号:
               1.175494351E-38 to 3.402823466E+38
    
    
    精确度: 
               **** 随着小数的增多,精度变得不准确 ****
    
    
    ======================================
    #DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
    
    定义:
               双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30
    
    有符号:
               -1.7976931348623157E+308 to -2.2250738585072014E-308
               2.2250738585072014E-308 to 1.7976931348623157E+308
    
    无符号:
               2.2250738585072014E-308 to 1.7976931348623157E+308
                
    精确度:
               ****随着小数的增多,精度比float要高,但也会变得不准确 ****
    
    ======================================
    decimal[(m[,d])] [unsigned] [zerofill]
    
    定义:
              准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
    
    
    精确度:
               **** 随着小数的增多,精度始终准确 ****
               对于精确数值计算时需要用此类型
               decaimal能够存储精确值的原因在于其内部按照字符串存储。
    mysql> create table t1(x float(256,31));
    ERROR 1425 (42000): Too big scale 31 specified for column 'x'. Maximum is 30.
    mysql> create table t1(x float(256,30));
    ERROR 1439 (42000): Display width out of range for column 'x' (max = 255)
    mysql> create table t1(x float(255,30)); #建表成功
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> create table t2(x double(255,30)); #建表成功
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> create table t3(x decimal(66,31));
    ERROR 1425 (42000): Too big scale 31 specified for column 'x'. Maximum is 30.
    mysql> create table t3(x decimal(66,30));
    ERROR 1426 (42000): Too-big precision 66 specified for 'x'. Maximum is 65.
    mysql> create table t3(x decimal(65,30)); #建表成功
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> show tables;
    +---------------+
    | Tables_in_db1 |
    +---------------+
    | t1            |
    | t2            |
    | t3            |
    +---------------+
    3 rows in set (0.00 sec)
    
    
    
    mysql> insert into t1 values(1.1111111111111111111111111111111); #小数点后31个1
    Query OK, 1 row affected (0.01 sec)
    
    mysql> insert into t2 values(1.1111111111111111111111111111111);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into t3 values(1.1111111111111111111111111111111);
    Query OK, 1 row affected, 1 warning (0.01 sec)
    
    mysql> select * from t1; #随着小数的增多,精度开始不准确
    +----------------------------------+
    | x                                |
    +----------------------------------+
    | 1.111111164093017600000000000000 |
    +----------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select * from t2; #精度比float要准确点,但随着小数的增多,同样变得不准确
    +----------------------------------+
    | x                                |
    +----------------------------------+
    | 1.111111111111111200000000000000 |
    +----------------------------------+
    1 row in set (0.00 sec)
    
    mysql> select * from t3; #精度始终准确,d为30,于是只留了30位小数
    +----------------------------------+
    | x                                |
    +----------------------------------+
    | 1.111111111111111111111111111111 |
    +----------------------------------+
    1 row in set (0.00 sec)
    验证

    日期类型

            YEAR
                YYYY(1901/2155)
    
            DATE
                YYYY-MM-DD(1000-01-01/9999-12-31)
    
            TIME
                HH:MM:SS('-838:59:59'/'838:59:59')
    
            DATETIME
    
                YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)
    
            TIMESTAMP
    
                YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
    类型说明
    ============year===========
    MariaDB [db1]> create table t10(born_year year); #无论year指定何种宽度,最后都默认是year(4)
    MariaDB [db1]> insert into t10 values  
        -> (1900),
        -> (1901),
        -> (2155),
        -> (2156);
    MariaDB [db1]> select * from t10;
    +-----------+
    | born_year |
    +-----------+
    |      0000 |
    |      1901 |
    |      2155 |
    |      0000 |
    +-----------+
    
    
    ============date,time,datetime===========
    MariaDB [db1]> create table t11(d date,t time,dt datetime);
    MariaDB [db1]> desc t11;
    +-------+----------+------+-----+---------+-------+
    | Field | Type     | Null | Key | Default | Extra |
    +-------+----------+------+-----+---------+-------+
    | d     | date     | YES  |     | NULL    |       |
    | t     | time     | YES  |     | NULL    |       |
    | dt    | datetime | YES  |     | NULL    |       |
    +-------+----------+------+-----+---------+-------+
    
    MariaDB [db1]> insert into t11 values(now(),now(),now());
    MariaDB [db1]> select * from t11;
    +------------+----------+---------------------+
    | d          | t        | dt                  |
    +------------+----------+---------------------+
    | 2017-07-25 | 16:26:54 | 2017-07-25 16:26:54 |
    +------------+----------+---------------------+
    
    
    
    ============timestamp===========
    MariaDB [db1]> create table t12(time timestamp);
    MariaDB [db1]> insert into t12 values();
    MariaDB [db1]> insert into t12 values(null);
    MariaDB [db1]> select * from t12;
    +---------------------+
    | time                |
    +---------------------+
    | 2017-07-25 16:29:17 |
    | 2017-07-25 16:30:01 |
    +---------------------+
    
    
    
    ============注意啦,注意啦,注意啦===========
    1. 单独插入时间时,需要以字符串的形式,按照对应的格式插入
    2. 插入年份时,尽量使用4位值
    3. 插入两位年份时,<=69,以20开头,比如50,  结果2050      
                    >=70,以19开头,比如71,结果1971
    MariaDB [db1]> create table t12(y year);
    MariaDB [db1]> insert into t12 values  
        -> (50),
        -> (71);
    MariaDB [db1]> select * from t12;
    +------+
    | y    |
    +------+
    | 2050 |
    | 1971 |
    +------+
    
    
    
    ============综合练习===========
    MariaDB [db1]> create table student(
        -> id int,
        -> name varchar(20),
        -> born_year year,
        -> birth date,
        -> class_time time,
        -> reg_time datetime);
    
    MariaDB [db1]> insert into student values
        -> (1,'alex',"1995","1995-11-11","11:11:11","2017-11-11 11:11:11"),
        -> (2,'egon',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12"),
        -> (3,'wsb',"1998","1998-01-01","13:13:13","2017-01-01 13:13:13");
    
    MariaDB [db1]> select * from student;
    +------+------+-----------+------------+------------+---------------------+
    | id   | name | born_year | birth      | class_time | reg_time            |
    +------+------+-----------+------------+------------+---------------------+
    |    1 | alex |      1995 | 1995-11-11 | 11:11:11   | 2017-11-11 11:11:11 |
    |    2 | egon |      1997 | 1997-12-12 | 12:12:12   | 2017-12-12 12:12:12 |
    |    3 | wsb  |      1998 | 1998-01-01 | 13:13:13   | 2017-01-01 13:13:13 |
    +------+------+-----------+------------+------------+---------------------+
    验证
    mysql> create table t1(x datetime not null default now()); # 需要指定传入空值时默认取当前时间
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> create table t2(x timestamp); # 无需任何设置,在传空值的情况下自动传入当前时间
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> insert into t1 values();
    Query OK, 1 row affected (0.00 sec)
    
    mysql> insert into t2 values();
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from t1;
    +---------------------+
    | x                   |
    +---------------------+
    | 2018-07-07 01:26:14 |
    +---------------------+
    1 row in set (0.00 sec)
    
    mysql> select * from t2;
    +---------------------+
    | x                   |
    +---------------------+
    | 2018-07-07 01:26:17 |
    +---------------------+
    1 row in set (0.00 sec)
    在实际应用的很多场景中,MySQL的这两种日期类型都能够满足我们的需要,存储精度都为秒,但在某些情况下,会展现出他们各自的优劣。下面就来总结一下两种日期类型的区别。
    
    1.DATETIME的日期范围是1001——9999年,TIMESTAMP的时间范围是1970——2038年。
    
    2.DATETIME存储时间与时区无关,TIMESTAMP存储时间与时区有关,显示的值也依赖于时区。在mysql服务器,操作系统以及客户端连接都有时区的设置。
    
    3.DATETIME使用8字节的存储空间,TIMESTAMP的存储空间为4字节。因此,TIMESTAMP比DATETIME的空间利用率更高。
    
    4.DATETIME的默认值为null;TIMESTAMP的字段默认不为空(not null),默认值为当前时间(CURRENT_TIMESTAMP),如果不做特殊处理,并且update语句中没有指定该列的更新值,则默认更新为当前时间。
    datetime和timestamp区别

    字符串类型

    char:定长       varchar:变长

     下面我们来看看两种类型存储数据的情况

    create  table t16(name char(5));
    create table t17(name varchar(5));
    
    //现在插入数据
    
    insert into t16('星星 ');//后面有一个空格
    insert into t17('星星 ');
    
    //查看它们的长度
    mysql> select char_length(name) from t16;
    +-------------------+
    | char_length(name) |
    +-------------------+
    |                 2 |
    +-------------------+
    1 row in set (0.00 sec)
    
    mysql> select char_length(name) from t17;
    +-------------------+
    | char_length(name) |
    +-------------------+
    |                 3 |
    +-------------------+
    1 row in set (0.00 sec)
    
    //让他们的空格也算上一个字符
    mysql> set sql_mode='PAD_CHAR_TO_FULL_LENGTH';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> select char_length(name) from t16; //定长的就是5个了
    +-------------------+
    | char_length(name) |
    +-------------------+
    |                 5 |
    +-------------------+
    1 row in set (0.00 sec)
    
    mysql> select char_length(name) from t17;
    +-------------------+
    | char_length(name) |
    +-------------------+
    |                 3 |
    +-------------------+
    1 row in set (0.00 sec)

     关于空格,等值比较的时候最后的空格忽略,前后的空格不会

    mysql> select * from t16 where name='星星';
    +-----------+
    | name      |
    +-----------+
    | 星星      |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> select * from t17 where name='星星';
    +---------+
    | name    |
    +---------+
    | 星星    |
    +---------+
    1 row in set (0.00 sec)
    
    mysql> select * from t17 where name='星星                 ';
    +---------+
    | name    |
    +---------+
    | 星星    |
    +---------+
    1 row in set (0.00 sec)

    mysql> select * from t17 where name=' 星星';
    Empty set (0.00 sec)

    like搭配 % _的使用

    mysql> select * from t16  where name like '星星_'; //char
    Empty set (0.00 sec)
    
    mysql> select * from t16  where name like '星星___';
    +-----------+
    | name      |
    +-----------+
    | 星星      |
    +-----------+
    1 row in set (0.00 sec)
    
    mysql> select * from t17  where name like '星星_'; //varchar
    +---------+
    | name    |
    +---------+
    | 星星    |
    +---------+
    1 row in set (0.00 sec)
    
    mysql> select * from t17  where name like '星星___';
    Empty set (0.00 sec)

     对于char(5)的存储:字符不够空格补充,取得时候方便,一次5个

    对于varchar(5)的存储:有几个字符就是几个字符,并且前面有1-2个bytes的头,

    ValueCHAR(4)Storage RequiredVARCHAR(4)Storage Required
    '' '    ' 4 bytes '' 1 byte
    'ab' 'ab  ' 4 bytes 'ab' 3 bytes
    'abcd' 'abcd' 4 bytes 'abcd' 5 bytes
    'abcdefgh' 'abcd' 4 bytes 'abcd' 5 bytes

    总结

    #InnoDB存储引擎:建议使用VARCHAR类型
    单从数据类型的实现机制去考虑,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度的50%。
    
    但对于InnoDB数据表,内部的行存储格式没有区分固定长度和可变长度列(所有数据行都使用指向数据列值的头指针),因此在本质上,使用固定长度的CHAR列不一定比使用可变长度VARCHAR列性能要好。因而,主要的性能因素是数据行使用的存储总量。由于CHAR平均占用的空间多于VARCHAR,因此使用VARCHAR来最小化需要处理的数据行的存储总量和磁盘I/O是比较好的。
    
    #其他字符串系列(效率:char>varchar>text)
    TEXT系列 TINYTEXT TEXT MEDIUMTEXT LONGTEXT
    BLOB 系列    TINYBLOB BLOB MEDIUMBLOB LONGBLOB 
    BINARY系列 BINARY VARBINARY
    
    text:text数据类型用于保存变长的大字符串,可以组多到65535 (2**16 − 1)个字符。
    mediumtext:A TEXT column with a maximum length of 16,777,215 (2**24 − 1) characters.
    longtext:A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**32 − 1) characters.

    枚举类型和集合类型

     枚举和集合都设置为not null时,插入数据时,集合不能为空,枚举对象为空时,默认使用第一个枚举对象

    mysql> desc consumer;
    +-------+----------------------------+------+-----+---------+-------+
    | Field | Type                       | Null | Key | Default | Extra |
    +-------+----------------------------+------+-----+---------+-------+
    | id    | int(11)                    | YES  |     | NULL    |       |
    | name  | varchar(50)                | YES  |     | NULL    |       |
    | sex   | enum('nale','female')      | NO   |     | NULL    |       |
    | fav   | set('play','music','read') | NO   |     | NULL    |       |
    +-------+----------------------------+------+-----+---------+-------+
    4 rows in set (0.01 sec)
    
    mysql> insert into consumer(name) values ('mayun');
    ERROR 1364 (HY000): Field 'fav' doesn't have a default value
    mysql> insert into consumer(name,fav) values ('mayun','play');
    Query OK, 1 row affected (0.06 sec)
    
    mysql> select * from consumer;
    +------+-------+------+------+
    | id   | name  | sex  | fav  |
    +------+-------+------+------+
    | NULL | mayun | nale | play |
    +------+-------+------+------+
    1 row in set (0.00 sec)
    枚举和集合插入为空的情况
    mysql> create table employee(
        -> id int,
        -> name char(10),
        -> sex enum('male','female','other'),
        -> hobbies set('play','eat','music','read')
        -> );
    Query OK, 0 rows affected (0.19 sec)
    
    mysql> insert into employee values
        -> (1,'zhangsan','male','music,read');
    Query OK, 1 row affected (0.06 sec)
    
    mysql> select * from employee;
    +------+------------+------+------------+
    | id   | name       | sex  | hobbies    |
    +------+------------+------+------------+
    |    1 | zhangsan   | male | music,read |
    +------+------------+------+------------+
    1 row in set (0.00 sec)
    
    mysql> insert into employee values
        -> (1,'zhangsan','xxxxx','music,read'); //插入里面没有的情况,结果为空
    Query OK, 1 row affected, 1 warning (0.06 sec)
    
    mysql> select * from employee;
    +------+------------+------+------------+
    | id   | name       | sex  | hobbies    |
    +------+------------+------+------------+
    |    1 | zhangsan   | male | music,read |
    |    1 | zhangsan   |      | music,read |
    +------+------------+------+------------+
    2 rows in set (0.00 sec)
  • 相关阅读:
    WordPress WooCommerce ‘hide-wc-extensions-message’参数跨站脚本漏洞
    WordPress WP-Realty插件‘listing_id’参数SQL注入漏洞
    WordPress Videowall插件‘page_id’参数跨站脚本漏洞
    MySQL备忘点(上)
    Print工具类
    用于图片缩放的工具类
    重载、重写、方法相同
    Try-Catch-Finally代码块中的return
    Fltiss项目的架构、包名的定义和类的划分
    优化版快速排序
  • 原文地址:https://www.cnblogs.com/mmyy-blog/p/9618018.html
Copyright © 2020-2023  润新知