We've got several Ricardos in the article table that have a foreign key to a category in the category
table.
Let's try and delete one of the category Ricardos that's being referenced in the article table.
We get an error.
This is because of this Category rijkaard is being used in a related rijkaard as we just saw.
When we added the relationship to the article table we accepted the default for these on delete and
on Update options which is restrict.
This means that if we try to update our deleter rijkaard that's referenced elsewhere like we just did
then the database will be strict that operation and raise an error.
There are other options for these constraints if we select no action.
The database will do nothing if a related record is deleted and will allow us to delete it with no error.
By selecting set null the foreign key column that references the record where deleting will be set to
NULL.
Let's try this out for when we delete a rijkaard.
Now if we try and delete the category that's referenced in the join table.
The database Lexa's with no error.
If we view the article Ricardos that previously referenced this eidy then we can see that this value
has been set to NULL.
Let's try changing the relationship to cascade.
Now let's see what happens when we delete a related rijkaard for example this article with eidy 10 is
related to the category with an I.D. of five.
If we delete that category.
And then go back to the article table.
We can see that the article that referenced it Article 10 has also been deleted.
The cascade option will delete our update.
Any related to Ricardos when one of the records in a relation is deleted.
You might want this behaviour.
Depending on the type of relationship in this example though we don't want articles to be deleted if
a category is deleted.
Instead we want the foreign key field to be set to NULL.
As for update this occurs if the I.D. of the category is updated are changed.
So in this case we do want the change to cascade.
We want the idea in the article rijkaard to be changed also.
How you set up the constraints depends entirely upon how your database is structured.
When working with P H P and specifically P.D.A Oh if an error does occur with a constraint an error
or exception will occur.
These can be cartin pedigo Using a try catch block and you can check for the s keywell Error Code and
act accordingly.