I want to add a check constraint that will make sure no one can enter more than one spouse in family table. I am trying to use a custom defined function for the same. I am doing some thing like this:
ALTER TABLE PMT_TRN_FAMILY
ADD CONSTRAINT CK_SPOUSE
CHECK (GETSPOUSE(M_CODE) = 'True');
Definition of Function GETSPOUSE is here:
CREATE OR REPLACE FUNCTION GETSPOUSE (
P_M_CODE IN VARCHAR2
)
RETURN VARCHAR
IS Output VARCHAR2(5);
S_CNT NUMBER(2,0);
BEGIN
SELECT COUNT(1) INTO S_CNT FROM PMT_TRN_FAMILY WHERE M_CODE = P_M_CODE AND RELATIONS='Spouse';
IF S_CNT > 0 THEN
return ('False');
END IF;
return ('True');
END;
Here M_code is the code of a candidate and Relations is column which stores type of relations.
Here I got to know that we cannot use user defined functions in check constraint, so is there any other way I can accomplish this in oracle?
Thanks in advance.