Skip to content
Advertisement

How to get details for a customer that is both a seller and a buyer

I have a database which contains customers that are both buyers and sellers. the customers can buy and sell houses.

I need to get details for the customers separately and still have a connection between them so i could know which customer sold to which customer.

In case a customer buys a house from a different customer how can i get the details for each customer separately?

The table I made so far for the customers is :

CREATE TABLE Customers (
  SellerID int,
  BuyerID int,
  HouseID int,
  SaleID int,
  FirstName varchar (50),
  LastName varchar (50),
  Adress varchar (50),
  BirthDate Date,
  City varchar (50),
  HomePhone varchar (50),
  PRIMARY KEY (SellerID, BuyerID),
  FOREIGN KEY (HouseID) REFERENCES House(HouseID),
  FOREIGN KEY (SaleID) REFERENCES Sale(SaleID),
);

I have a sale table

CREATE TABLE Sale (
  SaleID int,
  SalesManID int,
  SaleDate Date,
  SalePrice int,
  PRIMARY KEY (SaleID),
  FOREIGN KEY (SalesManID) REFERENCES SalesMan(SalesManID),
);

I have a table that contains both sale and customers

CREATE TABLE SaleToCustomers (
  SaleID int,
  CustomersID int,
  PRIMARY KEY (SaleID, CustomersID)
);

Advertisement

Answer

Your entities are confused. A customer should be a person, with information about the persion. Then Sales should refer to it twice, once for buyers and o once for sellers:

CREATE TABLE Customers (
  CustomerId int PRIMARY KEY,
  FirstName varchar(50),
  LastName varchar(50),
  Adress varchar(50),
  BirthDate Date,
  City varchar(50),
  HomePhone varchar(50)
);

CREATE TABLE Sales (
  SaleID int PRIMARY KEY,
  SellerId int,
  BuyerId int,
  HouseId int,
  SaleDate Date,
  SalePrice int,
  FOREIGN KEY (SellerId) REFERENCES Customers(CustomerId),
  FOREIGN KEY (BuyerId) REFERENCES Customers(CustomerId),
  FOREIGN KEY (HouseID) REFERENCES House(HouseID)
);

In other words, “buyer” and “seller” are attributes of a sale, not of a person.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement