Sobes.tech
Junior — Middle

How to organize storage of information about cars, their brands, and owners in a database using linked tables?

sobes.tech AI

Answer from AI

To organize storage of information about cars, their makes, and owners in a database using linked tables, a relational model with three main tables is usually used:

  1. Brands — contains unique records of car makes (e.g., Toyota, BMW).
  2. Cars — contains information about specific cars, including a foreign key to the brands table.
  3. Owners — contains data about car owners.

If one owner can have multiple cars, and a car belongs to one owner, then the relationship between owners and cars is one-to-many. If a car can have multiple owners (e.g., joint ownership), then a linking table (e.g., Owners_Cars) is needed to implement a many-to-many relationship.

Example table structure:

-- Brands table
CREATE TABLE Brands (
  id INT PRIMARY KEY,
  name VARCHAR(100) NOT NULL
);

-- Cars table
CREATE TABLE Cars (
  id INT PRIMARY KEY,
  brand_id INT,
  model VARCHAR(100),
  year INT,
  FOREIGN KEY (brand_id) REFERENCES Brands(id)
);

-- Owners table
CREATE TABLE Owners (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  contact_info VARCHAR(255)
);

-- If many-to-many relationship
CREATE TABLE Owners_Cars (
  owner_id INT,
  car_id INT,
  PRIMARY KEY (owner_id, car_id),
  FOREIGN KEY (owner_id) REFERENCES Owners(id),
  FOREIGN KEY (car_id) REFERENCES Cars(id)
);

Thus, linked tables allow efficient storage and connection of data, avoiding duplication and ensuring data integrity.

How to organize storage of information about cars… - sobes.tech