数据库-列的类型-整型数据类型

类型 字节数 有符号范围 无符号范围 描述
TINYINT 1 -128 到 127 0 到 255 非常小的整数
SMALLINT 2 -32768 到 32767 0 到 65535 小的整数
MEDIUMINT 3 -8388608 到 8388607 0 到 16777215 中等大小的整数
INT or INTEGER 4 -2147483648 到 2147483647 0 到 4294967295 标准整数
BIGINT 8 -9223372036854775808 到 9223372036854775807 0 到 18446744073709551615 大整数

TINYINT 演示

# tinyint demo
create Table  tinyintStudy(tinID tinyint );
# tinyint 
create Table  tinyintStudy(tinID tinyint );
# 查看 表结构
desc  tinyintStudy;

# 插入几条数据
insert into tinyintStudy values(0);
insert into tinyintStudy values(127);
insert into tinyintStudy values(-128);

select *from tinyintStudy;
# 尝试插入128
insert into tinyintStudy values(128);
#  ERROR 1264 (22003): Out of range value for column 'tinID' at row 1
select *from tinyintStudy;

mysql> use typeofcolumnintegertypestudy;
Database changed
mysql> create Table  tinyintStudy(tinID tinyint );
Query OK, 0 rows affected (0.13 sec)

mysql> desc  tinyintStudy;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| tinID | tinyint | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql> insert into tinyintStudy values(0);
Query OK, 1 row affected (0.01 sec)
mysql> insert into tinyintStudy values(127);
Query OK, 1 row affected (0.00 sec)

mysql> insert into tinyintStudy values(-128);
Query OK, 1 row affected (0.00 sec)

mysql> select *from tinyintStudy;
+-------+
| tinID |
+-------+
|     0 |
|   127 |
|  -128 |
+-------+
3 rows in set (0.00 sec)
# 尝试插入128
mysql> insert into tinyintStudy values(128);
ERROR 1264 (22003): Out of range value for column 'tinID' at row 1
mysql>select *from tinyintStudy;




整型附带的 属性(n) 演示

n代表填充多少宽度

mysql>  create Table width(id  int(8), widthName varchar(64));
Query OK, 0 rows affected, 1 warning (0.06 sec)
mysql> desc Width;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int         | YES  |     | NULL    |       |
| widthName | varchar(64) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> insert  into  width values(1,'插入1');
Query OK, 1 row affected (0.01 sec)

mysql> insert  into  width values(2,'插入2');
Query OK, 1 row affected (0.01 sec)

mysql> select * from width;
+------+-----------+
| id   | widthName |
+------+-----------+
|    1 | 插入1     |
|    2 | 插入2     |
+------+-----------+
2 rows in set (0.00 sec)

好像看不出来填充 这个要想到达显示一定输出的宽度 需要zeroFill

mysql> create Table FIllwidth(id  int(20) zerofill, widthName varchar(64));
mysql> insert  into  FIllwidth values(1,'插入1');
Query OK, 1 row affected (0.00 sec)
mysql> insert  into  FIllwidth values(2,'插入2');
Query OK, 1 row affected (0.01 sec)
mysql> select * from FIllwidth;
+----------------------+-----------+
| id                   | widthName |
+----------------------+-----------+
| 00000000000000000001 | 插入1     |
| 00000000000000000002 | 插入2     |
+----------------------+-----------+
2 rows in set (0.00 sec)
mysql> insert  into  FIllwidth values(1000,'插入1000');
Query OK, 1 row affected (0.00 sec)

mysql> select * from FIllwidth;
+----------------------+-----------+
| id                   | widthName |
+----------------------+-----------+
| 00000000000000000001 | 插入1     |
| 00000000000000000002 | 插入2     |
| 00000000000000001000 | 插入1000  |
+----------------------+-----------+
3 rows in set (0.00 sec)
mysql> # 试试插入负数 
mysql>  insert  into  FIllwidth values(-1,'插入-1');
ERROR 1264 (22003): Out of range value for column 'id' at row 1

create Table width(id  int(10), widthName varchar(64));

insert  into  width values(1,'插入1');
insert  into  width values(2,'插入2');

select * from width;

# 指定显示宽度 时配合 zeroFill
create Table FIllwidth(id  int(20) zerofill, widthName varchar(64));

insert  into  FIllwidth values(1,'插入1');
insert  into  FIllwidth values(2,'插入2');

insert  into  FIllwidth values(1000,'插入1000');
select * from FIllwidth;
# 负数对于 zerofill 来说 我本来就你填充0的你却当人家放屁不好意思啊,不能插入 
insert  into  FIllwidth values(-1,'插入-1');

