Greyfish
02-22-2009, 07:19 PM
Hey I was recently advised to use foreign keys in my MySQL database and I'm not very familiar with them. Can someone explain to me exactly what they do and how I query the database using them?
|
Click to See Complete Forum and Search --> : Tell me about foreign keys. Greyfish 02-22-2009, 07:19 PM Hey I was recently advised to use foreign keys in my MySQL database and I'm not very familiar with them. Can someone explain to me exactly what they do and how I query the database using them? shk 02-22-2009, 10:04 PM A Foreign Key is a referential constraint between two tables. For instance: Table 1: Article article_id article_title article_body (....) Table2: Category category_id category_name (...) Primary Keys (PK) are to identify each entry (article_id, category_id). In order to know which article belongs to which category you need to link both tables.. .the way to do it is using foreign keys (FK) So it will look like these: Table1: Article article_id article_title article_body article_category_id <-- (Foreign Key) (....) Table2: Category category_id category_name (...) So whenever you create an article you will need to store in Article table which category_id it belongs to as well. So when you do the query $sql="SELECT * FROM article WHERE article_category_id="1" "; You will be able to show all the articles that belong only to category_id= 1 Hope it helped a little. -- http://www.karinamyers.com webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |