MySQL links or PHP tools

When is it better to use built-in relationships between DB tables (in my case, MySQL) (one-to-one, one-to-many, etc.), and when is it better to implement this using server-side programming languages (in my case, PHP). The fact is that it is much more convenient for me to use php tools, because it is a more flexible structure ( as it seems to me). So I wanted to hear more opinions.

Author: findley, 2019-12-25

1 answers

When is better

The question is meaningless. These things solve fundamentally different problems.

The relationships (or rather, foreign keys) between the database tables are the basis for the work of the data integrity and consistency subsystem at the DBMS level, which do not allow you to insert data into the database tables for which the matching condition is not met, or to change the data in such a way as to violate this compliance.

Linking at the application level solves the problem establishing the correspondence of table records when performing data manipulation.

The former have absolute priority and the status of dogma. The second - so, the current wishlist for one request.


Data integrity control at the DBMS level using the FK mechanism is carried out continuously and continuously (in reality - at any time of data changes), without allowing mismatch to occur. If this happens for reasons beyond the control of the DBMS (as a rule, the reason is an error system software or physical storage/processing equipment) - the error is detected as soon as possible (in fact, when the first access to the damaged data block is made).

Application-level data control - does not control data in principle. To verify that the data is consistent, you need to create and periodically perform separate control procedures, and inconsistency is detected with a delay.


Control at the DBMS level will not allow you to perform a random or malicious mismatch of data, this will require first changing the storage structure and disabling or deleting foreign keys. With the correct access rights settings, it is not realistic.

Application-level control in this sense is about nothing at all. It just doesn't exist.


And the last thing. The presence of foreign keys in the storage structure does not eliminate the need to establish matches in the request text. they are absent from the structure, or even contradict it.


And the very last thing. If the logic on the client side meant all sorts of models from all sorts of frameworks - then they have nothing to do with it at all, because they do not work with data, but with a copy of it alienated to the client. What you don't do on the copy - the original will not change from this.

 4
Author: Akina, 2019-12-25 10:52:31