最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

mysql数据类型

IT圈 admin 23浏览 0评论

mysql数据类型

mediumint类型说明

类   型:mediumint
长   度:8
占用字节:3字节
有 符 号:-8388608~8388607
无 符 号:0~16777215
不指定长度时的长度为:9(实际长度还是8)id     mediumint(M) [UNSIGNED] [ZEROFILL] 
字段名  字段类型(长度)   [无符号]  [前导填充]unsigned:
01:smallint(M)后面加上unsigned后,就是无符号(mediumint的范围就是0~16777215)
02:smallint(M)后面不加上unsigned,且不加zerofill参数,就是有符号(mediumint的范围就是-8388608~8388607)zerofill:
01:进行前导零填充
02:smallint(M)加上zerofile后,同时也会把unsigned参数也带上(mediumint范围0~16777215)

实践环境准备

-- 创建chenliang库,字符集为utf8
mysql> create database if not exists chenliang character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.01 sec)mysql> show databases like "chenliang";
+----------------------+
| Database (chenliang) |
+----------------------+
| chenliang            |
+----------------------+
row in set (0.00 sec)
-- 进入chenliang库,并查看是否成功进入到了chenliang库 mysql> use chenliang; Database changedmysql> select database(); +------------+ | database() | +------------+ | chenliang | +------------+ row in set (0.00 sec)

测试1【加unsigned参数】

 -- 创建test1测试表(这里指定了UNSIGNED,也就是无符号)

mysql> create table if not exists test1(-> id mediumint(8) unsigned-> );
Query OK, 0 rows affected (0.19 sec)
^==表test1的id字段加了unsigned参数,那么id字段的范围就是0~16777215mysql> desc test1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | mediumint(8) unsigned | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
1 row in set (0.10 sec)
-- 测试01:测试插入范围0~16777215的整数以及超过16777215的整数 mysql> insert into test1 values(0); Query OK, 1 row affected (0.05 sec) ^==插入整数0,正确(没有超过范围0~16777215) mysql> insert into test1 values(16777215); Query OK, 1 row affected (0.00 sec) ^==插入数值1600000,正确(没有超过范围0~16777215) mysql> insert into test1 values(16777216); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入数值16777216,错误(超出范围0~16777215) mysql> select * from test1; +----------+ | id | +----------+ | 0 | | 16777215 | +----------+ 2 rows in set (0.00 sec)
-- 测试02:测试-1~-8388608范围的数据是否能够正常插入 mysql> insert into test1 values(-1); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负数报错(因为建表时,id字段加了unsigned参数,mediumint的范围为0~16777215) mysql> insert into test1 values(-8388608); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负数报错(因为建表时,在id字段加了unsigned参数,mediumint的范围为0~16777215) mysql> insert into test1 values(-8388609); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负数报错(因为建表时,在id字段加了unsigned参数,mediumint的范围为0~16777215) mysql> select * from test1; +----------+ | id | +----------+ | 0 | | 16777215 | +----------+ 2 rows in set (0.00 sec)

测试2【加zerofill参数】

-- 创建test2表,(这里指定了zerofill,也就是前导零填充)
mysql> create table if not exists test2(-> id mediumint(8) zerofill-> );
Query OK, 0 rows affected (0.03 sec)
^==表test2的id字段加了zerofile参数,它会把unsigned参数也带上,那么id字段的范围就是0~16777215mysql> desc test2;
+-------+--------------------------------+------+-----+---------+-------+
| Field | Type                           | Null | Key | Default | Extra |
+-------+--------------------------------+------+-----+---------+-------+
| id    | mediumint(8) unsigned zerofill | YES  |     | NULL    |       |
+-------+--------------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
-- 测试01:测试插入范围0~16777215的整数和超过16777215的整数 mysql> insert into test2 values(0); Query OK, 1 row affected (0.04 sec) ^==插入整数0,在0~16777215范围内,正确 mysql> insert into test2 values(16777215); Query OK, 1 row affected (0.04 sec) ^==插入整数16777215,在0~16777215范围内,正确 mysql> insert into test2 values(16777216); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入整数16777216,不在0~16777215范围内,错误 mysql> select * from test2; +----------+ | id | +----------+ | 00000000 | | 16777215 | +----------+ 2 rows in set (0.00 sec)
-- 测试02:测试-1~-8388608范围的数据是否能够正常插入 mysql> INSERT INTO test2 values(-1); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负整数-1,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以id字段的范围为0~16777215) mysql> insert into test2 values(-8388608); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负整数-8388608,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以id字段的范围为0~16777215,并且有符号的范围也只是-32768~32767) mysql> insert into test2 values(-8388609); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负整数-8388609,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以id字段的范围为0~16777215) mysql> select * from test2; +----------+ | id | +----------+ | 00000000 | | 16777215 | +----------+ 2 rows in set (0.00 sec)

测试3【不加unsigned和zerofill参数】

-- 创建test3表(不加unsigned和zerofill)
mysql> create table if not exists test3(-> id mediumint(8)-> );
Query OK, 0 rows affected (0.05 sec)
^==表test3的id字段没加unsigned和zerofile参数,那么id字段的范围就是-8388608~8388607mysql> desc test3;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | mediumint(8) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)
-- 测试01:测试插入整数0~8388607和超过8388607的整数 mysql> insert into test3 values(0); Query OK, 1 row affected (0.00 sec) <==插入整数0,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(8388607); Query OK, 1 row affected (0.08 sec) <==插入整数8388607,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(8388608); ERROR 1264 (22003): Out of range value for column 'id' at row 1 <==插入整数8388608,错误(不在范围-8388608~8388607范围内) mysql> select * from test3; +---------+ | id | +---------+ | 0 | | 8388607 | +---------+ 2 rows in set (0.00 sec)
-- 测试02:测试插入负数-1~-8388608和小于-8388608的负数 mysql> INSERT INTO test3 values(-1); Query OK, 1 row affected (0.10 sec) <==插入负整数-1,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(-8388608); Query OK, 1 row affected (0.04 sec) <==插入负整数-8388608,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(-8388609); ERROR 1264 (22003): Out of range value for column 'id' at row 1 <==插入负整数-8388609,错误(不在范围-8388608~8388607范围内) mysql> select * from test3; +----------+ | id | +----------+ | 0 | | 8388607 | | -1 | | -8388608 | +----------+ 4 rows in set (0.00 sec)

总结

格式:

id       mediumintM)      [UNSIGNED]  [ZEROFILL]

字段名    字段类型(长度)      [无符号]    [前导填充]

unsigned

01:mediumint(M)后面加上unsigned后,就是无符号(mediumnt的范围就是0~16777215)

02:mediumint(M)后面不加上unsigned,并且不加zerofill参数,就是有符号(mediumint的范围就是-8388608~8388607)

zerofill

01:进行前导零填充(插入数值1,字段显示的是00000001,因为mediumint的长度为8)

02:mediumint(M)加上zerofile后,同时也会把unsigned参数也带上(mediumint的范围为0~16777215)

转载于:.html

mysql数据类型

mediumint类型说明

类   型:mediumint
长   度:8
占用字节:3字节
有 符 号:-8388608~8388607
无 符 号:0~16777215
不指定长度时的长度为:9(实际长度还是8)id     mediumint(M) [UNSIGNED] [ZEROFILL] 
字段名  字段类型(长度)   [无符号]  [前导填充]unsigned:
01:smallint(M)后面加上unsigned后,就是无符号(mediumint的范围就是0~16777215)
02:smallint(M)后面不加上unsigned,且不加zerofill参数,就是有符号(mediumint的范围就是-8388608~8388607)zerofill:
01:进行前导零填充
02:smallint(M)加上zerofile后,同时也会把unsigned参数也带上(mediumint范围0~16777215)

实践环境准备

-- 创建chenliang库,字符集为utf8
mysql> create database if not exists chenliang character set utf8 collate utf8_general_ci;
Query OK, 1 row affected (0.01 sec)mysql> show databases like "chenliang";
+----------------------+
| Database (chenliang) |
+----------------------+
| chenliang            |
+----------------------+
row in set (0.00 sec)
-- 进入chenliang库,并查看是否成功进入到了chenliang库 mysql> use chenliang; Database changedmysql> select database(); +------------+ | database() | +------------+ | chenliang | +------------+ row in set (0.00 sec)

测试1【加unsigned参数】

 -- 创建test1测试表(这里指定了UNSIGNED,也就是无符号)

mysql> create table if not exists test1(-> id mediumint(8) unsigned-> );
Query OK, 0 rows affected (0.19 sec)
^==表test1的id字段加了unsigned参数,那么id字段的范围就是0~16777215mysql> desc test1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | mediumint(8) unsigned | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
1 row in set (0.10 sec)
-- 测试01:测试插入范围0~16777215的整数以及超过16777215的整数 mysql> insert into test1 values(0); Query OK, 1 row affected (0.05 sec) ^==插入整数0,正确(没有超过范围0~16777215) mysql> insert into test1 values(16777215); Query OK, 1 row affected (0.00 sec) ^==插入数值1600000,正确(没有超过范围0~16777215) mysql> insert into test1 values(16777216); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入数值16777216,错误(超出范围0~16777215) mysql> select * from test1; +----------+ | id | +----------+ | 0 | | 16777215 | +----------+ 2 rows in set (0.00 sec)
-- 测试02:测试-1~-8388608范围的数据是否能够正常插入 mysql> insert into test1 values(-1); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负数报错(因为建表时,id字段加了unsigned参数,mediumint的范围为0~16777215) mysql> insert into test1 values(-8388608); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负数报错(因为建表时,在id字段加了unsigned参数,mediumint的范围为0~16777215) mysql> insert into test1 values(-8388609); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负数报错(因为建表时,在id字段加了unsigned参数,mediumint的范围为0~16777215) mysql> select * from test1; +----------+ | id | +----------+ | 0 | | 16777215 | +----------+ 2 rows in set (0.00 sec)

