B2B: MySQL Joins and Foreign Keys


Welcome to the first of five articles in the Web Expose Back to Basics project. Each article is going to cover a seperate and distinct topic, with the aim of providing worthwhile beginner tutorials to aid in furthering your web development skills.

The topic for today is using the popular MySQL database to logically store and retrieve data stored in several tables, avoiding data duplication. This is commonly referred to as ‘joining tables’ - and the links between two or more tables are often called ‘foreign key links’. This article deals with MySQL because it is the most readily available and popular database for PHP developers at this point in time, but the examples should work with zero to minimum modifications on most other popular relational database systems.

Using a join to link two tables together refers to the fact that by identifying a common column in both tables, you can use this column to join the two tables together and return a combined result set.

Joining tables

In this example picture, the green columns illustrate two sets of data that are common between the two tables. For example, these could be two tables with user email addresses in them; you could then join the tables on that email column. In most cases though, you will hopefully have designed the database to avoid data duplication (called normalisation) and these common fields will usually be integer ID fields. We can now use a join statement to combine the two tables and return a result set that looks like this:

Tables joined

Here, all the columns from table 1 are laid out as normal, then the matching row’s columns from table 2 are appended to each row. This will only happen if you have the correct filter conditions in place though - without a proper WHERE statement, every row in table 1 will be duplicated as many times as there are rows in table 2, providing you with a potentially huge result set.

No join condition

I realise the above might be confusing, so I think it’s time to do some proper examples with SQL. I’m using MySQL 4.1.6 and phpMyAdmin 2.8.04 to control it, but almost any version will do. phpMyAdmin isn’t required, but is a very good web-based interface to the MySQL RDBMS. If you’re accessing MySQL using the command line tool, remember that you never pass the password through on the command line arguments.

  mysql -u username -p -h localhost

MySQL will prompt you for the password; neglecting the -p argument will make MySQL attempt to connect with a blank password.

Using the command line tool, or phpMyAdmin’s query tool, create two test tables to play with:

Creating tables

  CREATE TABLE `b_products` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR( 100 ) NOT NULL ,
  `category_id` INT NOT NULL
  );

  CREATE TABLE `b_categories` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR( 100 ) NOT NULL
  );

Populate tables with some data

  INSERT INTO `b_categories`(`name`)
  VALUES ('DVD');

  INSERT INTO `b_products`(`name` , `category_id`)
  VALUES ('Flatline', '2');

(Whenever you see data being populated, you should create some of your own as I haven’t listed all the INSERTs here, to keep the text a bit shorter. If you scroll to the bottom of the post, you can download all the tables and data I used in SQL format.)

The 2 in the last line of the INSERT is the relevant category id you want to link the product to. As you can see, the product table has a category_id, which is called a foreign key, as it refers to a table foreign to the current table. This abstraction of data allows us to avoid having to repeat data (i.e. we don’t have to type ‘DVD’ next to each product, we can simply enter the ID of the ‘DVD’ category). If ‘DVD’ ever changes to ‘HD DVD’, all we have to do is update the category table entry, and everything else will update automatically.

Sample query - fetch all products and their matching categories

  SELECT *
  FROM `b_products` p, `b_categories` c
  WHERE p.category_id = c.id;

Placing an identifier after the table name in the FROM statement allows us to assign an alias to the table; we can then refer to fields inside that table specifically by using the alias.columnname syntax. This is an important step when using joins, often tables share columns with the same name, and you have to explicitly state which one you want to match or fetch.

Result

Result 1

Here you can see that category_id and id are outlined in orange and next to each other - the first three columns are from the product table, and the last two columns from the category table - the join effect is apparent here.

On line 3 of the above query, you can see that we’re limiting each row to only return the correct row from the category table - i.e. we only want to fetch data where the product category ID is the same as the category ID in the external table.

Compare the above to the following query without a WHERE clause:

  SELECT *
  FROM `b_products` p, `b_categories` c;

If you run this query, you’ll see that each row of the products table is joined against every single row in the categories table, as described in the beginning of the post.

Fetch all CD products, and only select the 3 relevant columns

  SELECT p.id, p.name, c.name
  FROM `b_products` p, `b_categories` c
  WHERE p.category_id = c.id
  AND c.name LIKE 'CD';

Result

Result 2

Advanced: count the number of products in each category

  SELECT COUNT(p.id) AS count, c.name
  FROM `b_products` p, `b_categories` c
  WHERE c.id = p.category_id
  GROUP BY c.name;

Result

Result 3

Let’s move on to another example, one where the two tables can have multiple links between each other (sometimes called a has-and-belongs-to-many relationship).

Create two additional tables and some data links

  CREATE TABLE `b_owners_products` (
  `owner_id` INT NOT NULL ,
  `product_id` INT NOT NULL
  );

  CREATE TABLE `b_owners` (
  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR( 100 ) NOT NULL
  );

  INSERT INTO `b_owners_products`(`owner_id` , `product_id`)
  VALUES ('4', '3');

We don’t have to modify the owners table, or the product table - all the link information is stored in a third external table called owners_products (the convention is to put the table names alphabetically, seperated by a ‘_’).

Linking the two tables, using the third as a reference

  SELECT *
  FROM `b_owners` o, `b_products` p, `b_owners_products` op
  WHERE o.id = op.owner_id
  AND p.id = op.product_id;

