Basics of Foreign Keys in MySQL?

MysqlForeign Keys

Mysql Problem Overview


Is there any good explanation of how to use MySQL's foreign key construct?

I don't quite get it from the MySQL docs themselves. Up until now I've been handling things like foreign keys with joins and programming code.

And the second part of the question, are there any improvements to be made by using MySQL's inbuilt foreign keys?

Mysql Solutions


Solution 1 - Mysql

FOREIGN KEYS just ensure your data are consistent.

They do not improve queries in sense of efficiency, they just make some wrong queries fail.

If you have a relationship like this:

CREATE TABLE department (id INT NOT NULL)
CREATE TABLE employee (id INT NOT NULL, dept_id INT NOT NULL, FOREIGN KEY (dept_id) REFERENCES department(id))

, then you cannot delete a department if it has some employee's.

If you supply ON DELETE CASCADE to the FOREIGN KEY definition, the referencing rows will be deleted automatically along with the referenced ones.

As a constraint, FOREIGN KEY actually slows down the queries a little.

Extra checking needs to be performed when deleting from a referenced table or inserting into a referencing one.

Solution 2 - Mysql

The main benefits of using real foreign keys are ensuring data integrity, and being able to set up cascading actions on related items when something is modified or deleted.

For example, imagine you're programming a forum. You have a "topics" table with primary key topics.topic_id, and you have a "posts" table where posts are attached to topics with the column posts.topic_id, which is a foreign key to the topics table.

This foreign key relationship ensures that every post is attached to a valid topic. If the only topic you have has ID #1, it's impossible for there to exist a post in the database attached to topic #2. The database ensures this.

For the cascading benefit, you can set it up so that if a topic is deleted from the topic table, the database automatically deletes all the posts in the posts table that were attached to this topic. This is nice because it removes a step that you have to remember to do manually, which can get quite complex when you have many tables linked together. With foreign keys all the relationships can be cleaned up automatically.

Solution 3 - Mysql

1.FOREIGN KEYS just ensure your data are consistent.

  1. If we apply on delete cascade to the foreign key definition,referencing row will delete automatically when parent row will delete.

  2. If we apply on Update Cascade to the foreign key definition,Child row will update automatically when parent row will update.

Query: ALTER TABLE child ADD FOREIGN KEY(parent_id) REFERENCES parent(id) ON UPDATE CASCADE ON DELETE CASCADE ;

  1. you can not delete direct parent table , first delete foreign key from child table than delete parent table.

Solution 4 - Mysql

The main advantage is that you can limit which values you can enter in the table; if you try to enter a value that doesn't exist in the referenced table, you won't be able to do it.

Also, if you update or delete the value in the referenced table, you can set it to automatically update the value or delete in cascade any row containing that value.

It's indeed a great feature leveraging your code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMachaView Question on Stackoverflow
Solution 1 - MysqlQuassnoiView Answer on Stackoverflow
Solution 2 - MysqlChad BirchView Answer on Stackoverflow
Solution 3 - MysqlGaurav KumarView Answer on Stackoverflow
Solution 4 - MysqlSebView Answer on Stackoverflow