zoukankan      html  css  js  c++  java
  • 数据库:表操作-数据类型(数值类型)

    介绍

    存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的

    mysql常用数据类型概览

    #1. 数字:
        整型:tinyinit  int  bigint
        小数:
            float :在位数比较短的情况下不精准
            double :在位数比较长的情况下不精准
                0.000001230123123123
                存成:0.000001230000
    
            decimal:(如果用小数,则用推荐使用decimal)
                精准
                内部原理是以字符串形式去存
    
    #2. 字符串:
        char(10):简单粗暴,浪费空间,存取速度快
            root存成root000000
        varchar:精准,节省空间,存取速度慢
    
        sql优化:创建表时,定长的类型往前放,变长的往后放
                        比如性别           比如地址或描述信息
    
        >255个字符,超了就把文件路径存放到数据库中。
                比如图片,视频等找一个文件服务器,数据库中只存路径或url。
    
    
    #3. 时间类型:
        最常用:datetime
    
    
    #4. 枚举类型与集合类型
    

      

    1、整数类型

    整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT

    作用:存储年龄,等级,id,各种号码等

    ========================================
            tinyint[(m)] [unsigned] [zerofill]
    
                小整数,数据类型用于保存一些范围的整数数值范围:
                有符号:
                    -128 ~ 127
                无符号:
                    0 ~ 255
    
                PS: MySQL中无布尔值,使用tinyint(1)构造。
    
    
    
    ========================================
            int[(m)][unsigned][zerofill]
    
                整数,数据类型用于保存一些范围的整数数值范围:
                有符号:
                        -2147483648 ~ 2147483647
                无符号:
                        0 ~ 4294967295
    
    
    
    ========================================
            bigint[(m)][unsigned][zerofill]
                大整数,数据类型用于保存一些范围的整数数值范围:
                有符号:
                        -9223372036854775808 ~ 9223372036854775807
                无符号:
                        0  ~  18446744073709551615
    

      验证

    mysql> create table t1(x tinyint);    # 默认为有符号,即数字前有正负号
    Query OK, 0 rows affected (0.12 sec)
    
    mysql> desc t1;
    +-------+------------+------+-----+---------+-------+
    | Field | Type       | Null | Key | Default | Extra |
    +-------+------------+------+-----+---------+-------+
    | x     | tinyint(4) | YES  |     | NULL    |       |
    +-------+------------+------+-----+---------+-------+
    1 row in set (0.07 sec)
    
    mysql> insert into t1 values
        -> (-129),
        -> (-128),
        -> (127),
        -> (128);
    Query OK, 4 rows affected, 2 warnings (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 2
    
    mysql> select * from t1;
    +------+
    | x    |
    +------+
    | -128 |    #-129存成了-128
    | -128 |    #有符号,最小值-128
    |  127 |    # 有符号,最大值127
    |  127 |    # 128存成127
    +------+#
    

      

    # 设置无符号tinyint
    mysql> create table t2(x tinyint unsigned);
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> insert into t2 values
        -> (-1),
        -> (0),
        -> (255),
        -> (256);
    Query OK, 4 rows affected, 2 warnings (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 2
    
    mysql> select * from t2;
    +------+
    |    0 | -1存成了0
    |    0 | #无符号,最小值为0
    |  255 | #无符号,最大值为255
    |  255 | #256存成了255
    +------+
    4 rows in set (0.00 sec)
    
    ============有符号和无符号int=============
    #int默认为有符号
    mysql> create table t3(x int);
    mysql> insert into t3 values
        -> (-2147483649),
        -> (-2147483648),
        -> (2147483647),
        -> (2147483648);
    Query OK, 4 rows affected, 2 warnings (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 2
    
    mysql> select * from t3;
    +-------------+
    | x           |
    +-------------+
    | -2147483648 | #-2147483649存成了-2147483648
    | -2147483648 | #有符号,最小值为-2147483648
    |  2147483647 | #有符号,最大值为2147483647
    |  2147483647 | #2147483648存成了2147483647
    +-------------+
    4 rows in set (0.01 sec)
    
    #设置无符号int
    mysql> create table t4(x int unsigned);
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> insert into t4 values
        -> (-1),
        -> (0),
        -> (4294967295),
        -> (4294967296);
    Query OK, 4 rows affected, 2 warnings (0.03 sec)
    Records: 4  Duplicates: 0  Warnings: 2
    
    mysql> select * from t4;
    +------------+
    | x          |
    +------------+
    |          0 | #-1存成了0
    |          0 | #无符号,最小值为0
    | 4294967295 | #无符号,最大值为4294967295
    | 4294967295 | #4294967296存成了4294967295
    +------------+
    
    
    ==============有符号和无符号bigint=============
    MariaDB [db1]> create table t6(x bigint);
    MariaDB [db1]> insert into t5 values  
        -> (-9223372036854775809),
        -> (-9223372036854775808),
        -> (9223372036854775807),
        -> (9223372036854775808);
    
    MariaDB [db1]> select * from t5;
    +----------------------+
    | x                    |
    +----------------------+
    | -9223372036854775808 |
    | -9223372036854775808 |
    |  9223372036854775807 |
    |  9223372036854775807 |
    +----------------------+
    
    
    
    MariaDB [db1]> create table t6(x bigint unsigned);
    MariaDB [db1]> insert into t6 values  
        -> (-1),
        -> (0),
        -> (18446744073709551615),
        -> (18446744073709551616);
    
    MariaDB [db1]> select * from t6;
    +----------------------+
    | x                    |
    +----------------------+
    |                    0 |
    |                    0 |
    | 18446744073709551615 |
    | 18446744073709551615 |
    +----------------------+
    
    
    
    
    ======用zerofill测试整数类型的显示宽度=============
    MariaDB [db1]> create table t7(x int(3) zerofill);
    MariaDB [db1]> insert into t7 values
        -> (1),
        -> (11),
        -> (111),
        -> (1111);
    MariaDB [db1]> select * from t7;
    +------+
    | x    |
    +------+
    |  001 |
    |  011 |
    |  111 |
    | 1111 | #超过宽度限制仍然可以存
    +------+
    

      

    注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下

    其实我们完全没必要为整数类型指定显示宽度,使用默认的就可以了

    默认的显示宽度,都是在最大值的基础上加1

    int的存储宽度是4个Bytes,即32个bit,即2**32

    无符号最大值为:4294967296-1

    有符号最大值:2147483648-1

    有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的

    最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok

    2、浮点型

    定点数类型 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            |
    +---------------+
    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 |
    +----------------------------------+
    row in set (0.00 sec)
    
    mysql> select * from t2; #精度比float要准确点,但随着小数的增多,同样变得不准确
    +----------------------------------+
    | x                                |
    +----------------------------------+
    | 1.111111111111111200000000000000 |
    +----------------------------------+
    row in set (0.00 sec)
    
    mysql> select * from t3; #精度始终准确,d为30,于是只留了30位小数
    +----------------------------------+
    | x                                |
    +----------------------------------+
    | 1.111111111111111111111111111111 |
    +----------------------------------+
    row in set (0.00 sec)
    

      

  • 相关阅读:
    349元我们应该有什么样的期待原道N12豪华版 RK2906入手初体验
    漫谈国内智能手机市场现状
    将AltiumDesigner(Protel升级版)的PCB设计打造成利器——订制应用、操作、过滤表达式及其他一些微操作
    modelsim se 10.1a 下载与破解
    shell配置,选择,环境变量修改(ORACLE_HOME,ORACLE_SID),无法使用sqlplus
    /ibm/fan
    TrackPoint_configure_ThinkPad_squeeze(0616.2011)
    来个狠的
    JDBC for MSSql2005 简单示例
    oracle使用指南
  • 原文地址:https://www.cnblogs.com/mike-liu/p/9320726.html
Copyright © 2011-2022 走看看