1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > mysql:列类型之整数(tinyint smallint mediumint int bigint)

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

时间:2022-10-25 07:08:28

相关推荐

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

环境:

core 3.1mysql 8.0.25DBeaver

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

1. 整数类型

mysql支持5中整数类型,它们占用空间及表示范围如下所示:

一般我们建表时如下:

create table test(t_tinyint tinyint unsigned,t_smallint smallint,t_int int,t_mediumint mediumint,t_bitint bigint)

注意:mysql中虽然提供的语法中还有:tinyint(3)这种形式,但我们完全可以忽略它,因为实在是用不上,就连mysql自己也说了(将来会被移除):

另外,还有 ZEROFILL 也不推荐使用,将来会被移除,所以INT(4) ZEROFILL这种形式的定义不要再使用了。

使用sql测试的代码如下:

create table test(t_tinyint tinyint unsigned,t_smallint smallint,t_mediumint mediumint,t_int int,t_bitint bigint)-- 查看元数据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_bitint)values(1,2,3,4,5);insert into test.test(t_tinyint,t_smallint,t_mediumint,t_int,t_bitint)values(0x01,0x0F02,0x03,0x04,0x0005);select * from test.test t

输出如下:

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_bitint {get; set; }}

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 --查看定义sqlshow create table test.test2

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。