Search code examples
sqloracle-databaseoracle11gconstraintsalter

Constraint on multiple columns


I have a table that contains 7 columns(f_name, l_name, sex, birth_date, matri_num,dept,sal) and i want to add a constraint to verify that of out the 3 columns(f_name, l_name,birth_date) at least 2 was filled.


Solution

  • ALTER TABLE <your_table>
      ADD CONSTRAINT TWO_FILLED
      CHECK(NVL2(F_NAME, 1, 0) + NVL2(L_NAME, 1, 0) + NVL2(BIRTH_DATE, 1, 0) >= 2);