B
B
beetlezilla2017-09-12 10:30:57
Database design
beetlezilla, 2017-09-12 10:30:57

How important are foreign keys in databases?

I have been working for the same company for a short time. There is a database with 1000+ tables. I was confused by the fact that despite the presence of fields with the name of the key field of another table, there is no relationship between these tables and foreign keys. In other words, a lot of foreign keys are not labeled with foreign keys. From my little experience this seems very strange and hence I have a few questions:

  1. Are foreign keys always necessary?
  2. What is fraught with the absence of foreign keys? (I know about cascading update and deletion, which is why this question arose)
  3. What could be the reasons for the absence of foreign keys in this database?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
T
terrier, 2017-09-12
@terrier

1. Are foreign keys always needed?
Foreign keys are needed somewhat less than always.
Loss of data consistency
Performance and locations. Here I answered a similar question in the context of postgres, other RDBMS are about the same.

M
Maxim Fedorov, 2017-09-12
@qonand

1. It all depends on the organization of the project, if, for example, you have a sharded database, then no matter how you make foreign keys, or, for example, if you want to avoid data integrity checks with every data change, but do them yourself once a day. In general, foreign keys are highly desirable, but there are situations when you should not use them
2. Inconsistent data
3. In paragraph 1, he gave some examples

E
Eldar01, 2017-09-12
@Eldar01

If the application that works with this database itself checks all connections at its application level, higher than the database. - then why not.
It's easier to manipulate tables if the DBMS doesn't bother you with its dependency control.

A
Anton Anton, 2017-09-12
@Fragster

1. Only if you shove business logic into the DBMS
2. Data inconsistency if it is not provided by the business logic layer
3. For example, the application supports several DBMS, not all of which are able to foreign keys.

V
Vasily Nazarov, 2017-09-12
@vnaz

1000+ tables

It is very likely that this is the answer.
If you declare all connections as FK, modification (insertion, change, deletion) of one record in the case when all values ​​​​have already been validated (and this happens, apparently, always in competent code), will be noticeably slowed down by these same consistency checks.
Contrary to what others have said.
I have been working with bases for 20+ years.
10+ years ago, when I started working with MySQL (he didn’t know how to have any consistency then), I was very surprised how you can work on this. But there were no options, I had to work.
So.
In these 10+ years, there has never been any problem related to the lack of FK control on the DB side.
However, it is possible that for this it is still necessary to use the brain heavily in development.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question