0

I'm trying to read about how to use FOREIGN KEYS in MySQL. I see in many places that they have ON DELETE and ON UPDATE.

What's the difference between them? What exactly do they do?

Here is a small example of my database:

class
------
classId
profId    //Refers to a specific profId from professor table
courseId  //Refers to a specific courseId from course table
semId     //Refers to a specific semId from semester table

professor
---------
profId
first
last

course
------
courseId
department
number
title

semester
--------
semId
season
year

Also, a side note, which would be referred to as the FOREIGN KEY, the index id in the individual tables or the id's in the class table?

4

1 回答 1

1

Having foreign keys, means that your table has a relationship with an other table.

If you delete or update for example a record from the one, what should happen to the other?

For example I have the table "customer" and the table "loans". If I delete for one reason my customer, then I probably do not want to delete his loans. So i set the foreign key of the loans to the customer to be null (for example).

If I have a supermarket, and I delete my customer, then I would probably want to delete the products he bought as well.

I want to believe that I answered you :)

于 2013-03-31T03:10:15.683 回答