Jump to content

setting foreign keys


Chris.P

Recommended Posts

mysql version: 4.1.21

 

I am creating a database using MySQL and need to create a foreign key. From what I have read this is not possible in MySQL?

 

I have browsed through phpMyAdmin and cannot see the option so far. I have not tried doing it through basic MySQL commands as of yet as I don't know how to do it without rebuilding my tables.

 

If it is not possible I presume the only way will be to throw all the fields into one table?

 

Any help much appreciated.  8)

Link to comment
Share on other sites

I can't for the life of me figure out how to edit my post on this forum so heres a reply of a little more info on what I'm trying to do.

 

I have two tables one with user info inside it and one with details about images uploaded, obviously when I user logs in I want to call the user data from the user table and the related user picture from the picture table. Best way I presumed to do this would be to use a foreign key like so.

 

User Table        ImageTable

ID                  ID

Name              Location

DOB                Size

etc... 

 

Obviously ID primary in User table and foreign in Image Table. :-\

 

 

EDIT: Now I see how to edit although the edit button was covered up by that google ad in my first post, how annoying! :-\

Link to comment
Share on other sites

Ok cheers I have added an index to one of the tables, should I have added it to any particular one? And also if I set an index on the id field of one table how does it know its indexing the index of the other table?

 

A little confused I think.  ???

Link to comment
Share on other sites

Right, I'm confused now. I can see how to set an index on a field although I can't see how this helps me to link the tables through a reference id.

 

Like I said in my first post I have two tables like this:

 

User Table        ImageTable

ID                  ID

Name              Location

DOB                Size

 

User table is populated every time somebody signs up through the sign up form, Image table is not. So obviously I want the ID's of both tables to stay the same so I can reference them when the Image table is updated.

 

ImageTable needs to reference the User table so which ID field do I index? And how does Imagetable know to look in UserTable for its ID?

 

I know how I would do this in Oracle with primary and foreign keys although I see MySQL is slightly different and I don't understand this indexing business to well.

 

??? ??? :P 

 

Link to comment
Share on other sites

Just think of indexing the same way as foreign keys are applied in oracle with the difference being that there are no actual cascading rules implemented, unless the innoDB storage engine is used in the table create statement.  Using the index on what would normally be a foreign key in oracle just improves the linking speed of joined tables in mysql.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.