Home >Database >Mysql Tutorial >关于Sqlite数据库Update语句的一点介绍 .

关于Sqlite数据库Update语句的一点介绍 .

WBOY
WBOYOriginal
2016-06-07 14:59:461332browse

Sqlite数据库 中的 Update语句 ,你能了解多少呢?因为这种微型数据库用到的语句非常少,所以可能我们不会经常的用到。但要想真正的玩转sqlite这种微型数据库,掌握这些语句的用法是非常重要的。本文我们就来介绍一下update语句的使用。 1.典型的Update(支

Sqlite数据库中的Update语句,你能了解多少呢?因为这种微型数据库用到的语句非常少,所以可能我们不会经常的用到。但要想真正的玩转sqlite这种微型数据库,掌握这些语句的用法是非常重要的。本文我们就来介绍一下update语句的使用。

1.典型的Update(支持)

<ol>
<li><span><span>Update  </span></span></li>
<li><span> </span></li>
<li><span>T1  </span></li>
<li><span> </span></li>
<li><span>Set  </span></li>
<li><span> </span></li>
<li>
<span><span>Column1</span></span><span>=</span><span><span>v1</span></span><span>,  </span>
</li>
<li><span> </span></li>
<li>
<span><span>Column2</span></span><span>=</span><span><span>V2</span></span><span> </span>
</li>
<li><span> </span></li>
<li><span>Where  </span></li>
<li><span> </span></li>
<li>
<span><span>key</span></span><span>=</span><span><span>V3</span></span><span>; </span>
</li>
</ol>

2.Update…From(很不幸,Sqlite是不支持的)

<ol>
<li><span><span>UPDATE  </span></span></li>
<li><span> </span></li>
<li><span>t1  </span></li>
<li><span> </span></li>
<li><span>SET  </span></li>
<li><span> </span></li>
<li>
<span><span>Column1</span></span><span>=</span><span><span>t2</span></span><span>.Column1  </span>
</li>
<li><span> </span></li>
<li><span>FROM  </span></li>
<li><span> </span></li>
<li><span>t2,t1  </span></li>
<li><span> </span></li>
<li><span>WHERE  </span></li>
<li><span> </span></li>
<li>
<span><span>t2.key</span></span><span>=</span><span><span>t1</span></span><span>.key; </span>
</li>
</ol>

要进行表间更新Update…From是必须的,居然Sqlite不支持,有什么别的办法吗?还确实有,替代方法有两种:

首先,Sqlite里面有一个新鲜玩意“INSERTORREPLACE”,跟Mysql类似,这个结构能够保证在存在的情况下替换,不存在的情况下更新,用这个机制就可以轻松实现Update…From了。

<ol>
<li><span><span>INSERTORREPLACEINTO  </span></span></li>
<li><span> </span></li>
<li><span>t1(key,Column1,Column2)  </span></li>
<li><span> </span></li>
<li><span>SELECT  </span></li>
<li><span> </span></li>
<li><span>t2.key,t2.Column1,t2.Column2  </span></li>
<li><span> </span></li>
<li><span>FROM  </span></li>
<li><span> </span></li>
<li><span>t2,t1  </span></li>
<li><span> </span></li>
<li><span>WHERE  </span></li>
<li><span> </span></li>
<li>
<span><span>t2.key</span></span><span>=</span><span><span>t1</span></span><span>.key; </span>
</li>
</ol>

备注:这种方法要避免插入操作,首先要确保是依照主键执行的更新,如果where条件不是主键可能就有点麻烦了。

要是不是主键的更新怎么办能?另外还有其他的办法吗?我们在这中情况下只能向典型的Update…where寻求帮助了,下面是一个例子:

<ol>
<li><span><span>UPDATE  </span></span></li>
<li><span> </span></li>
<li><span>t1  </span></li>
<li><span> </span></li>
<li><span>SET  </span></li>
<li><span> </span></li>
<li>
<span><span>Column1</span></span><span>=(</span><span><span>SELECTColumnxFROMt2WHEREt2.key</span></span><span>=</span><span><span>t1</span></span><span>.key),  </span>
</li>
<li><span> </span></li>
<li>
<span><span>Column2</span></span><span>=(</span><span><span>SELECTColumnyFROMt2WHEREt2.key</span></span><span>=</span><span><span>t1</span></span><span>.key),  </span>
</li>
<li><span> </span></li>
<li>
<span><span>WHEREt1.key</span></span><span>=(</span><span><span>SELECTkeyFROMt2WHEREt2.key</span></span><span>=</span><span><span>t1</span></span><span>.key); </span>
</li>
</ol>

下面举一个主从表的例子,一个部门表,一个成员表,成员表中的部门名称和代码是冗余的信息,以部门表中的部门名称和代码更新成员表中的冗余信息:

<ol>
<li><span><span>update  </span></span></li>
<li><span> </span></li>
<li><span>userlogins  </span></li>
<li><span> </span></li>
<li><span>set  </span></li>
<li><span> </span></li>
<li>
<span><span>deptname</span></span><span>=(selectdeptnamefromDepartMentswhereDepartMents.[DeptID]=userlogins.[DeptID]),  </span>
</li>
<li><span> </span></li>
<li>
<span><span>deptcode</span></span><span>=(selectdeptcodefromDepartMentswhereDepartMents.[DeptID]=userlogins.[DeptID])  </span>
</li>
<li><span> </span></li>
<li><span>where  </span></li>
<li><span> </span></li>
<li><span>userlogins.[DeptID]=(SELECT[DeptID]FROM DepartMentswhereDepartMents.[DeptID]=userlogins.[DeptID]); </span></li>
</ol>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn