Answer the question
In order to leave comments, you need to log in
Why do we need foreign keys written in the database structure (MySQL) - are they really needed there?
Do not throw slippers)) but the question is this:
let's say I have two tables Users and Orders and in Orders I have user_id - the corresponding id in the Users table - the most standard situation))
And I can create these tables in the database, in a SQL query whether, or in phpMyAdmin, or in migration, specify that user_id is a FOREIGN KEY - right?
Question such:
- And what for it in general is necessary? :-)
Well, in the sense - it will still work anyway, and without it. All the logic I have in PHP is located.
Or will it be somehow faster with this key?
Or what in general?
Thanks in advance for a clear answer!
Answer the question
In order to leave comments, you need to log in
Any DBMS has a fairly powerful system for monitoring the integrity and consistency of data. This system works using a set of control rules described in the database structure, and strictly ensures that none of the rules is violated.
A foreign key is just such a rule. It is formulated as follows: a given table field cannot store a value that is not present in the table referenced by the foreign key (of course, depending on the specific text of the foreign key link and the field itself, options are possible - for example, this field can store not only the value present in the referenced table, but also NULL). And, having such a rule, the DBMS under no circumstances will allow it to be violated. Any attempt to insert a record with a value that is not in the referenced table will result in an error. Any attempt to change an existing value to one that is not "there" will result in an error. The same applies to the "second" side, the DBMS will not allow you to change the value in the reference table or delete it (because the records in our table "
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question