Democratic Underground Latest Greatest Lobby Journals Search Options Help Login
Google

Any database designers out there per chance?

Printer-friendly format Printer-friendly format
Printer-friendly format Email this thread to a friend
Printer-friendly format Bookmark this thread
This topic is archived.
Home » Discuss » The DU Lounge Donate to DU
 
Dirty Hippie Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:14 PM
Original message
Any database designers out there per chance?
I need to find the best way to solve a complex relationship between two entities. If anyone thinks they can help I'll post or PM the problem.

I'll be forever in your debt.
Printer Friendly | Permalink |  | Top
BlueEyedSon Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:14 PM
Response to Original message
1. Ok. Shoot.
Printer Friendly | Permalink |  | Top
 
BlueEyedSon Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 02:42 PM
Response to Reply #1
16. Here ya go....
Edited on Tue Nov-09-04 02:53 PM by BlueEyedSon
REL_TAB
------------------------
REL_ID unique key
REL_TYPE required
REL_NAME optional
(other relationship attribs)


HOSP_REL_TAB
------------------------
HOSP_REL_ID unique key
REL_ID foreign key
HOSP_ID foreign key


HOSP_TAB
------------------------
HOSP_ID unique key
(other hospital attribs)


select * from HOSP_TAB
where REL_ID in
(select REL_ID
from HOSP_REL_ID
where HOSP_ID = $1)


Notes:

you can screen on REL_TYPE in the subquery if you need to (otherwise you get all regardless of type)

you may want to exclude the hospital in question in the outer query with a not equal predicate

the HOSP_REL_TAB should have a constraint that the combo of REL_ID and HOSP_ID must be unique

you can reproduce your many-to-many table by a self join on HOSP_REL_TAB.REL_ID

The *secret* is to code a table relating hospitals to their relationship, not directly to each other.
Printer Friendly | Permalink |  | Top
 
sangh0 Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 03:41 PM
Response to Reply #16
20. Ding! We have a winner
That's how it's done.
Printer Friendly | Permalink |  | Top
 
BlueEyedSon Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 03:50 PM
Response to Reply #20
21. more info on my other post
Printer Friendly | Permalink |  | Top
 
molly Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:14 PM
Response to Original message
2. You didn't say what the platforms are?
Printer Friendly | Permalink |  | Top
 
sui generis Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:15 PM
Response to Original message
3. fire away -
what database platform are you using?
Printer Friendly | Permalink |  | Top
 
KurtNYC Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:16 PM
Response to Original message
4. Sounds like a many-to-many
Printer Friendly | Permalink |  | Top
 
LynzM Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:23 PM
Response to Original message
5. Depends what DB you're working in
But go ahead and shoot me a PM (though there are much more qualified people around...) :)
Printer Friendly | Permalink |  | Top
 
Dirty Hippie Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:29 PM
Response to Original message
6. OK
The platform as not yet been determined. Were talking the modeling phase.

Here is the story. We have a table of hospitals. We need to model the fact that hospitals form partnerships with each other. They form partnerships of different types, that is, they may share equipment, or they may share staff, or they may share supplies. Of course, more relationship types may be added in the future.

We have an entity (table) for hospitals I suggested a table for relationship types with a linking table in between. That is fairly straightforward.

The sticky part is the linking table. If hospital A has a relationship with hospital B to share equipment, the ID for A may be in the first field, the ID for B in the second OR they may be reversed.

I was able to write SQL code against a sample database to retrieve all the hospitals a certain hospital has relationship with by using the union operator between two select queries, each checking each field but it seemed kludgey.

I hope I've explained the problem well.
Printer Friendly | Permalink |  | Top
 
bobbobbins Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:42 PM
Response to Reply #6
7. if the IDs are numeric
perhaps you could just code it so the smaller ID is always in the first column and the larger always in the second.
Printer Friendly | Permalink |  | Top
 
bobbobbins Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:45 PM
Response to Reply #6
8. nevermind, that wouldn't solve the problem...
Printer Friendly | Permalink |  | Top
 
Mister K Donating Member (338 posts) Send PM | Profile | Ignore Tue Nov-09-04 12:53 PM
Response to Reply #6
9. Two tables will suffice
Hospital Table
--Hospital ID
--Hospital Specific Data Here


Hospital Sharing Table
--Hospital ID 1
--Hospital ID 2
--Type of Sharing (Equipment, people, etc)

Nice homework problem!

Printer Friendly | Permalink |  | Top
 
Dirty Hippie Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:53 PM
Response to Original message
10. Here is some clarification:
Edited on Tue Nov-09-04 12:54 PM by OhMyGod
Again, the linking table has 3 fields, Hospital1, Hospital2 and RelationshipID.

Here is some sample data:

Hospital1 Hospital2 RelationshipID
A B 3
A C 1
D A 2

So, if I want to find all of the hospitals that have a relationship with hospital A I have to specify A can be in field Hospital1 OR Hospital 2 and the hospital I want to list as haveing a relationship with A can be in either field.

