Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to create CHECK CONSTRAINT based on a query

Soukaina IDRISSIJun 29 2018 — edited Jun 29 2018

Hi everyone,

I created one table having this structure:

CREATE TABLE  "CORRESPONDANCE_FOLDER" (

"PARENT_FLD" VARCHAR2(500) NOT NULL ENABLE,

"CHILD_FLD" VARCHAR2(500) NOT NULL ENABLE,

CONSTRAINT "UC_CORR" UNIQUE ("PARENT_FLD", "CHILD_FLD")

USING INDEX  ENABLE,

CONSTRAINT "PARENT_NEQUAL_TO_CHILD_CHK" CHECK (PARENT_FLD <> CHILD_FLD) ENABLE )

- My need:

Is to create another CHECK CONSTRAINT, which makes to check that CHILD_FLD does not have to appear as much as PARENT_FLD.

For example:

PARENT_FLDCHILD_FLD
AA000AA001
AA000AA002
AA001AA011
AA012AA003

So here, the 'AA001' is already a sub folder of 'AA011; so following the RULE "It can't be a primary folder of any others folders".

My suggestion is to create a function like that :

CREATE FUNCTIONCheck_isChild (id_folder VARCHAR2)

RETURN INT

AS  

DECLARE v_nb_child NUMBER;

BEGIN   

  SELECT  COUNT(*)

   INTO v_nb_child

   FROM CORRESPONDANCE_FOLDER

  WHERE CHILD_FLD = id_folder;

 

RETURN v_nb_child;

END;

GO 

ALTER TABLE CORRESPONDANCE_FOLDER

ADD CONSTRAINT "IS_NOT_CHILD_CHK" CHECK (Check_isChild(?????) == 0 ); -- I dont know what should i put in function parameter

GO

Thank you for help.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 27 2018
Added on Jun 29 2018
18 comments
8,575 views