http://msdn.microsoft.com/zh-cn/library/ms187745.aspx
int、bigint、smallint 和 tinyint (Transact-SQL)
使用整数数据的精确数字数据类型。
|
当前版本)。 |
值插入到每列中并在 SELECT 语句中返回。
CREATE TABLE dbo.MyTable ( MyBigIntColumn bigint ,MyIntColumn int ,MySmallIntColumn smallint ,MyTinyIntColumn tinyint ); GO INSERT INTO dbo.MyTable VALUES (9223372036854775807, 214483647,32767,255); GO SELECT MyBigIntColumn, MyIntColumn, MySmallIntColumn, MyTinyIntColumn FROM dbo.MyTable;
下面是结果集:
MyBigIntColumn MyIntColumn MySmallIntColumn MyTinyIntColumn -------------------- ----------- ---------------- --------------- 9223372036854775807 214483647 32767 255 (1 row(s) affected)
sd
http://msdn.microsoft.com/zh-cn/library/ms187752.aspx
数据类型 (Transact-SQL)
数据类型是一种属性,用于指定对象可保存的数据的类型:整数数据、字符数据、货币数据、日期和时间数据、二进制字符串等。
用户定义类型从您使用 .NET Framework 支持的编程语言之一创建的类的方法和运算符中获取它们的特征。
当两个具有不同数据类型、排序规则、精度、小数位数或长度的表达式通过运算符进行组合时,结果的特征由以下规则确定:
-
数据类型优先级 (Transact-SQL)。
-
排序规则优先级 (Transact-SQL)。
-
精度、小数位数和长度 (Transact-SQL)。
数据类型同义词 (Transact-SQL)。
地方
http://www.cnblogs.com/MYSQLZOUQI/articles/3837100.html
2.7.2 类型溢出
举例
以MySQL5版本,int类型为例:
#建表
root@localhost(test2)14:46>create table test2 (a int(10) UNSIGNED);
Query OK, 0 rows affected (0.12 sec)
#插入数据
root@localhost(test2)14:56>insert test2 values (10);
Query OK, 1 row affected (0.00 sec)
#模拟更新溢出
root@localhost(test2)14:56>update test2 set a=a-11;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
#查看warnings
root@localhost(test2)14:57>show warnings;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'a' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)
#确定实际得到的值已经溢出
root@localhost(test2)14:57>select * from test2;
+------------+
| a |
+------------+
| 4294967295 |
+------------+
1 row in set (0.00 sec)
#清理数据
root@localhost(test2)14:59>delete from test2;
Query OK, 1 row affected (0.00 sec)
#模拟插入溢出
root@localhost(test2)14:59>insert test2 values (-1);
Query OK, 1 row affected, 1 warning (0.00 sec)
#查看warnings
root@localhost(test2)14:59>show warnings;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1264 | Out of range value for column 'a' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec)
#确定实际得到的值已经溢出
root@localhost(test2)14:59>select * from test2;
+------+
| a |
+------+
| 0 |
+------+
1 row in set (0.00 sec)
原因
int占用4个字节,而int又分为无符号型和有符号性。对于无符号型的范围是0 到 4294967295;有符号型的范围是-2147483648 到 2147483647。
举一反三,其他类型都可能有类似问题,均需要考量。
控制方法
可以通过sql_mode参数控制,但一般建议程序控制,比如:对表单项的值进行校验。