当前位置:   article > 正文

mysql:列类型之整数(tinyint、smallint、mediumint、int、bigint)_mysql smallint

mysql smallint

环境:

  • window10
  • vs2022
  • .net core 3.1
  • mysql 8.0.25
  • DBeaver

参考: 《mysql:11.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT》

1. 整数类型

mysql支持5中整数类型,它们占用空间及表示范围如下所示:
在这里插入图片描述
一般我们建表时如下:

create table test(
	t_tinyint tinyint,
	t_smallint smallint,
	t_int int,
	t_mediumint mediumint,
	t_bigint bigint,
	
	t_tinyint_unsigned tinyint unsigned,
	t_smallint_unsigned smallint unsigned,
	t_mediumint_unsigned mediumint unsigned,
	t_int_unsigned int unsigned,
	t_bigint_unsigned bigint unsigned
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

注意:mysql中虽然提供的语法中还有:tinyint(3)这种形式,但我们完全可以忽略它,因为实在是用不上,就连mysql自己也说了(将来会被移除):
在这里插入图片描述
另外,还有 ZEROFILL 也不推荐使用,将来会被移除,所以INT(4) ZEROFILL这种形式的定义不要再使用了。

使用sql测试的代码如下:

create table test(
	t_tinyint tinyint,
	t_smallint smallint,
	t_mediumint mediumint,
	t_int int,
	t_bigint bigint,
	
	t_tinyint_unsigned tinyint unsigned,
	t_smallint_unsigned smallint unsigned,
	t_mediumint_unsigned mediumint unsigned,
	t_int_unsigned int unsigned,
	t_bigint_unsigned bigint unsigned
)
-- 查看元数据
select c.TABLE_SCHEMA ,c.TABLE_NAME ,c.COLUMN_NAME ,c.ORDINAL_POSITION,c.DATA_TYPE,c.NUMERIC_PRECISION ,c.COLUMN_TYPE 
from information_schema.`COLUMNS` c  where TABLE_SCHEMA ='test' and TABLE_NAME ='test' order by ORDINAL_POSITION 

-- 插入数据
insert into test.test(t_tinyint,t_smallint,t_mediumint,t_int,t_bigint,t_tinyint_unsigned,t_smallint_unsigned,t_mediumint_unsigned,t_int_unsigned,t_bigint_unsigned)	
values (1,2,3,4,5,1,2,3,4,5),(-1,-2,-3,-4,-5,1,2,3,4,5);

select * from test.test t 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

输出如下:
在这里插入图片描述

在这里插入图片描述

2. c#中的使用方式

在c#中它们的使用方式

  • tinyint
    占用1个byte,和c#中的byte、sbyte正好一一对应。
  • smallint
    占用2个byte,和c#中的short、ushort正好一一对应。
  • mediumint
    占用3个byte,在c#中没有类型与之一一对象,可以使用int、uint代替。
  • int
    占用4个byte,和c#中的int、uint正好一一对应。
  • bigint
    占用8个byte,和c#中的long、ulong正好一一对应

上面表的模型可定义如下:

public class Model
{
    /// <summary>
    /// column: tinyint
    /// </summary>
    public sbyte? t_tinyint { get; set; }
    /// <summary>
    /// column: smallint
    /// </summary>
    public short? t_smallint { get; set; }
    /// <summary>
    /// column: mediumint
    /// </summary>
    public int? t_mediumint { get; set; }
    /// <summary>
    /// column: int
    /// </summary>
    public int? t_int { get; set; }
    /// <summary>
    /// column: bigint
    /// </summary>
    public long? t_bigint { get; set; }

    /// <summary>
    /// column: tinyint unsigned
    /// </summary>
    public byte? t_tinyint_unsigned { get; set; }
    /// <summary>
    /// column: smallint unsigned
    /// </summary>
    public ushort? t_smallint_unsigned { get; set; }
    /// <summary>
    /// column: mediumint unsigned
    /// </summary>
    public uint? t_mediumint_unsigned { get; set; }
    /// <summary>
    /// column: int unsigned
    /// </summary>
    public uint? t_int_unsigned { get; set; }
    /// <summary>
    /// column: bigint unsigned
    /// </summary>
    public ulong? t_bigint_unsigned { get; set; }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

3. 其他

在mysql中,

  • SERIALbigint unsigned NOT NULL AUTO_INCREMENT的别名;
  • bool是tinyint的同义词,0被认为是false,其他值认为是true;
  • INTEGER是int的同义词;
    可以使用create table test2( t_serial SERIAL),执行后,mysql会自动将其识别为:CREATE TABLE test2 ( t_serial bigint unsigned NOT NULL auto_increment unique)

如下:

create table test2(	t_serial SERIAL,t_bool bool,t_integer integer)

-- 查看元数据
select c.TABLE_SCHEMA ,c.TABLE_NAME ,c.COLUMN_NAME ,c.ORDINAL_POSITION,c.DATA_TYPE,c.NUMERIC_PRECISION ,c.COLUMN_TYPE 
from information_schema.`COLUMNS` c  where TABLE_SCHEMA ='test' and TABLE_NAME ='test2' order by ORDINAL_POSITION 

--查看定义sql
show create table test.test2
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/838249
推荐阅读
相关标签
  

闽ICP备14008679号