You can add the following check constraint:
... check (length(n||'')=11)
This check constraint is sufficiently simple. And you don't have to change the data type from number
to varchar2
, and don't have to check the input contains character other than digits.
I created simple table and checked whether it works properly:
create table t (n number check (length(n||'')=11));
insert into t values (1234567890); -- 10 digits
=> ORA-02290: check constraint (USER_4_552B1.SYS_C001297489) violated : insert into t values (1234567890)
insert into t values (12345678901);
=> Record Count: 0; Execution Time: 1ms
insert into t values (123456789012); -- 12 digits
=> ORA-02290: check constraint (USER_4_552B1.SYS_C001297489) violated : insert into t values (123456789012)
Another way is to use log
of base 10: (looks more complicated)
... check (trunc(log(10,phone_no)) = 10)
If the given number has 11 digits, its log
value would be 10.xxxx.