Design tools allow distinguish between "one TO zero-or-more" and "one TO one-or-more" relationships. I could imagine how to implement "one TO zero-or-more" relation:
(pk) chid_id
(fk) parent_id (required)
(pk) parent_id
How to implement "one TO one-or-more" relationship? How to say that parent requires at least one child? Or is "one-or-more" usually implemented as "zero-or-more" ?
When you need to establish a many-to-many relationship between two or more tables, the simplest way is to use a Junction Table. A Junction table in a database, also referred to as a Bridge table or Associative Table, bridges the tables together by referencing the primary keys of each data table.
There are 3 different types of relations in the database: one-to-one. one-to-many, and. many-to-many.
Example of one-to-many relation in SQL ServerCreate two tables (table 1 and table 2) with their own primary keys. Add a foreign key on a column in table 1 based on the primary key of table 2. This will mean that table 1 can have one or more records related to a single record in table 2.
It is obvious that enforcing constraints such as the one you specify, will require the clients to sometimes issue (and the DBMS engines to accept) something that could be labeled "simultaneous updates", that is, more than one distinct table being updated prior to any constraint checking.
The SQL language (I mean, the standard) offers support for that through CREATE ASSERTION. Alas, no currently existing engine supports this statement.
The only way this can be achieved with currently existing SQL engines, is to defer the constraint checking until after all updates are done (if your engine supports this, of course). Enforcing such constraints in application code or business logic ultimately amounts to not having the constraint at all in the first place, if your database is "shared" and can also be updated by other programs.
Systems do exist that offer support for enforcing your kind of constraint, but they are not SQL sytems.
aioobe's solution is quite original, but keep in mind that you can only do so by duplicating all columns of the "many" (/child) side to the "one" (/parent) side (because otherwise you still have the same problem between two tables, except that the "or-more" part has gone away, but that's not where the problem is). And if you do this, you will then be faced with great difficulties when :
So aioobe's solution, allthough original, is likely to create more new problems than it solves.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With