连接客户表和员工表的外键?

发布于 2025-01-09 23:58:21 字数 900 浏览 3 评论 0原文

我有两个表:

Employee ,其中包含以下列:

EmployeeIDFirstNameLastNameAddressSalaryDepartmentPosition

Customer ,其中包含以下列:

CustomerIDFirstNameLastNameAddressCompany

我还有另外两个表:

>产品

产品ID产品价格

购买

产品ID客户ID购买日期库存

这是我需要创建的查询:

  • 员工是客户报告 - 员工姓名、购买的产品、 产品成本,每位员工购买所有产品的总成本

通过查看数据,我已经知道所有客户也是员工。我需要在客户表或员工表中创建外键才能编写此查询。这就是我被困住的地方。

I have two tables:

Employee with the columns:

EmployeeIDFirstNameLastNameAddressSalaryDepartmentPosition

Customer with the columns:

CustomerIDFirstNameLastNameAddressCompany

I also have two other tables:

Product:

ProductIDProductPrice

Purchase:

ProductIDCustomerIDPurchaseDateInventory

This is the query I need to create:

  • Employee is Customer Report - Employee Name, Product Purchased,
    Product Cost, Total Purchased For All Products Per Employee

By looking at the data, I already know that all of the customers are also employees. I need to create a foreign key in either the customer table or the employee table in order to write this query. This is where I am stuck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2025-01-16 23:58:21

你有几种可能性。最简单的方法是对两者使用相同的号码,在这种情况下,您需要一种机制来阻止员工号码被客户使用。保留数字范围是一种简单的方法。
如果没有,您可以向员工添加一个 customer_number 列,这将是一个外键。
对于前三个表(Employee、Customer 和 Product),主键是 ID 列。对于购买,您需要创建一个列,因为同一名员工可以多次购买相同的产品。自动增量列作为主键会更简单。 2 个 ID 列是外键。

You have several possibilities. The simplest would be to use the same number for both, in which case you need a mechanism to stop an employees number being used for a customer. A reserved range of numbers would be a simple way.
If not you could add a customer_number column to employees, which would be a foreign key.
For the first three tables, Employee, Customer and Product, the primary key is the ID column. For purchases you need to invent a column as the same employee could buy the same product more than once. An auto increment column as primary key would be simpler. The 2 ID columns are foreign keys.

沫雨熙 2025-01-16 23:58:21

来自评论:

<块引用>

当员工是客户时,EmployeeIDCustomerID 匹配吗?如果是这样,那已经是您的外键了。如果不是,您如何确定特定员工何时是特定客户?

不幸的是,它们不匹配。根据名字、姓氏和地址,客户是员工

您不希望存在外键关系;相反,当您显示数据时,您需要连接 firstnamelastnameaddress 上的表。

SELECT c.firstname || ' ' || c.lastname AS name
       -- other columns you want to display
FROM   customers c
       INNER JOIN employees e
       ON (   c.firstname = e.firstname
          AND c.lastname  = e.lastname
          AND c.address   = e.address)
       -- and then join the product and purchase tables to get the other data

但是,当您的 John SmithJoanne Smith 居住在同一地址并且他们都以 J Smith 的身份存储在您的数据库中时,他们将即使他们不是同一个人,也能匹配。或者,您在客户表中有一个名为 Joanne Smith 的人,在员工表中名为 Jo Smith,那么他们将不匹配,即使他们是同一个人。等等。

您想要找到比匹配姓名和地址更好的解决方案。

我需要在客户表或员工表中创建外键才能编写此查询。

不,您真正想做的是将表重构为第三范式,这样就不会重复日期并确保 id 列在 customers之间匹配员工;您可以通过包含 people 表来做到这一点:

CREATE TABLE people (
  id        NUMBER(10,0)
            GENERATED ALWAYS AS IDENTITY
            CONSTRAINT people__id__pk PRIMARY KEY,
  firstname VARCHAR2(100)
            NOT NULL,
  lastname  VARCHAR2(100)
            NOT NULL,
  address   VARCHAR2(500)
            NOT NULL
);

CREATE TABLE employees (
  id         CONSTRAINT employees__id__pk PRIMARY KEY
             CONSTRAINT employees__id__fk REFERENCES people,
  salary     NUMBER(10,2),
  department NUMBER(5,0),
  position   VARCHAR2(100)
);

CREATE TABLE customers (
  id         CONSTRAINT customers__id__pk PRIMARY KEY
             CONSTRAINT customers__id__fk REFERENCES people,
  company    VARCHAR2(100)
);

然后您就知道客户和员工是否具有相同的 id 那么他们是同一个人。

From comments:

When an Employee is a Customer, do the EmployeeID and CustomerID match? If so, that's already your foreign key. If they don't, how do you determine when a specific Employee is a specific Customer?

They don't match unfortunately. Customers are employees based on the firstname, lastname and address

You do not want a foreign key relationship; instead when you display the data you would need to join the tables on the firstname, lastname and address.

SELECT c.firstname || ' ' || c.lastname AS name
       -- other columns you want to display
FROM   customers c
       INNER JOIN employees e
       ON (   c.firstname = e.firstname
          AND c.lastname  = e.lastname
          AND c.address   = e.address)
       -- and then join the product and purchase tables to get the other data

However, when you have John Smith and Joanne Smith living at the same address and they are both stored in your database as J Smith then they will match even though they are not the same person. Or you have one person in the customer table under the name Joanne Smith and in the employee table called Jo Smith then they will not match, even though they are the same person. Etc.

You want to find a better solution than matching on names and addresses.

I need to create a foreign key in either the customer table or the employee table in order to write this query.

No, what you really want to do is refactor your tables into third-normal form so that you do not duplicate date and ensure the id columns match between customers and employees; you can do that by including a people table:

CREATE TABLE people (
  id        NUMBER(10,0)
            GENERATED ALWAYS AS IDENTITY
            CONSTRAINT people__id__pk PRIMARY KEY,
  firstname VARCHAR2(100)
            NOT NULL,
  lastname  VARCHAR2(100)
            NOT NULL,
  address   VARCHAR2(500)
            NOT NULL
);

CREATE TABLE employees (
  id         CONSTRAINT employees__id__pk PRIMARY KEY
             CONSTRAINT employees__id__fk REFERENCES people,
  salary     NUMBER(10,2),
  department NUMBER(5,0),
  position   VARCHAR2(100)
);

CREATE TABLE customers (
  id         CONSTRAINT customers__id__pk PRIMARY KEY
             CONSTRAINT customers__id__fk REFERENCES people,
  company    VARCHAR2(100)
);

Then you know if a customer and an employee has the same id then they are the same person.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文