Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To use a foreign key in MySQL or not to?

I have been coding with MySQL DBs for a couple years now and I have never used a foreign key. Now, I understand that a foreign key is like an ID on one table that corresponds with a primary key of another table. I have a user table on my site and probably around 10 other tables that all correspond with the primary key of my user table - however they are not set as foreign keys.

What am I missing out on by not having these 10 other tables have a foreign key? I mean, as far as I can tell they basically are a foreign key except they do not have that value saved/assigned to them in the DB.

Is there some other benefit here that I am just not aware of?

I realize too that a primary key cannot be null, but a foreign key can be. This will never be an issue in my case as my user table is created, and and when a new user is added to my user table I add their appropriate entry to the 10 other tables.

like image 696
JasonDavis Avatar asked Aug 07 '09 18:08

JasonDavis


People also ask

Should I use foreign keys in MySQL?

Foreign keys aren't required to have a working relational database (in fact MySQL's default storage engine doesn't support FKs), but they are definitely essential to avoid broken relationships and orphan rows (ie. referential integrity).

Should I use foreign keys or not?

Yes, you should. Foreign keys are just constrains which helps you to make relationships and be sure that you have correct information in your database. You should use them to prevent incorrect data entry from whatsoever.

Why foreign key is not recommended?

Having active foreign keys on tables improves data quality but hurts performance of insert, update and delete operations. Before those tasks database needs to check if it doesn't violate data integrity. This is a reason why some architects and DBAs give up on foreign keys at all.

Do foreign keys improve performance MySQL?

It is true that foreign keys will impact INSERT, UPDATE and DELETE statements because they are data checking, but they improve the overall performance of a database. The main benefit of foreign keys is that they enforce data consistency, meaning that they keep the database clean.


3 Answers

Adding foreign keys is always a good idea - at least I've never seen a compelling reason not to use them.

  • enforces referential integrity (can't delete a parent if a child exists, can't insert orphans, or a child with an invalid parent id)
  • works as an index
  • With foreign keys, no matter how the data is accessed, whether through an app, an automated process, or someone without caffeine at the terminal the rules are uniformly enforced.
like image 185
Joe Cairns Avatar answered Nov 03 '22 11:11

Joe Cairns


A FOREIGN KEY serves two purposes:

  • It ensures that you relationships are always consistent at the cost of some checking overhead
  • It (disputably) simplifies cascaded updates and deletes.

In most cases, this functionality can be more efficiently implemented using other tools.

like image 23
Quassnoi Avatar answered Nov 03 '22 11:11

Quassnoi


With foreign keys you

  • can make sure that only valid user_id's are put to those fields
  • use cascades on delete easier don't
  • don't have to manually define indexes on those fields (innodb)
like image 39
Zed Avatar answered Nov 03 '22 10:11

Zed