<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fatkun&#039;s blog &#187; 数据库</title>
	<atom:link href="http://fatkun.com/category/database/feed" rel="self" type="application/rss+xml" />
	<link>http://fatkun.com</link>
	<description>又一个 WordPress 站点</description>
	<lastBuildDate>Sat, 19 May 2012 17:25:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>mysql修改表、字段、库的字符集</title>
		<link>http://fatkun.com/2011/05/mysql-alter-charset.html</link>
		<comments>http://fatkun.com/2011/05/mysql-alter-charset.html#comments</comments>
		<pubDate>Mon, 02 May 2011 08:20:27 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[charset]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[字符集]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=898</guid>
		<description><![CDATA[修改数据库字符集： ALTER DATABASE db_name DEFAULT CHARACTER SET character_name &#91;COLLATE ...&#93;; 把表默认的字符集和所有字符列（CHAR,VARCHAR,TEXT）改为新的字符集： ALTER TABLE tbl_name CONVERT TO CHARACTER SET character_name &#91;COLLATE ...&#93; 如：ALTER TABLE logtest CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 只是修改表的默认字符集： ALTER TABLE tbl_name DEFAULT CHARACTER SET character_name &#91;COLLATE...&#93;; 如：ALTER TABLE logtest DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; 修改字段的字符集： ALTER TABLE tbl_name CHANGE c_name [...]]]></description>
			<content:encoded><![CDATA[<p>修改数据库字符集：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> db_name <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> character_name <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">COLLATE</span> <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#93;</span>;</pre></div></div>

<p>把表默认的字符集和所有字符列（CHAR,VARCHAR,TEXT）改为新的字符集：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> tbl_name <span style="color: #993333; font-weight: bold;">CONVERT</span> <span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> character_name <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">COLLATE</span> <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#93;</span>
如：<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> logtest <span style="color: #993333; font-weight: bold;">CONVERT</span> <span style="color: #993333; font-weight: bold;">TO</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;</pre></div></div>

<p>只是修改表的默认字符集：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> tbl_name <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> character_name <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">COLLATE</span><span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#93;</span>;
如：<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> logtest <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;</pre></div></div>

<p>修改字段的字符集：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> tbl_name <span style="color: #993333; font-weight: bold;">CHANGE</span> c_name c_name <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> character_name <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">COLLATE</span> <span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#93;</span>;
如：<span style="color: #993333; font-weight: bold;">ALTER</span> <span style="color: #993333; font-weight: bold;">TABLE</span> logtest <span style="color: #993333; font-weight: bold;">CHANGE</span> title title <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci;</pre></div></div>

<p>查看数据库编码：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> db_name;</pre></div></div>

<p>查看表编码：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> tbl_name;</pre></div></div>

<p>查看字段编码：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">FULL</span> <span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">FROM</span> tbl_name;</pre></div></div>

<p>来源：<a href="http://www.diannaowa.com/index.php/archives/233">http://www.diannaowa.com/index.php/archives/233</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2011/05/mysql-alter-charset.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>数据库并发问题</title>
		<link>http://fatkun.com/2010/12/database-transaction-concurrency-problem.html</link>
		<comments>http://fatkun.com/2010/12/database-transaction-concurrency-problem.html#comments</comments>
		<pubDate>Sun, 26 Dec 2010 06:40:15 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=745</guid>
		<description><![CDATA[以下内容为在数据库并发中出现的各种问题。 脏读（dirty read）&#160; A事务读到B事务刚修改的值，但是B事务撤销了，也A事务读到的值就是假的了。 时间 转账事务A 取款事务B T1 &#160; 开始事务 T2 开始事务 &#160; T3 &#160;&#160;&#160;&#160;&#160; 查询账户余额为1000元&#160;&#160;&#160;&#160; T4 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 取出500元把余额改为500元 T5 查询账户余额为500元（脏读） &#160; T6 &#160; 撤销事务余额恢复为1000元 T7 汇入100元把余额改为600元 &#160; T8 提交事务 &#160; 不可重复读（unrepeatable read）&#160; &#160;&#160; 不可重复读是指A事务读取了B事务已经提交的更改数据。假设A在取款事务的过程中，B往该账户转账100元，A两次读取账户的余额发生不一致： 时间 取款事务A 转账事务B T1 &#160; 开始事务 T2 开始事务 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; T3 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 查询账户余额为1000元&#160;&#160;&#160;&#160;&#160; T4 查询账户余额为1000元 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; T5 &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; 取出100元把余额改为900元 T6 [...]]]></description>
			<content:encoded><![CDATA[<p>以下内容为在数据库并发中出现的各种问题。</p>
<h2>脏读（dirty read）&nbsp;</h2>
<p>A事务读到B事务刚修改的值，但是B事务撤销了，也A事务读到的值就是假的了。</p>
<table border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; clear: both; ">
<tbody>
<tr style="font-size: 12px; ">
<th style="font-size: 12px; " width="79">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">时间</div>
</th>
<th style="font-size: 12px; " width="204">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">转账事务A</div>
</th>
<th style="font-size: 12px; " width="285">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取款事务B</div>
</th>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T1</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">开始事务</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T2</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">开始事务</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T3</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元<span>&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T4</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取出500元把余额改为500元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T5</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>查询账户余额为500</strong><strong>元（脏读）</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T6</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">撤销事务余额恢复为1000元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T7</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">汇入100元把余额改为600元</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T8</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">提交事务</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
</tbody>
</table>
<h2>不可重复读（unrepeatable read）&nbsp;</h2>
<p>&nbsp;&nbsp; 不可重复读是指A事务读取了B事务已经提交的更改数据。假设A在取款事务的过程中，B往该账户转账100元，A两次读取账户的余额发生不一致：</p>
<table border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; clear: both; ">
<tbody>
<tr style="font-size: 12px; ">
<th style="font-size: 12px; " width="79">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">时间</div>
</th>
<th style="font-size: 12px; " width="204">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取款事务A</div>
</th>
<th style="font-size: 12px; " width="285">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">转账事务B</div>
</th>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T1</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T2</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T3</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T4</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T5</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取出100元把余额改为900元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T6</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>提交事务<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T7</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>查询账户余额为900</strong><strong>元（和T4</strong><strong>读取的不一致）</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
</tbody>
</table>
<h2>幻象读（phantom read）&nbsp;</h2>
<p><strong>幻象读与不可重复读的区分是，前者是新增时出现，后者是更改或删除出现。</strong></p>
<p>&nbsp;&nbsp; &nbsp;A事务读取B事务提交的新增数据，这时A事务将出现幻象读的问题。幻象读一般发生在计算统计数据的事务中，举一个例子，假设银行系统在同一个事务中，两次统计存款账户的总金额，在两次统计过程中，刚好新增了一个存款账户，并存入100元，这时，两次统计的总金额将不一致：&nbsp;&nbsp;</p>
<table border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; clear: both; ">
<tbody>
<tr style="font-size: 12px; ">
<th style="font-size: 12px; " width="79">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">时间</div>
</th>
<th style="font-size: 12px; " width="204">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">统计金额事务A</div>
</th>
<th style="font-size: 12px; " width="285">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">转账事务B</div>
</th>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T1</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T2</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T3</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">统计总存款数为10000元</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T4</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">新增一个存款账户，存款为100元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T5</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>提交事务<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T6</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>再次统计总存款数为10100</strong><strong>元（幻象读）</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;&nbsp;&nbsp; 如果新增数据刚好满足事务的查询条件，这个新数据就进入了事务的视野，因而产生了两个统计不一致的情况。&nbsp;</p>
<p><strong>幻象读和不可重复读是两个容易混淆的概念</strong>，前者是指读到了其它已经提交事务的新增数据，而后者是指读到了已经提交事务的更改数据（更改或删除），为了避免这两种情况，采取的对策是不同的，防止读取到更改数据，只需要对操作的数据添加行级锁，阻止操作中的数据发生变化，而防止读取到新增数据，则往往需要添加表级锁&mdash;&mdash;将整个表锁定，防止新增数据（Oracle使用多版本数据的方式实现）。&nbsp;</p>
<h2>第一类丢失更新&nbsp;</h2>
<p>&nbsp;&nbsp;&nbsp; A事务撤销时，把已经提交的B事务的更新数据覆盖了。这种错误可能造成很严重的问题，通过下面的账户取款转账就可以看出来：&nbsp;</p>
<table border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; clear: both; ">
<tbody>
<tr style="font-size: 12px; ">
<th style="font-size: 12px; " width="79">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">时间</div>
</th>
<th style="font-size: 12px; " width="204">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取款事务A</div>
</th>
<th style="font-size: 12px; " width="285">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">转账事务B</div>
</th>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T1</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>&nbsp;</strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T2</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T3</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元<span>&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T4</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T5</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">汇入100元把余额改为1100元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T6</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>提交事务</strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T7</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取出100元把余额改为900元</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T8</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>撤销事务</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T9</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>余额恢复为1000</strong><strong>元（丢失更新）</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
</tbody>
</table>
<p>A事务在撤销时，&ldquo;不小心&rdquo;将B事务已经转入账户的金额给抹去了。&nbsp;</p>
<h2>&nbsp;第二类丢失更新&nbsp;</h2>
<p>A事务覆盖B事务已经提交的数据，造成B事务所做操作丢失：&nbsp;&nbsp;</p>
<table border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; clear: both; ">
<tbody>
<tr style="font-size: 12px; ">
<th style="font-size: 12px; " width="79">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">时间</div>
</th>
<th style="font-size: 12px; " width="204">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">转账事务A</div>
</th>
<th style="font-size: 12px; " width="285">
<div align="center" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取款事务B</div>
</th>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T1</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>&nbsp;</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T2</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>开始事务</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T3</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元<span>&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T4</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">查询账户余额为1000元</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T5</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">取出100元把余额改为900元</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T6</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>提交事务<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></strong></div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T7</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">汇入100元</div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T8</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>提交事务</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
<tr style="font-size: 12px; ">
<td style="font-size: 12px; " valign="top" width="79">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">T9</div>
</td>
<td style="font-size: 12px; " valign="top" width="204">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; "><strong>把余额改为1100</strong><strong>元（丢失更新）</strong></div>
</td>
<td style="font-size: 12px; " valign="top" width="285">
<div style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; color: rgb(51, 51, 51); text-align: left; font-size: 14px; line-height: 21px; ">&nbsp;</div>
</td>
</tr>
</tbody>
</table>
<p>&nbsp;&nbsp;&nbsp; 上面的例子里由于支票转账事务覆盖了取款事务对存款余额所做的更新，导致银行最后损失了100元，相反如果转账事务先提交，那么用户账户将损失100元。</p>
<p><strong>第一类丢失更新和第二类丢失更新的差别</strong>：前者是由于撤销事务导致丢失更新，后者是因为更新被覆盖。</p>
<p>内容参考来源：<a href="http://tech.it168.com/db/t/2007-05-30/200705300938078_1.shtml">http://tech.it168.com/db/t/2007-05-30/200705300938078_1.shtml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/database-transaction-concurrency-problem.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>hibernate事务管理</title>
		<link>http://fatkun.com/2010/12/hibernate-transaction.html</link>
		<comments>http://fatkun.com/2010/12/hibernate-transaction.html#comments</comments>
		<pubDate>Sat, 25 Dec 2010 10:16:03 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[数据库事务]]></category>
		<category><![CDATA[隔离级别]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=722</guid>
		<description><![CDATA[1. 介绍数据库事务、事务隔离级别、悲观锁、乐观锁等概念。 2.数据库ACID特征： Atomic（原子性）：指整个数据库事务是不可分割的工作单元。 Consistency（一致性）：指数据库事务不能破坏关系数据的完整性以及业务逻辑上的一致性。 Isolation（隔离性）：指的是在并发环境中，当不同的事务同时操纵相同的数据时，每个事务都有各自的完整数据空间。 Durability（持久性）：指的是只要事务成功结束，它对数据库所作的更新就必须永久保存下来。 3.数据库系统支持两种事务模式： 自动提交模式：每个SQL语句都是一个独立的事务，当数据库系统执行完一个SQL语句后，会自动提交事务。 手动提交模式：必须由数据库客户程序显示指定事务开始边界和结束边界。 4.MySQL中数据库表分为3种类型： INNODB、BDB和MyISAM，其中MyISAM不支持数据库事务。MySQL中create table 语句默认为MyISAM类型。 5.并发问题 对于同时运行的多个事务，当这些事务访问数据库中相同的数据时，如果没有采取必要的隔离机制，就会导致各种并发问题，这些并发问题可归纳为以下几类： A.第一类丢失更新：撤销一个事务时，把其他事务已提交的更新数据覆盖。 B.脏读：一个事务读到另一个事务为提交的更新数据。 C.幻读：一个事务读到另一个事务已提交的新插入的数据。 D.不可重复读：一个事务读到另一个事务已提交的更新数据。 E.第二类丢失更新：这是不可重复读中的特例，一个事务覆盖另一个事务已提交的更新数据。 6.数据库系统四种事务隔离级别 A.Serializable（串行化）：一个事务在执行过程中完全看不到其他事务对数据库所做的更新。 B.Repeatable Read（可重复读）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，但是不能看到其他其他事务对已有记录的更新。 C.Read Commited（读已提交数据）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，而且能看到其他事务已经提交的对已有记录的更新。 D.Read Uncommitted（读未提交数据）：一个事务在执行过程中可以拷打其他事务没有提交的新插入的记录，而且能看到其他事务没有提交的对已有记录的更新。 隔离级别 脏读 不可 重复读 幻象读 第一类丢失更新 第二类丢失更新 READ UNCOMMITED 允许 允许 允许 不允许 允许 READ COMMITTED 不允许 允许 允许 不允许 允许 REPEATABLE READ 不允许 不允许 允许 不允许 [...]]]></description>
			<content:encoded><![CDATA[<h2>1. 介绍数据库事务、事务隔离级别、悲观锁、乐观锁等概念。</h2>
<h2>2.数据库ACID特征：</h2>
<blockquote><p>Atomic（原子性）：指整个数据库事务是不可分割的工作单元。</p>
<p>Consistency（一致性）：指数据库事务不能破坏关系数据的完整性以及业务逻辑上的一致性。</p>
<p>Isolation（隔离性）：指的是在并发环境中，当不同的事务同时操纵相同的数据时，每个事务都有各自的完整数据空间。</p>
<p>Durability（持久性）：指的是只要事务成功结束，它对数据库所作的更新就必须永久保存下来。</p></blockquote>
<h2>3.数据库系统支持两种事务模式：</h2>
<p style="padding-left: 30px;">自动提交模式：每个SQL语句都是一个独立的事务，当数据库系统执行完一个SQL语句后，会自动提交事务。</p>
<p style="padding-left: 30px;">手动提交模式：必须由数据库客户程序显示指定事务开始边界和结束边界。</p>
<h2>4.MySQL中数据库表分为3种类型：</h2>
<p style="padding-left: 30px;">INNODB、BDB和MyISAM，其中MyISAM不支持数据库事务。MySQL中create table 语句默认为MyISAM类型。</p>
<h2>5.并发问题</h2>
<p>对于同时运行的多个事务，当这些事务访问数据库中相同的数据时，如果没有采取必要的隔离机制，就会导致各种并发问题，这些并发问题可归纳为以下几类：</p>
<blockquote><p>A.第一类丢失更新：撤销一个事务时，把其他事务已提交的更新数据覆盖。</p>
<p>B.脏读：一个事务读到另一个事务为提交的更新数据。</p>
<p>C.幻读：一个事务读到另一个事务已提交的新插入的数据。</p>
<p>D.不可重复读：一个事务读到另一个事务已提交的更新数据。</p>
<p>E.第二类丢失更新：这是不可重复读中的特例，一个事务覆盖另一个事务已提交的更新数据。</p></blockquote>
<h2>6.数据库系统四种事务隔离级别</h2>
<p style="padding-left: 30px;">A.Serializable（串行化）：一个事务在执行过程中完全看不到其他事务对数据库所做的更新。</p>
<p style="padding-left: 30px;">B.Repeatable Read（可重复读）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，但是不能看到其他其他事务对已有记录的更新。</p>
<p style="padding-left: 30px;">C.Read Commited（读已提交数据）：一个事务在执行过程中可以看到其他事务已经提交的新插入的记录，而且能看到其他事务已经提交的对已有记录的更新。</p>
<p style="padding-left: 30px;">D.Read Uncommitted（读未提交数据）：一个事务在执行过程中可以拷打其他事务没有提交的新插入的记录，而且能看到其他事务没有提交的对已有记录的更新。</p>
<table border="1" cellspacing="0" cellpadding="0" width="576">
<tbody>
<tr>
<td width="164">
<div>隔离级别</div>
</td>
<td width="82">
<div>脏读</div>
</td>
<td width="82">
<div>不可</div>
<div>重复读</div>
</td>
<td width="82">
<div>幻象读</div>
</td>
<td width="82">
<div>第一类丢失更新</div>
</td>
<td width="82">
<div>第二类丢失更新</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>READ UNCOMMITED</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>READ COMMITTED</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>REPEATABLE READ</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
</tr>
<tr>
<td width="164" valign="top">
<div>SERIALIZABLE</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
<td width="82">
<div>不允许</div>
</td>
</tr>
</tbody>
</table>
<p style="padding-left: 30px;">隔离级别越高，越能保证数据的完整性和一致性，但是对并发性能的影响也越大。对于多数应用程序，可以有优先考虑把数据库系统的隔离级别设为Read Commited，它能够避免脏读，而且具有较好的并发性能。尽管它会导致不可重复读、幻读和第二类丢失更新这些并发问题，在可能出现这类问题的个别场合，可以由应用程序采用悲观锁或乐观锁来控制。</p>
<h2>7.当数据库系统采用read Commited隔离级别时</h2>
<p>会导致不可重复读喝第二类丢失更新的并发问题，可以在应用程序中采用悲观锁或乐观锁来避免这类问题。从应用程序的角度，锁可以分为以下几类：</p>
<p style="padding-left: 30px;">A.悲观锁：指在应用程序中显示的为数据资源加锁。尽管能防止丢失更新和不可重复读这类并发问题，但是它会影响并发性能，因此应该谨慎地使用。</p>
<p style="padding-left: 30px;">B.乐观锁：乐观锁假定当前事务操作数据资源时，不回有其他事务同时访问该数据资源，因此完全依靠数据库的隔离级别来自动管理锁的工作。应用程序采用版本控制手段来避免可能出现的并发问题。</p>
<h2>8.悲观锁有两种实现方式：</h2>
<p style="padding-left: 30px;">A.在应用程序中显示指定采用数据库系统的独占所来锁定数据资源。SQL语句：select &#8230; for update，在Hibernate中使用get，load时如session.get(Account.class,new Long(1),LockMode,UPGRADE)</p>
<p style="padding-left: 30px;">B.在数据库表中增加一个表明记录状态的LOCK字段，当它取值为“Y”时，表示该记录已经被某个事务锁定，如果为“N”，表明该记录处于空闲状态，事务可以访问它。增加锁标记字段就可以实现。</p>
<h2>9.利用Hibernate的版本控制来实现乐观锁</h2>
<p style="padding-left: 30px;">乐观锁是由程序提供的一种机制，这种机制既能保证多个事务并发访问数据，又能防止第二类丢失更新问题。</p>
<p style="padding-left: 30px;">在应用程序中可以利用Hibernate提供的版本控制功能来视线乐观锁，OR映射文件中的&lt;version&gt;元素和&lt;timestamp&gt;都具有版本控制的功能，一般推荐采用&lt;version&gt;</p>
<p>本文来源：<a href="http://blog.csdn.net/yueguangyuan/archive/2006/09/20/1253229.aspx">http://blog.csdn.net/yueguangyuan/archive/2006/09/20/1253229.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/12/hibernate-transaction.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SQL的连接（JOIN，左连接，右连接，全连接，内连接）</title>
		<link>http://fatkun.com/2010/11/sql-join.html</link>
		<comments>http://fatkun.com/2010/11/sql-join.html#comments</comments>
		<pubDate>Tue, 23 Nov 2010 06:33:25 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[left join]]></category>
		<category><![CDATA[right join]]></category>
		<category><![CDATA[外连接]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=677</guid>
		<description><![CDATA[下图中，有两个表，T1,T2 自然连接时的语句是 SELECT * FROM T1 JOIN T2，把表1,2的值全显示出来 等值连接（内连接），按照某个条件，只返回符合条件的值 左连接（LEFT JOIN）与左外连接一样，返回的结果包括左边的全部行，右边没有的用null填充 右连接（RIGHT JOIN）与右外连接一样，返回的结果包括右边的全部行，左边没有的用null填充 全连接(FULL JOIN) 相当于左右连接的并集，包含左右表为空的数据]]></description>
			<content:encoded><![CDATA[<p>下图中，有两个表，T1,T2</p>
<p>自然连接时的语句是 SELECT * FROM T1 JOIN T2，把表1,2的值全显示出来</p>
<p>等值连接（内连接），按照某个条件，只返回符合条件的值</p>
<p>左连接（LEFT JOIN）与左外连接一样，返回的结果包括左边的全部行，右边没有的用null填充</p>
<p>右连接（RIGHT JOIN）与右外连接一样，返回的结果包括右边的全部行，左边没有的用null填充</p>
<p>全连接(FULL JOIN) 相当于左右连接的并集，包含左右表为空的数据</p>
<p><img src="http://wah88w.blu.livefilestore.com/y1pOdX-NjvbC1vBQc4wNWr_fbQkM8wirtpcnrzdpURauuWLblQsoA_t_EWImst3JMf5ifUopwS5bR14u06aCH4mi_SDAmFogJtA/JOIN.png?psid=1" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/11/sql-join.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>数据库存储过程的优缺点</title>
		<link>http://fatkun.com/2010/11/database-procedure.html</link>
		<comments>http://fatkun.com/2010/11/database-procedure.html#comments</comments>
		<pubDate>Mon, 22 Nov 2010 05:37:54 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[优点与缺点]]></category>
		<category><![CDATA[存储过程]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=674</guid>
		<description><![CDATA[优点 （1） 存储过程允许标准组件式编程 存储过程在被创建以后可以在程序中被多次调用，而不必重新编写该存储过程的SQL语句。而且数据库专业人员可随时对存储过程进行修改，但对应用程序源代码毫无影响（因为应用程序源代码只包含存储过程的调用语句），从而极大地提高了程序的可移植性。 （2） 存储过程能够实现较快的执行速度 如果某一操作包含大量的Transaction-SQL 代码或分别被多次执行，那么存储过程要比批处理的执行速度快很多。因为存储过程是预编译的，在首次运行一个存储过程时，查询优化器对其进行分析、优化，并给出最终被存在系统表中的执行计划。而批处理的Transaction-SQL 语句在每次运行时都要进行编译和优化，因此速度相对要慢一些。 （3） 存储过程能够减少网络流量 对于同一个针对数据数据库对象的操作（如查询、修改），如果这一操作所涉及到的Transaction-SQL 语句被组织成一存储过程，那么当在客户计算机上调用该存储过程时，网络中传送的只是该调用语句，否则将是多条SQL 语句，从而大大增加了网络流量，降低网络负载。 （4） 存储过程可被作为一种安全机制来充分利用 系统管理员通过对执行某一存储过程的权限进行限制，从而能够实现对相应的数据访问权限的限制，避免非授权用户对数据的访问，保证数据的安全 缺点: 1,依赖于数据库厂商,难以移植(当一个小系统发展到大系统时,对数据库的要求也会发生改变) 2,业务逻辑大的时候,封装性不够,难调试难以维护 3,复杂的应用用存储过程来实现，就把业务处理的负担压在数据库服务器上了。没有办法通过中间层来灵活分担负载和压力.均衡负载等 参考： http://www.javaeye.com/topic/734702 http://blog.csdn.net/fsclc/archive/2007/01/16/1484264.aspx]]></description>
			<content:encoded><![CDATA[<h2>优点</h2>
<p>（1） 存储过程允许标准组件式编程<br />
存储过程在被创建以后可以在程序中被多次调用，而不必重新编写该存储过程的SQL语句。而且数据库专业人员可随时对存储过程进行修改，但对应用程序源代码毫无影响（因为应用程序源代码只包含存储过程的调用语句），从而极大地提高了程序的可移植性。<br />
（2） 存储过程能够实现较快的执行速度<br />
如果某一操作包含大量的Transaction-SQL 代码或分别被多次执行，那么存储过程要比批处理的执行速度快很多。因为存储过程是预编译的，在首次运行一个存储过程时，查询优化器对其进行分析、优化，并给出最终被存在系统表中的执行计划。而批处理的Transaction-SQL 语句在每次运行时都要进行编译和优化，因此速度相对要慢一些。</p>
<p>（3） 存储过程能够减少网络流量<br />
对于同一个针对数据数据库对象的操作（如查询、修改），如果这一操作所涉及到的Transaction-SQL 语句被组织成一存储过程，那么当在客户计算机上调用该存储过程时，网络中传送的只是该调用语句，否则将是多条SQL 语句，从而大大增加了网络流量，降低网络负载。</p>
<p>（4） 存储过程可被作为一种安全机制来充分利用<br />
系统管理员通过对执行某一存储过程的权限进行限制，从而能够实现对相应的数据访问权限的限制，避免非授权用户对数据的访问，保证数据的安全</p>
<h2>缺点:</h2>
<p>1,依赖于数据库厂商,难以移植(当一个小系统发展到大系统时,对数据库的要求也会发生改变)<br />
2,业务逻辑大的时候,封装性不够,难调试难以维护<br />
3,复杂的应用用存储过程来实现，就把业务处理的负担压在数据库服务器上了。没有办法通过中间层来灵活分担负载和压力.均衡负载等</p>
<p>参考：</p>
<p><a href="http://www.javaeye.com/topic/734702">http://www.javaeye.com/topic/734702</a></p>
<p><a href="http://blog.csdn.net/fsclc/archive/2007/01/16/1484264.aspx">http://blog.csdn.net/fsclc/archive/2007/01/16/1484264.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/11/database-procedure.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>数据库UNION和UNION ALL比较</title>
		<link>http://fatkun.com/2010/11/union-and-union-all.html</link>
		<comments>http://fatkun.com/2010/11/union-and-union-all.html#comments</comments>
		<pubDate>Mon, 22 Nov 2010 05:22:08 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[UNION]]></category>
		<category><![CDATA[UNION ALL]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=671</guid>
		<description><![CDATA[UNION能够把结果集合并 SELECT * FROM A UNION SELECT * FROM B 如果能确定没有重复行，建议使用UNION ALL，不用排序。 order by子句必须写在最后一个结果集里，并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。 Union可以对字段名不同但数据类型相同的结果集进行合并； 如果字段名不同的结果集进行Union，那么对此字段的Order by子句将失效。 Union，对两个结果集进行并集操作，不包括重复行，同时进行默认规则的排序； Union All，对两个结果集进行并集操作，包括重复行，不进行排序； Intersect，对两个结果集进行交集操作，不包括重复行，同时进行默认规则的排序； Minus，对两个结果集进行差操作，不包括重复行，同时进行默认规则的排序。 可以在最后一个结果集中指定Order by子句改变排序方式。]]></description>
			<content:encoded><![CDATA[<p>UNION能够把结果集合并</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> A <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> B</pre></div></div>

<p>如果能确定没有重复行，建议使用UNION ALL，不用排序。</p>
<ol>
<li>order by子句必须写在最后一个结果集里，并且其排序规则将改变操作后的排序结果。对于Union、Union All、Intersect、Minus都有效。</li>
<li>Union可以对字段名不同但数据类型相同的结果集进行合并；</li>
<li>如果字段名不同的结果集进行Union，那么对此字段的Order by子句将失效。</li>
<li>Union，对两个结果集进行并集操作，不包括重复行，同时进行默认规则的排序；</li>
<li>Union All，对两个结果集进行并集操作，包括重复行，不进行排序；</li>
<li>Intersect，对两个结果集进行交集操作，不包括重复行，同时进行默认规则的排序；</li>
<li>Minus，对两个结果集进行差操作，不包括重复行，同时进行默认规则的排序。</li>
<li>可以在最后一个结果集中指定Order by子句改变排序方式。</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/11/union-and-union-all.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>如何在SQL中先排序后分组</title>
		<link>http://fatkun.com/2010/07/group-by-and-order-by.html</link>
		<comments>http://fatkun.com/2010/07/group-by-and-order-by.html#comments</comments>
		<pubDate>Sat, 03 Jul 2010 13:50:44 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[group by]]></category>
		<category><![CDATA[hibernate子查询]]></category>
		<category><![CDATA[order by]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=427</guid>
		<description><![CDATA[标题说的有点奇怪，换句话说是让order by比group by先执行。还不明白对吧？ 举个例子：实现场景，要实现QQ空间的动态消息，首先要按最新的消息查到QQ好友排序，然后再按好友分别查询他们的动态消息 有如下的Feed表 id userid type msg who time 1 2 add_twitter hahah 1 2010-04-27 19:12:38 2 2 add_twitter 怎么啦 2 2010-04-27 19:12:44 3 4 add_twitter asddas 3 2010-04-27 19:20:26 4 2 reply_twitter 放大法 4 2010-04-27 19:24:09 5 5 reply_twitter 噶 5 2010-04-27 19:24:13 6 3 add_twitter saf 6 2010-04-27 19:35:48 7 2 [...]]]></description>
			<content:encoded><![CDATA[<p>标题说的有点奇怪，换句话说是让order by比group by先执行。还不明白对吧？<br />
举个例子：实现场景，要实现QQ空间的动态消息，首先要按最新的消息查到QQ好友排序，然后再按好友分别查询他们的动态消息</p>
<p>有如下的Feed表</p>
<pre>id	userid	type		msg	who	time
1	2	add_twitter		hahah	1	2010-04-27 19:12:38
2	2	add_twitter		怎么啦	2	2010-04-27 19:12:44
3	4	add_twitter		asddas	3	2010-04-27 19:20:26
4	2	reply_twitter		放大法	4	2010-04-27 19:24:09
5	5	reply_twitter		噶	5	2010-04-27 19:24:13
6	3	add_twitter		saf	6	2010-04-27 19:35:48
7	2	add_twitter		先谢谢谢谢谢谢	7	2010-04-27 19:54:44</pre>
<p>现在想要，按时间先后，把用户的id查询出来，每个id只出现一次。</p>
<p>当然，第一时间就想到用group by语句来分组嘛</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> feed <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> userid <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">TIME</span></pre></div></div>

<p>可是，得到的结果并不是自己想要的，group by比order by先解析了，结果就是得到最旧的那一条。</p>
<h2>正确的方法</h2>
<p>使用子查询</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> feed <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> T 
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> T<span style="color: #66cc66;">.</span>userid <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> T<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TIME</span></pre></div></div>

<p>上面这句在mysql可以正常取值，但是在hibernate不能使用from 后面的子查询，杯具。。<br />
换个折中的方法，以下方法只是为了取得userid而已</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> feed <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> userid <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TIME</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/07/group-by-and-order-by.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mysql指定编码创建数据库</title>
		<link>http://fatkun.com/2010/06/mysql-charset.html</link>
		<comments>http://fatkun.com/2010/06/mysql-charset.html#comments</comments>
		<pubDate>Mon, 21 Jun 2010 06:42:23 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=391</guid>
		<description><![CDATA[mysql 创建数据库时指定编码很重要，一般都指定为统一的UTF-8编码，如果不指定编码，默认编码为lan1，插入中文时是会提示错误的，类似”\x22\x34&#8243; CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci]]></description>
			<content:encoded><![CDATA[<p>mysql 创建数据库时指定编码很重要，一般都指定为统一的UTF-8编码，如果不指定编码，默认编码为lan1，插入中文时是会提示错误的，类似”\x22\x34&#8243;</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">DATABASE</span> <span style="color: #ff0000;">`test2`</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">CHARACTER</span> <span style="color: #993333; font-weight: bold;">SET</span> utf8 <span style="color: #993333; font-weight: bold;">COLLATE</span> utf8_general_ci</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/06/mysql-charset.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MSSQL多个外键对应同一个表时查询外键的内容</title>
		<link>http://fatkun.com/2010/05/mssql-foreign-key.html</link>
		<comments>http://fatkun.com/2010/05/mssql-foreign-key.html#comments</comments>
		<pubDate>Sat, 29 May 2010 04:11:05 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[join]]></category>
		<category><![CDATA[sqlserver]]></category>
		<category><![CDATA[外键]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=319</guid>
		<description><![CDATA[当有一个表同时有多个外键同时指向某一个表时，需要通过外键来查询到相应的信息。 举个例子：这是一个订车票的 城市表 CityID CityName 1 广州 2 湛江 3 江门 4 肇庆 5 惠州 6 汕头 车次表 CarID CarName StartCityID EndCityID 1 G001 1 2 2 G002 1 3 这里的StartCityID和EndCityID分别是城市表的外键 解决方法 现在要把对应的始发城市和终点城市的名称取到，mssql语句可以这样 方法一： SELECT 始发站=c1.CityName,终点站=c2.CityName FROM car,City c1,City c2 WHERE StartCityID = c1.CityID AND EndCityID = c2.CityID 方法二： SELECT 始发站=c1.CityName,终点站=c2.CityName FROM car JOIN City [...]]]></description>
			<content:encoded><![CDATA[<p>当有一个表同时有多个外键同时指向某一个表时，需要通过外键来查询到相应的信息。</p>
<p>举个例子：这是一个订车票的</p>
<p>城市表</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">CityID	CityName
<span style="color: #cc66cc;">1</span>	广州
<span style="color: #cc66cc;">2</span>	湛江
<span style="color: #cc66cc;">3</span>	江门
<span style="color: #cc66cc;">4</span>	肇庆
<span style="color: #cc66cc;">5</span>	惠州
<span style="color: #cc66cc;">6</span>	汕头</pre></div></div>

<p>车次表</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">CarID	CarName	StartCityID	EndCityID
<span style="color: #cc66cc;">1</span>	G001	<span style="color: #cc66cc;">1</span>	<span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">2</span>	G002	<span style="color: #cc66cc;">1</span>	<span style="color: #cc66cc;">3</span></pre></div></div>

<p>这里的StartCityID和EndCityID分别是城市表的外键</p>
<h2>解决方法</h2>
<p>现在要把对应的始发城市和终点城市的名称取到，mssql语句可以这样<br />
方法一：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> 始发站<span style="color: #66cc66;">=</span>c1<span style="color: #66cc66;">.</span>CityName<span style="color: #66cc66;">,</span>终点站<span style="color: #66cc66;">=</span>c2<span style="color: #66cc66;">.</span>CityName
<span style="color: #993333; font-weight: bold;">FROM</span> car<span style="color: #66cc66;">,</span>City c1<span style="color: #66cc66;">,</span>City c2
<span style="color: #993333; font-weight: bold;">WHERE</span> StartCityID <span style="color: #66cc66;">=</span> c1<span style="color: #66cc66;">.</span>CityID <span style="color: #993333; font-weight: bold;">AND</span> EndCityID <span style="color: #66cc66;">=</span> c2<span style="color: #66cc66;">.</span>CityID</pre></div></div>

<p>方法二：</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> 始发站<span style="color: #66cc66;">=</span>c1<span style="color: #66cc66;">.</span>CityName<span style="color: #66cc66;">,</span>终点站<span style="color: #66cc66;">=</span>c2<span style="color: #66cc66;">.</span>CityName
<span style="color: #993333; font-weight: bold;">FROM</span> car
<span style="color: #993333; font-weight: bold;">JOIN</span> City c1 <span style="color: #993333; font-weight: bold;">ON</span> StartCityID <span style="color: #66cc66;">=</span> c1<span style="color: #66cc66;">.</span>CityID
<span style="color: #993333; font-weight: bold;">JOIN</span> City c2 <span style="color: #993333; font-weight: bold;">ON</span> EndCityID <span style="color: #66cc66;">=</span> c2<span style="color: #66cc66;">.</span>CityID</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2010/05/mssql-foreign-key.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MSSQL触发器基础（在触发器遍历所有数据）</title>
		<link>http://fatkun.com/2009/11/mssql%e8%a7%a6%e5%8f%91%e5%99%a8%e5%9f%ba%e7%a1%80%ef%bc%88%e5%9c%a8%e8%a7%a6%e5%8f%91%e5%99%a8%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e6%95%b0%e6%8d%ae%ef%bc%89.html</link>
		<comments>http://fatkun.com/2009/11/mssql%e8%a7%a6%e5%8f%91%e5%99%a8%e5%9f%ba%e7%a1%80%ef%bc%88%e5%9c%a8%e8%a7%a6%e5%8f%91%e5%99%a8%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e6%95%b0%e6%8d%ae%ef%bc%89.html#comments</comments>
		<pubDate>Mon, 23 Nov 2009 21:14:07 +0000</pubDate>
		<dc:creator>fatkun</dc:creator>
				<category><![CDATA[数据库]]></category>
		<category><![CDATA[CURSOR]]></category>
		<category><![CDATA[TRIGGER]]></category>
		<category><![CDATA[触发器]]></category>
		<category><![CDATA[遍历数据表]]></category>

		<guid isPermaLink="false">http://fatkun.com/?p=54</guid>
		<description><![CDATA[最近很懒，也有忙的原因~~一些东西懒得发上来了·~~还是要写上来才可以，不然学到的东西以后我就忘记了 以下只保证在MSSQL下顺利执行~~可能在MySql不太一样。 最近在做作业的时候想用用数据库的触发器写写~发觉还不是一般的烦呀~~变量多了就觉得有点烦了~~不过语言还是挺容易上手的~~ 插播一句：ID自增的写法 CREATE TABLE ritem&#40; id INT IDENTITY&#40;1,1&#41; &#41;; 触发器的基本写法 CREATE TRIGGER 触发器名 ON 数据表名 FOR &#40;INSERT&#124;update&#124;delete&#41; AS BEGIN 在这里写代码 END 定义变量，并赋值 CREATE TRIGGER t_insert_ritem ON ritem FOR INSERT AS BEGIN DECLARE @id INT SELECT @id=id FROM inserted -- 从插入的数据获得ID的值，还有一个deleted -- inserted表示新的数据，deleted表示旧的数据（自己理解的，不知道对不对？） &#160; SET @id = @id - 1 -- 另一种赋值 &#160; PRINT [...]]]></description>
			<content:encoded><![CDATA[<p>最近很懒，也有忙的原因~~一些东西懒得发上来了·~~还是要写上来才可以，不然学到的东西以后我就忘记了<br />
以下只保证在MSSQL下顺利执行~~可能在MySql不太一样。<br />
最近在做作业的时候想用用数据库的触发器写写~发觉还不是一般的烦呀~~变量多了就觉得有点烦了~~不过语言还是挺容易上手的~~<br />
插播一句：ID自增的写法</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> ritem<span style="color: #66cc66;">&#40;</span>
	id <span style="color: #993333; font-weight: bold;">INT</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>触发器的基本写法</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> 触发器名 <span style="color: #993333; font-weight: bold;">ON</span> 数据表名 <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">INSERT</span><span style="color: #66cc66;">|</span>update<span style="color: #66cc66;">|</span>delete<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span> 
<span style="color: #993333; font-weight: bold;">BEGIN</span>
	在这里写代码
<span style="color: #993333; font-weight: bold;">END</span></pre></div></div>

<p>定义变量，并赋值</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> t_insert_ritem <span style="color: #993333; font-weight: bold;">ON</span> ritem <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">INSERT</span>
<span style="color: #993333; font-weight: bold;">AS</span> 
<span style="color: #993333; font-weight: bold;">BEGIN</span>
	<span style="color: #993333; font-weight: bold;">DECLARE</span> @id <span style="color: #993333; font-weight: bold;">INT</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> @id<span style="color: #66cc66;">=</span>id <span style="color: #993333; font-weight: bold;">FROM</span> inserted
	<span style="color: #808080; font-style: italic;">-- 从插入的数据获得ID的值，还有一个deleted</span>
	<span style="color: #808080; font-style: italic;">-- inserted表示新的数据，deleted表示旧的数据（自己理解的，不知道对不对？）</span>
&nbsp;
	<span style="color: #993333; font-weight: bold;">SET</span> @id <span style="color: #66cc66;">=</span> @id <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>
	<span style="color: #808080; font-style: italic;">-- 另一种赋值</span>
&nbsp;
	PRINT @id
	<span style="color: #808080; font-style: italic;">-- 可以把值打印出来，在查询分析器结果可以看到</span>
	raiserror<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'抛出错误 id的值为：%d'</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>@id<span style="color: #66cc66;">&#41;</span> 
	<span style="color: #808080; font-style: italic;">-- 也可以这样输出</span>
&nbsp;
	<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #66cc66;">&#40;</span>@id&amp;lt;<span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">BEGIN</span>
		<span style="color: #993333; font-weight: bold;">ROLLBACK</span> <span style="color: #993333; font-weight: bold;">TRANSACTION</span>
		<span style="color: #808080; font-style: italic;">-- 回滚事务</span>
	<span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
	<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> ritem <span style="color: #993333; font-weight: bold;">WHERE</span> id<span style="color: #66cc66;">=</span>@id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">BEGIN</span>
		<span style="color: #808080; font-style: italic;">-- 还可以用exists来判断是否为空</span>
	<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> TRIGGER_Update <span style="color: #993333; font-weight: bold;">ON</span> ritem
<span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">UPDATE</span>
<span style="color: #993333; font-weight: bold;">AS</span>
    <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">UPDATE</span><span style="color: #66cc66;">&#40;</span>UserName<span style="color: #66cc66;">&#41;</span>
    <span style="color: #993333; font-weight: bold;">BEGIN</span>
	<span style="color: #808080; font-style: italic;">-- 判断UserName有没有更新</span>
    <span style="color: #993333; font-weight: bold;">END</span></pre></div></div>

<blockquote><div class="quote">
　　1.插入操作(Insert)<br />
　　Inserted表有数据，Deleted表无数据<br />
　　2.删除操作(Delete)<br />
　　Inserted表无数据，Deleted表有数据<br />
　　3.更新操作(Update)<br />
　　Inserted表有数据(新数据)，Deleted表有数据(旧数据)
</div>
</blockquote>
<p>当你更新一个数据想要把相关联的所有数据都更新的时候就要用游标(CURSOR)鸟~~MSSQL的遍历</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">	<span style="color: #993333; font-weight: bold;">DECLARE</span> @id <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> @rid <span style="color: #993333; font-weight: bold;">INT</span>
	<span style="color: #993333; font-weight: bold;">DECLARE</span> cur_tmp CURSOR <span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">SELECT</span> id<span style="color: #66cc66;">,</span>rid <span style="color: #993333; font-weight: bold;">FROM</span> ritem
	<span style="color: #993333; font-weight: bold;">OPEN</span> cur_tmp
	FETCH <span style="color: #993333; font-weight: bold;">NEXT</span> <span style="color: #993333; font-weight: bold;">FROM</span> cur_tmp <span style="color: #993333; font-weight: bold;">INTO</span> @id<span style="color: #66cc66;">,</span>@rid <span style="color: #808080; font-style: italic;">-- 这里是取到select的值</span>
	WHILE @@FETCH_STATUS <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
	<span style="color: #993333; font-weight: bold;">BEGIN</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">-- 在这中间加入循环</span>
&nbsp;
	FETCH <span style="color: #993333; font-weight: bold;">NEXT</span> <span style="color: #993333; font-weight: bold;">FROM</span> cur_tmp <span style="color: #993333; font-weight: bold;">INTO</span> @id<span style="color: #66cc66;">,</span>@rid
	<span style="color: #993333; font-weight: bold;">END</span>
	CLOSE cur_tmp
	DEALLOCATE cur_tmp <span style="color: #808080; font-style: italic;">-- 释放游标</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://fatkun.com/2009/11/mssql%e8%a7%a6%e5%8f%91%e5%99%a8%e5%9f%ba%e7%a1%80%ef%bc%88%e5%9c%a8%e8%a7%a6%e5%8f%91%e5%99%a8%e9%81%8d%e5%8e%86%e6%89%80%e6%9c%89%e6%95%b0%e6%8d%ae%ef%bc%89.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

