﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-Skynet-随笔分类-Database</title><link>http://www.blogjava.net/Good-Game/category/23017.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 29 May 2008 07:03:57 GMT</lastBuildDate><pubDate>Thu, 29 May 2008 07:03:57 GMT</pubDate><ttl>60</ttl><item><title>数据库表地址数据（中国地区）</title><link>http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 29 May 2008 02:51:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/203747.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/203747.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/203747.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 为方便项目使用，特留！表结构：Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> CREATE TABLE `location` (  `id` bigint(20) NOT NULL auto_increment,  `name` varchar(50...&nbsp;&nbsp;<a href='http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html'>阅读全文</a><img src ="http://www.blogjava.net/Good-Game/aggbug/203747.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-05-29 10:51 <a href="http://www.blogjava.net/Good-Game/archive/2008/05/29/203747.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql 权限控制笔记</title><link>http://www.blogjava.net/Good-Game/archive/2008/04/29/197206.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Tue, 29 Apr 2008 05:52:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/04/29/197206.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/197206.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/04/29/197206.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/197206.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/197206.html</trackback:ping><description><![CDATA[
		<p>
				<font color="#006400">
						<strong>
								<span>
			MySQL</span>
						</strong>存取控制包含<span>2</span>个阶段：</font>
		</p>
		<ul type="disc">
				<li>阶段<span>1</span>：服务器检查是否允许你连接。</li>
				<li>阶段<span>2</span>：假定你能连接，服务器检查你发出的每个请求。看你是否有足够的权限实施它。例如，如果你从数据库表中选择<span>(select)</span>行或从数据库删除表，服务器确定你对表有<span>SELECT</span>权限或对数据库有<span>DROP</span>权限。</li>
		</ul>
		<span class="section">
		</span>
		<a href="http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#user-account-management">参考 ：
		5.8. MySQL用户账户管理</a>
		<br />
		<br />1.权限查看<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> show grants </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">root</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">localhost</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> ;<br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">-------------------------------------------------------------------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> Grants </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> root</span><span style="color: rgb(0, 128, 0);">@localhost</span><span style="color: rgb(0, 0, 0);">                                           </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">-------------------------------------------------------------------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">ALL</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">PRIVILEGES</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">root</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">localhost</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">WITH</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">OPTION</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">-------------------------------------------------------------------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);"> row </span><span style="color: rgb(128, 128, 128);">in</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 0); font-weight: bold;">0.06</span><span style="color: rgb(0, 0, 0);"> sec)</span></div><br />2.权限设置<br /><span class="section"></span><a href="http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#adding-users">
			5.8.2. 向MySQL增加新用户账户</a><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">ALL</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">PRIVILEGES</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">monty</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">localhost</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     IDENTIFIED </span><span style="color: rgb(0, 0, 255);">BY</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">some_pass</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">WITH</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">OPTION</span><span style="color: rgb(0, 0, 0);">;<br /></span><font color="#006400"><span><span></span></span></font><span style="color: rgb(0, 0, 0);"><br />mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">ALL</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">PRIVILEGES</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">monty</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">%</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     IDENTIFIED </span><span style="color: rgb(0, 0, 255);">BY</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">some_pass</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">WITH</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">OPTION</span><span style="color: rgb(0, 0, 0);">;</span><font color="#006400"></font><br /><font color="#006400"><span><span>  
			</span></span>其中两个账户有相同的用户名<span>monty</span>和密码<span>some_pass</span>。两个账户均为超级用户账户，具有完全的权限可以做任何事情。一个账户<span> 
			(</span><span>'monty'@'localhost'</span><span>)</span>只用于从本机连接时。另一个账户<span>(</span><span>'monty'@'%'</span><span>)</span>可用于从其它主机连接。</font><br /><span style="color: rgb(0, 0, 0);"><br /></span><br /><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> RELOAD,PROCESS </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">admin</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">localhost</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><font color="#006400">该账户只用于从本机连接。授予了<span>RELOAD</span>和<span>PROCESS</span>管理权限。这些权限允许<span>admin</span>用户执行<strong><span>mysqladmin 
			reload</span></strong>、<strong><span>mysqladmin 
			refresh</span></strong>和<strong><span>mysqladmin 
			flush-</span></strong><span><b><i><span>xxx</span></i></b></span>命令，以及<strong><span>mysqladmin 
			processlist</span></strong>。未授予访问数据库的权限。你可以通过<span>GRANT</span>语句添加此类权限。</font><br /><span style="color: rgb(0, 0, 0);"><br />mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> USAGE </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);">.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">dummy</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">localhost</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /></span><font color="#006400"><span><span>   
			</span></span>一个账户有用户名<span>dummy</span>，没有密码。该账户只用于从本机连接。未授予权限。通过<span>GRANT</span>语句中的<span>USAGE</span>权限，你可以创建账户而不授予任何权限。它可以将所有全局权限设为<span>'N'</span>。假定你将在以后将具体权限授予该账户。</font><br /></div><br />下面的例子创建<span>3</span>个账户，允许它们访问专用数据库。每个账户的用户名为<span>custom</span>，密码为<span><span>obscure</span>。<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"></span><span style="color: rgb(0, 128, 128);"></span><span style="color: rgb(0, 128, 128);"></span><span style="color: rgb(0, 0, 0);"><br />mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">SELECT</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">INSERT</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">UPDATE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">DELETE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">DROP</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> bankaccount.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">custom</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">localhost</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     IDENTIFIED </span><span style="color: rgb(0, 0, 255);">BY</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">obscure</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /><br />mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">SELECT</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">INSERT</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">UPDATE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">DELETE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">DROP</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> expenses.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">custom</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">whitehouse.gov</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     IDENTIFIED </span><span style="color: rgb(0, 0, 255);">BY</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">obscure</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /><br />mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">GRANT</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">SELECT</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">INSERT</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">UPDATE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">DELETE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">DROP</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">ON</span><span style="color: rgb(0, 0, 0);"> customer.</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     </span><span style="color: rgb(0, 0, 255);">TO</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">custom</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">@</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">server.domain</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(128, 128, 128);">-&gt;</span><span style="color: rgb(0, 0, 0);">     IDENTIFIED </span><span style="color: rgb(0, 0, 255);">BY</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">obscure</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);">;<br /><font color="#006400"><br />这3个账户可以用于：<br /><br />·         第1个账户可以访问bankaccount数据库，但只能从本机访问。<br /><br />·         第2个账户可以访问expenses数据库，但只能从主机whitehouse.gov访问。<br /><br />·         第3个账户可以访问customer数据库，但只能从主机server.domain访问。<br /><br />要想不用GRANT设置custom账户，使用INSERT语句直接修改 授权表：</font></span></div><br /></span><h3 class="title">
			5.8.3. 从MySQL删除用户账户<span></span></h3><h3 class="title"><span>DROP USER <span><i>user</i></span>;</span></h3><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/197206.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-04-29 13:52 <a href="http://www.blogjava.net/Good-Game/archive/2008/04/29/197206.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql 游标使用</title><link>http://www.blogjava.net/Good-Game/archive/2008/04/27/196486.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Sun, 27 Apr 2008 09:05:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/04/27/196486.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/196486.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/04/27/196486.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/196486.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/196486.html</trackback:ping><description><![CDATA[请使用 mysql 1.5 或以上version;<br />测试表 level ;<br />create table <span style="color: rgb(0, 0, 0);">test.</span>level (name varchar(20));<br />再 insert 些数据 ;<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">初始化</span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"> <br /> </span><span style="color: rgb(0, 0, 255);">drop</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">procedure</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">exists</span><span style="color: rgb(0, 0, 0);">  useCursor </span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);">    <br /> <br /> </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">建立 存储过程 create </span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"> <br /> </span><span style="color: rgb(0, 0, 255);">CREATE</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">PROCEDURE</span><span style="color: rgb(0, 0, 0);"> useCursor()<br />    </span><span style="color: rgb(0, 0, 255);">BEGIN</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">局部变量的定义 declare</span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"> <br />         </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> tmpName </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">20</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">default</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);"> ;<br />         </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> allName </span><span style="color: rgb(0, 0, 0); font-weight: bold;">varchar</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 0, 0); font-weight: bold;">255</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">default</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 0);">''</span><span style="color: rgb(0, 0, 0);"> ;<br />         <br />         </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> cur1 </span><span style="color: rgb(0, 0, 255);">CURSOR</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">FOR</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">SELECT</span><span style="color: rgb(0, 0, 0);"> name </span><span style="color: rgb(0, 0, 255);">FROM</span><span style="color: rgb(0, 0, 0);"> test.</span><b><span style="color: rgb(0, 0, 255);">level</span></b><span style="color: rgb(0, 0, 0);"> ;<br />         <br />         </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">    mysql 不知道为什么用异常加入判断 ？<br />          *    此请参考官方文档 </span><span class="section"><span></span></span><a href="http://dev.mysql.com/doc/refman/5.1/zh/stored-procedures.html#cursors">20.2.11.   
光标</a><span style="color: rgb(0, 128, 128);"> 光标 <br />          *        这把 游标 异常后 捕捉 <br />          *        并设置 循环使用 变量 tmpname 为 null 跳出循环。<br />          </span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"><br />         </span><span style="color: rgb(0, 0, 255);">declare</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">CONTINUE</span><span style="color: rgb(0, 0, 0);"> HANDLER </span><span style="color: rgb(0, 0, 255);">FOR</span><span style="color: rgb(0, 0, 0);"> SQLSTATE </span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(255, 0, 0);">02000</span><span style="color: rgb(255, 0, 0);">'</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">SET</span><span style="color: rgb(0, 0, 0);"> tmpname </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">;<br />    <br />    <br />    </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">开游标</span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"> <br />     </span><span style="color: rgb(0, 0, 255);">OPEN</span><span style="color: rgb(0, 0, 0);"> cur1;<br />         </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">游标向下走一步</span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"> <br />         </span><span style="color: rgb(0, 0, 255);">FETCH</span><span style="color: rgb(0, 0, 0);"> cur1 </span><span style="color: rgb(0, 0, 255);">INTO</span><span style="color: rgb(0, 0, 0);"> tmpName;<br />         <br />         </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);"> 循环体 这很明显 把游标查询出的 name 都加起并用 ; 号隔开 </span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">WHILE</span><span style="color: rgb(0, 0, 0);"> ( tmpname </span><span style="color: rgb(0, 0, 255);">is</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">not</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">null</span><span style="color: rgb(0, 0, 0);">) DO<br />          </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> tmpName </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> CONCAT(tmpName ,";") ;<br />          </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> allName </span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(0, 0, 0);"> CONCAT(allName ,tmpName) ;<br />        </span><span style="color: rgb(0, 128, 128);">/*</span><span style="color: rgb(0, 128, 128);">游标向下走一步</span><span style="color: rgb(0, 128, 128);">*/</span><span style="color: rgb(0, 0, 0);"> <br />        </span><span style="color: rgb(0, 0, 255);">FETCH</span><span style="color: rgb(0, 0, 0);"> cur1 </span><span style="color: rgb(0, 0, 255);">INTO</span><span style="color: rgb(0, 0, 0);"> tmpName;<br />      </span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">WHILE</span><span style="color: rgb(0, 0, 0);">;<br />  <br />    </span><span style="color: rgb(0, 0, 255);">CLOSE</span><span style="color: rgb(0, 0, 0);"> cur1;<br />    <br />    </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> allName ;<br /></span><span style="color: rgb(0, 0, 255);">END</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);"><br />call useCursor()</span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);"><br /> </span></div><br />运行结果：<br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> call useCursor()</span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">------------------------------------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> allName                              </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">------------------------------------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> f1;c3;c6;c5;c2;c4;c1;f1;f3;f4;f2;f5; </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">------------------------------------+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);"> row </span><span style="color: rgb(128, 128, 128);">in</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 0); font-weight: bold;">0.00</span><span style="color: rgb(0, 0, 0);"> sec)</span></div><br /><br /><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/196486.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-04-27 17:05 <a href="http://www.blogjava.net/Good-Game/archive/2008/04/27/196486.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql 数据库学习（触发器）</title><link>http://www.blogjava.net/Good-Game/archive/2008/01/24/177559.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 24 Jan 2008 10:26:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/01/24/177559.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/177559.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/01/24/177559.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/177559.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/177559.html</trackback:ping><description><![CDATA[ 引用：http://bbs.mysql.cn/thread-9135-1-2.html<br /> <span class="chapter"></span>引用：<a href="http://dev.mysql.com/doc/refman/5.1/zh/triggers.html">21. 触发程序</a><br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">delimiter </span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(0, 0, 255);">create</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">trigger</span><span style="color: rgb(0, 0, 0);"> a_bi<br />before </span><span style="color: rgb(0, 0, 255);">insert</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">on</span><span style="color: rgb(0, 0, 0);"> a<br /></span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);"> each row </span><span style="color: rgb(0, 0, 255);">Begin</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> new.s2</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(128, 0, 0); font-weight: bold;">0</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">then</span><span style="color: rgb(0, 0, 0);"><br />      </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(new.s3</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">then</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">insert</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> b(s2) </span><span style="color: rgb(0, 0, 255);">values</span><span style="color: rgb(0, 0, 0);">(new.s1);<br />      elseif(new.s3</span><span style="color: rgb(128, 128, 128);">=</span><span style="color: rgb(128, 0, 0); font-weight: bold;">2</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 255);">then</span><span style="color: rgb(0, 0, 0);"><br />        </span><span style="color: rgb(0, 0, 255);">insert</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> c(s2) </span><span style="color: rgb(0, 0, 255);">values</span><span style="color: rgb(0, 0, 0);">(new.s1);<br />      </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">;<br />    </span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);">;</span><span style="color: rgb(128, 128, 128);">//</span><span style="color: rgb(0, 0, 0);"><br />delimiter ;</span></div><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/177559.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-01-24 18:26 <a href="http://www.blogjava.net/Good-Game/archive/2008/01/24/177559.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql 数据库学习（存储过程）</title><link>http://www.blogjava.net/Good-Game/archive/2008/01/22/177105.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Tue, 22 Jan 2008 09:58:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2008/01/22/177105.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/177105.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2008/01/22/177105.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/177105.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/177105.html</trackback:ping><description><![CDATA[1。存储过程实现：<br />引用：<span class="section"><span><a href="http://dev.mysql.com/doc/refman/5.1/zh/stored-procedures.html#create-procedure">20.2.1. CREATE   
PROCEDURE和CREATE FUNCTION（mysql在线文档）</a><br /><br /></span></span><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"><b>--test.sql</b><br /><br />DELIMITER $$<br /></span><span style="color: rgb(0, 0, 255);">drop</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">procedure</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 128, 128);">exists</span><span style="color: rgb(0, 0, 0);"> test.sproc $$<br /></span><span style="color: rgb(0, 0, 255);">create</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">procedure</span><span style="color: rgb(0, 0, 0);"> sproc(out param1 </span><span style="color: rgb(0, 0, 0); font-weight: bold;">int</span><span style="color: rgb(0, 0, 0);">)<br /></span><span style="color: rgb(0, 0, 255);">begin</span><span style="color: rgb(0, 0, 0);"><br />    </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(255, 0, 255);">count</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(128, 128, 128);">*</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 255);">into</span><span style="color: rgb(0, 0, 0);"> param1 </span><span style="color: rgb(0, 0, 255);">from</span><span style="color: rgb(0, 0, 0);"> account;<br /></span><span style="color: rgb(0, 0, 255);">end</span><span style="color: rgb(0, 0, 0);"> $$<br />DELIMITER ;</span></div><br />命令 cmd-&gt; <b>mysql -u root test&lt;c:\test.sql<br /><br /></b>使用：<br /><b><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);">mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> call sproc(</span><span style="color: rgb(0, 128, 0);">@a</span><span style="color: rgb(0, 0, 0);">);<br />Query OK, </span><span style="color: rgb(128, 0, 0); font-weight: bold;">0</span><span style="color: rgb(0, 0, 0);"> rows affected (</span><span style="color: rgb(128, 0, 0); font-weight: bold;">0.00</span><span style="color: rgb(0, 0, 0);"> sec)<br /><br />mysql</span><span style="color: rgb(128, 128, 128);">&gt;</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">select</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@a</span><span style="color: rgb(0, 0, 0);"> ;<br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">----+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 128, 0);">@a</span><span style="color: rgb(0, 0, 0);">   </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">----+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(128, 128, 128);">|</span><span style="color: rgb(0, 0, 0);"><br /></span><span style="color: rgb(128, 128, 128);">+</span><span style="color: rgb(0, 128, 128);">--</span><span style="color: rgb(0, 128, 128);">----+</span><span style="color: rgb(0, 128, 128);"><br /></span><span style="color: rgb(128, 0, 0); font-weight: bold;">1</span><span style="color: rgb(0, 0, 0);"> row </span><span style="color: rgb(128, 128, 128);">in</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">set</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(128, 0, 0); font-weight: bold;">0.00</span><span style="color: rgb(0, 0, 0);"> sec)</span></div><br /><br /></b><br /><img src ="http://www.blogjava.net/Good-Game/aggbug/177105.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2008-01-22 17:58 <a href="http://www.blogjava.net/Good-Game/archive/2008/01/22/177105.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ORACLE笔记 sql语句 (转)</title><link>http://www.blogjava.net/Good-Game/archive/2007/04/12/110243.html</link><dc:creator>G_G</dc:creator><author>G_G</author><pubDate>Thu, 12 Apr 2007 10:09:00 GMT</pubDate><guid>http://www.blogjava.net/Good-Game/archive/2007/04/12/110243.html</guid><wfw:comment>http://www.blogjava.net/Good-Game/comments/110243.html</wfw:comment><comments>http://www.blogjava.net/Good-Game/archive/2007/04/12/110243.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/Good-Game/comments/commentRss/110243.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/Good-Game/services/trackbacks/110243.html</trackback:ping><description><![CDATA[　--================================================<br />　　--==================================== 一.sql语句<br />　　--================================================<br />　　1.增加主键<br />　　 alter table TABLE_NAME add constraint KEY_NAME primary key (TABLE_COLUMN);<br />　　 指定表空间<br />　　 alter table TABLE_NAME add constraint KEY_NAME primary key (TABLE_COLUMN) using index tablespace TABLE_SPACE_NAME;<br />　　2.增加外键<br />　　 alter table TABLE_NAME add constraint FK_NAME foreign key (TABLE_COLUMN) references KEY_TABLE_NAME;<br />　　3.使主键或外键失效、生效<br />　　 alter table TABLE_NAME disable(enable) constraint KEY_NAME;<br />　　4、查看各种约束<br />　　 select constraint_name,table_name,constraint_type,status from user_constraints;<br />
select constraint_name, constraint_type,search_condition,
r_constraint_name from user_constraints where table_name =
upper('&amp;table_name')<br />　　 select c.constraint_name,c.constraint_type,cc.column_name <br />　　 from user_constraints c,user_cons_columns cc<br />　　 where c.owner = upper('&amp;table_owner') and c.table_name = upper('&amp;table_name')<br />　　 and c.owner = cc.owner and c.constraint_name = cc.constraint_name<br />　　 order by cc.position; <br />　　5、删除主键或外键<br />　　 alter table TABLE_NAME drop constraint KEY_NAME;<br />　　6、建外键<br />　　 单字段时：create table 表名 (col1 char(8),<br />　　 cno char(4) REFERENCE course);<br />　　 多个字段时,在最后加上 Foreign Key (字段名) REFERENCE 表名(字段)<br />　　 连带删除选项 (on delete cascade<br />　　 当指定时，如果父表中的记录被删除，则依赖于父表的记录也被删除<br />　　 REFERENCE 表名（） on delete cascade;<br />　　7、删除带约束的表<br />　　 Drop table 表名 cascade constraints;<br />　　8：索引管理<br />　　&lt;1&gt;.creating function-based indexes <br />　　sql&gt; create index summit.item_quantity on summit.item(quantity-quantity_shipped); <br />　　&lt;2&gt;.create a B-tree index <br />　　sql&gt; create [unique] index index_name on table_name(column,.. asc/desc) tablespace <br />　　sql&gt; tablespace_name [pctfree integer] [initrans integer] [maxtrans integer] <br />　　sql&gt; [logging | nologging] [nosort] storage(initial 200k next 200k pctincrease 0 <br />　　sql&gt; maxextents 50); <br />　　&lt;3&gt;.pctfree(index)=(maximum number of rows-initial number of rows)*100/maximum number of rows <br />　　&lt;4&gt;.creating reverse key indexes <br />　　sql&gt; create unique index xay_id on xay(a) reverse pctfree 30 storage(initial 200k <br />　　sql&gt; next 200k pctincrease 0 maxextents 50) tablespace indx; <br />　　&lt;5&gt;.create bitmap index <br />　　sql&gt; create bitmap index xay_id on xay(a) pctfree 30 storage( initial 200k next 200k <br />　　sql&gt; pctincrease 0 maxextents 50) tablespace indx; <br />　　&lt;6&gt;.change storage parameter of index <br />　　sql&gt; alter index xay_id storage (next 400k maxextents 100); <br />　　7.allocating index space <br />　　sql&gt; alter index xay_id allocate extent(size 200k datafile 'c:/oracle/index.dbf'); <br />　　&lt;8&gt;.alter index xay_id deallocate unused; <br />　　&lt;9&gt;、查看索引<br />　　 SQL&gt;select index_name,index_type,table_name from user_indexes order by table_name;<br />　　&lt;10&gt;、查看索引被索引的字段<br />　　 SQL&gt;select * from user_ind_columns where index_name=upper('&amp;index_name');<br />　　11、创建序列<br />　　 select * from user_sequences;<br />　　 create sequence SEQ_NAME start with 1000 <br />　　 maxvalue 1000 increment by 1;<br />　　 alter sequence SEQ_NAME minvalue 50 maxvalue 100;<br />　　12、删除重复行<br />　　 update a set aa=null where aa is not null;<br />　　 <br />　　 delete from a where rowid!=<br />　　 (select max(rowid) from a b where a.aa=b.aa);<br />　　13、删除同其他表相同的行<br />　　 delete from a where exits<br />　　 (select 'X' from b where b.no=a.no);<br />　　 或<br />　　 delete from a where no in (select no from b);<br />　　14、查询从多少行到多少行的记录(可以用在web开发中的分页显示)<br />　　 select * from ( select rownum row_id,b.* from (select a.* from sys_oper a) b )<br />　　 where row_id between 15 and 20<br />　　15、对公共授予访问权<br />　　 grant select on 表名 to public;<br />　　 create public synonym 同义词名 for 表名;<br />　　16、填加注释<br />　　 comment on table 表名 is '注释';<br />　　 comment on column 表名.列名 is '注释';<br />　　17、分布式数据库,创建数据库链路<br />　　 create [public] database link LINKNAME <br />　　 [connect to USERNAME identified by PASSWORD]<br />　　 [using 'CONNECT_STRING']<br />　　 可以在服务器端，也可以在客户端建立，但必须注意，两台服务器之间<br />　　 数据库必须可以互访，必须各有各自的别名数据库<br />　　18、查看数据库链路<br />　　 select * from all_db_links;<br />　　 select * from user_db_links;<br />　　 查询 select * from <a href="mailto:TABLENAME@DBLNKNAME">TABLENAME@DBLNKNAME</a>;<br />　　 创建远程数据库同义词<br />　　 create synonym for <a href="mailto:TABLENAME@DBLNKNAME">TABLENAME@DBLNKNAME</a>;<br />　　 操纵远程数据库记录<br />　　 insert into <a href="mailto:TABLENAME@DBLNKNAME">TABLENAME@DBLNKNAME</a> (a,b) values (va,vb);<br />　　 update <a href="mailto:TABLENAME@DBLNKNAME">TABLENAME@DBLNKNAME</a> set a='this';<br />　　 delete from <a href="mailto:TABLENAME@DBLNKNAME">TABLENAME@DBLNKNAME</a>;<br />　　 怎样执行远程的内嵌过程<br />　　 begin<br />　　 <a href="mailto:otherdbpro@to_html">otherdbpro@to_html</a>(参数);<br />　　 end;<br />　　19、数据库链路用户密码有特殊字符的时候，可以用双引号把密码引起来<br />　　create public database link dblink1 connect to db1 identified by "123*456" using 'db11'<br />　　20.oracle8中扩充了group by rollup和cube的操作。有时候省了你好多功夫的。<br />　　 &lt;1&gt;下面的语句可以进行总计<br />　　 select region_code,count(*) from aicbs.acc_woff_notify<br />　　 group by rollup(region_code);<br />　　 &lt;2&gt; 对第1个字段小计，最后合计<br />　　 select region_code,write_status,count(*) from aicbs.acc_woff_notify<br />　　 group by rollup(region_code,write_status);<br />　　 ----------------------<br />　　 570 0 3<br />　　 570 1 2<br />　　 570 5 --此处小计了570的记录<br />　　 571 0 10<br />　　 571 1 2<br />　　 571 12 --此处小计了571的记录<br />　　 .....<br />　　 100 --此处有总计<br />　　 &lt;3&gt; 复合rollup表达式,只做总计<br />　　 select region_code,write_status,count(*) from aicbs.acc_woff_notify<br />　　 group by rollup(region_code,write_status);<br />　　 <br />　　 &lt;4&gt; 对第1个字段小计，再对第2个字段小计，最后合计<br />　　 select region_code,write_status,count(*) from aicbs.acc_woff_notify<br />　　 group by cube(region_code,write_status);<br />　　 ----------------------<br />　　 100 --此处有总计<br />　　 0 60 --对write_status＝0的小计<br />　　 1 39 --对write_status＝1的小计<br />　　 3 1 --对write_status＝3的小计<br />　　 570 5 --此处小计了570的记录<br />　　 570 0 3<br />　　 570 1 2<br />　　 571 12 --此处小计了571的记录<br />　　 571 0 10<br />　　 571 1 2<br />　　 ....<br />　　 &lt;3&gt; 复合cube表达式,只做总计<br />　　 select region_code,write_status,count(*) from aicbs.acc_woff_notify<br />　　 group by cube(region_code,write_status);<br />　　 <br />　　 <br />　　 &lt;4&gt;下面的语句可以按照rollup不同的字段进行小计<br />　　 select region_code,write_status,count(*) from aicbs.acc_woff_notify<br />　　 group by region_code,rollup(write_status);<br />　　21.查询view的创建语句<br />　　 sql&gt;set long 1000<br />　　 sql&gt;select * from user_views where view_name='MY_VIEW_NAME';<br />　　 or<br />　　 sql&gt;select * from all_views where view_name='MY_VIEW_NAME';<br />　　22、去除数据库中特殊字符<br />　　 &lt;1&gt;.字符串字段中含有"'",如果用来组合sql语句，会造成语句不准确。<br />　　 比如：replace(f1,'''','')<br />　　 &lt;2&gt;.字符串字段中含有"\t \n",如果用来在c或者c＋＋程序中输出到文件，格式无法保证。<br />　　 比如：replace(f2,'\t','')<br />　　 &lt;3&gt;.清除换行和回车<br />　　 比如: replace(f2,chr(13)||chr(10),'')<br />　　23、如何在字符串里加回车或者tab键 <br />　　 在sqlplus中执行<br />　　 sql&gt;select 'UserId=1233111'||chr(10)||'AccId=13431'||chr(9)||'AccId2=11111' from dual;<br />　　24、树形查询<br />　　create table zj(<br />　　bm number(8),<br />　　bmmc varchar2(20),<br />　　sjbm number(8)<br />　　)<br />　　insert into zj values(1,'aaa',0)<br />　　insert into zj values(11,'aaa1',1)<br />　　insert into zj values(12,'aaa2',1)<br />　　insert into zj values(111,'aaa11',11)<br />　　insert into zj values(112,'aaa12',11)<br />　　insert into zj values(113,'aaa13',11)<br />　　insert into zj values(121,'aaa21',12)<br />　　insert into zj values(122,'aaa22',12)<br />　　insert into zj values(123,'aaa23',12)<br />　　--<br />　　select bm,bmmc,sjbm,level<br />　　from zj<br />　　start with sjbm=0<br />　　connect by prior bm = sjbm<br />　　或者<br />　　select bm,bmmc,sjbm,level<br />　　from zj<br />　　start with sjbm=0<br />　　connect by sjbm = prior bm <br />　　25、快照<br />　　 create snapshot SNAPSHOT_NAME <br />　　 [storage (storage parameter)]<br />　　 　　 [refresh [fast\complete\force]<br />　　 [start with START_DATE next NEXT_DATE]<br />　　 as QUERY;<br />　　 <br />　　 create snapshot snapshot_to_study as select * from <a href="mailto:TABLE_NAME@to_study">TABLE_NAME@to_study</a>;<br />　　 创建角色<br />　　 create role aa identified by aaa;<br />　　 授权 grant create snapshot,alter snapshot to aaa;<br />　　 grant aaa to emp;<br />　　 create snapshot SNAPSHOT_TO_HTML refresh complete start with sysdate next <br />　　 sysdate+5/(24*60*60) as select * from <a href="mailto:a@to_html">a@to_html</a>;<br />　　 删除 drop snapshot snap_to_html<br />　　 手工刷新快照,(调用DBMS_SNAPSHOT包中的refresh过程)DBMS_SNAPSHOT.refresh(snapshot_name,refresh_type);<br />　　 begin<br />　　 DBMS_SNAPSHOT.REFRESH('snap_to_html','c');<br />　　 end;<br />　　 对所有快照进行刷新<br />　　 begin<br />　　 DBMS_SNAPSHOT.REFRESH_ALL;<br />　　 end;<br />　　 怎样执行远程的内嵌过程<br />　　 begin<br />　　 <a href="mailto:otherdbpro@to_html">otherdbpro@to_html</a>(参数);<br />　　 end;<br />　　26、用户管理<br />　　 create a user: database authentication <br />　　 sql&gt; create user juncky identified by oracle default tablespace users <br />　　 sql&gt; temporary tablespace temp quota 10m on data password expire <br />　　 sql&gt; [account lock|unlock] [profile profilename|default]; <br />　　 &lt;1&gt;.查看当前用户的缺省表空间<br />　　 SQL&gt;select username,default_tablespace from user_users;<br />　　 &lt;2&gt;生成用户时指定缺省表空间<br />　　 create user 用户名 identified by 口令 default tablespace 表空间名;<br />　　 <br />　　 &lt;3&gt;重新指定用户的缺省表空间<br />　　 alter user 用户名 default tablespace 表空间名<br />　　 &lt;4&gt;查看当前用户的角色<br />　　 SQL&gt;select * from user_role_privs;<br />　　 &lt;5&gt;查看当前用户的系统权限和表级权限<br />　　 SQL&gt;select * from user_sys_privs;<br />　　 SQL&gt;select * from user_tab_privs;<br />　　 &lt;6&gt;查看用户下所有的表<br />　　 SQL&gt;select * from user_tables;<br />　　 &lt;7&gt; alter user语句的quota子句限制用户的磁盘空间<br />　　 如:alter user jf quota 10M on system;<br />　　27、查看放在ORACLE的内存区里的表 <br />　　 SQL&gt;select table_name,cache from user_tables where instr(cache,'Y')&gt;0;<br />　　28、约束条件<br />　　 create table employee<br />　　 (empno number(10) primary key,<br />　　 name varchar2(40) not null,<br />　　 deptno number(2) default 10,<br />　　 salary number(7,2) check salary&lt;10000,<br />　　 birth_date date,<br />　　 soc_see_num char(9) unique,<br />　　 foreign key(deptno) references dept.deptno)<br />　　 tablespace users;<br />　　 <br />　　 关键字(primary key)必须是非空,表中记录的唯一性<br />　　 not null 非空约束<br />　　 default 缺省值约束<br />　　 check 检查约束,使列的值符合一定的标准范围<br />　　 unqiue 唯一性约束<br />　　 foreign key 外部键约束<br />　　29、查看创建视图的select语句<br />　　 SQL&gt;set view_name,text_length from user_views;<br />　　 SQL&gt;set long 2000; 说明：可以根据视图的text_length值设定set long 的大小<br />　　 SQL&gt;select text from user_views where view_name=upper('&amp;view_name');<br />　　30、查看同义词的名称<br />　　 SQL&gt;select * from user_synonyms;<br />　　31、用Sql语句实现查找一列中第N大值 <br />　　select * from <br />　　(select t.*,dense_rank() over (order by sal) rank from employee) <br />　　where rank = N;<br />　　32 虚拟自段<br />　　 &lt;1&gt;. CURRVAL 和 nextval<br />　　 为表创建序列<br />　　 CREATE SEQUENCE EMPSEQ ... ;<br />　　 SELECT empseq.currval FROM DUAL ;<br />　　 自动插入序列的数值<br />　　 INSERT INTO emp <br />　　 VALUES (empseq.nextval, 'LEWIS', 'CLERK', <br />　　 7902, SYSDATE, 1200, NULL, 20) ;<br />　　 &lt;2&gt;. ROWNUM<br />　　 按设定排序的行的序号<br />　　 SELECT * FROM emp WHERE ROWNUM &lt; 10 ;<br />　　 &lt;3&gt;. ROWID<br />　　 返回行的物理地址<br />　　 SELECT ROWID, ename FROM emp WHERE deptno = 20 ;<br />　　33、对CLOB字段进行全文检索<br />　　SELECT * FROM A WHERE dbms_lob.instr(a.a,'K',1,1)&gt;0;<br />　　34. 特殊字符的插入，比如"&amp;" <br />　　insert into a values (translate ('at{&amp;}t','at{}','at')); <br />　　35.表管理<br />　　 &lt;1&gt;.create a table <br />　　 sql&gt; create table table_name (column datatype,column datatype]....) <br />　　 sql&gt; tablespace tablespace_name [pctfree integer] [pctused integer] <br />　　 sql&gt; [initrans integer] [maxtrans integer] <br />　　 sql&gt; storage(initial 200k next 200k pctincrease 0 maxextents 50) <br />　　 sql&gt; [logging|nologging] [cache|nocache] <br />　　 &lt;2&gt;.copy an existing table <br />　　 sql&gt; create table table_name [logging|nologging] as subquery <br />　　 <br />　　 &lt;3&gt; create table ... as 方式建表的时候，指定表参数<br />　　 create table a<br />　　 storage(<br />　　 initial 1M /*第一次创建时分配空间*/<br />　　 next 1M /*第一次分配的存储空间用完时在分配*/<br />　　 )<br />　　 as select * from b;<br />　　 &lt;4&gt;.创建临时表<br />　　 sql&gt; create global temporary table xay_temp as select * from xay; <br />　　 on commit preserve rows/on commit delete rows <br />　　 在Oracle中，可以创建以下两种临时表:<br />　　 a 会话特有的临时表:<br />　　 create global temporary table () on commit preserve rows;<br />　　 会话指定，当中断会话时ORACLE将截断表<br />　　 b 事务特有的临时表:<br />　　 create global temporary table () on commit delete rows;<br />　　 事务指定,每次提交后ORACLE将截断表（删除全部行） <br />　　 c 说明<br />　　 临时表只在当前连接内有效 <br />　　临时表不建立索引,所以如果数据量比较大或进行多次查询时,不推荐使用 <br />　　数据处理比较复杂的时候时表快,反之视图快点 <br />　　在仅仅查询数据的时候建议用游标: open cursor for 'sql clause';<br />　　 &lt;5&gt;<br />　　 pctfree = (average row size - initial row size) *100 /average row size <br />　　 pctused = 100-pctfree- (average row size*100/available data space) <br />　　 &lt;6&gt;.change storage and block utilization parameter <br />　　 sql&gt; alter table table_name pctfree=30 pctused=50 storage(next 500k <br />　　 sql&gt; minextents 2 maxextents 100); <br />　　 &lt;7&gt;.manually allocating extents <br />　　 sql&gt; alter table table_name allocate extent(size 500k datafile 'c:/oracle/data.dbf'); <br />　　 &lt;8&gt;.move tablespace <br />　　 sql&gt; alter table employee move tablespace users; <br />　　 &lt;9&gt;.deallocate of unused space <br />　　 sql&gt; alter table table_name deallocate unused [keep integer] <br />　　 &lt;10&gt;.drop a column <br />　　 sql&gt; alter table table_name drop column comments cascade constraints checkpoint 1000; <br />　　 alter table table_name drop columns continue; <br />　　 &lt;11&gt;.mark a column as unused <br />　　 sql&gt; alter table table_name set unused column comments cascade constraints; <br />　　 alter table table_name drop unused columns checkpoint 1000; <br />　　 alter table orders drop columns continue checkpoint 1000 <br />　　 data_dictionary : dba_unused_col_tabs<br />　　37. 中文是如何排序的？ <br />　　Oracle9i之前，中文是按照二进制编码进行排序的。 <br />　　在oracle9i中新增了按照拼音、部首、笔画排序功能。设置NLS_SORT值 <br />　　SCHINESE_RADICAL_M 按照部首（第一顺序）、笔划（第二顺序）排序 <br />　　SCHINESE_STROKE_M 按照笔划（第一顺序）、部首（第二顺序）排序 <br />　　SCHINESE_PINYIN_M 按照拼音排序 <br />　　38. 数据表中的字段最大数:<br />　　表或视图中的最大列数为 1000<br />　　39. oracle中的裸设备:<br />　　 裸设备就是绕过文件系统直接访问的储存空间<br />　　40. 在Oracle服务器上通过SQLPLUS查看本机IP地址 ? <br />　　select sys_context('userenv','ip_address') from dual; <br />　　如果是登陆本机数据库，只能返回127.0.0.1<br />　　41. 在ORACLE中取毫秒? <br />　　 9i之前不支持,9i开始有timestamp. <br />　　 9i可以用select systimestamp from dual;<br />　　42. 将N秒转换为时分秒格式？ <br />　　 set serverout on <br />　　 declare <br />　　 N number := 1000000; <br />　　 ret varchar2(100); <br />　　 begin <br />　　 ret := trunc(n/3600) || '小时' || to_char(to_date(mod(n,3600),'sssss'),'fmmi"分 "ss"秒"') ; <br />　　 dbms_output.put_line(ret); <br />　　 end; <br />　　43、在某个用户下找所有的索引<br />　　 select user_indexes.table_name, user_indexes.index_name,uniqueness, column_name <br />　　 from user_ind_columns, user_indexes <br />　　 where user_ind_columns.index_name = user_indexes.index_name <br />　　 and user_ind_columns.table_name = user_indexes.table_name <br />　　 order by user_indexes.table_type, user_indexes.table_name, <br />　　 user_indexes.index_name, column_position;<br />　　44. not in的替代。<br />　　 一般not in的效率比较低。特别是数据量大的时候，几乎不能执行。<br />　　 用下面几种方式可以替换写法<br />　　 比如要查询在fee_rev_info表中已经销户的用户（不在cm_user中的）(不过下面的例子不是很好，因为bill_id是cm_user的唯一索引）<br />　　 select * from fee_rev_info where bill_id not in (select bill_id from cm_user) <br />　　 &lt;1&gt; 用not exists<br />　　 select * from fee_rev_info a where not exists (select 'p' from cm_user b where b.bill_id = a.bill_id)<br />　　 &lt;2&gt; 用外连接(+)<br />　　 select a.* from fee_rev_info a,cm_user b<br />　　 where a.bill_id = b.bill_id (+)<br />　　 and b.bill_id is null<br />　　 &lt;3&gt; 用hash_aj<br />　　 select /*+HASH_AJ*/* from fee_rev_info where bill_id not in (select bill_id from cm_user) <br />　　45.怎么样查询特殊字符，如通配符%与_<br />　　假如数据库中有表 STATIONTYPE,STATION_571 STATION_572 ... <br />　　select * from tab where tname like 'STATION_%'<br />　　会显示 STATIONTYPE,STATION_571 ... 可以用下面的语句<br />　　select * from tab where tname like 'STATION\_%' escape'\'<br />　　46.如果存在就更新，不存在就插入可以用一个语句实现吗<br />　　9i已经支持了，是Merge，但是只支持select子查询，<br />　　如果是单条数据记录，可以写作select .... from dual的子查询。<br />　　语法为：<br />　　MERGE INTO table<br />　　USING data_source<br />　　ON (condition)<br />　　WHEN MATCHED THEN update_clause<br />　　WHEN NOT MATCHED THEN insert_clause;<br />　　如<br />　　MERGE INTO cm_user_credit <br />　　USING (select * from dual) ON (user_id =1302514690 )<br />　　when MATCHED then update set credit_value = 1000<br />
when NOT MATCHED then insert
(user_id,acc_id,bill_id,plan_id,region_code,credit_value)
values(1302514690,1305032158,'13857141218',10070247,'571',1000);<br />　　47.怎么实现一条记录根据条件多表插入<br />　　9i以上可以通过Insert all语句完成，仅仅是一个语句，如：<br />　　INSERT ALL<br />　　WHEN (id=1) THEN<br />　　INTO table_1 (id, name)<br />　　values(id,name)<br />　　WHEN (id=2) THEN<br />　　INTO table_2 (id, name)<br />　　values(id,name)<br />　　ELSE<br />　　INTO table_other (id, name)<br />　　values(id, name)<br />　　SELECT id,name<br />　　FROM a;<br />　　如果没有条件的话，则完成每个表的插入，如<br />　　INSERT ALL<br />　　INTO table_1 (id, name)<br />　　values(id,name)<br />　　INTO table_2 (id, name)<br />　　values(id,name)<br />　　INTO table_other (id, name)<br />　　values(id, name)<br />　　SELECT id,name<br />　　FROM a;<br />　　48.如何实现行列转换<br />　　&lt;1&gt;、固定列数的行列转换<br />　　如<br />　　student subject grade<br />　　---------------------------<br />　　student1 语文 80<br />　　student1 数学 70<br />　　student1 英语 60<br />　　student2 语文 90<br />　　student2 数学 80<br />　　student2 英语 100<br />　　...<br />　　转换为 <br />　　语文 数学 英语<br />　　student1 80 70 60<br />　　student2 90 80 100<br />　　...<br />　　语句如下：<br />　　select student,sum(decode(subject,'语文', grade,null)) "语文",<br />　　sum(decode(subject,'数学', grade,null)) "数学",<br />　　sum(decode(subject,'英语', grade,null)) "英语"<br />　　from table<br />　　group by student<br />　　&lt;2&gt;、不定列行列转换<br />　　如<br />　　c1 c2<br />　　--------------<br />　　1 我<br />　　1 是<br />　　1 谁<br />　　2 知<br />　　2 道<br />　　3 不<br />　　...<br />　　转换为<br />　　1 我是谁<br />　　2 知道<br />　　3 不<br />　　这一类型的转换必须借助于PL/SQL来完成，这里给一个例子<br />　　CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER) <br />　　RETURN VARCHAR2 <br />　　IS <br />　　Col_c2 VARCHAR2(4000); <br />　　BEGIN<br />　　FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP <br />　　Col_c2 := Col_c2||cur.c2; <br />　　END LOOP; <br />　　Col_c2 := rtrim(Col_c2,1);<br />　　RETURN Col_c2; <br />　　END;<br />　　/<br />　　SQL&gt; select distinct c1 ,get_c2(c1) cc2 from table;即可<br />　　--例子：<br />　　create table okcai_1<br />　　(<br />　　user_id varchar2(10),<br />　　user_number varchar2(10),<br />　　user_num number(8)<br />　　)<br />　　user_id user_number user_num<br />　　---------------------<br />　　1 123 2<br />　　1 456 5<br />　　1 789 6<br />　　2 11 2<br />　　2 22 3<br />　　2 33 4<br />　　2 44 5<br />　　2 55 6<br />　　2 66 7<br />　　2 77 8<br />　　3 1234 1<br />　　3 5678 2<br />　　方式一：<br />　　create or replace function get_col(<br />　　 p_userId number,<br />　　 p_col number<br />　　) return varchar<br />　　as<br />　　v_tmp varchar2(255);<br />　　begin<br />　　 select user_number||chr(9)||user_num into v_tmp<br />　　 from <br />　　 (select user_number,user_num,rownum row_id <br />　　 from okcai_1<br />　　 where user_id = p_userId) a<br />　　 where row_id = p_col;<br />　　 return ltrim(v_tmp);<br />　　 --return v_tmp;<br />　　end;<br />　　然后<br />　　select distinct user_id,get_col(user_id,1),get_col(user_id,2),get_col(user_id,3) .... from okcai_1<br />　　方式二：<br />　　create or replace function get_col(<br />　　 p_userId number,<br />　　 p_col number<br />　　) return varchar<br />　　as<br />　　v_tmp varchar2(255);<br />　　begin<br />　　 select user_number||chr(9)||user_num into v_tmp<br />　　 from <br />　　 (select user_number,user_num,rownum row_id <br />　　 from okcai_1<br />　　 where user_id = p_userId) a<br />　　 where row_id = p_col;<br />　　 return ltrim(v_tmp);<br />　　 --return v_tmp;<br />　　end;<br />　　select distinct user_id,get_col_new(user_id) from okcai_1;<br />　　49.怎么设置存储过程的调用者权限<br />　　普通存储过程都是所有者权限，如果想设置调用者权限，请参考如下语句<br />　　create or replace<br />　　procedure ...()<br />　　AUTHID CURRENT_USER<br />　　As<br />　　begin<br />　　...<br />　　end;<br />　　50.Oracle有哪些常见关键字<br />　　详细信息可以查看v$reserved_words视图<br />　　51.怎么查看数据库参数<br />　　&lt;1&gt; show parameter 参数名<br />　　如通过show parameter spfile可以查看9i是否使用spfile文件<br />　　其中参数名是可以匹配的。<br />　　比如show parameter cursor ,则会显示跟cursor相关的参数<br />　　&lt;2&gt;<br />　　select * from v$parameter<br />　　&lt;3&gt;<br />　　除了这部分参数，Oracle还有大量隐含参数，可以通过如下语句查看:<br />　　SELECT NAME <br />　　,VALUE <br />　　,decode(isdefault, 'TRUE','Y','N') as "Default" <br />　　,decode(ISEM,'TRUE','Y','N') as SesMod <br />　　,decode(ISYM,'IMMEDIATE', 'I', <br />　　'DEFERRED', 'D', <br />　　'FALSE', 'N') as SysMod <br />　　,decode(IMOD,'MODIFIED','U', <br />　　'SYS_MODIFIED','S','N') as Modified <br />　　,decode(IADJ,'TRUE','Y','N') as Adjusted <br />　　,description <br />　　FROM ( --GV$SYSTEM_PARAMETER <br />　　SELECT x.inst_id as instance <br />　　,x.indx+1 <br />　　,ksppinm as NAME <br />　　,ksppity <br />　　,ksppstvl as VALUE <br />　　,ksppstdf as isdefault <br />　　,decode(bitand(ksppiflg/256,1),1,'TRUE','FALSE') as ISEM <br />　　,decode(bitand(ksppiflg/65536,3), <br />　　1,'IMMEDIATE',2,'DEFERRED','FALSE') as ISYM <br />　　,decode(bitand(ksppstvf,7),1,'MODIFIED','FALSE') as IMOD <br />　　,decode(bitand(ksppstvf,2),2,'TRUE','FALSE') as IADJ <br />　　,ksppdesc as DESCRIPTION <br />　　FROM x$ksppi x <br />　　,x$ksppsv y <br />　　WHERE x.indx = y.indx <br />　　AND substr(ksppinm,1,1) = '_' <br />　　AND x.inst_id = USERENV('Instance') <br />　　) <br />　　ORDER BY NAME<br />　　52.怎样建立基于函数索引<br />　　8i以上版本，确保<br />　　Query_rewrite_enabled=true<br />　　Query_rewrite_integrity=trusted<br />　　Compatible=8.1.0以上<br />　　Create index indexname on table (function(field));<br />　　53.怎么样移动表或表分区<br />　　[A]移动表的语法<br />　　Alter table tablename move<br />　　[Tablespace new_name<br />　　Storage(initial 50M next 50M <br />　　pctincrease 0 pctfree 10 pctused 50 initrans 2) nologging]<br />　　移动分区的语法<br />　　alter table tablename move (partition partname)<br />　　[update global indexes]<br />　　之后之后必须重建索引<br />　　Alter index indexname rebuild<br />　　如果表有Lob段，那么正常的Alter不能移动Lob段到别的表空间，而仅仅是移动了表段，可以采用如下的方法移动Lob段<br />　　alter table tablename move <br />　　lob(lobsegname) store as (tablespace newts);<br />　　54.怎么样修改表的列名<br />　　[A]9i以上版本可以采用rname命令<br />　　ALTER TABLE UserName.TabName <br />　　RENAME COLUMN SourceColumn TO DestColumn<br />　　9i以下版本可以采用create table …… as select * from SourceTable的方式。<br />　　另外，8i以上可以支持删除列了<br />　　ALTER TABLE UserName.TabName <br />　　SET UNUSED (ColumnName) CASCADE CONSTRAINTS<br />　　ALTER TABLE UserName.TabName <br />　　DROP (ColumnName) CASCADE CONSTRAINTS<br />　　55.case的用法<br />　　在sql语句中<br />　　CASE test_value<br />　　WHEN expression1 THEN value1<br />　　[[WHEN expression2 THEN value2] [...]]<br />　　[ELSE default_value]<br />　　END <br />　　比如1<br />　　SELECT last_name, job_id, salary<br />　　 CASE job_id <br />　　 WHEN 'IT_PROG' THEN 1.10*salary<br />　　 WHEN 'ST_CLERK' THEN 1.15*salary<br />　　 WHEN 'SA_REP' THEN 1.20*salary<br />　　 ELSE salary END "REVISED_SALARY"<br />　　FROM employees <br />　　比如2<br />　　select <br />　　 case<br />　　 when real_charge&gt;=20000 and real_charge&lt;30000 then 5000<br />　　 when real_charge&gt;=30000 and real_charge&lt;40000 then 9000<br />　　 when real_charge&gt;=40000 and real_charge&lt;50000 then 10000<br />　　 when real_charge&gt;=50000 and real_charge&lt;60000 then 14000<br />　　 when real_charge&gt;=60000 and real_charge&lt;70000 then 18000<br />　　 when real_charge&gt;=70000 and real_charge&lt;80000 then 19000<br />　　 when real_charge&gt;=80000 and real_charge&lt;90000 then 24000<br />　　 when real_charge&gt;=90000 and real_charge&lt;100000 then 27000 <br />　　 when real_charge&gt;=100000 and real_charge&lt;110000 then 27000<br />　　 when real_charge&gt;=110000 and real_charge&lt;120000 then 29000 <br />　　 when real_charge&gt;=120000 then 36000<br />　　 else<br />　　 0 <br />　　 end ,acc_id,user_id,real_charge from okcai_jh_charge_200505<br />　　在存储过程中<br />　　 case v_strGroupClassCode<br />　　 when '1' then<br />　　 v_nAttrNum := v_nAttrNum + 300;<br />　　 v_strAttrFlag := '1'||substr(v_strAttrFlag,2,7);<br />　　 when '2' then<br />　　 v_nAttrNum := v_nAttrNum + 200;<br />　　 v_strAttrFlag := '2'||substr(v_strAttrFlag,2,7);<br />　　 else<br />　　 NULL;<br />　　 end case;<br />　　注意的是存储过程和sql语句有的细微差别是用end case，而不是end。语句后面跟";"
<br /><img src ="http://www.blogjava.net/Good-Game/aggbug/110243.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/Good-Game/" target="_blank">G_G</a> 2007-04-12 18:09 <a href="http://www.blogjava.net/Good-Game/archive/2007/04/12/110243.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>