美文网首页
MySQL-数据类型

MySQL-数据类型

作者: 遇明不散 | 来源:发表于2019-05-27 23:48 被阅读0次

MySQL数据类型

数值类型
整型
  • int 大整型 4个字节 0~2^32-1~-2^32+1(42亿多)
  • tinyint 微小整型 1个字节
    • 有符号(signed默认): -128~127
    • 无符号(unsigned):0~255
    • 常用:age tinyint unsigned
  • smallint 小整型 2个字节 0~65535
  • bigint 极大整型 8个字节 0~2^64-1
浮点型
  • float 4个字节,最多显示7个有效位
    • 用法:字段名 float(m,n), m表示总位数,n表示小数位数
    • 浮点型插入整数时会自动补全小数位数
    • 小数位如果多于指定的位数,会对下一位四舍五入
  • double 8个字节,最多显示15个有效位
    • 用法: 字段名 double(m,n)
  • decimal
    • decimal(M,D),用于保留准确精确度的列
    • M范围为1〜65,D的范围是0~30,MySQL要求D小于或等于P
    • 存储形式:整数、小数分开存储,将9的倍数包装成4个字节
字符类型
  • char 定长 0~255,不给定宽度默认宽度为1
  • varchar 变长 1~65535
    • varchar没有默认宽度,必须给定一个宽度
    • charvarchar使用时都给定宽度,但不能超过各自的范围
  • charvarchar的特点
    • char 浪费存储空间,性能高
    • varchar 节省存储空间,性能低
  • 字符类的宽度和数值类型的宽度的区别
    • 数值类型的宽度为显示宽度,只用于select查询时使用,和占用存储空间大小无关,可用zerofill查看效果
    • 字符类型的宽度超过则无法存储
枚举类型
  • 字段值只能在列举的范围内选择
  • enum 单选,最多有65535个选项 field_name enum(值1,值2,...)
  • set 多选,最多有64个选项 field_name set(值1,值2,...)
create table test(
id int(3) zerofill,
name varchar(15),
sex enum("M","F","Secret"),
likes set("F","M","study","Python")
);
# 插入示例
insert into test(sex,likes) values("Secret","F,study,Python");
日期时间类型
日期基本表示
  • year: 年 YYYY
  • date: 日期 YYYY-MM-DD
  • time: 时间 HH:MM:SS
  • datetime: 日期时间 YYYY-MM-DD HH:MM:SS 默认值返回NULL
  • timestamp: 日期时间 YYYY-MM-DD HH:MM:SS 默认值返回系统当前时间
日期时间函数
  • now() 返回服务器当前时间
  • curdate() 返回当前日期
  • curtime() 返回当前时间
  • year(date) 返回指定时间的年份
  • date(date) 返回指定时间的日期
  • time(date) 返回指定时间的时间
# 查询具体某一天
select * from table_name where date(field_name) = "2018-01-01";
# 查询某年某个月份
select * from table_name 
where date(field_name) >= "2018-03-01" and date(field_name) <= "2018-03-31";
# 查询某一天的某个时间段
select * from table_name 
where date(field_name) = "2018-07-31" and time(field_name)>="10:00:00" and
time(field_name)<="12:00:00"; 
日期时间运算
select * from table_name where field_name 运算符
# 时间间隔单位
1 day | 2 hour | 1 minute | 2 year | 3 month

# 查询1天以内的记录
select * from table_name
where field_name > (now() - interval 1 day);
# 查询1年以前的记录 
select * from table_name
where field_name < (now() - interval 1 year);
# 查询1天以前,3天以内的记录
select * from table_name
where field_name < (now() - interval 1 day) and
field_name > (now() - interval 3 day);

相关文章

  • MYSQL-数据类型优化

    MYSQL-数据类型优化 优化数据类型 MySQL支持的数据类型非常多,选择正确的数据类型对获得高性能至关重要。选...

  • Mysql-数据类型

    title: mysql-数据类型date: 2016-12-28 17:42:45tags:categories...

  • MySQL-数据类型

    ·整数型 Tinyint:迷你整型,使用一个字节存储,表示的状态最多有256种 (常用)Int: 标准整型,使用4...

  • MYSQL-数据类型

    整型 TINYINT SMALLINT MEDIUMINT INT BIGINT 浮点型 FLOAT[(M,...

  • MySQL-数据类型

    MySQL数据类型 数值类型 整型 int 大整型 4个字节 0~2^32-1~-2^32+1(42亿多) ti...

  • MySQL-常用数据类型

    作为一个数据库设计人员,掌握数据库的数据类型是非常有必要的!MySQL支持常用的数据类型:数值类型、日期/时间类型...

  • 08 MySQL-初识MySQL-事务-隔离鉴别

    如果没有特别说明,都是默认autocommit=1 根据我的第三篇03 MySQL-初识MySQL-事务隔离级别提...

  • 05 MySQL-初识MySQL-索引-下

    04 MySQL-初识MySQL-索引-上 篇中介绍了InnoDB索引的数据结构模型以及索引维护。本篇继续针对My...

  • Mysql only_full_group_by以及其他关于sq

    MySQL-"this is incompatible with sql_mode=only_full_group...

  • MySQL-范式

    MySQL-范式 、 MySQL-范式是一种分层结构的规范,分为6层,每一次层都比上一层更加严格范式只为解决空间问...

网友评论

      本文标题:MySQL-数据类型

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