mysql插入大量資料時避免重複記錄出現

來源:互聯網
上載者:User

1.我們先來添加聯合唯一索引,一般就是除去id欄位後的其他欄位

ALTER ignore TABLE `test` ADD unique INDEX( `loan_account`, `num`, `name`, `monney_start`, `monney_yue`, `date`, `end`, `li`, `jiaoyi`);

注意這個ignore

 

2.然後我們插入相同的資料時他會報錯,怎麼辦呢。我們先這樣做。

INSERT IGNORE INTO `test`.`test` (`id`, `loan_account`, `num`, `name`, `monney_start`, `monney_yue`, `date`, `end`, `li`, `jiaoyi`) VALUES (NULL, '66815011232000638015', '106610104809', '洛陽強匯商貿有限公司', '1000000', '1000001', '20130807', '20140607', '11.28', '20130807')

這樣我們再插入一條相同的記錄時他就不會報錯了。而且會顯示插入了0行。

本文出自 “編程之路” 部落格,請務必保留此出處http://huangby.blog.51cto.com/5199904/1293270

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.