Summary
Keywords
Full Transcript
You can provide actions on the foreign key with keywords ON DELEE or ON UPDATE. Do not hesitate to put constraints on the database. You want to make sure you have a consistent database, especially if you have several apps using it. The actions that you can specify for a foreign key are: ON DELETE – responds when you try to delete a primary key that is used as a foreign key in another table ON UPDATE – responds when you try to update a primary key that is used as a foreign key in another table The ON DELETE or ON UPDATE actions have a few responses: RESTRICT – Restrict is the default action (if none is specified), and it will reject any update, insert, or delete if it violates the referential integrity. SET DEFAULT - sets invalid foreign keys to a default primary key value. SET NULL - sets an invalid foreign key value to NULL. CASCADE – will make all the same primary key changes to the foreign keys. If you delete or update a primary key then the rows containing matching foreign keys are also deleted or updated to the new value. For example, DeanID is a foreign key that references the Dean ID column. ON DELETE CASCADE causes the database to delete the row with DeanID 123 from the Department table when the dean with ID 123 is deleted from the Dean table ON UPDATE SET NULL causes the database to set DeanID 456 to NULL when the Dean with ID 456 is changed to 789. Subscribe to Appficial for more programming videos coming soon. Also, don't forget to click LIKE and comment on the video if it helped you out!
