If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4

Thread: Foreign Keys

  1. #1
    Senior Member
    Join Date
    Aug 2006
    Member #
    Hey all... I have a decent understanding of PHP and mySQL. I can write apps that do the basic such as updating, deleting, adding new records and support paging. What I have no clue how to do is link tables. Using foreign keys I suppose is how to do it. There still is a ton of stuff I need to learn but I can't seem to find the correct resources. All I keep running into on Google are the basic php mysql integration tutorials that show you how to do what I've already learned. Where should I be looking?
    Christopher Carvache
    Web Developer / SEO
    Northeast Web Design


  3. #2
    Join Date
    Apr 2007
    Member #
    I have found that the resources on the Internet for database design are not in the usual tutorial "click through my five webpages to see how to do it" kind of things. I have run across many many blog like posting desribing one approach versus another approach. Example.

    There is a lot of debate going on about how to map OO to a database.

    But as to Foreign Keys... they really sound more complicated than it really is.

    You make a column a Foreign Key and then tell it that all the data that resides in this column must be data that exists in another column from somewhere else.

    So if you had a table like this.
    colour_id colour_name
    1 black
    2 blue

    and then another table
    toy_id toy_name toy_colour
    1 watergun blue
    2 pokemon black

    If you put a Foreign Key on toy_colour and pointed it to colour_name, then the database would keep you from entering "garbage" or even "white" into the toy_colour column.

    Note however, it would be better to use numeric ids for such things. I just used actual words as it makes it easier to explain.

  4. #3
    Senior Member
    Join Date
    Jun 2005
    Atlanta, GA
    Member #
    1 times
    Also note that there can be some weirdness if you're using an ORM (Object-Relational Mapping) framework if it needs to save data and it doesn't do so in the right order, thus violating your foreign key constraints. Testing in Rails has this problem, which is why a lot of Railsers don't use FK constraints (well, that and the fact that they like to keep data in only one place, and the place they choose to put it is in the Rails models rather than the database; I personally think it would be nicer if they were in the database and Rails inferred them as it does field names).

  5. #4
    Senior Member paintingtheweb's Avatar
    Join Date
    Jul 2007
    Las Vegas
    Member #
    Going along with what kingmundi said - I'll try and make it a little easier.

    you have 2 tables. These 2 tables store information about furniture. Each piece of furniture belongs in a category.

    categories: (table name)
    category_id (Primary Key)
    furniture: (table name)
    furniture_id (Primary Key)
    category_id (Foreign Key)
    these two are linked by the category_id. If you wanted to find all of the furniture in a category - you would do something like this:

    select * from furniture where furniture.category_id = 1

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 07:42 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: