[Solved] Database relationships why should I use them? [closed]


You are talking about two different concepts here:

  1. Data input: Relationships is one of the methods used to ensure data integrity. It restricts what data can be inserted to the database.

  2. Data retrieval: The LEFT JOIN that you mentioned is on of the methods used to retrieve the data from the database. It, kind of, filters the data so you get what you want instead of returning the entire table(s).

You don’t have to use any of those two. They are there to help you, but they are not required and you can achieve similar results by other means. More importantly, they are completely unrelated. If you use relationships, you are not required to use LEFT JOIN or any other joins. And if you don’t use relationships, you can still use joins and get the same results.

If you don’t use relationships, your app can dictates how the data are entered to the database. If you don’t use joins, you can use sub-queries for example. Which way to go is really greatly dependant on your requirements, but probably for most scenarios, using relationships and joins is the way to go.

For example the two queries below are equivalent. The first one uses joins, and the second one does not:

SELECT users.name, orders.id
FROM users
INNER JOIN orders ON users.id = orders.user_id

SELECT users.name, orders.id
FROM users, orders
WHERE users.id = orders.user_id

4

solved Database relationships why should I use them? [closed]