简体   繁体   中英

What does the ON DELETE and ON UPDATE mean where setting a FOREIGN KEY in MySQL?

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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM