Sobes.tech
Junior

Tell me about the types of relationships in a database.

sobes.tech AI

Answer from AI

Relationships define how data from one table relates to data in another. The main types of relationships:

  • One-to-One: Each record in one table is related to at most one record in another table, and vice versa. Used for splitting large tables or storing confidential information.

  • One-to-Many: Each record in one table can be related to multiple records in another table, but each record in the second table is related to at most one record in the first. This is the most common type of relationship.

  • Many-to-Many: Each record in one table can be related to multiple records in another table, and each record in the second table can be related to multiple records in the first. Implementing this type requires an intermediate (linking) table.

  • Self-Referencing: The table is related to itself. Used to represent hierarchical structures (e.g., directory trees or employee subordinate structures).

Relationships are implemented using primary keys and foreign keys. A foreign key in one table references the primary key in another table.