I'm building a small application and setting up foreign key relationships between tables. However I'm confused as to WHY I really need this? What is the advantage - does it assist me when writing my queries that I don't have to perform any joins? Here's an example snippet of my database:
+-------------------+
| USERS |
+-------------------+
| user_id |
| username |
| create_date |
+-------------------+
+-------------------+
| PROJECTS |
+-------------------+
| project_id |
| creator |
| name |
| description |
+-------------------+
There is a key relationship between users
.user_id
and projects
.creator
Would I be able to perform a query like so?
SELECT * FROM PROJECTS WHERE USERS.username = "a real user";
Since MySQL should know the relationship between the tables? If not then what is the real function of Foreign keys in a database design?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…