Maison > base de données > tutoriel mysql > le corps du texte

mysql小技巧

WBOY
Libérer: 2016-06-07 15:01:30
original
1008 Les gens l'ont consulté

INSERT语法 INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [ IGNORE ] [INTO] tbl_name [( col_name ,...)] VALUES ({ expr | DEFAULT},...),(...),... [ ON DUPLICATE KEY UPDATE col_name = expr , ... ] 或: INSERT [LOW_PRIORITY | DELAYED | HIGH_

INSERT语法

<font>INSERT [LOW_PRIORITY | <strong><font>DELAYED</font></strong> | HIGH_PRIORITY] [<strong><font>IGNORE</font></strong>]</font>
Copier après la connexion
Copier après la connexion
<font>      [INTO] <em>tbl_name</em> [(<em>col_name</em>,...)]</font>
Copier après la connexion
Copier après la connexion
<font>      VALUES ({<em>expr</em> | DEFAULT},...),(...),...</font>
Copier après la connexion
<font>      [ <font>ON DUPLICATE KEY UPDATE</font> <span><em>col_name</em></span>=<em>expr</em>, ... ]</font>
Copier après la connexion

或:

<font>INSERT [LOW_PRIORITY | <strong><font>DELAYED</font></strong> | HIGH_PRIORITY] [<strong><font>IGNORE</font></strong>]</font>
Copier après la connexion
Copier après la connexion
<font>      [INTO] <em>tbl_name</em></font>
Copier après la connexion
<font>      SET <em>col_name</em>={<em>expr</em> | DEFAULT}, ...</font>
Copier après la connexion
<span><font>      [ <font>ON DUPLICATE KEY UPDATE</font> <em>col_name</em>=<em>expr</em>, ... ]</font></span>
Copier après la connexion
Copier après la connexion

或:

<font>INSERT [LOW_PRIORITY | HIGH_PRIORITY] [<strong><font>IGNORE</font></strong>]</font>
Copier après la connexion
<font>      [INTO] <em>tbl_name</em> [(<em>col_name</em>,...)]</font>
Copier après la connexion
Copier après la connexion
<font>      SELECT ...</font>
Copier après la connexion
<span><font>      [ <font>ON DUPLICATE KEY UPDATE</font> <em>col_name</em>=<em>expr</em>, ... ]</font></span>
Copier après la connexion
Copier après la connexion
Copier après la connexion
一、<strong><font>DELAYED</font></strong>的使用
Copier après la connexion
    使用延迟插入操作
Copier après la connexion
DELAYED调节符应用于INSERT和REPLACE语句。当DELAYED插入操作到达的时候,<br>
服务器把数据行放入一个队列中,并立即给客户端返回一个状态信息,这样客户<br>
端就可以在数据表被真正地插入记录之前继续进行操作了。如果读取者从该数据<br>
表中读取数据,队列中的数据就会被保持着,直到没有读取者为止。接着服务器<br>
开始插入延迟数据行(delayed-row)队列中的数据行。在插入操作的同时,服务器<br>
还要检查是否有新的读取请求到达和等待。如果有,延迟数据行队列就被挂起,<br>
允许读取者继续操作。当没有读取者的时候,服务器再次开始插入延迟的数据行。<br>
这个过程一直进行,直到队列空了为止。
Copier après la connexion
几点要注意事项:<br>
· INSERT DELAYED应该仅用于指定值清单的INSERT语句。服务器忽略用于INSERT DELAYED...SELECT语句的DELAYED。<br>
· 服务器忽略用于INSERT DELAYED...ON DUPLICATE UPDATE语句的DELAYED。<br>
· 因为在行被插入前,语句立刻返回,所以您不能使用LAST_INSERT_ID()来获取AUTO_INCREMENT值。AUTO_INCREMENT值可能由语句生成。<br>
· 对于SELECT语句,DELAYED行不可见,直到这些行确实被插入了为止。<br>
· DELAYED在从属复制服务器中被忽略了,因为DELAYED不会在从属服务器中产生与主服务器不一样的数据。
Copier après la connexion
<span>注意,目前在队列中的各行只保存在存储器中,直到它们被插入到表中为止。这意味着,如果您强行中止了mysqld(例如,使用kill -9)<br>
或者如果mysqld意外停止,则所有没有被写入磁盘的行都会丢失。</span>
Copier après la connexion
二、<font><strong>IGNORE</strong></font>的使用
Copier après la connexion
IGNORE是<u><strong>MySQL</strong></u>相对于标准SQL的扩展。如果在新表中有重复关键字,<br>
或者当STRICT模式启动后出现警告,则使用IGNORE控制ALTER TABLE的运行。<br>
如果没有指定IGNORE,当重复关键字错误发生时,复制操作被放弃,返回前一步骤。<br>
如果指定了IGNORE,则对于有重复关键字的行,只使用第一行,其它有冲突的行被删除。<br>
并且,对错误值进行修正,使之尽量接近正确值。
Copier après la connexion
insert ignore into tb(...) value(...)
Copier après la connexion
这样不用校验是否存在了,有则忽略,无则添加<br>
<br>
三、<font>ON DUPLICATE KEY UPDATE</font>的使用
Copier après la connexion
<p>如果您指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或<span>PRIMARY KEY</span>中出现重复值,则执行旧行UPDATE。例如,如果列a被定义为UNIQUE,并且包含值1,则以下两个语句具有相同的效果:</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">mysql> <strong>INSERT INTO table (a,b,c) VALUES (1,2,3)</strong>
Copier après la connexion
Copier après la connexion
      -> <span><strong>ON DUPLICATE KEY UPDATE c=c+1;</strong></span>
Copier après la connexion
 
Copier après la connexion
mysql> <strong>UPDATE table SET c=c+1 WHERE a=1;</strong>
Copier après la connexion

如果行作为新记录被插入,则受影响行的值为1;如果原有的记录被更新,则受影响行的值为2。

注释:如果列b也是唯一列,则INSERT与此UPDATE语句相当:

mysql> <strong>UPDATE table SET c=c+1 WHERE a=1 OR b=2 LIMIT 1;</strong>
Copier après la connexion

如果a=1 OR b=2与多个行向匹配,则只有一个行被更新。通常,您应该尽量避免对带有多个唯一关键字的表使用ON DUPLICATE KEY子句。

您可以在UPDATE子句中使用VALUES(col_name)函数从INSERT...UPDATE语句的INSERT部分引用列值。换句话说,如果没有发生重复关键字冲突,则UPDATE子句中的VALUES(col_name)可以引用被插入的col_name的值。本函数特别适用于多行插入。VALUES()函数只在INSERT...UPDATE语句中有意义,其它时候会返回NULL

示例:

mysql> <strong>INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6)</strong>
Copier après la connexion
      -> <strong>ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);</strong>
Copier après la connexion

本语句与以下两个语句作用相同:

mysql> <strong>INSERT INTO table (a,b,c) VALUES (1,2,3)</strong>
Copier après la connexion
      -> <span><strong>ON DUPLICATE KEY UPDATE c=3;</strong></span>
Copier après la connexion
mysql> <strong>INSERT INTO table (a,b,c) VALUES (4,5,6)</strong>
Copier après la connexion
      -> <strong>ON DUPLICATE KEY UPDATE c=9;</strong>
Copier après la connexion

当您使用ON DUPLICATE KEY UPDATE时,DELAYED选项被忽略。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!