简体   繁体   English

MySQL表关系/外键?

[英]Mysql tables relationships / Foreign key?

I'm having a hard time figuring how to link database rows in a PHP / MySql project. 我很难弄清楚如何在PHP / MySql项目中链接数据库行。 My order submission script currently splits information and stores it into 2 tables. 我的订单提交脚本当前将信息拆分并将其存储到2个表中。

The first one is called "Orders" and contains: 第一个称为“订单”,其中包含:

$OrderId, $CustomerName, $CustomerEmail, $OrderTotal, $OrderTaxes
//and other infos about the ORDER

The second one is called "Items" and contains all the BOUGHT products infos: 第二个称为“商品”,其中包含所有BOUGHT产品的信息:

$ProductId, $OrderedQty 
//for each one and such...

It has to be this way because the "Items" table will be searched by different "departments" who will only be shown the parts of the orders they are responsible for. 之所以必须这样,是因为“项目”表将由不同的“部门”搜索,这些“部门”将仅显示其负责的订单部分。 But they all have to get the "Orders" infos for shipping purposes. 但他们所有人都必须获取“订单”信息以进行运输。

Knowing that the "OrderId" column is a primary key generated on the "Orders" table itself, and that my INSERT TO commands are both executed at the same time, how can I link an "Order Id" column in both tables ? 知道“ OrderId”列是在“ Orders”表本身上生成的主键,并且我的INSERT TO命令都同时执行,如何在两个表中链接“ Order Id”列?

Do I have to generate some random key to match them ? 我是否需要生成一些随机密钥来匹配它们?

If I were to use a foreign key, how would the database know which product goes with which order since they are submited at the same time ? 如果我要使用外键,由于数据库是同时提交的,数据库将如何知道哪个产品按哪个订单进行?

Or is it fast enough to INSERT in "Orders" -> SELECT $OrderID -> INSERT in "Items" ? 还是在“订单”-> SELECT $ OrderID->在“项目”中插入INSERT的速度足够快?

How does one usually do this ? 通常如何做到这一点? Can't figure this one out. 无法弄清楚这一点。

Thanks in advance for your precious help! 在此先感谢您的宝贵帮助!

The bought product info should have an extra column the bought product tables called orderid, so you know which products belong to which order. 购买产品信息应在购买产品表中有一个额外的列,称为orderid,以便您知道哪些产品属于哪个订单。 As for the inserting in to the database this depends on what you are using to execute the queries. 至于插入数据库,这取决于您用于执行查询的内容。 Some query classes allow you to run multiple query statements in one go, if this is the case you could run something similar to: 一些查询类允许您一次运行多个查询语句,如果是这种情况,则可以运行类似于以下内容的代码:

INSERT INTO Orders (OrderId, CustomerName, CustomerEmail, OrderTotal, OrderTaxes) Values(...)
SET @order_id = LAST_INSERT_ID();
INSERT INTO boughtItems (OrderId,ProductId,OrderedQty) Values (@order_id, :productid_1, :name_1),(@order_id, :productid_2, :name_2),(@order_id, :productid_3, :name_3) ....

In order cases you would need to run the insert statement on orders and then obtain the primary key. 在订单情况下,您将需要对订单运行insert语句,然后获取主键。 Take a look at these links: 看一下这些链接:

In other cases you could use a class which allows you to obtain the last inserted id. 在其他情况下,您可以使用允许您获取最后插入的ID的类。 This id is connection bound so should give no issues (as long as the insert works, you are not doing multiple inserts in one query, do rollbacks or other weird stuff). 此ID是受连接限制的,因此不会出现任何问题(只要插入有效,您就不会在一个查询中进行多次插入,回滚或其他奇怪的操作)。 In this case you would do an insert and then call a secondary function to get the inserted id. 在这种情况下,您将执行插入操作,然后调用辅助函数来获取插入的ID。 See these links: 请参阅以下链接:

Alternatively you could also execute 2 queries. 或者,您也可以执行2个查询。 First the insert query followed by this query: 首先是插入查询,然后是此查询:

SELECT LAST_INSERT_ID() as id;

Other related links: 其他相关链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM