美文网首页
类型测试===整数类型测试:tinyint,int

类型测试===整数类型测试:tinyint,int

作者: name_cc2f | 来源:发表于2019-01-15 19:20 被阅读0次

    作用:用于存储用户的年龄、游戏的Level、经验值等。

    一、LAB1:(int,tinyint的最大值)

    {(前言)TINYINT有符号型最大127

                      INT有符号型最大2147483647

    1、创建一个表

    示例

    LAB1:(int,tinyint的最大值)

    示例:mysql> create table test1(

        tinyint_test tinyint,

      int_test int

      );        (注意TINYINT类型)     

    2、查询表结构

    示例:

    mysql> desc test1;

    +--------------+------------+------+-----+---------+-------+

    | Field        | Type      | Null | Key | Default | Extra |

    +--------------+------------+------+-----+---------+-------+

    | tinyint_test | tinyint(4) | YES  |    | NULL    |      |

    | int_test    | int(11)    | YES  |    | NULL    |      |

    +--------------+------------+------+-----+---------+-------+

    2 rows in set (0.01 sec)

    (Field字段名称(列名)

    TYPE字段类型(字?数?日?)

    NULL KEY DEFAULT EXTRA 略)

    3、插入数值

    示例:

    1)插入合法数值

    mysql> insert into test1 values (111,111);

    Query OK, 1 row affected (0.09 sec)

    查询表内容:

    select * from test1

    2)插入非法数值

    mysql> insert into test1(tinyint_test) values(128);

    ERROR 1264 (22003): Out of range value for column 'tinyint_test' at row 1

    (TINYINT有符号型最大127)

    3、插入合法数值

    示例:

    mysql> insert into test1(int_test) values(2147483647);

    Query OK, 1 row affected (0.05 sec)

    4、插入非法数值

    示例:

    mysql> insert into test1(int_test) values(2147483648);

    ERROR 1264 (22003): Out of range value for column 'int_test' at row 1

    (INT有符号型最大2147483647)

    二、LAB2:(无符号unsigned)

    {(前言)

    数值无符号,就只能输入正值,不能输入负值

    1、创建一个表

    示例:

    mysql> create table test2(

    tinyint_test tinyint unsigned,      

    int_test int unsigned

    );

    Query OK, 0 rows affected (0.00 sec)

    (  //约束条件unsigned限定只能存正值(无符号))

    2、查询表结构

    示例:

    mysql> desc test2;

    +--------------+---------------------+------+-----+---------+-------+

    | Field        | Type                | Null | Key | Default | Extra |

    +--------------+---------------------+------+-----+---------+-------+

    | tinyint_test | tinyint(3) unsigned | YES  |    | NULL    |      |

    | int_test    | int(10) unsigned    | YES  |    | NULL    |      |

    +--------------+---------------------+------+-----+---------+-------+

    2 rows in set (0.00 sec)

    3、插入数据

    示例:

    插入合法数据1

    mysql> insert into test2(tinyint_test) values(255);

    Query OK, 1 row affected (0.06 sec)

    插入合法数据2

    mysql> insert into test2(int_test) values(2147483648);

    Query OK, 1 row affected (1.87 sec)

    插入非法数据

    mysql> insert into test2 values(-20,-20);

    ERROR 1264 (22003): Out of range value for column 'tinyint_test' at row 1

    (mysql和mariadb不同。

    mysql提示输入错误

    mariadb会输入0到表中。

    但结果是肯定的,无符号只能输入正值)

    三、LAB3:(整数型,长度可变)

    {(前言)

    插入大于INT宽度限制的值,仍然可以存储。

    但不能超过上限2147483647

    INT整形的宽度仅为显示宽度,不是限制。因此建议整形无须指定宽度。

    字符型需要使用宽度

    1、创建一个表

    示例:

    mysql> create table t1 (

    id1 int,

    id2 int(6)

    );

    2、查询表结构

    示例:

    mysql> desc t1;

    +-------+---------+------+-----+---------+-------+

    | Field | Type    | Null | Key | Default | Extra |

    +-------+---------+------+-----+---------+-------+

    | id1  | int(11) | YES  |    | NULL    |      |

    | id2  | int(6)  | YES  |    | NULL    |      |

    +-------+---------+------+-----+---------+-------+

    3、插入数据

    示例:

    mysql> insert into t1 values(22,22222222);

    Query OK, 1 row affected (0.01 sec)

    4、查询数据

    示例:

    mysql> select * from t1;

    (插入大于INT宽度限制的值,仍然可以存储。

    但不能超过上限2147483647)

    四、LAB4:(零填充zerofill)

    {(前言) zerofill 自动填充0}

    1、创建一个表

    示例:

    mysql> create table t2 (

    id1 int zerofill,

    id2 int(6) zerofill

    );

    Query OK, 0 rows affected (0.05 sec)

    (zerofill)

    2、查询表结构

    示例:

    mysql> desc t2;

    +-------+---------------------------+------+-----+---------+-------+

    | Field | Type                      | Null | Key | Default | Extra |

    +-------+---------------------------+------+-----+---------+-------+

    | id1  | int(10) unsigned zerofill | YES  |    | NULL    |      |

    | id2  | int(6) unsigned zerofill  | YES  |    | NULL    |      |

    +-------+---------------------------+------+-----+---------+-------+

    2 rows in set (0.00 sec)

    3、插入数据

    示例:

    mysql> insert into t2 values(2,2);

    Query OK, 1 row affected (0.01 sec)

    4、查询表内容

    mysql> select * from t2;

    +------------+--------+

    | id1        | id2    |

    +------------+--------+

    | 0000000002 | 000002 |

    +------------+--------+

    1 row in set (0.00 sec)

    相关文章

      网友评论

          本文标题:类型测试===整数类型测试:tinyint,int

          本文链接:https://www.haomeiwen.com/subject/bnxudqtx.html