my question is what the purpose of using relations between tables , when i can achieve that with joining tables with sql joins ?
Asked
Active
Viewed 55 times
-3
-
3Foreign keys are there to keep data consistent. Nothing to do with joins. – jarlh Aug 03 '16 at 08:54
-
http://stackoverflow.com/questions/10183116/what-are-the-advantages-of-defining-a-foreign-key it shows your answer – PaulShovan Aug 03 '16 at 08:55
-
Because you have to. That's why – KaeL Aug 03 '16 at 08:59
-
Possible duplicate of [What's wrong with foreign keys?](http://stackoverflow.com/questions/83147/whats-wrong-with-foreign-keys) – KaeL Aug 03 '16 at 08:59
1 Answers
0
Foreign Keys give another layer of safety. They provide data integrity on data level. They prevent you from adding a record which does not fulfill the relation.
For example, if you have a table users
with three Users (IDs 1, 2 and 3) and another table entries
, where entries.uid
has a relation to users.id
, this means you can't add an entry to entries
where you would add an entry.uid
of 4, if you don't have an user with ID 4 defined.

UeliDeSchwert
- 1,146
- 10
- 23