In MySQL, you can create tables that have relationships but you can't create
what a lot of people call "Foreign Key relationships". In reality you CAN
create Foreign key (FK) relationships between tables, you just can't create
constraints that enforce them automatically.

Example time:

Let's say I have 2 tables, person and phone.

person
------------
personID PK
============
name
phoneID  FK
------------

phone
------------
phoneID
============
phoneNumber
------------

See the phoneID in the person table? In it we store the value of
phone.phoneID for this person's phone number.  This is a Foreign key
relationship. (So named because we are storing a foreign primary key in our
table.)  In other implementations of SQL, you could define a constraint
(rule) that says that you can't store a value in person.phoneID that does
not exist in phone.phoneID.  This is a Foreign Key Constraint.

So, to answer your question, to create a FK relationship between 2 tables,
put a field in table1 of the same type (does not have to be the same name
but I always do) as the primary key of table 2.

Then when you are selecting and want to gather everything together you use:

Select t1.*,
       t2.*
  from table1 t1,
       table2 t2
 where t1.t2ID = t2.t2ID

Clear as mud?

Cal
http://www.calevans.com


-----Original Message-----
From: GERARDO GALLARDO [mailto:[EMAIL PROTECTED]]
Sent: Monday, March 26, 2001 5:57 PM
To: [EMAIL PROTECTED]
Subject: STUPID QUESTION


I'm new to MySQL and SQL in general.  I have been searching and searching
but I can't find an answer to a question which I know is simple.  I have
read a book called Teach Yourself MySQL in 21 days.  It talkes about
defining relationships between tables but it never actually shows you how
you would create these relationships or reference data from one table in
another table.  How do I do this?  Please help.
Gerardo

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to