在 MySQL 中常用数据类型主要分为以下几类: 数值类型, 字符串类型, 日期时间类型.
数值类型
字符串类型
日期时间类型
数据类型 | 字节数 | 取值范围 | 格式 | 备注 & nbsp; |
year | 1 | 1901~2155 | yyyy | 存储年 |
date | 3 | 1000-01-01~9999-12-31 | yyyy-MM-dd | 存储日期值 |
time | 3 | -838:59:59~838:59:59 | HH:mm:ss | 存储时间值 (时分秒) |
datetime | 8 | 1000-01-01 00:00:00~9999-12-31 23:59:59 | yyyy-MM-dd HH:mm:ss | 存储日期 + 时间 |
timestamp | 4 | 1970-01-01 08:00:01~2038-01-19 11:14:07 | yyyy-MM-dd HH:mm:ss | 存储日期 + 时间,可作时间戳 |
- drop table if exists test_time;
- create table test_time(
- year_value year,
- date_value date,
- time_value time,
- datetime_value datetime,
- timestamp_value timestamp
- )engine=innodb default charset=utf8 comment="测试时间表";
- MySQL> insert into test_time values(now(), now(), now(), now(), now());
- MySQL> insert into test_time values(2019,20190910,160628,20190910160636,null);
- create_time datetime default current_timestamp,
- update_time datetime default current_timestamp on update current_timestamp,
- create_time timestamp default current_timestamp comment "创建时间",
- update_time timestamp default current_timestamp on update current_timestamp comment "修改时间",
数据类型 (data_type) 是指系统中所允许的数据的类型.
数据库中的每个列都应该有适当的数据类型, 用于限制或允许该列中存储的数据. 例如, 列中存储的为数字, 则相应的数据类型应该为数值类型.
使用数据类型有助于对数据进行正确排序, 并在优化磁盘使用方面起着重要的作用. 因此, 在创建表时必须为每个列设置正确的数据类型及可能的长度.
MySQL 常见数据类型
在 MySQL 中常见的数据类型如下:
1) 整数类型
包括 TINYINT,SMALLINT,MEDIUMINT,INT,BIGINT, 浮点数类型 FLOAT 和 DOUBLE, 定点数类型 DECIMAL.
2) 日期 / 时间类型
包括 YEAR,TIME,DATE,DATETIME 和 TIMESTAMP.
3) 字符串类型
包括 CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM 和 SET 等.
4) 二进制类型
包括 BIT,BINARY,VARBINARY,TINYBLOB,BLOB,MEDIUMBLOB 和 LONGBLOB.
来源: http://www.linuxidc.com/Linux/2019-09/160658.htm