本文的unique列上插入重复值解决方案,主要基于MySQL平台。通过这些,可以做到一些新的功能和应用。希望本文能对大家有所帮助。 当unique列在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ON DUPLICATE K
本文的unique列上插入重复值解决方案,主要基于MySQL平台。通过这些,可以做到一些新的功能和应用。希望本文能对大家有所帮助。
当unique列在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ON DUPLICATE KEY UPDATE子句跳过INSERT、中断操作或者更新旧记录为新值。
<ol class="dp-sql"> <li class="alt"><span><span>mysql> </span><span class="keyword">create</span><span> </span><span class="keyword">table</span><span> menus(id tinyint(4) </span><span class="op">not</span><span> </span><span class="op">null</span><span> auto_increment, </span></span></li> <li class=""> <span> -> label </span><span class="keyword">varchar</span><span>(10) </span><span class="op">null</span><span>,url </span><span class="keyword">varchar</span><span>(20) </span><span class="op">null</span><span>,</span><span class="keyword">unique</span><span> </span><span class="keyword">key</span><span>(id)); </span> </li> <li class="alt"> <span>Query OK, 0 </span><span class="keyword">rows</span><span> affected (0.13 sec) </span> </li> <li class=""> <span>mysql> </span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> menus(label,url) </span><span class="keyword">values</span><span>(</span><span class="string">'Home'</span><span>,</span><span class="string">'home.html'</span><span>); </span> </li> <li class="alt"><span>Query OK, 1 row affected (0.06 sec) </span></li> <li class=""> <span>mysql> </span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> menus(label,url) </span><span class="keyword">values</span><span>(</span><span class="string">'About us'</span><span>,</span><span class="string">'aboutus.html'</span><span>); </span> </li> <li class="alt"><span>Query OK, 1 row affected (0.05 sec) </span></li> <li class=""> <span>mysql> </span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> menus(label,url) </span><span class="keyword">values</span><span>(</span><span class="string">'Services'</span><span>,</span><span class="string">'services.html'</span><span>); </span> </li> <li class="alt"><span>Query OK, 1 row affected (0.05 sec) </span></li> <li class=""> <span>mysql> </span><span class="keyword">insert</span><span> </span><span class="keyword">into</span><span> menus(label,url) </span><span class="keyword">values</span><span>(</span><span class="string">'Feedback'</span><span>,</span><span class="string">'feedback.html'</span><span>); </span> </li> <li class="alt"><span>Query OK, 1 row affected (0.05 sec) </span></li> </ol>
<ol class="dp-sql"> <li class="alt"><span><span>mysql> </span><span class="keyword"><strong><font color="#006699">select</font></strong></span><span> * </span><span class="keyword"><strong><font color="#006699">from</font></strong></span><span> menus; </span></span></li> <li class=""> <span>+</span><span class="comment"><font color="#008200">----+----------+---------------+ </font></span><span> </span> </li> <li class="alt"><span>| id | label | url | </span></li> <li class=""> <span>+</span><span class="comment"><font color="#008200">----+----------+---------------+ </font></span><span> </span> </li> <li class="alt"><span>| 1 | Home | home.html | </span></li> <li class=""><span>| 2 | About us | aboutus.html | </span></li> <li class="alt"><span>| 3 | Services | services.html | </span></li> <li class=""><span>| 4 | Feedback | feedback.html | </span></li> <li class="alt"> <span>+</span><span class="comment"><font color="#008200">----+----------+---------------+ </font></span><span> </span> </li> <li class=""> <span>4 </span><span class="keyword"><strong><font color="#006699">rows</font></strong></span><span> </span><span class="op"><font color="#808080">in</font></span><span> </span><span class="keyword"><strong><font color="#006699">set</font></strong></span><span> (0.00 sec) </span> </li> </ol>
如果现在在unique列插入一条违背唯一约束的记录,MySQL会中断操作,提示出错:
<ol class="dp-sql"> <li class="alt"><span><span>mysql> </span><span class="keyword"><strong><font color="#006699">insert</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">into</font></strong></span><span> menus(id,label,url) </span><span class="keyword"><strong><font color="#006699">values</font></strong></span><span>(4,</span><span class="string"><font color="#0000ff">'Contact us'</font></span><span>,</span><span class="string"><font color="#0000ff">'contactus.html'</font></span><span>); </span></span></li> <li class=""> <span>ERROR 1062 (23000): Duplicate entry </span><span class="string"><font color="#0000ff">'4'</font></span><span> </span><span class="keyword"><strong><font color="#006699">for</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">key</font></strong></span><span> </span><span class="string"><font color="#0000ff">'id'</font></span><span> </span> </li> </ol>
在前面的INSERT语句添加IGNORE关键字时,如果认为语句违背了唯一约束,MySQL甚至不会尝试去执行这条语句,因此,下面的语句不会返回错误:
<ol class="dp-sql"> <li class="alt"><span><span>mysql> </span><span class="keyword"><strong><font color="#006699">insert</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">ignore</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">into</font></strong></span><span> menus(id,label,url) </span><span class="keyword"><strong><font color="#006699">values</font></strong></span><span>(4,</span><span class="string"><font color="#0000ff">'Contact us'</font></span><span>,</span><span class="string"><font color="#0000ff">'contactus.html'</font></span><span>); </span></span></li> <li class=""> <span>Query OK, 0 </span><span class="keyword"><strong><font color="#006699">rows</font></strong></span><span> affected (0.00 sec) </span> </li> <li class="alt"> <span>mysql> </span><span class="keyword"><strong><font color="#006699">select</font></strong></span><span> * </span><span class="keyword"><strong><font color="#006699">from</font></strong></span><span> menus; </span> </li> <li class=""> <span>+</span><span class="comment"><font color="#008200">----+----------+---------------+ </font></span><span> </span> </li> <li class="alt"><span>| id | label | url | </span></li> <li class=""> <span>+</span><span class="comment"><font color="#008200">----+----------+---------------+ </font></span><span> </span> </li> <li class="alt"><span>| 1 | Home | home.html | </span></li> <li class=""><span>| 2 | About us | aboutus.html | </span></li> <li class="alt"><span>| 3 | Services | services.html | </span></li> <li class=""><span>| 4 | Feedback | feedback.html | </span></li> <li class="alt"> <span>+</span><span class="comment"><font color="#008200">----+----------+---------------+ </font></span><span> </span> </li> <li class=""> <span>4 </span><span class="keyword"><strong><font color="#006699">rows</font></strong></span><span> </span><span class="op"><font color="#808080">in</font></span><span> </span><span class="keyword"><strong><font color="#006699">set</font></strong></span><span> (0.00 sec) </span> </li> </ol>
当有很多的INSERT语句需要被顺序地执行时,IGNORE关键字就使操作变得很方便。使用它可以保证不管哪一个INSERT包含了重复的键值,MySQL都回跳过它(而不是放弃全部操作)。
在这种情况下,我们还可以通过添加MySQL4.1新增加的ON DUPLICATE KEY UPDATE子句,使MySQL自动把INSERT操作转换为UPDATE操作。这个子句必须具有需要更新的字段列表,这个列表和UPDATE语句使用的列表相同。
<ol class="dp-sql"> <li class="alt"><span><span>mysql> </span><span class="keyword"><strong><font color="#006699">insert</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">into</font></strong></span><span> menus(id,label,url) </span><span class="keyword"><strong><font color="#006699">values</font></strong></span><span>(4,</span><span class="string"><font color="#0000ff">'Contact us'</font></span><span>,</span><span class="string"><font color="#0000ff">'contactus.html'</font></span><span>) </span></span></li> <li class=""> <span> -> </span><span class="keyword"><strong><font color="#006699">on</font></strong></span><span> duplicate </span><span class="keyword"><strong><font color="#006699">key</font></strong></span><span> </span><span class="keyword"><strong><font color="#006699">update</font></strong></span><span> label=</span><span class="string"><font color="#0000ff">'Contact us'</font></span><span>,url=</span><span class="string"><font color="#0000ff">'contactus.html'</font></span><span>; </span> </li> <li class="alt"> <span>Query OK, 2 </span><span class="keyword"><strong><font color="#006699">rows</font></strong></span><span> affected (0.05 sec) </span> </li> </ol>
在这种情况下,如果MySQL发现表已经包含具有相同唯一键的记录,它会自动更新旧的记录为ON DUPLICATE KEY UPDATE从句中指定的新值:
<ol class="dp-sql"> <li class="alt"><span><span>mysql> </span><span class="keyword"><strong><font color="#006699">select</font></strong></span><span> * </span><span class="keyword"><strong><font color="#006699">from</font></strong></span><span> menus; </span></span></li> <li class=""> <span>+</span><span class="comment"><font color="#008200">----+------------+----------------+ </font></span><span> </span> </li> <li class="alt"><span>| id | label | url | </span></li> <li class=""> <span>+</span><span class="comment"><font color="#008200">----+------------+----------------+ </font></span><span> </span> </li> <li class="alt"><span>| 1 | Home | home.html | </span></li> <li class=""><span>| 2 | About us | aboutus.html | </span></li> <li class="alt"><span>| 3 | Services | services.html | </span></li> <li class=""><span>| 4 | Contact us | contactus.html | </span></li> <li class="alt"> <span>+</span><span class="comment"><font color="#008200">----+------------+----------------+ </font></span><span> </span> </li> <li class=""> <span>4 </span><span class="keyword"><strong><font color="#006699">rows</font></strong></span><span> </span><span class="op"><font color="#808080">in</font></span><span> </span><span class="keyword"><strong><font color="#006699">set</font></strong></span><span> (0.01 sec) </span> </li> </ol>