简体   繁体   English

使用变量作为外键ID将数据插入2个表中

[英]Inserting data into 2 tables using a variable as the ID for the foreign key

So I have two MySQL tables (Lets say these are rough tables) Location is a multivalued attribute of the Delivery, so I decided to break it off into its own table 因此,我有两个MySQL表(可以说这些是粗糙表)Location是Delivery的多值属性,因此我决定将其分解为自己的表

CREATE TABLE `Delivery` (
  `Date` varchar(10) NOT NULL,
  `Time` varchar(10) NOT NULL,
  `OrderTotal` float NOT NULL,
  `DeliveryID` int(11) PRIMARY KEY AUTO_INCREMENT ,
  `Tip$` float NOT NULL,
  `Username` varchar(50) NOT NULL
) 

And

CREATE TABLE `Location` (
  `DeliveryID` int(10) NOT NULL,
  `APT` varchar(10) DEFAULT NULL,
  `Street` varchar(50) NOT NULL,
  `Address` int(20) NOT NULL, 
) FOREIGN KEY ('DeliveryID') REFERENCES `Delivery` (`DeliveryID`);

So what I want to do is say I insert into delivery with its own respective data, and add data to the location table with its own matching id to the delivery obviously. 因此,我想做的是说我将带有其各自数据的数据插入到传递中,然后将具有自己匹配ID的数据添加到位置表中。 I've been told to break multivalued attributes into their own table. 有人告诉我将多值属性分解为自己的表。 So what I have been doing is 所以我一直在做的是

INSERT INTO Delivery 
VALUES (DEFAULT, '12345','10:53am','admin','100','10');

SELECT @Var :=  Last_INSERT_ID();

INSERT INTO Location
VALUES (@Var, '1234','address',DEFAULT);

My Question is, is this bad practice? 我的问题是,这是不好的做法吗? Or is there any way to get around this, like just having a bigger Delivery table that has all Location data inside of it? 还是有什么方法可以解决这个问题,例如只拥有一个更大的Delivery表,其中包含所有Location数据? My fear is, that say I have multiple users of this database somehow someway they will be able to insert into the Delivery table and mess up my synchronization in the 0.0005 Seconds it takes to grab the MAX(DeliveryID) from the table? 我担心的是,这意味着我有这个数据库的多个用户,无论如何,他们将能够插入到Delivery表中,并在从表中获取MAX(DeliveryID)花费0.0005秒的时间弄乱我的同步? For a project of this size I know it won't be a problem, but if this were to be a large scale project can errors crop up like I have mentioned? 对于一个如此大的项目,我知道这不会有问题,但是如果这是一个大型项目,是否会像我提到的那样出现错误?

It's much better to use LAST_INSERT_ID() because it is maintained by session. 使用LAST_INSERT_ID()更好,因为它是由会话维护的。 Sessions can still be shared but it's definitely less risky that getting the max key from the table. 会话仍然可以共享,但是从表中获取最大密钥绝对没有风险。

If you have multiple sessions inserting in the same table, you'll certainly have problems with MAX() 如果您在同一张表中插入多个会话,那么MAX()肯定会出现问题

Keep in mind that LAST_INSERT_ID() only works for auto incremented keys. 请记住,LAST_INSERT_ID()仅适用于自动增量键。

An alternative approach would be to select the last item (order by delivery_id desc) from the delivery table that matches all criteria identifying an item as unique - ex. 一种替代方法是从交货表中选择与将某项标识为唯一的所有标准匹配的最后一项(按delivery_id desc排序)。 useraname, date, order total. 用户名,日期,订单总数。 Then you can have confidence that the record was successfully created and that you have the right id. 然后,您可以确信该记录已成功创建,并且您具有正确的ID。

I would also recommend that you add a primary key to your location table. 我还建议您向位置表中添加一个主键。

You should be using the last inserted id instead ( https://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id ). 您应该改用最后插入的ID( https://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id )。 Never encountered your method before of doing an extra select with MAX() and yes, it's possible for you to grab the wrong id with that approach. 在使用MAX()进行额外选择之前,从未遇到过您的方法,是的,您有可能用这种方法获取错误的ID。

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

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