简体   繁体   English

首先更新或删除然后INSERT

[英]UPDATE or DELETE first then INSERT

I'm asking this again, because I can't get a straight answer from other questions on SO. 我再次问这个问题,因为我无法直接回答其他问题。

My situation is as follows, two tables, phones and features . 我的情况如下,两个表, phonesfeatures

Structure: 结构体:

phones 手机

  • id ID
  • brand_id brand_id
  • model 模型
  • picture 图片
  • published 发表

features 特征

  • id ID
  • phone_id phone_id
  • feature 特征

Now, features contains a whole lot of values, which only between 1 & 4 will correspond to a certain phone via phone_id . 现在, features包含很多值,只有1到4之间的值将通过phone_id对应于某个手机。

Now, when I update features, do you think deleting and then inserting new features again or updating existing features will be best? 现在,当我更新功能时,您是否认为删除然后再次插入新功能或更新现有功能将是最佳选择?

// edit: //编辑:

specials 特价

  • id* ID*
  • phone_id phone_id
  • contract_id contract_id
  • start_publish start_publish
  • finish_publish finish_publish
  • published 发表

contract 合同

  • id* ID*
  • name 名称
  • benefit 效益
  • duration 持续时间
  • price, etc. 价格等

brand

  • id ID
  • name 名称
  • logo 商标

So basically, a special links a phone and a contract together, and grabs info from both tables for output. 所以基本上,一个特殊的联系电话和合同,并从两个表格中获取信息以输出。 Then during that process, each phone's features get loaded separately using a function. 然后在此过程中,每个手机的功能都会使用一个功能单独加载。

// edit 2: //编辑2:

Oh, and only one phone's features will be deleted/updated at a time, and also, only between 1-4 features will be deleted at a time. 哦, 一次只能删除/更新一部手机的功能,而且一次只能删除1-4个功能。 Also, features can always be more or less, and never a set number. 此外,功能可以总是或多或少,而不是一组数字。

当您“更新功能”时,您只需update

UPDATE should be used. 应该使用UPDATE。 Alternatively, deleting and inserting can be done in one REPLACE command. 或者,可以在一个REPLACE命令中完成删除和插入。

http://dev.mysql.com/doc/refman/5.0/en/replace.html http://dev.mysql.com/doc/refman/5.0/en/replace.html

Also, if features can be shared (many to many relationship), you may want to consider a third table that only links phone ids to feature ids. 此外,如果可以共享功能(多对多关系),您可能需要考虑仅将电话ID与功能ID相关联的第三个表。

If the number of features per phone will ever change, then you must delete and insert. 如果每部手机的功能数量发生变化,则必须删除并插入。 To update, you'd need to: 要更新,您需要:

  • get the current set of features 获取当前的功能集
  • figure out what is different, what was added, and what was removed 弄清楚什么是不同的,添加了什么,以及删除了什么
  • run queries to update, insert new data, and delete old data. 运行查询以更新,插入新数据和删除旧数据。

Where as if you just delete the whole list and re-insert them, you save yourself some maintenance headaches. 就像你只是删除整个列表并重新插入它们一样,你可以省去一些维护问题。

If consistency of your data is an issue or concern (the data is deleted but an error happens before insert causing the script to not complete) then wrap your queries in a transaction. 如果数据的一致性是一个问题或问题(数据被删除但在插入导致脚本无法完成之前发生错误),则将查询包装在事务中。

Take this example: 举个例子:

I have a blog with Posts(id, title, body) and Categories(id, post_id, category) . 我有一个博客Posts(id, title, body)Categories(id, post_id, category) My first Post, "Hello World" is categorized under "meta, php, and javascript". 我的第一篇文章“Hello World”被归类为“meta,php和javascript”。 Then a year later, I revise the post to have some information about mysql (or I add a mysql category) and take out javascript. 然后一年后,我修改帖子以获得有关mysql的一些信息(或者我添加一个mysql类别)并取出javascript。

Here's what I'd need to do (mostly psuedocode): 这是我需要做的(主要是伪代码):

//old set
SELECT * FROM Categories WHERE post_id=1

foreach($categories as $category) {
    if(in_array($category['id'], $_POST['categories']) {
        //this category is in the new set and the old set, do nothing
    }
    elseif(!in_array($category['id'], $_POST['categories']) {
        //this category is in the old set, but not the new one, delete it
        $queries[] = "DELETE FROM Categories WHERE id=".$category['id'];
    }
    else {
        //new category, add it
        $queries[] = "INSERT INTO Categories()..."
    }
}

foreach($queries as $item) {
    //run query
}

Versus this: 对此:

DELETE FROM Categories WHERE post_id=$_POST['id']

foreach($_POST['categories'] as $item) {
    INSERT INTO Categories() ...
}

Personally, I think option #2 is better. 就个人而言,我认为选项#2更好。

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

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