连接客户表和员工表的外键?
我有两个表:
Employee ,其中包含以下列:
EmployeeID | FirstName | LastName | Address | Salary | Department | Position |
---|
Customer ,其中包含以下列:
CustomerID | FirstName | LastName | Address | Company |
---|
我还有另外两个表:
>产品:
产品ID | 产品 | 价格 |
---|
购买:
产品ID | 客户ID | 购买日期 | 库存 |
---|
这是我需要创建的查询:
- 员工是客户报告 - 员工姓名、购买的产品、 产品成本,每位员工购买所有产品的总成本
通过查看数据,我已经知道所有客户也是员工。我需要在客户表或员工表中创建外键才能编写此查询。这就是我被困住的地方。
I have two tables:
Employee with the columns:
EmployeeID | FirstName | LastName | Address | Salary | Department | Position |
---|
Customer with the columns:
CustomerID | FirstName | LastName | Address | Company |
---|
I also have two other tables:
Product:
ProductID | Product | Price |
---|
Purchase:
ProductID | CustomerID | PurchaseDate | Inventory |
---|
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有几种可能性。最简单的方法是对两者使用相同的号码,在这种情况下,您需要一种机制来阻止员工号码被客户使用。保留数字范围是一种简单的方法。
如果没有,您可以向员工添加一个 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.
来自评论:
您不希望存在外键关系;相反,当您显示数据时,您需要连接
firstname
、lastname
和address
上的表。但是,当您的
John Smith
和Joanne Smith
居住在同一地址并且他们都以J Smith
的身份存储在您的数据库中时,他们将即使他们不是同一个人,也能匹配。或者,您在客户表中有一个名为Joanne Smith
的人,在员工表中名为Jo Smith
,那么他们将不匹配,即使他们是同一个人。等等。您想要找到比匹配姓名和地址更好的解决方案。
不,您真正想做的是将表重构为第三范式,这样就不会重复日期并确保
id
列在customers
和之间匹配员工
;您可以通过包含people
表来做到这一点:然后您就知道客户和员工是否具有相同的
id
那么他们是同一个人。From comments:
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
andaddress
.However, when you have
John Smith
andJoanne Smith
living at the same address and they are both stored in your database asJ Smith
then they will match even though they are not the same person. Or you have one person in the customer table under the nameJoanne Smith
and in the employee table calledJo 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.
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 betweencustomers
andemployees
; you can do that by including apeople
table:Then you know if a customer and an employee has the same
id
then they are the same person.