0

I have a table called TEST:

PAR                    CHLD                   
---------------------- ---------------------- 
1                      2                      
1                      3                      
1                      4                      
2                      5                      
3                      6        

The PAR and CHLD column form a composite primary key.

My requirement is that if I'm updating a value in the CHLD column it should only accept any of the existing values in the column.

This should fail because value 7 is not in column CHLD:

UPDATE TEST SET CHLD = 7 WHERE PAR = 3;

This should succeed because value 4 is in column CHLD

UPDATE TEST SET CHLD = 4 WHERE PAR = 3;

Please note I cannot have check constraint for fixed values as I dont know the list of values during table design.

3
  • Hi Prabhu, can you explain your question in details actually i am not getting, mainly what you want as output? Commented Jun 18, 2014 at 10:06
  • In your example you say that CHLD contains 1, 2, 5, and 4, and that an update from 4 to 5 should not be allowed, but an update from 4 to 2 should be allowed. What is different between updating 4 to 5 and updating 4 to 2? Both 5 and 2 are in the list of existing values. Please edit your question and add further explanation of what you're trying to accomplish. Thanks. Commented Jun 18, 2014 at 10:53
  • I've updated my comments. Please check Commented Jun 18, 2014 at 10:58

1 Answer 1

1

Create another table, e.g. CHILDREN, which contains the valid values (2,3,4,5,6). Then add a referential constraint from your TEST table to CHILDREN.

Sign up to request clarification or add additional context in comments.

1 Comment

Hope I have no choice other than having a table that stores list of value and having a referential constraint to that table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.