oracle如何用number定义字段只能输入整数?

sandy1990927 2013-05-28 05:12:27
number(5,0)这样怎么不行啊,还是可以接收小数
...全文
1781 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fushiqilang 2015-08-25
  • 打赏
  • 举报
回复
number类型的语法很简单:number(p,s): p (precision)精度,s (scale) 小数位 p:is the precision,or the total number of digits. Oracle guarantees the portability of numbers with precision ranging from 1 to 38. s:is the scale, or the number of digits to the right of the decimal point. The scale can range from -84 to 127. 其中的 s 还分为正数和负数 s正数,小数点右边指定位置开始四舍五入 s负数,小数点左边指定位置开始四舍五入 所以 number(2,-0) 表示只能输入两位数的正整数
xiongwenhua365 2013-06-16
  • 打赏
  • 举报
回复
可通过添加check约束来满足你的需求
SQL> create table test(id number check(instr(to_char(id),'.') = 0));
----------------------------- -------测试如下------------ -----------------------------
SQL> insert into test values(5.1);
insert into test values(5.1)
*
ERROR at line 1:
ORA-02290: check constraint (XIONGWENHUA.SYS_C006977) violated


SQL> select * from test;

        ID
----------
         5

SQL> 
forgetsam 2013-06-16
  • 打赏
  • 举报
回复
number(5) 就是 number(5,0)没有任何区别。 number(5) 只保证它存储的是整数,你输入小数自动四舍五入。
sych888 2013-06-09
  • 打赏
  • 举报
回复
内部转换???
zcs_1 2013-05-29
  • 打赏
  • 举报
回复
可以用INT类型。
Wentasy 2013-05-28
  • 打赏
  • 举报
回复
引用 3 楼 u010695254 的回复:
[quote=引用 1 楼 Wentasy 的回复:] 如果你不想接受小数的话,可以这样定义:number(5)。 number(p,s):表示可容纳的数字位数为p位,包括整数部分和小数部分,但不包括小数点。整数部分只能容纳(p-s)位。
SQL> create table test1 (t1 number(5),t2 number(5,2)); 表已创建。 SQL> insert into test1(t1,t2) values(1234.2,123.32); 已创建 1 行。 还是不行啊,输入小数还能创建成功 [/quote] 我再查查。
Wentasy 2013-05-28
  • 打赏
  • 举报
回复

SQL> create table testnum(num1 number(5), num2 number(5,2));

表已创建。

SQL> insert into testnum(num1, num2) values(123.1, 123.12);

已创建 1 行。

SQL> insert into testnum(num1, num2) values(12312, 123.12);

已创建 1 行。

SQL> insert into testnum(num1, num2) values(123121, 123.12);
insert into testnum(num1, num2) values(123121, 123.12)
                                       *
第 1 行出现错误:
ORA-01438: 值大于为此列指定的允许精度


SQL> insert into testnum(num1, num2) values(12312, 123.121);

已创建 1 行。

SQL> insert into testnum(num1, num2) values(12312, 123.1211);

已创建 1 行。

SQL> insert into testnum(num1, num2) values(12312, 1213.1211);
insert into testnum(num1, num2) values(12312, 1213.1211)
                                              *
第 1 行出现错误:
ORA-01438: 值大于为此列指定的允许精度


SQL> insert into testnum(num1, num2) values(12312.1, 1213.1211);
insert into testnum(num1, num2) values(12312.1, 1213.1211)
                                                *
第 1 行出现错误:
ORA-01438: 值大于为此列指定的允许精度
好吧,我也纳闷了。 Oracle在这方面应该有一定的机制。
sandy1990927 2013-05-28
  • 打赏
  • 举报
回复
引用 1 楼 Wentasy 的回复:
如果你不想接受小数的话,可以这样定义:number(5)。 number(p,s):表示可容纳的数字位数为p位,包括整数部分和小数部分,但不包括小数点。整数部分只能容纳(p-s)位。
SQL> create table test1 (t1 number(5),t2 number(5,2)); 表已创建。 SQL> insert into test1(t1,t2) values(1234.2,123.32); 已创建 1 行。 还是不行啊,输入小数还能创建成功
sandy1990927 2013-05-28
  • 打赏
  • 举报
回复
SQL> create table test1 (t1 number(5),t2 number(5,2)); 表已创建。 SQL> insert into test1(t1,t2) values(1234.2,123.32); 已创建 1 行。 还是不行啊,输入小数还能创建成功
Wentasy 2013-05-28
  • 打赏
  • 举报
回复
如果你不想接受小数的话,可以这样定义:number(5)。 number(p,s):表示可容纳的数字位数为p位,包括整数部分和小数部分,但不包括小数点。整数部分只能容纳(p-s)位。

3,499

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