Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Just as a notice: in mysql, foreign key are only enforced when you use an innodb table format, which is not the default, so if you have a shared hosting somewher (you are not the person taking care of the server), you probably don't have them.
Now for the generic theory...
An foreign key is a constraint that you put on a field, that basically means "I wanot only something in this field that already exists in the table x on the field y".
It enforces, for example, that when you create an invoice, the corresponding user is already existing in the db.
If it was not the case, the DB would return an error saying that the given userId is not found in the user list.
I haven't done mysql in years, but the syntax is generally something along those lines:
Code:
create table other_table(
id integer
)
create table example(
id interger not null,
fk integer not null reference other_table(id)
);
__________________
Only a biker knows why a dog sticks his head out the window.
|