0

a simple table:

ID, NAME, POST_ID, GROUP_ID 

either POST_ID or GROUP_ID must be set, but never both of them, NEITHER none of them. So,

there are valid cases:

ID, NAME, POST_ID, GROUP_ID x, y, 1, NULL x, y, NULL, 4 

and NOT VALID cases:

ID, NAME, POST_ID, GROUP_ID x, y, NULL, NULL x, y, 4, 4 

is it possible to set such complicated restriction rule?

1 Answer 1

1

I don't think it's possible. I would catch such cases in the front-end where the user enters the data. Also, you can perhaps replace both fields with two other fields:

A category field of type enum with possible values 'post' or 'group' A reference field containing the ID.

That way you can easily demand both to be non-empty and you can only reference a post ID or a group ID.

1
  • And then you can't define an FK. That's why this should be on the DBA site. Commented Aug 30, 2018 at 15:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.