Home » SQL & PL/SQL » SQL & PL/SQL » Constraint to force existence in another table?
Constraint to force existence in another table? [message #21171] Wed, 17 July 2002 02:01 Go to next message
ChiChiWaWa
Messages: 5
Registered: July 2002
Junior Member
I hope I'm in the right forum here, but this is what I want to accomplish, but fail to find a resolve for :

Two tables, Content and Category, both have a Primary Key named ID.
A third table joins a Content to a Category by storing corresponding ID's in a row. A Content can be part of several Categories, hence the reason for the existence of this third table.

However, a Content cannot exist without it being linked to a Category. So, there should be a constraint to check the existence of a corresponding record in the third table, when a record is added to the Content-table.
Using a foreign key, I can enforce a check from the joining table to check the ID's existence in the Content-table as well as the Categories-table, but I cannot ad a foreign key on the Content-table referencing the Content_ID in the joining table, because in that latter table it is not a unique key.

I feel like I'm missing the obvious here, so if any of you could help me out it would greately be appreciated!
Subquery in check-constraint ??? [message #21179 is a reply to message #21171] Wed, 17 July 2002 05:14 Go to previous messageGo to next message
ChiChiWaWa
Messages: 5
Registered: July 2002
Junior Member
If I could do a subquery in a check-constraint, I would be in the clear.
I found a solution to use PL/SQL in a trigger, but I can't seem to do this in a check constraint...

HELP!
Re: Subquery in check-constraint ??? [message #21187 is a reply to message #21179] Wed, 17 July 2002 10:54 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
No, you cannot use a query in a CHECK constraint. The proper way to validate that an entry exists in another table is to use a referential integrity constraint (foreign key).
Re: Subquery in check-constraint ??? [message #21190 is a reply to message #21179] Wed, 17 July 2002 11:31 Go to previous messageGo to next message
ChiChiWaWa
Messages: 5
Registered: July 2002
Junior Member
But for a Foreign Key I need a unique or primary key in the other referenced table. In the current situation, that is impossible.

Basically what I have is an m:n relation between Content and Category. To realise such a relation, a third table has been created that holds the relations, so I can have a 1:m relation between Content and the formentioned table, and a 1:n relation with Category.

However, I need to check that a Content is related to at least one (or more) category.

See the problem?
Re: Subquery in check-constraint ??? [message #21202 is a reply to message #21179] Wed, 17 July 2002 22:19 Go to previous messageGo to next message
ChiChiWaWa
Messages: 5
Registered: July 2002
Junior Member
If the description doesn't match the original question, I guess I tried to explain it wrong the first time ...

This delete trigger you suggest is fine if you want to prevent a content with multiple catogories to lose all of its categories.
What I need first is a check that a Content has AT LEAST one category linked to it in the intersection table.
Re: Subquery in check-constraint ??? [message #21216 is a reply to message #21179] Thu, 18 July 2002 08:59 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
The problem with that is that there are times when that is NOT the case. Where and when would you enforce this?

You can only enforce an 'at least' check during a delete (or an update, if allowed) on the intersection table. This validation cannot be tied to the Content table because no cross-reference rows for a Content can be added until the Content actually exists.
Re: Subquery in check-constraint ??? [message #21237 is a reply to message #21179] Fri, 19 July 2002 04:11 Go to previous messageGo to next message
ChiChiWaWa
Messages: 5
Registered: July 2002
Junior Member
Yeah
That poses a problem I can't seem to work my brain around ...

But in proper database design, is there any other way of solving this m:n-relationship ?
Re: Subquery in check-constraint ??? [message #21381 is a reply to message #21179] Wed, 31 July 2002 00:20 Go to previous message
asha
Messages: 3
Registered: December 2001
Junior Member
asaaaaa
Previous Topic: Problem with dynamic sql for ASP access.
Next Topic: problem with dates
Goto Forum:
  


Current Time: Fri Apr 26 03:51:50 CDT 2024