Junior — Middle
Can you name the types of relationships between tables in a database and explain their features?
sobes.tech AI
Answer from AI
There are three main types of relationships between tables in databases:
-
One-to-One
- Each record in the first table is related to exactly one record in the second table and vice versa.
- Used when data is logically separated but directly related.
-
One-to-Many
- One record in the first table can be related to multiple records in the second table.
- For example, one user can have many orders.
-
Many-to-Many
- Records in the first table can be related to multiple records in the second table and vice versa.
- Implemented through an intermediate join table that contains foreign keys to both tables.
Example: tables Students and Courses are related through the Enrollments table, which stores which students are enrolled in which courses.
Each type of relationship affects the query structure and data integrity in the database.