显示宽度就是能够存放是多少位数 TINYINT(3)这个三是指 百位 十位以及个位
取决于类型支持多少位
以下具体的类型显示的宽度

数据类型 显示宽度
TINYINT 3
SMALLINT 5
MEDIUMINT 8
INT or INTEGER 11
BIGINT 20

unsigned 演示

假设有一个订单表 表里 数据格式 订单 id 订单客户名字 订单实体 (神秘值)

# 无符号数据
#订单  
create Table XOrder(id  int unsigned,name varchar(64), OrderEntity varchar(128));
desc Xorder;

insert into xorder values(1001,'蔡徐坤真ikun粉丝1','荔枝');
insert into xorder values(1002,'蔡徐坤真ikun粉丝2','拿什么荔枝');

insert into xorder values(1003,'蔡徐坤真ikun粉丝3','食不食油饼');
insert into xorder values(1004,'蔡徐坤真ikun粉丝4','香精煎鱼');

select *from Xorder;

insert into xorder values(-1,'蔡徐坤真ikun粉丝5','鸡你太美');
select *from xorder;


insert into xorder values(1005,'蔡徐坤真ikun粉丝5','鸡你太美');
select *from xorder;
 
mysql> #订单
mysql> create Table XOrder(id  int unsigned,name varchar(64), OrderEntity varchar(128));
Query OK, 0 rows affected (0.04 sec)

mysql> desc Xorder;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | int unsigned | YES  |     | NULL    |       |
| name        | varchar(64)  | YES  |     | NULL    |       |
| OrderEntity | varchar(128) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into xorder values(1001,'蔡徐坤真ikun粉丝1','荔枝');
Query OK, 1 row affected (0.01 sec)

mysql> insert into xorder values(1002,'蔡徐坤真ikun粉丝2','拿什么荔枝');
Query OK, 1 row affected (0.01 sec)

mysql>
mysql> insert into xorder values(1003,'蔡徐坤真ikun粉丝3','食不食油饼');
Query OK, 1 row affected (0.01 sec)

mysql> insert into xorder values(1004,'蔡徐坤真ikun粉丝4','香精煎鱼');
Query OK, 1 row affected (0.00 sec)
mysql> select *from Xorder;
+------+-------------------+-------------+
| id   | name              | OrderEntity |
+------+-------------------+-------------+
| 1001 | 蔡徐坤真ikun粉丝1 | 荔枝        |
| 1002 | 蔡徐坤真ikun粉丝2 | 拿什么荔枝  |
| 1003 | 蔡徐坤真ikun粉丝3 | 食不食油饼  |
| 1004 | 蔡徐坤真ikun粉丝4 | 香精煎鱼    |
+------+-------------------+-------------+
4 rows in set (0.00 sec)
mysql> insert into xorder values(-1,'蔡徐坤真ikun粉丝5','鸡你太美');
ERROR 1264 (22003): Out of range value for column 'id' at row 1
mysql> select *from Xorder;
+------+-------------------+-------------+
| id   | name              | OrderEntity |
+------+-------------------+-------------+
| 1001 | 蔡徐坤真ikun粉丝1 | 荔枝        |
| 1002 | 蔡徐坤真ikun粉丝2 | 拿什么荔枝  |
| 1003 | 蔡徐坤真ikun粉丝3 | 食不食油饼  |
| 1004 | 蔡徐坤真ikun粉丝4 | 香精煎鱼    |
+------+-------------------+-------------+
4 rows in set (0.00 sec)
mysql> insert into xorder values(1005,'蔡徐坤真ikun粉丝5','鸡你太美');
Query OK, 1 row affected (0.01 sec)

mysql> select *from xorder;
+------+-------------------+-------------+
| id   | name              | OrderEntity |
+------+-------------------+-------------+
| 1001 | 蔡徐坤真ikun粉丝1 | 荔枝        |
| 1002 | 蔡徐坤真ikun粉丝2 | 拿什么荔枝  |
| 1003 | 蔡徐坤真ikun粉丝3 | 食不食油饼  |
| 1004 | 蔡徐坤真ikun粉丝4 | 香精煎鱼    |
| 1005 | 蔡徐坤真ikun粉丝5 | 鸡你太美    |
+------+-------------------+-------------+
5 rows in set (0.00 sec)

补充:当使用zerofill时,默认会自动加unsigned属性

auto_increment (自动増涨) 演示

mysql> create Table COrder(Uid int auto_increment ,name varchar(64), OrderEntity varchar(128));
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

错误1075(42000):不正确的表定义;auto列只能有一个,并且必须定义为键

auto_increment 应该定义为 not null 并且同时 使用 primary key 来作为 主键 或者 unique;唯一键 进行定义 但定义其中这两个键 不用写not null 因为这两个都有附有 not null
primary key和unique 定义的列 就不能为空

