阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

关于 MySQL 数据类型

248次阅读
没有评论

共计 1378 个字符,预计需要花费 4 分钟才能阅读完成。

在 MySQL 中常用数据类型主要分为以下几类:数值类型、字符串类型、日期时间类型。

数值类型

字符串类型

日期时间类型

数据类型  字节数  取值范围  格式 备注 
 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。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计1378字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中