I have an interesting problem here. I have a MySql database table that has is linked to several other tables. For example, the “main” table is a set of teaching materials, along with connected tables:
- Grades that the material is aimed at;
- Grammar points that the material is best used for;
- Keywords associated with the material
I want to be able to update and change these records. Changing the main record is easy enough, but what about the connected records? For example, let’s say that I have a teaching material that I originally intend to teach verbs and adjectives, and is best used for grades 1-4. Now later on, I decide to add grade 5 to the list, remove grade 1, and that the material is actually better for teaching nouns and adjectives, but not verbs.
What would be the best way to update the grade and grammar tables? I don’t want to inadvertently overwrite data that I wanted to keep, nor to keep data that I wanted deleted. Should I just wipe out all the records associated with that particular teaching material and re-enter them, or is there some other way that I can index them so that they can be more efficiently updated?
In MySQL, you can link your tables by foreign keys. That is, you have your connected records which share these keys. Then, depending on how you instantiate your tables, you can use a
CASCADEwhen you update a value in one table. From the manual on MySQL, when you define this foreign key you can set theCASCADEas follows:Where:
By default, this reference option is set to
No Action.