zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql数据类型tinyint_公司的类型及区别是什么

mysql公司 什么 类型 区别 数据类型 tinyint
2023-06-13 09:14:46 时间

大家好,又见面了,我是你们的朋友全栈君。

MySQL支持多种数据类型,大致可以分为三类:数值、日期/时间和字符串(字符)类型。其中, 整数类型包括:tinyint、smallint、mediumint、int和bigint。

其中,tinyint的大小为1字节,即8位二进制。在无符号的情况下,值得范围(0,255)。在有符号的情况下,值得范围(-128,127)。本文将通过测试验证tinyint值的范围。

1.有符号

1.1建表

创建表person,包含name 和score两列。其中score的类型是Tinyint,默认为有符号。

create table person (
  name varchar(20),
  score tinyint
);

1.2插入数据

mysql> insert into person values('April',128);
ERROR 1264 (22003): Out of range value for column 'score' at row 1
mysql> insert into person values('April',127);
Query OK, 1 row affected (0.00 sec)

插入128时报错,原因是值越界。插入127时成功。这验证了tinyint在有符号的情况下,上界是127。

mysql> insert into person values('April',-129);
ERROR 1264 (22003): Out of range value for column 'score' at row 1
mysql> insert into person values('April',-128);
Query OK, 1 row affected (0.00 sec)

插入-129时报错,原因是值越界。插入-128时成功。这验证了tinyint在有符号的情况下,下界是-128。

1.3 查询数据

select * from person;

2.无符号

2.1建表

创建表person,包含name 和score两列。其中score的类型是Tinyint unsigned 。

create table person (
  name varchar(20),
  score tinyint unsigned
);

2.2插入数据

mysql> insert into person values('April',256);
ERROR 1264 (22003): Out of range value for column 'score' at row 1
mysql> insert into person values('April',255);
Query OK, 1 row affected (0.00 sec)

插入256时报错,原因是值越界。插入255时成功。这验证了tinyint在无符号的情况下,上界是255。

mysql> insert into person values('April',-1);
ERROR 1264 (22003): Out of range value for column 'score' at row 1
mysql> insert into person values('April',0);
Query OK, 1 row affected (0.00 sec)

插入-1时报错,原因是值越界。插入0时成功。这验证了tinyint在无符号的情况下,下界是0。

2.3查询数据

select * from person;

综上,tinyint在无符号的情况下,值得范围(0,255)。在有符号的情况下,值得范围(-128,127)。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191142.html原文链接:https://javaforall.cn