how to add check constraint in oracle

1k views Asked by At

I have a table,

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)
);

I want add constraint check at phone. example. phone is 08175210868

I want to only be input with +628175210868

sorry my bad english.

2

There are 2 answers

0
Siby On

You can try the below

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

Try to add a row-level trigger

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;