Sobes.tech
Junior

What is a one-to-one relationship?

sobes.tech AI

Answer from AI

One to One relationship is a type of relationship between two entities in a database, where each record in one table corresponds to exactly one record in the other table, and vice versa.

For example, each user can have only one profile, and each profile belongs to only one user. In implementation, this is usually achieved using a unique foreign key in one of the tables.

Example in SQL:

CREATE TABLE User (
  id INT PRIMARY KEY,
  name VARCHAR(100)
);

CREATE TABLE UserProfile (
  id INT PRIMARY KEY,
  user_id INT UNIQUE,
  bio TEXT,
  FOREIGN KEY (user_id) REFERENCES User(id)
);

Here, user_id in the UserProfile table references User and is unique, ensuring a one-to-one relationship.