• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

FK relationship in MySQL in WorkBench IDE

i'm having trouble creating a one to many foreign key relationship between two InnoDB tables that I have created in my MySQL db using the workbench IDE. I'm using this in conjuction with vs 2010 and the entity framework. so i'm looking for some visual aids (tutorials preferably) that address this and show me the proper way to achieve this. Any suggestions and or suggested resources would be appreciated.
0
Michael Sterling
Asked:
Michael Sterling
  • 2
  • 2
1 Solution
 
dqmqCommented:
You implement a one-many relationship simply by including a key of the parent table (the one) in the child table (the many).  Usually, it is the primary key, but any unique key will do.

If you want the database to enforce the integrity of that relationship, then you use a foreign key constraint.  The foreign key constraint is a "rule" applied to the child table that guarantees the value of the foreign key will be present in the referenced unique key of the parent table.

alter table CHILDTABLE add constraint CONSTRAINTNAME foreign key (COL1, COL2...) references PARENTTABLE (COL1, COL2)

I haven't used MySQL IDE, but most development enviornments provide a graphical way to create the necessary keys and constraints.  Someone else will need to assist you with that.  
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
thank you. as a sidenote, good, bad or indifferent, can you have a primary key that is a character string?
0
 
dqmqCommented:
Absolutely!  But if it's longer than 16 bytes, I'd reconsider (for optimization reasons).
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
thanks for your help....
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now