更新所有记录的外键字段以反映新的主键

时间:2013-09-09 21:02:50

标签: mysql foreign-keys auto-increment

            idcustomers,firstname,lastname,address1,address2,city,state,zip,phone,email,cell,company,newid
            307,Annaa,Mondet,"705 Congressional Rd.",,"Simi Valley",CA,93065,"(805) 300-3188",,,,1
            308,Gayla,Lindquist,"2752 Sapra st",,"Thousand Oaks",CA,91360,"(805) 750-8429",,,,2
            309,Suzie,Dotan,"4568 Winnetka Ave.",,"Woodland Hills",CA,91364,"(818) 346-1732",,,,3
            310,Katie,Mulloy,"4024 Revello Pl.",,Moorpark,CA,93021,"(805) 390-7440",,,,4
            311,,,"2935 Shadowbrook ave",,Westlake,CA,91361,,,,NULL,5
            312,"Steve & Kimber",Foy,"2874 Chippewaare Ave",,"Simi Valley",CA,93063,"(805) 582-0851",,"(818) 292-4550",,6
            313,Robert,Clarke,"17433 Smoke tree",,"Santa Clarita",CA,91387,"(661) 373-8922",,,,7
            314,Brett,Bucciocco,"5266 Via Capote",,"Newbury Park",CA,91320,"(818) 599-8809",,,,8
            315,Allan,Ray,"784 Holbertson Ct.",,"Simi Valley",CA,93065,"(805) 522-1412",area1@sbcgobal.net,,,9
            316,John,Zahedi,"5122 Mammoth Ave.",,"Sherman Oaks",CA,91423,"(818) 501-5344",,,,10
            317,Carmin,Appice,"26848 Marina Point Ct.",,Valencia,CA,91355,"(661) 367-4442",powerrock@sbc.gobal,"(661) 618-6201 Son cell",,11
            318,Sam,han,"7433 Shadyglade Ave #3",,"North Hollywood",CA,91605,"(213) 700-9978",hanandkoo@yahoo.com,,,12
            319,Rose,Lane,"6459 Deerbrook Cir.",,"Oak Park",CA,91377,"(818) 624-7673",,,,13
            320,Eric,Scott,"3190 Dalhart Ave",,"Simi Valley",CA,93063,"(805) 501-0185",,,,14
            321,Donna,Vella,"13021 E. Cloverdale",,Moorpark,CA,93021,,DV4Re.Aol,,NULL,15


            idevents,title,start,end,allday,url,customerid,memo,dispatchstatus
            13,"AA Meeting","2013-09-30 00:00:00","2013-09-30 00:01:00",false,,336,"AA Meeting",scheduled
            14,"AA Meeting","2013-09-30 00:00:00","2013-09-30 00:01:00",false,NULL,307,"AA Meeting",scheduled
            15,"AA Meeting","2013-09-30 00:00:00","2013-09-30 00:01:00",false,NULL,308,"AA Meeting",scheduled

我在客户中创建了一个名为newid的新主键。事件的现场客户反映了客户中的客户。我希望idcustomers事件字段现在更改为newid中的新值。我开始查询但它还没有工作......

更新事件设置events.customerid = customers.newid where events.customerid = customers.customerid;

这可以通过更新完成吗?

提前致谢...

2 个答案:

答案 0 :(得分:2)

如果您实际上已经为MySql定义了一个外键,那么您的更新有问题,因为events.customerid引用customers.idcustomers而不是customers.newid。如果Customers.idcustomers中的customers.newid中的值尚不存在,那么您的参照完整性检查将导致更新失败。

假设这是一次性更新,您需要:

  • 删除events.customerid和customers.customerid之间的外键索引。
  • 对事件设置events.customerid = customers.newid
  • 执行更新
  • 在客户设置customers.customerid = customers.newid上执行更新。请注意,如果customers.customerid
  • 中已存在任何customers.newid,则会出现问题
  • 重新创建外键索引

另一种选择是:

  • 创建新客户表设置customers.customerid = customers.newid
  • 删除events.customerid和customers.customerid之间的外键索引。
  • 对事件设置events.customerid = customers.newid
  • 执行更新
  • 将原始客户表替换为新客户
  • 重新创建外键索引

答案 1 :(得分:1)

UPDATE
  events INNER JOIN customers
  ON events.customerid=customers.idcustomers
SET
  events.customerid = customers.newid

请参阅小提琴here