Use a foreign key in MySQL or not?

I have been coding MySQL databases for several years now and I have never used a foreign key. Now I understand that a foreign key is similar to an ID on one table, which corresponds to the primary key of another table. I have a user table on my site and probably about 10 other tables that correspond to the main key of my user table - however they are not set as foreign keys.

What did I miss without having these 10 other tables, is there a foreign key? I mean, as far as I can tell, they are basically a foreign key, except that they do not have the value stored / assigned to them in the database.

Is there any other advantage here that I just don't know about?

I also understand that the primary key cannot be null, but it can also be a foreign key. This will never be a problem in my case, since my user table is created, and when a new user is added to my user table, I add their corresponding entry to 10 other tables.

+5
source share
5 answers

A FOREIGN KEYfulfills two goals:

  • This ensures that your relationship is always agreed upon at the cost of some control premiums.
  • This (controversially) simplifies cascading updates and deletes.

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

+3
source

- - , .

  • ( , , )
  • , , , , - , .
+6

  • , user_id.
  • (innodb)
+1

, , - (.. user_id 27, ID 27 ) (.. 27, ..).

-, . , - .

0

( MySQL), FOREIGN KEY , . , , ..

0
source

All Articles