![]() ![]() In some cases, doing this might not be much more workthan issuing a few extra DELETE statements to make sure that when youdelete a record from one table, you also delete the corresponding records in anyrelated tables. Without foreign keys, you are responsible forkeeping track of inter-table dependencies and maintaining their consistency fromwithin your applications. Forexample, with cascaded update, changing a student's student_idvalue in the student table also changes the value in the student'scorresponding score table records.įoreign keys help maintain the consistency of your data, and they provide acertain measure of convenience. For example, we could set up a constraint such that if astudent is deleted from the student table, all corresponding recordsfor the student in the score table are deleted automatically as well.This is called "cascaded delete," because the effect of the deletecascades from one table to another. In other words, the foreign key prevents entry of scoresfor non-existent students.įoreign keys are not useful just for record entry, but for deletes andupdates as well. That prevents a record from being enteredinto the score table unless its student_id value exists in the student table. One of these was that we declared score.student_id to be a foreign key for the student.student_id column. When we created these tables in Chapter 1, we set upsome explicit relationships between them. For example, the score table in the sampdb sample database contains a student_id column, which we use to relate score records to students inthe student table. The database enforces therules of this relationship to maintain referential integrity. ![]() ![]() It also allows you to place constraints onwhat may be done to the tables in the relationship. Learn More Buy Foreign Keys and Referential IntegrityĪ foreign key relationship allows you to declare that an index in one tableis related to an index in another. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |