Junior — Middle
Can you explain what is meant by a one-to-one relationship in the context of databases or object-oriented programming?
sobes.tech AI
Answer from AI
A "one-to-one" relationship in databases means that each record in one table corresponds to exactly one record in another table. For example, each user may have only one profile, and each profile corresponds to only one user.
In object-oriented programming, this means that one object is linked to exactly one other object. For example, a Person class may have a Passport property, and each Person has exactly one Passport.
Example in C#:
class Person
{
public int Id { get; set; }
public Passport Passport { get; set; }
}
class Passport
{
public int Id { get; set; }
public string Number { get; set; }
public Person Owner { get; set; }
}
Here, each Person is linked to one Passport, and vice versa.