Sobes.tech
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 Users table and the UserProfiles table, where each user has exactly one profile.
  • 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 Departments table and the Employees table, where many employees can work in one department, but each employee works in only one department.
  • 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 Books and Authors tables, where a book can have multiple authors, and an author can write multiple books. The intermediate table BooksAuthors will contain foreign keys to Books and Authors.