美文网首页
43-表字段的类型

43-表字段的类型

作者: 天行_b6d0 | 来源:发表于2020-09-08 14:39 被阅读0次

    1、表字段类型之整型

    强调:整型的宽度是显示宽度,无需设置,存储宽度是固定死的

    mysql> create table t5(id tinyint)
    
    mysql> desc t4;
    +-------+------------+------+-----+---------+-------+
    | Field | Type       | Null | Key | Default | Extra |
    +-------+------------+------+-----+---------+-------+
    | id    | tinyint(4) | YES  |     | NULL    |       |
    +-------+------------+------+-----+---------+-------+
    1 row in set (0.01 sec)
    
    mysql> insert t4 values(128);
    ERROR 1264 (22003): Out of range value for column 'id' at row 1
    mysql>
    mysql>
    mysql> insert t4 values(127);
    Query OK, 1 row affected (0.05 sec)
    
    mysql> select * from t4;
    +------+
    | id   |
    +------+
    |  127 |
    +------+
    1 row in set (0.00 sec)
    
    mysql>
    

    2、表字段类型之浮点类型

    create table t7(x float(255,30),y double(255,30),z decimal(65,30));
    
    insert t7 values
    (1.111111111111111111111111111111,1.111111111111111111111111111111,1.111111111111111111111111111111);
    

    3、表字段类型之日期类型

    • year(1901/2155)

    • time 时:分:秒 ('-838:59:59'/'838:59:59')

    • date 年:月:日 (1000-01-01/9999-12-31)

    • datetime 年:月:日 时:分:秒 1000-01-01 00:00:00/9999-12-31 23:59:59

    • timestamp 年:月:日 时:分:秒 1970-01-01 00:00:00/2037

    create table t8(y year,t time,d date,dt datetime,ts timestamp);
    insert t8 values(now(),now(),now(),now(),now());
    
    
    create table student(
        id int,
        name char(10),
        born_year year,
        bitrh date,
        reg_time datetime
    );
    
    
    insert student values
    (1,"xiaoming","1911","1911-11-11","1911-11-11 11:11:11"),
    (2,"xiaohong","1988","1988-11-11","1988-11-11 11:11:11");
    
    
    insert student values
    (3,"xiaolong","1999","19991010","199910101010");
    

    注意:timestamp应该用于记录更新时间
    在sql语句中--是注释的意思,如果想用datetime记录更新时间应该在定义该字段时在后面加上not null default now() on update now()

    create table t3(
        id int,
        name varchar(16),
        -- update_time datetime not null default now() on update now(),
        update_time timestamp,
        reg_time datetime not null default now()
    );
    
    
    insert into t3(id,name) values(1,"xiaox");
    

    测试效果

    修改之后

    4、表字段类型之字符类型

    1. char 定长,不够则补全空格
      看起来的特点:
      浪费空间
      读取速度快

    2. varchar 变长,预留1-2bytes来存储真实数据的长度
      看起来的特点:
      节省空间
      读取速度慢

    ps:在存储的数据量刚好达到存储宽度限制时,其实varchar更费空间

    总结:大多数情况下存储的数据量都达不到宽度限制,所以大多数情况下varchar更省空间,但省空间不是关键,关键是省空间 会带来io效率的提升,进而提升了查询效率。

    ab |abc |abcd |
    1bytes+ab|1bytes+abc|1bytes+abcd|

    <<<<<>>>>>验证

    create table t11(x char(5));
    create table t12(x varchar(5));
    
    insert t11 values("我擦嘞 ");  -- "我擦嘞  "
    insert t12 values("我擦嘞 ");  -- "我擦嘞 "
    

    t11=>字符个数 5 字节个数 11
    t12=>字符个数 4 字节个数 10

    set sql_mode="pad_char_to_full_length";  # 设置这个模式现出原形
    select char_length(x) from t11;
    select char_length(x) from t12;
    
    
    select length(x) from t11;
    select length(x) from t12;
    

    5、表字段类型之枚举类型与集合

    枚举类型enum("a","b","c","d") 多选1
    集合类型set("a","b","c","d") 多选

    CREATE TABLE shirts (
        name VARCHAR(40),
        size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
    );
    
    INSERT INTO shirts(name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');
    
    查看结果
    CREATE TABLE user (
        name VARCHAR(16),
        hobbies set("read","jump","drink","lol")
    );
    insert user values("xiaoming","lol,jump");
    insert user values("hhh","drink");
    

    相关文章

      网友评论

          本文标题:43-表字段的类型

          本文链接:https://www.haomeiwen.com/subject/qtjisktx.html