
Tech_updates
May 22, 2025 at 10:22 AM
Understanding Table Relationships in SQL
*What are Table Relationships in SQL?*
In real-world databases, data is stored across multiple related tables. Instead of repeating the same data, we link tables using keys.
*Types of Relationships:*
*1. One-to-One (1:1):*
Each row in Table A is related to exactly one row in Table B.
Example: A user and their profile.
user_id in users links to profile_id in profiles.
*2. One-to-Many (1:N):*
A row in Table A can relate to multiple rows in Table B.
Example: One customer can place many orders.
customer_id in customers links to many rows in orders.
*3. Many-to-Many (M:N):*
Rows in both tables relate to multiple rows in the other.
Example: Students and Courses.
This needs a third table (bridge) like student_courses.
*Keys You Should Know:*
Primary Key: Uniquely identifies each row in a table.
Foreign Key: A column that links to the primary key in another table.
*React with ❤️ if you're ready for the next quiz.* 📝
❤️
❤
👍
18