赞
踩
环境:
参考: 《mysql:11.1.2 Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT》
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
)
注意: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
输出如下:
在c#中它们的使用方式
上面表的模型可定义如下:
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; } }
在mysql中,
SERIAL
是 bigint 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。