Should I use foreign keys?

Should I use foreign keys for each linked table or shouldn't? If I have to use, why?

+6
source share
6 answers

"All related tables" are not always clear, so this is not an obvious situation. There are tables that have a common column, but may never see each other.

However, this is useful to prevent errors that slip past your primary protection and allow you to enter data that is easily tracked.

They do not help make queries more efficient if you have the appropriate indexes in place, and a good application will filter the input so that it will never be needed. But mistakes do happen, and this is a cheap line of defense.

If you just like to create databases, they cannot worry and refine much if you have a basic relationship between parents and children.

Here is some background -

What happened to foreign keys?

+6
source

You must. There are three main points:

  • Depending on your database system, you may increase performance
  • Foreign keys provide data integrity, for example. can help avoid orphaned entries, etc.
  • This is an explicit way to document the structure of your database, which can be used by tools for visualization, code generation, etc.
+6
source

Yes. But at moderate levels. This helps in querying data. This helps in indexing your data, therefore faster queries. It also helps maintain relationships between objects.

+6
source

Using foreign keys is one of the basic (if not the only) basic concepts of relational databases. Of course, you should use foreign keys where necessary. Because it helps:

  • Make sure data validation and integrity
  • Saving excess space

The first means, instead of manually adding a value to the field that will be repeated for other records, you simply choose from what you have in the corresponding table of primary keys. If you try to enter something that does not exist as a primary key in another table, you will be rejected (in some databases you can configure this behavior, though).

The second means that you do not need to write “United States of America” every time, which takes up much more space than just writing the identifier “United States of America”.

+5
source

Yes you should. Significant keys are simply constraints that help you establish relationships and make sure that you have the right information in your database. You must use them to prevent incorrect data entry.

+4
source

Currently, more and more are moving away from relational databases based on foreign keys. Document database systems such as MongoDB are becoming increasingly popular. This is largely because the world is becoming much more widespread with the cloud.

This means that once it is unreasonable or inefficient to accept direct data consistency.

Read about a NoSQL-based database, MongoDB CouchDB and convincing consistency if you're interested.

This is kind of a weird thing for our relational trained minds to understand, but many great web solutions

+1
source

Source: https://habr.com/ru/post/905282/


All Articles