Add a constraint to a column on an Oracle table so that only positive values are allowed

dba.stackexchange https://dba.stackexchange.com/questions/262195

  •  26-02-2021
  •  | 
  •  

Question

I'm not allowed to drop the table, or delete the columns. The task is to add a constraint to a column possibly by using an ALTER statement so that it disallows any negative numbers. Is this possible?

I tried the following:

SQL> alter table SPEND_PLAN_DETAIL_VALUE modify (SP_DETAIL_VALUE_ID CHECK (SP_DETAIL_VALUE_ID > 0));

but this doesn't work.

Was it helpful?

Solution

To add a constraint you need to use add constraint:

alter table SPEND_PLAN_DETAIL_VALUE 
  add constraint check_detailvalue CHECK (SP_DETAIL_VALUE_ID > 0);

Online example

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top