2 and only 2 characters for oracle table

I am trying to create a table in oracle that will accept 2 and only 2 characters. I tried using char (2), but if I put 1 character in the insert statement, it will accept it. How to make oracle only accept any insertion of two exact characters and reject characters 1 and 3 and above? I searched all over the Internet and did not seem to find an answer to this question.

Thanks! Christopher

+4
source share
2 answers

You can create a CHECK constraint that applies this constraint

SQL> create table foo ( 2 col1 varchar2(2) NOT NULL 3 ,check( length(col1) = 2 ) 4 ); Table created. SQL> insert into foo values( 'ab' ); 1 row created. SQL> ed Wrote file afiedt.buf 1* insert into foo values( 'a' ) SQL> / insert into foo values( 'a' ) * ERROR at line 1: ORA-02290: check constraint (SCOTT.SYS_C0022134) violated 
+11
source

You can use the pre-insert trigger to check the length - I can't think of a way to prevent a single character.

0
source

All Articles