1

I am creating a table in oracle DB and trying to add constraint so that the numbers allowed in the column are 1,2,3.

CREATE TABLE "TABLE_EXAMPLE"
(
.
.
"PROTOCOL" NUMBER (1,2,3),

....)

CONSTRAINT "CH1"
        CHECK ("PROTOCOL" BETWEEN 1 AND 3),

Am I doing right or any better way?

constantlearner
  • 5,157
  • 7
  • 42
  • 64

2 Answers2

5
CREATE TABLE TABLE_EXAMPLE 
(
 ...
  PROTOCOL NUMBER(1) NOT NULL CONSTRAINT CH1 CHECK (PROTOCOL IN (1,2,3))
 ...
);

BETWEEN 1 AND 3 includes 1.5, 1.6, etc.

And I'd recommend not to use quotes " unless you have special characters in table or column names...

wolφi
  • 8,091
  • 2
  • 35
  • 64
  • whats the difference between NUMBER(1) and NUMBER(0,1) ? – constantlearner Jan 22 '13 at 11:46
  • 1
    The data type `NUMBER` takes two parameters `NUMBER(p,s)`. The first one, `p`, is the precision, the second one, `s` is the scale. There are some examples in the documentation http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm#sthref117 – wolφi Jan 22 '13 at 11:56
2

If you are going to check in the table level Check Constraint is the best way. because if you are inserting larger value then the check constraint ll throw the error.

Dileep
  • 624
  • 3
  • 10
  • 20