Like I said, I can UNION two SELECTS, I just want to know if I'm overlooking simpler solution and if the underlying design approach is the best.
Printer Friendly | Permalink |  | Top
 
Mister K Donating Member (338 posts) Send PM | Profile | Ignore Tue Nov-09-04 12:55 PM
Response to Reply #10
11. Are you looking for the SQL to select the data?
Printer Friendly | Permalink |  | Top
 
bobbobbins Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 12:57 PM
Response to Reply #10
12. can't you just do
select * from whatever where (hospital1='$x' and hospital2='$y') or (hospital1='$y' and hospital2='$x');
Printer Friendly | Permalink |  | Top
 
Dirty Hippie Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 01:06 PM
Response to Reply #12
13. Maybe I'm confused but
If you look at my post 10

If I want a list of all hospitals that have a relatiuonship with A I want the results to be:


B 3
C 1
D 2


So you see, the hospital in the result can be from either field.
Printer Friendly | Permalink |  | Top
 
Dirty Hippie Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 01:08 PM
Response to Reply #13
14. BTW
in case anyone is wondering, I'm not cheating on homework. I'm actually a teacher.
Printer Friendly | Permalink |  | Top
 
bobbobbins Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 01:11 PM
Response to Reply #13
15. well then wouldn't this work...
select * from whatever where hospital1='A' or hospital2='A';
Printer Friendly | Permalink |  | Top
 
BlueEyedSon Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 03:01 PM
Response to Original message
17. delete
Edited on Tue Nov-09-04 03:01 PM by BlueEyedSon
Printer Friendly | Permalink |  | Top
 
DS1 Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 03:34 PM
Response to Original message
18. What if you were to build two tables for hospitals and a third containing
the linkage. Build a unique column into all three, with the third having that column as a primary key. Use software to write in a guaranteed unique character/integer value *maybe take system time and run it through some randomizer*.

Third table would be

unique key / hosp 1 / hosp 2 / shared goods

Then use something like Select * from hosp 1 where select hosp2 from table3 where unique key in table1.

I dunno, been awhile :7
Printer Friendly | Permalink |  | Top
 
BlueEyedSon Donating Member (1000+ posts) Send PM | Profile | Ignore Tue Nov-09-04 03:39 PM
Response to Original message
19. Try this
Edited on Tue Nov-09-04 03:52 PM by BlueEyedSon
REL_TAB
------------------------
REL_ID unique key
REL_TYPE required
REL_NAME optional
(other relationship attribs)


HOSP_REL_TAB
------------------------
HOSP_REL_ID unique key
REL_ID foreign key
HOSP_ID foreign key


HOSP_TAB
------------------------
HOSP_ID unique key
(other hospital attribs)


select * from HOSP_TAB
where REL_ID in
(select REL_ID
from HOSP_REL_ID
where HOSP_ID = $1)


Notes:

you can screen on REL_TYPE in the subquery if you need to (otherwise you get all regardless of type)

you may want to exclude the hospital in question in the outer query with a not equal predicate

the HOSP_REL_TAB should have a constraint that the combo of REL_ID and HOSP_ID must be unique

you can reproduce your many-to-many table by a self join on HOSP_REL_TAB.REL_ID

The *secret* is to code a table relating hospitals to their relationship, not directly to each other.

on edit:
recursive equijoin works too... what was I thinkin'?

select A.whatever from HOSP_TAB A, REL_TAB B , REL_TAB C
where B.REL_ID = C.REL_ID
and A.HOSP_ID = B.HOSP_ID
and C.HOSP_ID = $1

Once again, if you are looking for a specific group of hospitals (sharing staff or whatever) you will have to have a predicate on the REL_TYPE or REL_NAME (if known).

And no, the platform is not a factor.
Printer Friendly | Permalink |  | Top
 
BlueEyedSon Donating Member (1000+ posts) Send PM | Profile | Ignore Wed Nov-10-04 07:33 AM
Response to Original message
22. kick
Printer Friendly | Permalink |  | Top
 
DU AdBot (1000+ posts) Click to send private message to this author Click to view 
this author's profile Click to add 
this author to your buddy list Click to add 
this author to your Ignore list Sun May 05th 2024, 08:39 PM
Response to Original message
Advertisements [?]
 Top

Home » Discuss » The DU Lounge Donate to DU

Powered by DCForum+ Version 1.1 Copyright 1997-2002 DCScripts.com
Software has been extensively modified by the DU administrators


Important Notices: By participating on this discussion board, visitors agree to abide by the rules outlined on our Rules page. Messages posted on the Democratic Underground Discussion Forums are the opinions of the individuals who post them, and do not necessarily represent the opinions of Democratic Underground, LLC.

Home  |  Discussion Forums  |  Journals |  Store  |  Donate

About DU  |  Contact Us  |  Privacy Policy

Got a message for Democratic Underground? Click here to send us a message.

© 2001 - 2011 Democratic Underground, LLC