zoukankan      html  css  js  c++  java
  • mysql之数据类型

    一、概述: 

      所谓建表,就是声明列的过程:

        数据是以文件的形式放在硬盘中(也有放在内存里的)

         列:不同的列类型占的空间不一样

         选列的原则:够用又不浪费

    二、mysql的数据类型:

       整形:Tinyint(1字节)  Smallint(2个字节)  Mediumint(3个字节)  int(4个字节)  bigint(8个字节);

       Tinyint在mysql默认是有符号的(-128----127);

        Tinyint(M) unsigned zerofill
        unsigned : 是无符号,影响存储范围;
        M代表宽度,(必须配合zerofill时才有意义)
        Zerofill 零填充,如果某列是zerofill,默认是unsigned(类似00005);

        insert into classs (name, age4)values ('zhaoliu', 9);
        列可以声明默认值,而且推荐声明默认值: NOT NULL default 0

        alter table class add age5 tinyint not null default 0;

      小数型/浮点型定点型:
        Float(M, D)
        decimal(M, D)
              //M:精度(总位数,不包含点) D:标度(小数位)

      create table goods(
        name varchar(10) not null default '',
        price float(6, 2) not null default 0.00) //9999.99, -9999.99
        //price float(6, 2) unsigned not null default 0.00) //0-9999.99,
      charset utf8;

      inert into goods
      (name, price)
      values
      ('跑步机', ‘688,896’)
                  //记录在表中price的值为688.90

      alter table goods add bigprice float(9.2) not null default 0.0;

      alter table goods add deciprice decimal(9.2) not null default 0.0;

      alter table goods (name, bigprice, deciprice)
      values
      ('自行车', 1234567.23, 1234567.23);

      字符型 

        char 定长字符串 char(M),M代表宽度,即可容纳的字符数;    Varchar 变长字符串 Varchar(M),M代表宽度,即可容纳的字符数;

      区别:
        char定长: M个字符如果存的小于M个字符,实占M个字符;利用率是100%
        varchar变长: M个字符如果存的小于M个字符,假设为N,实占N个字符;实占的字符需要记录消耗1--2个字符;实际占有(N+1~2)个字符;

      char 与varchar选择原则:
        1、空间利用效率;
        2、速度;
      示例:  

        四字成语表,char(4);
        个人微博, varchar(140);

        用户名:char,牺牲空间,提供速度;

      text 文本串,比较大段文本,速度稍慢;
        注意:text不要加默认值,加了也不生效;


      create table stu(
        name char(8) not null default '',
        waihao varchar(16) not null default ''
        )charset utf8; //name最多容纳8个utf8字符;

      insert into stu(name, waihao)
        values
        ('zhangxiaosan', 'saner'); //拆入不进去,zhangxiaosan太长了;

      insert into stu(name, waihao)
        values
        ('zhangsan', 'saner');

      insert into stu(name, waihao)
        values
        ('默罕默德买买提',‘异步拉欣’);

      select concat (name, '!'), concat(waihao, '!') from stu;

      alter table std add info text not null default '';
        //执行错误;不能默认值;

    三、mysql中的日期时间类型:

        //年---------------------year
        //年-月-日---------------date
        //00:00:00-------------time
        //年-月-日 hh:nn:ss----datatime
          Year类型:1个字节,1901年到2155年;【0000年表示错误选择】
        如果输入2位,‘00-69’表示2000---2069;    ‘70-99’表示1970---1999;
        如果麻烦,输入的时候输入4位即可;

      Date类型:典型格式:1992-08-12 日期范围:1000-01-01 ---9999-12-31;

      time类型:典型格式:hh:mm:ss

      Datatime类型:典型格式:'1989-06-09 14:23:02'
          范围:1000-01-01 00:00:00 ---9999-12-31 23:59:59

      注意:
          在开发中,很少用日期时间类型来表示一个需要的精确到秒
        原因:虽然日期时间类型能精确到秒,而且方便查看;
        而是用时间戳:1970-01-01 00:00:00到当前的秒数;

      create table t2(
        gender enum('男',‘女’)
        )charset utf8;

      insert into t2 values('男');
      insert into t2 values('春哥'); //error

      create table user(
        name varchar(20) not null default '',
        regtime datetime not null default '1000-01-01 00:00:00')
        charset utf8;

     

      create table t( tm time);

      insert into t values ('14:33:56');

       create table y(

        ya year(4)
      )

      insert into y  values ('1901');

        insert into y values('2280'); //error

      insert into y values('97'); //存入了1997

      insert into y values('12'); //存入的是2012

      create table d
        title varchar(30),da data)
      charset utf8;

      insert into d values
        ('开国大典', ‘1949-10-01’);

      insert into d values
        ('世界末日', ‘2012-02-30’); //输入不合法

  • 相关阅读:
    pat 甲级 1065. A+B and C (64bit) (20)
    pat 甲级 1064. Complete Binary Search Tree (30)
    pat 甲级 1010. Radix (25)
    pat 甲级 1009. Product of Polynomials (25)
    pat 甲级 1056. Mice and Rice (25)
    pat 甲级 1078. Hashing (25)
    pat 甲级 1080. Graduate Admission (30)
    pat 甲级 团体天梯 L3-004. 肿瘤诊断
    pat 甲级 1099. Build A Binary Search Tree (30)
    Codeforce 672B. Different is Good
  • 原文地址:https://www.cnblogs.com/chris-cp/p/4149946.html
Copyright © 2011-2022 走看看