Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Table Relationship Problem, many parent many child
sonesay
post Nov 2 2007, 07:24 AM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 591
Joined: 20-June 07
From: Auckland
Member No.: 45,102



Ok I did poorly in my database class so Im very bad at normalizing forms. I started creating my database and eventually ran into a problem with how I've set it up. Im not 100% sure how I'm going to fix it at this stage but heres the problem.

I have 2 tables MONSTER and ITEMS. Basically its layed out like this.
CODE

MONSTER TABLE
--------------------
m_id int (PK)
m_name varchar
m_level int
...etc


ITEMS TABLE
--------------------
i_id int (PK)
i_name varchar
i_nm int



I'm using them like this. item (Big sword with i_nm = 1) so the big sword drops off the monster of m_id of 1. I think this is what you call a 1 to 1 relationship. Later on I find out that the big sword with i_nm of 1 can also drop off another monster so it is a 1 to many.

I cant figure out how im going to alter my table to address this. I dont want to insert 2 similar items into the ITEMS table with 2 different i_nm ids to link them to each NM.


Go to the top of the page
 
+Quote Post
jlhaslip
post Nov 2 2007, 03:04 PM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 3,744
Joined: 24-July 05
From: In Trouble Again... still?
Member No.: 9,787
Spam Patrol



I think yu need a third table to allow the many-to-one relationship.

CODE
MONSTER TABLE
--------------------
m_id int (PK)
m_name varchar
m_level int
...etc


ITEMS TABLE
--------------------
i_id int (PK)
i_name varchar
i_nm int

Links Table
-------------
l_id int (PK)
m_id int (PK)
i_id int (PK)
Like that...
Go to the top of the page
 
+Quote Post
AiryDragon
post Dec 11 2007, 09:43 AM
Post #3


Newbie
*

Group: Members
Posts: 5
Joined: 11-December 07
Member No.: 54,581



The rails way like that:

CODE
Links Table
----------------
Linked_id
Linked_class
Linking_id
Linking_class


This links table enables polimorphic associations. And you can add 1 more field like relation_type for relation types.
Samples to be clear:
CODE
1- Girl wearing blue shirt
  Linked_id : Girl.id
  Linked_class : Girl.class
  Linking_id : BlueShirt.id
  Linking_class : BlueShirt.class
  Relation_type : wear

2- Cat with red ribbon
  Linked_id : Cat.id
  Linked_class : Cat.class
  Linking_id : RedRibbon.id
  Linking_class : RedRibbon.class
  Relation_type : with

3- Boy holding cat
  Linked_id : Boy.id
  Linked_class : Boy.class
  Linking_id : Cat.id
  Linking_class : Cat.class
  Relation_type : hold
Notice from jlhaslip:
Added code tags to the lists
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. C Code, Can U Solve This?(22)
  2. Did You Face Problem Opening Pdf With Firefox?(14)
  3. Problem Installing Sims 2 Nightlife(22)
  4. Imap Problem(1)
  5. C Problem: "two Or More Data Types In Declaration"(4)
  6. What Do You Do When You Love Two People?(36)
  7. How Do I Move On To Next Relationship?(6)
  8. A Sudden Long Term Relationship Breakup(23)
  9. Dandruff Problem(27)
  10. Problem Creating Email Accounts In Cpanel(13)
  11. What's The Problem With America's Youth?(69)
  12. Rpg Maker 2003 Music Problem(1)
  13. Server Problem?(11)
  14. Selling Bone Marrow, Dire Problem.(7)
  15. Sub Domain Problem : Urgent(10)
  1. Man Arrested For Having Intercourse With A Table?(21)
  2. Heart Problem(0)
  3. Nvidia Display Driver Problem(2)
  4. Can Anyone Help Me With My Graphic Card - 8600 Gt(8)
  5. Frontpage Problem(0)
  6. I Have A Problem With A Question(8)
  7. Connection Problem!(3)
  8. Ip Problem(2)
  9. Gamma Servers' Problem(1)
  10. Problem With Time/date On My Computer(6)
  11. I'm In Love! Now What Do I Do?(5)
  12. Flash Problem(4)
  13. Problem With My Email : [urgent](1)


 



- Lo-Fi Version Time is now: 13th May 2008 - 06:24 PM