Result

Result 4

Doing something useful with the link

  SELECT o.name, p.name
  FROM `b_owners` o, `b_products` p, `b_owners_products` op
  WHERE o.id = op.owner_id
  AND p.id = op.product_id
  AND o.name = 'Bob';

Result

Result 5

This fetches all products belonging to ‘Bob’.

More information

  1. Sample tables and data for this article
  2. A detailed explanation of JOINs
  3. Ensuring foreign key integrity
  4. More information on JOINs
Share this post
  • Digg
  • StumbleUpon
  • Reddit
  • del.icio.us
  • Facebook
  • muti
  • Mixx
  • Google
  • laaik.it

This entry was posted on Tuesday, January 23rd, 2007 at 12:27 am and is filed under Code. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

25 Responses to “B2B: MySQL Joins and Foreign Keys”

  1. readings medium psychic said this on

    medium authentic psychic medium psychic best

  2. Blazz said this on

    You code for Advanced: count the number of products in each category is flawed, if a category is empty it wont return it.

  3. therapy counseling said this on

    I use to be skeptical about psychic readings until I went to a psychic myself. For all those who are still skeptical, I would recommend to give it a try once.

  4. Demi Moore said this on

    “You cannot escape the responsibility of tomorrow by evading it today.” - Abraham Lincoln
    Demi Moore

  5. Free Phone Psychic Readings said this on

    Very interesting post I enjoy your website keep up the amazing posts

  6. Aura Liscano said this on

    Having completed a search for online psychics on Bing I came across your blog, and am I grateful I did. I am impressed with the level of information I have discovered here, and I am sure that this will be not merely helpful to me but to others also. I have added you to my list of favorite blogs and will be back on a regular basic

  7. Columbus Winfield said this on

    Please go to our site and add this wonderful blog to the directory, not only will we be able to share your blog with our readers, you will get more traffic.

  8. Arianna Torres said this on

    What i know is that there are real pyschics and bogus pyschics that just wants your money.;;*

  9. classics today hurwitz said this on

    i like your blog and article.thanks and bookmark it

  10. Marketing Data UK said this on

    A really good blog on B2B Data. Thanks.

  11. Dumbbell Set Weights  said this on

    i have a friend who is a psychic, she is great in prediction and also in remote viewing-**

  12. scrubs said this on

    I have been previously attempting for a time for any smart study associated with the following issue . Looking out in Aol I lastly uncovered this incredible website. After reading this information I’m happy to say that I get a positive impression I found the very things I needed. I most certainly will make certain to remember this site and take a visit consistently.

  13. Sheet Plastic : said this on

    i can only wish that i were a psychic too which can tell the future and have the power of remote viewing-~’

  14. Ryan Groder said this on

    when thery’re a year old switch gradually to adult food by mix the two foods together and slowing inclease the amount of adult dog food until eventually it’s all adult dog food if you don’t they could get diarrea or they could quit eating for a while

  15. Claribel Leonardi said this on

    Have you ever considered including far more movies to your blog posts to maintain the readers much more entertained? I indicate I just study as a result of the entire article of yours and it was fairly great but since I’m far more of a visual learner,I located that to become much more helpful. Just my my thought, Beneficial luck.

    my website is Stopping Smoking .Also welcome you!

  16. Steven Denman said this on

    Many thanks for useful info. Keep up the great work. I will be coming back often.

  17. hdtv tuner card said this on

    Fine post! this will without fail support me.

  18. Pearl Perras said this on

    Hey Travel Blogger, Next month I’ll be starting my expat blog about making money while traveling. I will be working as a “professional” blogger and freelance writer, and probably teach a little English as well. I have tried this twice, and failed (but have learned a lot), this time I will succeed! It will be called “A Broad Abroad” and it will be fun!

    See me on Facebook http://www.facebook.com/people/Pearl-Perras/100001813539215

  19. Sublingual Vitamins %0B said this on

    i do admire psychics for their ability to sense some weird stuffs like predict the future or something ‘.:

  20. Samantha Lee said this on

    If it is all too weird for you, and you’d rather make paper mache, or collect stamps or something, then by all means- go ahead. So the question is, are they safe? Reborners have even taken this craft to a entire new level with the birth of custom reborn babies, that are basically babies created with the precise specifications of a buyer. Other folks who can’t relate to the idea of parenting basically buy these babies for their collection. Talented Sculptors perfected baby attributes adding in various facial expressions, some with closed eyes, some with open, all using the potential to be completely transformed in the hands of a talented reborner. She’ll be overjoyed to have it and treasure it for the rest of her life. Is it for me?

  21. Vitamin Water %0B said this on

    `*- I am very thankful to this topic because it really gives up to date information ~”~

  22. Suzie Leveston said this on

    I actually certainly must think far more in that way and see things i can do regarding this.

  23. Carolle1187 said this on

    Attractive section of content. I just stumbled upon your site and in accession capital to assert that I get actually enjoyed account your blog posts. Any way I??ll be subscribing to your augment and even I achievement you access consistently fast.

  24. Coxyboidomhob said this on

    ??? ?? ????????, ??? ?????????, ??? ? ????????? ???? ???????????
    ????? ?????? ????? ?????????

  25. Singapore Property said this on

    Your post is excellent. I found your site through Bing. Thanks for everything. singapore property price

Leave a Reply