# 自动增涨
create Table COrder(Uid int primary key auto_increment ,name varchar(64), OrderEntity varchar(128));
desc corder;

insert into corder values(10001,'大伟哥测试原神号','可能是大伟哥原神号');
insert into corder values(10002,'公司测试uid','xxx');
insert into corder values(10003,'公司测试uid','xxx');
#居然自动+1 那么岂不是可以不用写uid?
insert into corder values('公司测试uid','xxx');
# 若想自动增涨 插入表数据时需要指定其他的列名
insert into corder(name,OrderEntity) values('公司测试uid','xxx');
select * from corder;
# 当测试服改为 正式服时一切找个最大的uid作为正式服起始
insert into corder values(100000,'玩家uid','xxx');
insert into corder(name,OrderEntity) values('玩家uid','xxx');
insert into corder(name,OrderEntity) values('玩家uid','xxx');
select * from corder;
#B站也是id  通过空间ID访问ta的个人空间 

#居然大伟哥的号怎么香 那么 插入第二个大伟哥的号 会怎么样 
insert into corder values(10001,'大伟哥测试原神号','可能是大伟哥原神号');
# ERROR 1062 (23000): Duplicate entry '10001' for key 'corder.PRIMARY'
#错误1062(23000):键'命令的重复条目'10001'。主要的

# 自动增涨
mysql> create Table COrder(Uid int auto_increment ,name varchar(64), OrderEntity varchar(128));
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key

mysql> create Table COrder(Uid int primary key auto_increment ,name varchar(64), OrderEntity varchar(128));
Query OK, 0 rows affected (0.04 sec)

mysql> insert into corder values(10001,'大伟哥测试原神号','可能是大伟哥原神号');
Query OK, 1 row affected (0.02 sec)

mysql> insert into corder values(10002,'公司测试uid','xxx');
Query OK, 1 row affected (0.01 sec)

mysql> insert into corder values(10003,'公司测试uid','xxx');
Query OK, 1 row affected (0.00 sec)

mysql> #居然自动+1 那么岂不是可以不用写uid?
mysql> insert into corder values('公司测试uid','xxx');
ERROR 1136 (21S01): Column count doesn t match value count at row 1
mysql> # 若想自动增涨 插入表数据时需要指定其他的列名
mysql> insert into corder(name,OrderEntity) values('公司测试uid','xxx');
Query OK, 1 row affected (0.01 sec)

mysql> select * from corder;
+-------+------------------+--------------------+
| Uid   | name             | OrderEntity        |
+-------+------------------+--------------------+
| 10001 | 大伟哥测试原神号 | 可能是大伟哥原神号 |
| 10002 | 公司测试uid      | xxx                |
| 10003 | 公司测试uid      | xxx                |
| 10004 | 公司测试uid      | xxx                |
+-------+------------------+--------------------+
4 rows in set (0.00 sec)

mysql> # 当测试服改为 正式服时一切找个最大的uid作为正式服起始
mysql> insert into corder values(100000,'玩家uid','xxx');
Query OK, 1 row affected (0.01 sec)

mysql> insert into corder(name,OrderEntity) values('玩家uid','xxx');
Query OK, 1 row affected (0.02 sec)

mysql> insert into corder(name,OrderEntity) values('玩家uid','xxx');
Query OK, 1 row affected (0.01 sec)

mysql> select * from corder;
+--------+------------------+--------------------+
| Uid    | name             | OrderEntity        |
+--------+------------------+--------------------+
|  10001 | 大伟哥测试原神号 | 可能是大伟哥原神号 |
|  10002 | 公司测试uid      | xxx                |
|  10003 | 公司测试uid      | xxx                |
|  10004 | 公司测试uid      | xxx                |
| 100000 | 玩家uid          | xxx                |
| 100001 | 玩家uid          | xxx                |
| 100002 | 玩家uid          | xxx                |
+--------+------------------+--------------------+
7 rows in set (0.00 sec)
mysql> #居然大伟哥的号怎么香 那么 插入第二个大伟哥的号 会怎么样
mysql> insert into corder values(10001,'大伟哥测试原神号','可能是大伟哥原神号');
# ERROR 1062 (23000): Duplicate entry '10001' for key 'corder.PRIMARY'
# 错误1062(23000):键'命令的重复条目'10001'。主要的

auto_increment 只能用于整数 若插入值大于历史记录的值 按照插入的值继续增涨 反之指定插入值小于 列如101 插入最前面
由于必须是主键 或者唯一建 才能使用自增涨 所以插入重复的 会出现错误

你可能感兴趣的:(小森数据库技术总结,数据库,mysql)