如何在oracle中添加检查约束

时间:2015-06-15 10:00:54

标签: sql oracle constraints check-constraint

我有一张桌子,

create table a(
id_a number(5) not null, 
name varchar2(15) not null, 
address varchar2(30), 
phone varchar2(15), 
constraint pk_a primary key (id_a)
);

我想在手机上添加约束检查。 例。电话号码是08175210868

我想只输入+628175210868

抱歉,我的英语不好。

2 个答案:

答案 0 :(得分:0)

尝试添加行级触发器

CREATE OR REPLACE TRIGGER VALIDATE_PHONE 
BEFORE INSERT OR UPDATE OF PHONE ON A 
REFERENCING OLD AS OLD NEW AS NEW 
FOR EACH ROW 
DECLARE
    ex_phone       EXCEPTION;
BEGIN
  IF :NEW.PHONE not like '+62%' THEN raise ex_phone; END IF;
END;

答案 1 :(得分:-1)

您可以尝试以下

alter table b add constraint phone check (phone like '+62%');