主表中需要的主要或独特的约束:“订单”
抱歉,如果标题不清楚。
截至目前,我只有一个包含大量客户和订单的电子表格。例如,电子表格中的一行可能如下所示:
ID 为 1
且姓名为 Sally
且地址为 291 North Street
的客户购买了商品id 2.
实际的电子表格类似于此表:
客户 ID | 客户名称 | 客户地址 | 项目 ID | 名称 | 成本 | 订单 ID | 订购日期 |
---|---|---|---|---|---|---|---|
1 | Sally | 291 North Street | 2 | Long Sleeves | $20 | 1 | 1/1/2022 |
1 | Sally | 291 North Street | 1 | 衬衫 | $15 | 1 | 1/1/2022 |
2 | George | 892 Lakes Ave | 3 | 背包 | $30 | 5 | 4/9/2022 |
我的目标是正确标准化此数据,使其不那么冗余。我已将数据分成 3 个表:Items
、Orders
和 OrderInfo
。
商品遵循如下结构:
商品 ID (PK) | 名称 | 成本 |
---|---|---|
1 | XL 衬衫 | $15 |
2 | 长袖衬衫 | $20 |
3 | 背包 | $30 |
订单:
订单 ID (PK/FK?) | 客户 ID | 订购日期 |
---|---|---|
1 | 1 | 1/1/2022 |
5 | 2 | 4 /9/2022 |
订单信息:
订单 ID(PK/FK?) | 商品 ID(PK/FK?) |
---|---|
1 | 2 |
1 | 1 |
5 | 3 |
正如您从订单表中看到的,我尝试合并所有冗余订单,其中用户 Sally 在同一订单中订购了一件长袖衬衫和一件普通衬衫。但是,这会在 OrdersInfo 表中留下冗余数据,其中多个字段的 OrderId 相同,因为客户在一个订单中购买了多个商品。
这是正确的吗?我正在尝试在 LibreOffice Base 中的表上定义关系,并且可以为除 OrderInfo 和 Orders 之外的所有表定义正确的一对多关系。
这是我尝试链接 OrderID 字段时关系和错误的屏幕截图。
错误代码:
SQL Status: S0011
Error code: -170
Primary or unique constraint required on main table: "Orders" in statement [ALTER TABLE "Order_Info" ADD FOREIGN KEY ("order_id") REFERENCES "Orders" ("order_id")]
Sorry if the title is unclear.
As of right now, I just have a spreadsheet of a bunch of customers and orders. For example, a line in the spreadsheet might look like:
A Customer with an ID of 1
with name Sally
and address 291 North Street
bought item id 2.
The actual spreadsheet looks something like this table:
Customer Id | Customer Name | Customer Address | Item Id | Name | Cost | Order Id | Ordered Date |
---|---|---|---|---|---|---|---|
1 | Sally | 291 North Street | 2 | Long Sleeves | $20 | 1 | 1/1/2022 |
1 | Sally | 291 North Street | 1 | Shirt | $15 | 1 | 1/1/2022 |
2 | George | 892 Lakers Ave | 3 | Backpack | $30 | 5 | 4/9/2022 |
My goal is to properly normalize this data so it's not as redundant. I've already separated the data into 3 tables, Items
, Orders
, and OrderInfo
.
Items follows a structure like so:
Item Id (PK) | Name | Cost |
---|---|---|
1 | XL Shirt | $15 |
2 | Long sleeves shirt | $20 |
3 | Backpack | $30 |
Orders:
Order ID (PK/FK?) | Customer ID | Ordered Date |
---|---|---|
1 | 1 | 1/1/2022 |
5 | 2 | 4/9/2022 |
OrderInfo:
Order ID (PK/FK?) | Item ID (PK/FK?) |
---|---|
1 | 2 |
1 | 1 |
5 | 3 |
As you can see from the orders table, I tried to combine all redundant orders where say user Sally ordered a long sleeves shirt and a regular shirt in the same order. However, this leaves redundant data in the OrdersInfo table, where the OrderId is the same for multiple fields because the customer bought multiple items in one order.
Is this correct? I am trying to define relationships on the tables in LibreOffice Base, and I can define the correct one-to-many relationships for all of them except for OrderInfo and Orders.
Here's a screenshot of the relations and the error when I try to link the OrderID field.
Error code:
SQL Status: S0011
Error code: -170
Primary or unique constraint required on main table: "Orders" in statement [ALTER TABLE "Order_Info" ADD FOREIGN KEY ("order_id") REFERENCES "Orders" ("order_id")]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
外键必须引用引用表的主键(或唯一键)。如果没有定义此类主/唯一键,您将获得显示的错误。
例子:
A foreign key must reference the primary key (or unique key) of the referenced table. You will get the error you show if no such primary/unique key is defined.
Example: