本文的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>

SQL中MINUS的用法及具体代码示例在SQL中,MINUS是一种用于在两个结果集之间执行差集操作的运算符。它用于从第一个结果集中删除与第二个结果集中相同的行。MINUS操作符返回的结果集将包含仅存在于第一个结果集中的行。下面通过具体的代码示例来演示MINUS的用法:假设有两个表-"table1"和"table2",它们的结构如下:表名:table1字段

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

在win10上使用edge浏览器的小伙伴们很多都遇到了网页一直不停重复打开的问题很是头疼,那么该怎么解决呢?下面就一起来看看详细的解决方法吧。edge浏览器一直重复打开网页怎么办:1、进入edge浏览器,点击右上角的三个点。2、在任务栏中点击“设置”。3、找到“Microsoftedge打开方式”。4、点击下拉,选择“起始页”。5、完成后重新启动浏览器即可解决问题。

SolutionYes,Wecaninsertnullvaluestoalisteasilyusingitsadd()method.IncaseofListimplementationdoesnotsupportnullthenitwillthrowNullPointerException.Syntaxbooleanadd(Ee)将指定的元素追加到此列表的末尾。类型参数E −元素的运行时类型。参数e −要追加到此列表的元

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

1、打开wps软件,进入wps文字的操作界面。2、在该界面内找到插入选项。3、点击插入选项,在其编辑区域内找到形状选项。4、点击形状选项,在其子级菜单那里找到推荐选项。5、在推荐选项内找到中国地图选项。6、点击中国地图选项,用鼠标左键在编辑输入区里拖拽,就得到了我们需要的中国地图。

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了mysql高级篇的一些问题,包括了索引是什么、索引底层实现等等问题,下面一起来看一下,希望对大家有帮助。

在mysql中,是否需要commit取决于存储引擎:1、若是不支持事务的存储引擎,如myisam,则不需要使用commit;2、若是支持事务的存储引擎,如innodb,则需要知道事务是否自动提交,因此需要使用commit。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use