测试2【加zerofill参数】

-- 创建test2表,(这里指定了zerofill,也就是前导零填充)
mysql> create table if not exists test2(-> id mediumint(8) zerofill-> );
Query OK, 0 rows affected (0.03 sec)
^==表test2的id字段加了zerofile参数,它会把unsigned参数也带上,那么id字段的范围就是0~16777215mysql> desc test2;
+-------+--------------------------------+------+-----+---------+-------+
| Field | Type                           | Null | Key | Default | Extra |
+-------+--------------------------------+------+-----+---------+-------+
| id    | mediumint(8) unsigned zerofill | YES  |     | NULL    |       |
+-------+--------------------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
-- 测试01:测试插入范围0~16777215的整数和超过16777215的整数 mysql> insert into test2 values(0); Query OK, 1 row affected (0.04 sec) ^==插入整数0,在0~16777215范围内,正确 mysql> insert into test2 values(16777215); Query OK, 1 row affected (0.04 sec) ^==插入整数16777215,在0~16777215范围内,正确 mysql> insert into test2 values(16777216); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入整数16777216,不在0~16777215范围内,错误 mysql> select * from test2; +----------+ | id | +----------+ | 00000000 | | 16777215 | +----------+ 2 rows in set (0.00 sec)
-- 测试02:测试-1~-8388608范围的数据是否能够正常插入 mysql> INSERT INTO test2 values(-1); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负整数-1,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以id字段的范围为0~16777215) mysql> insert into test2 values(-8388608); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负整数-8388608,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以id字段的范围为0~16777215,并且有符号的范围也只是-32768~32767) mysql> insert into test2 values(-8388609); ERROR 1264 (22003): Out of range value for column 'id' at row 1 ^==插入负整数-8388609,错误(因为在id字段加了zerofill参数,它会把unsigned也带上,所以id字段的范围为0~16777215) mysql> select * from test2; +----------+ | id | +----------+ | 00000000 | | 16777215 | +----------+ 2 rows in set (0.00 sec)

测试3【不加unsigned和zerofill参数】

-- 创建test3表(不加unsigned和zerofill)
mysql> create table if not exists test3(-> id mediumint(8)-> );
Query OK, 0 rows affected (0.05 sec)
^==表test3的id字段没加unsigned和zerofile参数,那么id字段的范围就是-8388608~8388607mysql> desc test3;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | mediumint(8) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)
-- 测试01:测试插入整数0~8388607和超过8388607的整数 mysql> insert into test3 values(0); Query OK, 1 row affected (0.00 sec) <==插入整数0,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(8388607); Query OK, 1 row affected (0.08 sec) <==插入整数8388607,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(8388608); ERROR 1264 (22003): Out of range value for column 'id' at row 1 <==插入整数8388608,错误(不在范围-8388608~8388607范围内) mysql> select * from test3; +---------+ | id | +---------+ | 0 | | 8388607 | +---------+ 2 rows in set (0.00 sec)
-- 测试02:测试插入负数-1~-8388608和小于-8388608的负数 mysql> INSERT INTO test3 values(-1); Query OK, 1 row affected (0.10 sec) <==插入负整数-1,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(-8388608); Query OK, 1 row affected (0.04 sec) <==插入负整数-8388608,正确(在范围-8388608~8388607范围内) mysql> insert into test3 values(-8388609); ERROR 1264 (22003): Out of range value for column 'id' at row 1 <==插入负整数-8388609,错误(不在范围-8388608~8388607范围内) mysql> select * from test3; +----------+ | id | +----------+ | 0 | | 8388607 | | -1 | | -8388608 | +----------+ 4 rows in set (0.00 sec)

总结

格式:

id       mediumintM)      [UNSIGNED]  [ZEROFILL]

字段名    字段类型(长度)      [无符号]    [前导填充]

unsigned

01:mediumint(M)后面加上unsigned后,就是无符号(mediumnt的范围就是0~16777215)

02:mediumint(M)后面不加上unsigned,并且不加zerofill参数,就是有符号(mediumint的范围就是-8388608~8388607)

zerofill

01:进行前导零填充(插入数值1,字段显示的是00000001,因为mediumint的长度为8)

02:mediumint(M)加上zerofile后,同时也会把unsigned参数也带上(mediumint的范围为0~16777215)

转载于:.html

发布评论

评论列表 (0)

  1. 暂无评论