验证列应仅允许整数/正/负值

时间:2016-06-29 06:26:42

标签: sql oracle plsql

我有一列数量NUMBER(12)。我需要检查此列不应该允许任何小数,并且0的值为允许为负和正值。

为此,我写了一个查询

select case when trim(TRANSLATE('quantity','+-123456789', ' ')) is null
            then 'T'
            else 'F'
       end
from dual

quantity like 10/100/1000 etc

时,上述查询无效

请帮我修改查询以允许10或100或等等但不允许+ -0。

2 个答案:

答案 0 :(得分:0)

如果您正在使用MYSQL数据库,那么您只需更改表格并将其设为signed int即可。像这样:

ALTER TABLE 'myTable' MODIFY 'quantity' INT SIGNED 

在PLSQL中:

alter table
   myTable
modify
   quantity INT;

alter table myTable
    modify quantity INT,
         constraint ckValue check (quantity <> 0);

答案 1 :(得分:0)

更新:更改案例陈述以处理0。 注意:-00+0相同,仅作为0存储在数据库中。

case when trim(TRANSLATE(quantity,'-1234567890', ' ')) is null 
and quantity<>0

示例示例

with tbl (quantity) as
(select 1.1 from dual union all
select 10 from dual union all
select 100 from dual union all
select 1000 from dual union all
select -44 from dual union all
select  0 from dual union all
select -0 from dual union all
select +0 from dual)
select quantity,case when trim(TRANSLATE(quantity,'-1234567890', ' ')) is null 
and quantity<>0
            then 'T'
            else 'F'
       end as is_int
from tbl

输出

+----------+--------+
| Quantity | IS_INT |
+----------+--------+
|      1.1 | F      |
|       10 | T      |
|      100 | T      |
|     1000 | T      |
|      -44 | T      |
|        0 | F      |
|        0 | F      |
|        0 | F      |
+----------+--------+
相关问题