Junior
What types of relationships exist in a database? Provide examples.
sobes.tech AI
Answer from AI
In relational databases, the following types of relationships between tables exist:
-
One-to-One: Each record in one table is related to at most one record in another table, and vice versa.
- Example: The relationship between the
Userstable and theUserProfilestable, where each user has exactly one profile.
- Example: The relationship between the
-
One-to-Many: Each record in one table can be related to one or multiple records in another table. However, each record in the second table is related to at most one record in the first table.
- Example: The relationship between the
Departmentstable and theEmployeestable, where many employees can work in one department, but each employee works in only one department.
- Example: The relationship between the
-
Many-to-Many: Each record in one table can be related to multiple records in another table, and vice versa. To implement such a relationship, an intermediate (junction or associative) table is required.
- Example: The relationship between the
BooksandAuthorstables, where a book can have multiple authors, and an author can write multiple books. The intermediate tableBooksAuthorswill contain foreign keys toBooksandAuthors.
- Example: The relationship between the