{"id":1703,"date":"2023-08-25T16:29:17","date_gmt":"2023-08-25T08:29:17","guid":{"rendered":"https:\/\/zhang.mba\/?p=1703"},"modified":"2023-08-25T17:23:40","modified_gmt":"2023-08-25T09:23:40","slug":"mysql","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/08\/25\/16\/29\/17\/1703\/mysql\/database\/zhangzhiqi\/","title":{"rendered":"MySQL"},"content":{"rendered":"<h2><a id=\"1%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D\" class=\"anchor\" aria-hidden=\"true\" href=\"#1%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E6%9C%AC%E4%BB%8B%E7%BB%8D\"><span class=\"octicon octicon-link\"><\/span><\/a>1. \u6570\u636e\u5e93\u57fa\u672c\u4ecb\u7ecd<\/h2>\n<h3><a id=\"1-1%E4%BB%80%E4%B9%88%E6%98%AF%E6%95%B0%E6%8D%AE%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\" href=\"#1-1%E4%BB%80%E4%B9%88%E6%98%AF%E6%95%B0%E6%8D%AE%E5%BA%93\"><span class=\"octicon octicon-link\"><\/span><\/a>1.1 \u4ec0\u4e48\u662f\u6570\u636e\u5e93<\/h3>\n<p>\u200b \u6570\u636e\u5e93: \u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u7684\u4ed3\u5e93. \u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf, \u53ea\u662f\u9700\u8981\u8bbf\u95ee\u8fd9\u4e2a\u6587\u4ef6\u7cfb\u7edf\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u8bed\u53e5(sql)\u8fdb\u884c\u64cd\u4f5c.<\/p>\n<h3><a id=\"1-2%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E4%BD%9C%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\" href=\"#1-2%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E4%BD%9C%E7%94%A8\"><span class=\"octicon octicon-link\"><\/span><\/a>1.2 \u6570\u636e\u5e93\u7684\u4f5c\u7528<\/h3>\n<p>\u200b \u6211\u4eec\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u6700\u540e\u90fd\u9700\u8981\u4fdd\u5b58\u5230\u4e13\u4e1a\u8f6f\u4ef6\u4e2d\u3002\u8fd9\u4e9b\u4e13\u4e1a\u7684\u4fdd\u5b58\u6570\u636e\u7684\u8f6f\u4ef6\u6211\u4eec\u79f0\u4e3a\u6570\u636e\u5e93\u3002<\/p>\n<p>\u200b \u6211\u4eec\u5b66\u4e60\u6570\u636e\u5e93\uff0c\u5e76\u4e0d\u662f\u5b66\u4e60\u5982\u4f55\u53bb\u5f00\u53d1\u4e00\u4e2a\u6570\u636e\u5e93\u8f6f\u4ef6\uff0c\u6211\u4eec\u5b66\u4e60\u7684\u662f\u5982\u4f55\u4f7f\u7528\u6570\u636e\u5e93\u4ee5\u53ca\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u8bb0\u5f55\u7684\u64cd\u4f5c\u3002\u800c\u6570\u636e\u5e93\u8f6f\u4ef6\u662f\u7531\u7b2c\u4e09\u65b9\u516c\u53f8\u7814\u53d1\u3002<\/p>\n<h3><a id=\"1-3%E5%B8%B8%E8%A7%81%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\" href=\"#1-3%E5%B8%B8%E8%A7%81%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93\"><span class=\"octicon octicon-link\"><\/span><\/a>1.3 \u5e38\u89c1\u7684\u6570\u636e\u5e93<\/h3>\n<p>\u200b \u6570\u636e\u5e93\u4e00\u822c\u88ab\u5206\u4e3a\u4e24\u5927\u7c7b: \u5173\u7cfb\u578b \u3001\u975e\u5173\u7cfb\u578b<\/p>\n<p>\u5e38\u89c1\u7684\u5173\u7cfb\u578b\u6570\u636e\u5e93\u8f6f\u4ef6:<\/p>\n<ul>\n<li>\n<p>Oracle: \u662fOracle\u516c\u53f8\u7684\u5927\u578b\u5173\u7cfb\u578b\u6570\u636e\u5e93, \u6536\u8d39\u7684<\/p>\n<\/li>\n<li>\n<p>DB2: IBM\u516c\u53f8\u63d0\u4f9b\u7684\u6570\u636e\u5e93\u4ea7\u54c1, \u6536\u8d39\u7684<\/p>\n<\/li>\n<li>\n<p>SqlServer: \u5fae\u8f6f\u63d0\u4f9b\u7684\u6570\u636e\u5e93\u4ea7\u54c1, \u6536\u8d39\u7684<\/p>\n<\/li>\n<li>\n<p>Sybase: Sybase\u516c\u53f8\u7684 , \u76ee\u524d\u5df2\u7ecf\u8fc7\u65f6\u4e86, \u4f46\u5176\u6570\u636e\u5e93\u5efa\u6a21\u5de5\u5177(PowerDesign)\u4f7f\u7528\u8f83\u4e3a\u5e7f\u6cdb<\/p>\n<\/li>\n<li>\n<p>MySQL: \u65e9\u671f\u745e\u5178\u4e00\u4e2a\u516c\u53f8\u53d1\u660e\uff0c\u540e\u671f\u88absun\u516c\u53f8\u6536\u8d2d\uff0c\u540e\u671f\u88abOracle\u3002<\/p>\n<\/li>\n<\/ul>\n<p>java \u4e2d\u5e38\u7528\u7684\u6570\u636e\u5e93\u4ea7\u54c1:<\/p>\n<p>\u200b MySQL(5.x) \u3001Oracle\u3001DB2<\/p>\n<h3><a id=\"1-4%E4%BB%80%E4%B9%88%E6%98%AF%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\" href=\"#1-4%E4%BB%80%E4%B9%88%E6%98%AF%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93\"><span class=\"octicon octicon-link\"><\/span><\/a>1.4 \u4ec0\u4e48\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93<\/h3>\n<p>\u200b \u5728\u5f00\u53d1\u8f6f\u4ef6\u7684\u65f6\u5019\uff0c\u8f6f\u4ef6\u4e2d\u7684\u6570\u636e\u4e4b\u95f4\u5fc5\u7136\u4f1a\u6709\u4e00\u5b9a\u7684\u5173\u7cfb\u5b58\u5728\uff0c\u9700\u8981\u628a\u8fd9\u4e9b\u6570\u636e\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u540c\u65f6\u4e5f\u8981\u7ef4\u62a4\u6570\u636e\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u8fd9\u65f6\u5c31\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u4e0a\u8ff0\u7684\u90a3\u4e9b\u6570\u636e\u5e93\u3002\u800c\u4e0a\u8ff0\u7684\u6240\u6709\u6570\u636e\u5e93\u90fd\u5c5e\u4e8e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u3002<\/p>\n<p>\u200b \u63cf\u8ff0\u6570\u636e\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5e76\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u540c\u65f6\u5b66\u4e60\u5982\u679c\u6839\u636e\u8fd9\u4e9b\u5173\u7cfb\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\uff0c<\/p>\n<p>\u5173\u7cfb\u578b\u6570\u636e\uff1a\u8bbe\u8ba1\u6570\u636e\u5e93\u7684\u65f6\u5019\uff0c\u9700\u8981\u4f7f\u7528E-R\u56fe\u6765\u63cf\u8ff0\u3002<\/p>\n<p>E-R\uff1a\u5b9e\u4f53\u5173\u7cfb\u56fe\u3002<\/p>\n<p>\u200b \u5b9e\u4f53\uff1a\u53ef\u4ee5\u7406\u89e3\u6210\u6211\u4eecJava\u7a0b\u5e8f\u4e2d\u7684\u4e00\u4e2a\u5bf9\u8c61\u3002\u5728E-R\u56fe\u4e2d\u4f7f\u7528 \u77e9\u5f62(\u957f\u65b9\u5f62) \u8868\u793a\u3002<\/p>\n<p>\u9488\u5bf9\u4e00\u4e2a\u5b9e\u4f53\u4e2d\u7684\u5c5e\u6027\uff0c\u6211\u4eec\u79f0\u4e3a\u8fd9\u4e2a\u5b9e\u4f53\u7684\u6570\u636e\uff0c\u5728E-R\u56fe\u4e2d\u4f7f\u7528 \u692d\u5706\u8868\u793a\u3002<\/p>\n<p>\u5b9e\u4f53\u548c\u5b9e\u4f53\u4e4b\u95f4\u7684\u5173\u7cfb\uff1a\u5728E-R\u56fe\u4e2d\u4f7f\u7528\u83f1\u5f62\u8868\u793a\u3002<\/p>\n<p>ER\u56fe\u793a:<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qn.fivedata.cn\/1554566856470.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/qn.fivedata.cn\/1554566856470.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<pre><code>\u77e9\u5f62\u7528\u6765\u8868\u793a\u5b9e\u4f53\n\u692d\u5706\u7528\u6765\u8868\u793a\u5b9e\u4f53\u7684\u5c5e\u6027\n\u83f1\u5f62\u7528\u6765\u8868\u793a\u4e4b\u95f4\u7684\u5173\u7cfb\n<\/code><\/pre>\n<h2><a id=\"2-mysql%E5%A6%82%E4%BD%95%E5%9C%A8linux%E5%AE%89%E8%A3%85\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-mysql%E5%A6%82%E4%BD%95%E5%9C%A8linux%E5%AE%89%E8%A3%85\"><span class=\"octicon octicon-link\"><\/span><\/a>2. mysql\u5982\u4f55\u5728linux\u5b89\u88c5<\/h2>\n<h3><a id=\"2-1-vmware%E4%B8%AD%E5%AE%89%E8%A3%85%E5%AE%8Clinux%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-1-vmware%E4%B8%AD%E5%AE%89%E8%A3%85%E5%AE%8Clinux%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9\"><span class=\"octicon octicon-link\"><\/span><\/a>2.1 vmware\u4e2d\u5b89\u88c5\u5b8clinux\u6ce8\u610f\u4e8b\u9879<\/h3>\n<h4><a id=\"2-1-1%E9%98%B2%E7%81%AB%E5%A2%99%E7%9A%84%E9%97%AE%E9%A2%98\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-1-1%E9%98%B2%E7%81%AB%E5%A2%99%E7%9A%84%E9%97%AE%E9%A2%98\"><span class=\"octicon octicon-link\"><\/span><\/a>2.1.1 \u9632\u706b\u5899\u7684\u95ee\u9898<\/h4>\n<pre><code>service iptables stop  (\u7acb\u5373\u751f\u6548,\u91cd\u542f\u5931\u6548)\nchkconfig iptables off (\u91cd\u542f\u751f\u6548,\u6c38\u4e45\u5173\u95ed\u9632\u706b\u5899)\n<\/code><\/pre>\n<h4><a id=\"2-1-2%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85%E7%BB%9F%E4%B8%80%E7%9A%84%E7%AE%A1%E7%90%86%E7%9B%AE%E5%BD%95\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-1-2%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85%E7%BB%9F%E4%B8%80%E7%9A%84%E7%AE%A1%E7%90%86%E7%9B%AE%E5%BD%95\"><span class=\"octicon octicon-link\"><\/span><\/a>2.1.2 \u8f6f\u4ef6\u5b89\u88c5\u7edf\u4e00\u7684\u7ba1\u7406\u76ee\u5f55<\/h4>\n<pre><code>\u5b89\u88c5\u5305\u5b58\u653e\u4f4d\u7f6e:  mkdir -p \/export\/software\n\u8f6f\u4ef6\u5b89\u88c5\u4f4d\u7f6e: mkdir -p \/export\/servers\n\u6570\u636e\u5b58\u50a8\u7684\u4f4d\u7f6e: mkdir -p \/export\/data\n\u65e5\u5fd7\u5b58\u50a8\u7684\u4f4d\u7f6e: mkdir -p \/export\/logs\n<\/code><\/pre>\n<h4><a id=\"2-1-3%E8%BD%AF%E4%BB%B6%E7%8E%AF%E5%A2%83\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-1-3%E8%BD%AF%E4%BB%B6%E7%8E%AF%E5%A2%83\"><span class=\"octicon octicon-link\"><\/span><\/a>2.1.3 \u8f6f\u4ef6\u73af\u5883<\/h4>\n<pre><code>vmware(12\u4ee5\u4e0a)\u3001CRT(\u8fde\u63a5linux\u5de5\u5177)\u3001centos6.9\n<\/code><\/pre>\n<h3><a id=\"2-1-4%E5%AE%89%E8%A3%85%E7%8E%AF%E5%A2%83\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-1-4%E5%AE%89%E8%A3%85%E7%8E%AF%E5%A2%83\"><span class=\"octicon octicon-link\"><\/span><\/a>2.1.4 \u5b89\u88c5\u73af\u5883<\/h3>\n<pre><code>1\u3001VMware\u8f6f\u4ef6\u5b89\u88c5\n\n2\u3001\u6784\u5efa\u865a\u62df\u673a\n\n3\u3001\u9700\u8981\u914d\u7f6eLinux\uff08ip,mac\u5730\u5740\uff0chostname\uff0c\u9632\u706b\u5899\uff09\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7crt\u8fd9\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u8fdb\u884c\u64cd\u4f5c\n\n4\u3001\u5728linux\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u5b89\u88c5msyql-5.6\n\t\n\t\u8bf4\u660e\uff1a\u56e0\u4e3a\u5728linux\u64cd\u4f5c\u7cfb\u7edf\u4e2d\uff0c\u5b89\u88c5\u8f6f\u4ef6\u7684\u65b9\u5f0f\u4e3b\u8981\u67093\u79cd\uff1a\n\t1\u3001\u6e90\u7801\u5b89\u88c5\uff08redis\uff09\n\t2\u3001rpm\u5b89\u88c5 \t\t\n\t3\u3001yum\u5728\u7ebf\u5b89\u88c5 ---linux\u8054\u7f51\uff08\uff09\n\n<\/code><\/pre>\n<h3><a id=\"2-2%E8%99%9A%E6%8B%9F%E6%9C%BA%E4%B8%AD%E5%AE%89%E8%A3%85-mysql\" class=\"anchor\" aria-hidden=\"true\" href=\"#2-2%E8%99%9A%E6%8B%9F%E6%9C%BA%E4%B8%AD%E5%AE%89%E8%A3%85-mysql\"><span class=\"octicon octicon-link\"><\/span><\/a>2.2 \u865a\u62df\u673a\u4e2d\u5b89\u88c5MySQL<\/h3>\n<ul>\n<li>\n<ol>\n<li>\u68c0\u6d4b\u662f\u5426\u81ea\u5e26\u7684MySQL<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>rpm -qa |grep mysql\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u5378\u8f7d\u81ea\u5e26\u7684MySQL<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u4e0a\u4f20mysql\u7684\u5b89\u88c5\u5305<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>\u5efa\u8bae\u4f7f\u7528rz\u8fdb\u884c\u4e0a\u4f20\u5373\u53ef:  \n\t\u4e0b\u8f7d\u4e0a\u4f20\u63d2\u4ef6(\u4e0b\u8f7d\u4e00\u6b21\u5373\u53ef): yum -y install lrzsz\n\t\u4f7f\u7528 rz \u547d\u4ee4\u4e0a\u4f20\u5373\u53ef\u6216\u8005\u76f4\u63a5\u62d6\n\t\n\u4e0a\u4f20\u76ee\u5f55:  \/export\/software\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"4\">\n<li>\u5b89\u88c5\u5373\u53ef<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>rpm -ivh *.rpm\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"5\">\n<li>\u67e5\u770b\u521d\u59cb\u5316\u5bc6\u7801:<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u5b89\u88c5\u6210\u529f\u540e, \u4f1a\u4ea7\u751f\u4e00\u4e2a\u968f\u673a\u5bc6\u7801, \u968f\u673a\u5bc6\u7801\u653e\u7f6e\u7684\u4f4d\u7f6e: \/root\/.mysql_secret<\/p>\n<\/blockquote>\n<pre><code>cat \/root\/.mysql_secret\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"6\">\n<li>\u542f\u52a8MySQL\u5e76\u767b\u9646<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>\u542f\u52a8mysql\u670d\u52a1:\n\tservice mysql start\n\t\n\u767b\u9646mysql:\n\tmysql -uroot -p\u5bc6\u7801\n\t\t(\u5bc6\u7801\u5373\u4e3a\u67e5\u770b\u5230\u7684\u968f\u673a\u5bc6\u7801)\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"7\">\n<li>\u4fee\u6539\u5bc6\u7801<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>set PASSWORD=PASSWORD('123456');\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"8\">\n<li>\u9000\u51faMySQL\u5ba2\u6237\u7aef<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>quit;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"9\">\n<li>\u7528\u65b0\u5bc6\u7801\u8fdb\u884c\u767b\u5f55\u5373\u53ef<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>mysql -uroot -p(\u65b0\u5bc6\u7801)\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"10\">\n<li>\u8fdc\u7a0b\u6388\u6743<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>grant all privileges on *.* to 'root' @'%' identified by '123456'; \nflush privileges;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"11\">\n<li>\u9a8c\u8bc1\u8fdc\u7a0b\u6388\u6743\u662f\u5426\u6210\u529f<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>\u901a\u8fc7Windows\u7684mysql\u8fde\u63a5\u5de5\u5177\u8fde\u63a5\u8fdc\u7a0blinux\u4e0a\u7684mysql, \u5982\u679c\u6b63\u5e38\u8fde\u63a5\u6210\u529f\u8bf4\u660e\u6388\u6743\u6210\u529f\n<\/code><\/pre>\n<h2><a id=\"3-mysql%E7%9A%84%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C%E7%AF%87\" class=\"anchor\" aria-hidden=\"true\" href=\"#3-mysql%E7%9A%84%E5%9F%BA%E7%A1%80%E6%93%8D%E4%BD%9C%E7%AF%87\"><span class=\"octicon octicon-link\"><\/span><\/a>3. MySQL\u7684\u57fa\u7840\u64cd\u4f5c\u7bc7<\/h2>\n<h3><a id=\"3-1%E7%99%BB%E5%BD%95%E4%B8%8E%E9%80%80%E5%87%BA\" class=\"anchor\" aria-hidden=\"true\" href=\"#3-1%E7%99%BB%E5%BD%95%E4%B8%8E%E9%80%80%E5%87%BA\"><span class=\"octicon octicon-link\"><\/span><\/a>3.1 \u767b\u5f55\u4e0e\u9000\u51fa<\/h3>\n<pre><code>\u767b\u5f55:\nmysql -uroot -p\u5bc6\u7801\n\n\u9000\u51fa:\n\tquit\n<\/code><\/pre>\n<h3><a id=\"3-2%E8%BE%93%E5%85%A5%E6%9F%A5%E8%AF%A2\" class=\"anchor\" aria-hidden=\"true\" href=\"#3-2%E8%BE%93%E5%85%A5%E6%9F%A5%E8%AF%A2\"><span class=\"octicon octicon-link\"><\/span><\/a>3.2 \u8f93\u5165\u67e5\u8be2<\/h3>\n<ul>\n<li>\n<ol>\n<li>\u67e5\u770b\u5f53\u524dmysql\u7684\u7248\u672c\u53f7\u53ca\u5f53\u524d\u65f6\u95f4<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>SELECT VERSION(),CURRENT_DATE();\n<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qn.fivedata.cn\/1554604032234.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/qn.fivedata.cn\/1554604032234.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<blockquote>\n<p>\u6ce8\u610f:<\/p>\n<p>\u200b 1) SQL\u8bed\u53e5\u4e0d\u533a\u5206\u5927\u5c0f\u5199, \u53ea\u8981\u5355\u8bcd\u5199\u5bf9\u4e86, \u5373\u53ef\u67e5\u8be2<\/p>\n<\/blockquote>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u4f7f\u7528SQL \u53ef\u4ee5\u8fdb\u884c\u7b80\u5355\u7684\u8fd0\u7b97<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>SELECT SIN(PI()\/4), (4+1)*5; \n<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qn.fivedata.cn\/1554604139163.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/qn.fivedata.cn\/1554604139163.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<blockquote>\n<p>\u6ce8\u610f:<\/p>\n<p>\u200b 1) PI() \u8868\u793a\u7684\u5706\u5468\u7387 3.141593<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qn.fivedata.cn\/1554604197929.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/qn.fivedata.cn\/1554604197929.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u200b 2) \u8ba1\u7b97\u65f6\u5982\u679c\u6709\u5c0f\u62ec\u53f7, \u4f1a\u5148\u8ba1\u7b97\u5c0f\u62ec\u53f7\u91cc\u9762\u7684\u5185\u5bb9<\/p>\n<p>\u200b 3) SQL\u8bed\u53e5\u4ee5\u5206\u53f7\u7ed3\u5c3e, \u4e00\u4e2a\u5206\u53f7\u5c31\u4ee3\u8868\u662f\u4e00\u53e5SQL\u7684\u7ed3\u675f<\/p>\n<p>\u200b select SIN(PI()\/4) ; select (4+1)*5; \u8fd9\u662f\u4e24\u53e5SQL<\/p>\n<p>\u200b 4) \u5728\u8fdb\u884c\u4e66\u5199\u7684\u65f6\u5019,\u5982\u679c\u4e00\u6761SQL\u592a\u957f, \u53ef\u4ee5\u5206\u4e3a\u591a\u884c\u4e66\u5199, \u53ea\u8981\u4e0d\u8f93\u5165\u5206\u53f7, \u5c31\u4e0d\u4f1a\u8ba4\u4e3aSQL\u5df2\u7ecf\u7ed3\u675f<\/p>\n<p>\u200b 5) \u5199\u5230\u4e00\u534a, \u5982\u679c\u4e0d\u60f3\u6267\u884cSQL\u4e86, \u5728SQL\u540e\u9762\u52a0\u4e0a\\c \u7136\u540e\u56de\u8f66\u5373\u53ef<\/p>\n<\/blockquote>\n<h3><a id=\"3-3%E5%88%9B%E5%BB%BA%E5%92%8C%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\" href=\"#3-3%E5%88%9B%E5%BB%BA%E5%92%8C%E4%BD%BF%E7%94%A8%E6%95%B0%E6%8D%AE%E5%BA%93\"><span class=\"octicon octicon-link\"><\/span><\/a>3.3 \u521b\u5efa\u548c\u4f7f\u7528\u6570\u636e\u5e93<\/h3>\n<ul>\n<li>\n<ol>\n<li>\u67e5\u770b\u5f53\u524d\u6709\u90a3\u4e9b\u6570\u636e\u5e93<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>show databases ; \n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u521b\u5efa\u6570\u636e\u5e93<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>create databases \u6570\u636e\u5e93\u540d\u79f0;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u548c\u5207\u6362\u6570\u636e\u5e93<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>use \u6570\u636e\u5e93\u540d\u79f0 ; \n<\/code><\/pre>\n<h3><a id=\"3-4%E5%88%9B%E5%BB%BA%E8%A1%A8%E5%8F%8A%E4%BD%BF%E7%94%A8\" class=\"anchor\" aria-hidden=\"true\" href=\"#3-4%E5%88%9B%E5%BB%BA%E8%A1%A8%E5%8F%8A%E4%BD%BF%E7%94%A8\"><span class=\"octicon octicon-link\"><\/span><\/a>3.4 \u521b\u5efa\u8868\u53ca\u4f7f\u7528<\/h3>\n<ul>\n<li>\n<ol>\n<li>\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u4e2d\u6709\u90a3\u4e9b\u8868<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>show tables ;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u521b\u5efa\u4e00\u4e2a\u8868<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>create table \u8868\u540d (\n\t\u5b57\u6bb5\u540d\u79f0(\u957f\u5ea6) \u6570\u636e\u7c7b\u578b  [\u7ea6\u675f] ,\n\t\u5b57\u6bb5\u540d\u79f0(\u957f\u5ea6) \u6570\u636e\u7c7b\u578b  [\u7ea6\u675f] ,\n\t\u5b57\u6bb5\u540d\u79f0(\u957f\u5ea6) \u6570\u636e\u7c7b\u578b  [\u7ea6\u675f] \n)\n\n\n\u4f8b\u5982: \n  CREATE TABLE pet (\n  \t\tname VARCHAR(20),   \n  \t\towner VARCHAR(20),\n  \t\tspecies VARCHAR(20), \n  \t\tsex CHAR(1), \n  \t\tbirth DATE, \n  \t\tdeath DATE\n  );\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u67e5\u770b\u8868\u7684\u7ed3\u6784<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>show create table \u8868\u540d  ; \ndesc \u8868\u540d ;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"4\">\n<li>\u5728\u8868\u521b\u5efa\u540e, \u5bfc\u5165\u6570\u636e<\/li>\n<\/ol>\n<ul>\n<li>\u5728\u8868\u4e2d\u5bfc\u5165\u6570\u636e\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\n<ul>\n<li>\u7b2c\u4e00\u79cd\uff1a\u5c06\u4ee5\u4e0a\u6570\u636e\u6574\u7406\u6210SQL\u8bed\u53e5\uff0cinsert into pet&#8230;.<\/li>\n<li>\u7b2c\u4e8c\u79cd\uff1a\u901a\u8fc7\u52a0\u8f7d\u6587\u4ef6\u7684\u65b9\u5f0f\u5c06\u6570\u636e\u5bfc\u5165\u5230\u8868\u4e2d<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>\u7b2c\u4e8c\u79cd: \u5728\u672c\u5730\u521b\u5efa\u4e00\u4e2apet.txt\u6587\u4ef6, \u540d\u79f0\u53ef\u4ee5\u81ea\u5b9a\u4e49 \uff08\u6ce8\uff1a\u6bcf\u4e2a\u5b57\u6bb5\u4e2d\u7528tab\u952e\u9694\u5f00\uff0c\u5b57\u6bb5\u6ca1\u6709\u503c\u5f97\u8bb0\u5f55\u7528\\N\u4ee3\u66ff\uff09\n\n\u6587\u4ef6\u7684\u5185\u5bb9:\nFluffy  Harold  cat     f       1993-02-04\nClaws   Gwen    cat     m       1994-03-17\nBuffy   Harold  dog     f       1989-05-13\nFang    Benny   dog     m       1990-08-27\nBowser  Diane   dog     m       1979-08-31      1995-07-29\nChirpy  Gwen    bird    f       1998-09-11\nWhistler        Gwen    bird    \\N      1997-12-09      \\N\nSlim    Benny   snake   m       1996-04-29\n\n\u6267\u884c\u52a0\u8f7d\u6587\u4ef6\u7684SQL\u5c06\u5176\u5bfc\u5165\u8868\u4e2d:\n\tload data local infile '\u672c\u5730\u8def\u5f84' into table \u8868\u540d ;\n\t\u8303\u4f8b:\n\t\tload data local infile '\/root\/pet.txt' into table pet;\n\t\n\u68c0\u9a8c\u662f\u5426\u5df2\u7ecf\u5bfc\u5165\u6210\u529f:\n\tselect * from \u8868\u540d ; \n\u6ce8\u610f:\n\tput -a \u6587\u4ef6\u8def\u5f84\t\/\/\u5c06\u6307\u5b9a\u6587\u4ef6\u4e0a\u4f20\u7684\u670d\u52a1\u5668.\n\t\t\u793a\u4f8b: put -a D:\\compile\\pet.txt\n\trm -rf \u6587\u4ef6\u540d   \/\/\u79fb\u9664\u6307\u5b9a\u7684\u6587\u4ef6.\n\t\t\u793a\u4f8b: rm -rf test01.sql\n<\/code><\/pre>\n<h3><a id=\"3-5%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E9%83%A8%E5%88%86\" class=\"anchor\" aria-hidden=\"true\" href=\"#3-5%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E9%83%A8%E5%88%86\"><span class=\"octicon octicon-link\"><\/span><\/a>3.5 \u6570\u636e\u67e5\u8be2\u90e8\u5206<\/h3>\n<ul>\n<li>\n<ol>\n<li>\u67e5\u8be2\u5168\u90e8\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select * from \u8868\u540d ; \n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u5220\u9664\u8868\u4e2d\u5168\u90e8\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>delete from \u8868\u540d ;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u66f4\u65b0\u8868\u4e2d\u6307\u5b9a\u8bb0\u5f55\u7684\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>update \u8868\u540d set \u5b57\u6bb5\u540d\u79f01 = \u503c1,\u5b57\u6bb5\u540d\u79f02 = \u503c2,\u5b57\u6bb5\u540d\u79f03 = \u503c3 where \u6761\u4ef6 ; \n\n\u6ce8\u610f: \u5982\u679c\u6ca1\u6709where\u6761\u4ef6, \u5c31\u4f1a\u5bf9\u8868\u4e2d\u6240\u6709\u7684\u6570\u636e\u8fdb\u884c\u5bf9\u5e94\u4fee\u6539\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"4\">\n<li>\u67e5\u8be2\u7279\u5b9a\u7684\u884c<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select * from \u8868\u540d where \u5b57\u6bb5\u540d\u79f0 = '\u503c' ; \n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"5\">\n<li>\u67e5\u8be2\u67d0\u4e2a\u8303\u56f4\u7684\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select * from \u8868\u540d where \u5b57\u6bb5\u540d\u79f0 &gt;|&gt;=|&lt;|&lt;= \u503c ;\nselect * from \u8868\u540d where \u5b57\u6bb5\u540d\u79f0 between  min  and  max ;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"6\">\n<li>\u591a\u6761\u4ef6\u67e5\u8be2: and \u548c or<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select * from \u8868\u540d where \u6761\u4ef61 and|or  \u6761\u4ef62 and|or (\u6761\u4ef63  and|or \u6761\u4ef64) ....;\n\nand:  \u5e76\u5217\u5173\u7cfb, \u7ed3\u679c\u5fc5\u987b\u662fand\u6761\u4ef6\u5185\u7684\u6570\u636e\nor:  \u6216\u8005\u5173\u7cfb , \u7ed3\u679c\u5982\u679c\u6709\u5c55\u793a, \u6ca1\u6709\u5c31\u4e0d\u5c55\u793a\n\u5982\u679c\u6709\u62ec\u53f7\u7684, \u4f18\u5316\u6267\u884c\u62ec\u53f7\u5185\u7684\u6570\u636e\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"7\">\n<li>\u67e5\u8be2\u8fd4\u56de\u6307\u5b9a\u5217\u7684\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select \u5b57\u6bb5\u540d\u79f01,\u5b57\u6bb5\u540d\u79f02... from  \u8868\u540d ; \n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"8\">\n<li>\u5982\u679c\u8868\u4e2d\u6709\u91cd\u590d\u6570\u636e, \u53ef\u4ee5\u5c06\u5176\u53bb\u91cd : distinct<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select distinct \u5b57\u6bb5\u540d\u79f01,\u5b57\u6bb5\u540d\u79f02... from  \u8868\u540d ;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"9\">\n<li>\u6392\u5e8f\u64cd\u4f5c: order by<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>SELECT *|\u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d ORDER BY \u6392\u5e8f\u5b57\u6bb51 [\u6392\u5e8f\u65b9\u5f0f],\u6392\u5e8f\u5b57\u6bb52 [\u6392\u5e8f\u65b9\u5f0f];\n\n\n\u6392\u5e8f\u65b9\u5f0f:  desc  \u548c asc  , \u9ed8\u8ba4\u4e3a\u5347\u5e8f\u6392\u5e8f\n\n\u5982\u679c\u662f\u591a\u4e2a\u5b57\u6bb5\u6392\u5e8f, \u4f1a\u5148\u6309\u7167\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f,\u5982\u679c\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u6709\u76f8\u540c\u7684, \u7136\u540e\u5728\u6309\u7167\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u6392\u5e8f\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"10\">\n<li>\u65e5\u671f\u8ba1\u7b97<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>\u67e5\u8be2\u5f53\u524d\u7684\u65e5\u671f:\n\tselect curdate() from pet;\n\u83b7\u53d6\u5f53\u5e74\u7684\u5e74 :\n\tselect YEAR('2018-02-05') AS YEARS from pet;\n\u83b7\u53d6\u5f53\u5e74\u7684\u6708\n\tselect month('2018-02-05') AS month from pet;\n\u83b7\u53d6\u5f53\u5e74\u7684\u65e5\n\tselect day('2018-02-05') AS day from pet; \n\t\n\u8ba1\u7b97\u5e74\u9f84:\n\tSELECT name, birth, CURDATE(), TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age FROM pet;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"11\">\n<li>is null\u548c is not null\u503c<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>SELECT name, birth, death, TIMESTAMPDIFF(YEAR,birth,death) AS age FROM pet WHERE death IS NOT NULL ORDER BY age;\n\n\u683c\u5f0f:  \u5b57\u6bb5  is null   \u7b5b\u9009\u8fd9\u4e2a\u5b57\u6bb5\u4e3anull\u7684\u6570\u636e\n\t  \u5b57\u6bb5  is not null \u7b5b\u9009\u8fd9\u4e2a\u5b57\u6bb5\u4e0d\u4e3anull\u7684\u6570\u636e\n<\/code><\/pre>\n<h2><a id=\"4%E6%A1%88%E4%BE%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#4%E6%A1%88%E4%BE%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>4. \u6848\u4f8b<\/h2>\n<ul>\n<li>\n<ol>\n<li>\u521b\u5efa\u8868\u548c\u5bfc\u5165\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>CREATE TABLE shop (\n\tarticle INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,\n\tdealer CHAR(20) DEFAULT '' NOT NULL,\n\tprice DOUBLE(16,2) DEFAULT '0.00' NOT NULL,\n\tPRIMARY KEY(article, dealer)\n);\n\n\nINSERT INTO shop VALUES\n(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),\n(3,'C',1.69),(3,'D',1.25),(4,'D',19.95);\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u68c0\u7d22\u8868\u4e2d\u7684\u5168\u90e8\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>select * from shop;\n\n+---------+--------+-------+\n| article | dealer | price |\n+---------+--------+-------+\n|    0001 | A      |  3.45 |\n|    0001 | B      |  3.99 |\n|    0002 | A      | 10.99 |\n|    0003 | B      |  1.45 |\n|    0003 | C      |  1.69 |\n|    0003 | D      |  1.25 |\n|    0004 | D      | 19.95 |\n+---------+--------+-------+\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u6c42\u67d0\u4e00\u5217\u7684\u6700\u5927\u503c\u6216\u8005\u6700\u5c0f\u503c<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>SELECT MAX(article) AS article FROM shop;\n\n+---------+\n| article |\n+---------+\n|       4 |\n+---------+\n\n\t\/\/\u6c42\u67d0\u4e00\u5217\u7684\u6700\u5c0f\u503c\nselect min(price) as article from shop; \n \n+---------+\n| article |\n+---------+\n|    1.25 |\n+---------+\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"4\">\n<li>\u8fc7\u6ee4\u51fa\u67d0\u4e2a\u5b57\u6bb5\u503c\u6700\u5927\u7684\u6574\u6761\u8bb0\u5f55\u6570\u636e<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>\u4f7f\u7528\u5b50\u67e5\u8be2:\n\tSELECT article, dealer, price FROM shop WHERE price=(SELECT MAX(price) FROM shop);\n\n+---------+--------+-------+\n| article | dealer | price |\n+---------+--------+-------+\n|    0004 | D      | 19.95 |\n+---------+--------+-------+\n\n\u4f7f\u7528\u81ea\u5173\u8054\u67e5\u8be2:\n\tSELECT s1.article, s1.dealer, s1.price FROM shop s1 LEFT JOIN shop s2 ON s1.price &lt; s2.price WHERE s2.article IS NULL;\n\t\n\tSELECT article, dealer, price FROM shop ORDER BY price DESC LIMIT 1;\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"5\">\n<li>\u6c42\u51fa\u6bcf\u4e00\u5217\u7684\u6700\u5927\u503c\uff0c\u5e76\u4e14\u6839\u636e\u67d0\u4e00\u4e2a\u5b57\u6bb5\u8fdb\u884c\u5206\u7ec4&#8211;\u5206\u7ec4topn\u6c42\u6cd5<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>SELECT article, MAX(price) AS price FROM shop GROUP BY article;\n\n+---------+-------+\n| article | price |\n+---------+-------+\n|    0001 |  3.99 |\n|    0002 | 10.99 |\n|    0003 |  1.69 |\n|    0004 | 19.95 |\n+---------+-------+\n<\/code><\/pre>\n<h2><a id=\"5-sql%E4%B8%AD%E7%9A%84%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#5-sql%E4%B8%AD%E7%9A%84%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>5. SQL\u4e2d\u7684\u805a\u5408\u51fd\u6570<\/h2>\n<p>\u200b SQL\u8bed\u8a00\u4e2d\u5b9a\u4e49\u4e86\u90e8\u5206\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u5bf9\u67e5\u8be2\u7ed3\u679c\u7684\u8ba1\u7b97\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>count \u7edf\u8ba1\u4e2a\u6570\uff08\u884c\u6570\uff09<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>sum\u51fd\u6570\uff1a\u6c42\u548c<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>avg\u51fd\u6570\uff1a\u6c42\u5e73\u5747\u503c<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"4\">\n<li>max\u3001min \u6c42\u6700\u5927\u503c\u548c\u6700\u5c0f\u503c<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h3><a id=\"5-1-count%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#5-1-count%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>5.1 count\u51fd\u6570<\/h3>\n<p>\u200b \u8bed\u6cd5: select count(*|\u5217\u540d) from \u8868\u540d;<\/p>\n<p>\u200b \u6ce8\u610f\uff1a count\u5728\u6839\u636e\u6307\u5b9a\u7684\u5217\u7edf\u8ba1\u7684\u65f6\u5019\uff0c\u5982\u679c\u8fd9\u4e00\u5217\u4e2d\u6709null \u4e0d\u4f1a\u88ab\u7edf\u8ba1\u5728\u5176\u4e2d\u3002<\/p>\n<pre><code>mysql&gt; select * from pet;\n+----------+--------+---------+------+------------+------------+\n| name     | owner  | species | sex  | birth      | death      |\n+----------+--------+---------+------+------------+------------+\n| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL       |\n| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL       |\n| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |\n| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |\n| Bowser   | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |\n| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |\n| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |\n| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL       |\n+----------+--------+---------+------+------------+------------+\n8 rows in set (0.00 sec)\n\nmysql&gt; select count(sex) from pet; \n+------------+\n| count(sex) |\n+------------+\n|          7 |\n+------------+\n1 row in set (0.00 sec)\n\nmysql&gt; select count(owner) from pet;   \n+--------------+\n| count(owner) |\n+--------------+\n|            8 |\n+--------------+\n1 row in set (0.00 sec)\n\nmysql&gt; select count(death) from pet;     \n+--------------+\n| count(death) |\n+--------------+\n|            1 |\n+--------------+\n<\/code><\/pre>\n<h3><a id=\"5-2-sum%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#5-2-sum%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>5.2 sum \u51fd\u6570<\/h3>\n<p>\u200b \u8bed\u6cd5: select sum(\u5217\u540d) from \u8868\u540d;<\/p>\n<p>\u200b \u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n<p>\u200b 1\u3001\u5982\u679c\u4f7f\u7528sum \u591a\u5217\u8fdb\u884c\u6c42\u548c\u7684\u65f6\u5019\uff0c\u5982\u679c\u67d0\u4e00\u5217\u4e2d\u7684\u6709null\uff0c\u8fd9\u4e00\u5217\u6240\u5728\u7684\u884c\u4e2d\u7684\u5176\u4ed6\u6570\u636e\u4e0d\u4f1a\u88ab\u52a0\u5230\u603b\u548c\u3002<\/p>\n<p>\u200b 2\u3001\u53ef\u4ee5\u4f7f\u7528mysql \u6570\u636e\u5e93\u63d0\u4f9b\u7684\u51fd\u6570 ifnull(\u5217\u540d,\u503c)<\/p>\n<p>\u200b 3\u3001\u5728\u6570\u636e\u5e93\u4e2d\u5b9a\u4e49double\u7c7b\u578b\u6570\u636e\uff0c\u662f\u4e00\u4e2a\u8fd1\u4f3c\u503c\uff0c\u9700\u8981\u786e\u5b9a\u51c6\u786e\u7684\u4f4d\u6570\uff0c\u8fd9\u65f6\u53ef\u4ee5\u628a\u8fd9\u4e00\u5217\u8bbe\u8ba1\u6210numeric\u7c7b\u578b\u3002numeric(\u6570\u636e\u7684\u603b\u5217\u6570,\u5c0f\u6570\u4f4d\u6570)<\/p>\n<pre><code>mysql&gt; select sum(price) from shop;  \n+------------+\n| sum(price) |\n+------------+\n|      42.77 |\n+------------+\n<\/code><\/pre>\n<h3><a id=\"5-3-avg%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#5-3-avg%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>5.3 avg \u51fd\u6570<\/h3>\n<p>\u200b \u8bed\u6cd5\uff1aselect avg(\u5217\u540d) from \u8868\u540d;<\/p>\n<pre><code>mysql&gt; select avg(price) from shop;   \n+------------+\n| avg(price) |\n+------------+\n|   6.110000 |\n+------------+\n<\/code><\/pre>\n<h3><a id=\"5-4-max%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#5-4-max%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>5.4 max\u51fd\u6570<\/h3>\n<p>\u200b \u8bed\u6cd5\uff1aselect max(\u5217\u540d) from \u8868\u540d;<\/p>\n<pre><code>mysql&gt; select max(price) from shop;   \n+------------+\n| max(price) |\n+------------+\n|      19.95 |\n+------------+\n<\/code><\/pre>\n<h3><a id=\"5-5-min%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#5-5-min%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>5.5 min\u51fd\u6570<\/h3>\n<p>\u200b \u8bed\u6cd5\uff1aselect min(\u5217\u540d) from \u8868\u540d;<\/p>\n<pre><code>mysql&gt; select min(price) from shop;   \n+------------+\n| min(price) |\n+------------+\n|       1.25 |\n+------------+\n<\/code><\/pre>\n<h2><a id=\"6-sql%E5%88%86%E7%B1%BB\" class=\"anchor\" aria-hidden=\"true\" href=\"#6-sql%E5%88%86%E7%B1%BB\"><span class=\"octicon octicon-link\"><\/span><\/a>6. SQL\u5206\u7c7b<\/h2>\n<h3><a id=\"6-1-ddl%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%9A%E4%B9%89%E8%AF%AD%E8%A8%80\" class=\"anchor\" aria-hidden=\"true\" href=\"#6-1-ddl%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%9A%E4%B9%89%E8%AF%AD%E8%A8%80\"><span class=\"octicon octicon-link\"><\/span><\/a>6.1 DDL\u8bed\u53e5 (\u6570\u636e\u5e93\u5b9a\u4e49\u8bed\u8a00)<\/h3>\n<p>\u200b \u5bf9\u6570\u636e\u5e93 \u8868 \u5217 \u8fdb\u884c\u64cd\u4f5c<\/p>\n<pre><code>\u67e5\u770b\u6570\u636e\u5e93:show database;\n\u521b\u5efa\u6570\u636e\u5e93 create database \u6570\u636e\u5e93\u540d;\n          create database \u6570\u636e\u5e93\u540d character set \u9700\u8981\u4f7f\u7528\u7684\u7f16\u7801;\n\u67e5\u8be2\u521b\u5efa\u6570\u636e\u5e93\u8bed\u53e5:show create database \u6570\u636e\u5e93\u540d;\n\u5220\u9664\u6570\u636e\u5e93 drop database \u9700\u8981\u5220\u9664\u7684\u6570\u636e\u5e93\u540d;\n\u67e5\u770b\u5f53\u524d\u6b63\u5728\u4f7f\u7528\u7684\u6570\u636e\u5e93 select database();\n\u4f7f\u7528\u6570\u636e\u5e93: use \u6570\u636e\u5e93\u540d\n\n\n\u521b\u5efa\u8868: creat table \u8868\u540d(\u5b57\u6bb5\u540d \u7c7b\u578b(\u957f\u5ea6) [\u7ea6\u675f],\u5b57\u6bb5\u540d \u7c7b\u578b(\u957f\u5ea6) [\u7ea6\u675f]..);\n\u67e5\u770b\u8868: show tables;\n\u67e5\u770b\u521b\u5efa\u8868\u7684\u8bed\u53e5: show creat table \u8868\u540d;\n\u67e5\u770b\u8868\u7ed3\u6784: desc \u8868\u540d;\n\u5220\u9664\u8868: drop table \u8868\u540d;\n\u5411\u8868\u4e2d\u6dfb\u52a0\u5b57\u6bb5: alter table \u8868\u540d add \u5b57\u6bb5\u540d \u7c7b\u578b [\u7ea6\u675f];\n\u4fee\u6539 \u5bf9\u5b57\u6bb5\u8fdb\u884c\u4fee\u6539 \u4f46\u4e0d\u4fee\u6539\u5b57\u6bb5\u540d: alter table \u8868\u540d modify \u5b57\u6bb5\u540d \u7c7b\u578b [\u7ea6\u675f];\n\u6539\u53d8 \u5bf9\u5b57\u6bb5\u8fdb\u884c\u4fee\u6539 \u53ef\u4ee5\u4fee\u6539\u5b57\u6bb5\u540d: alter table \u8868\u540d change \u65e7\u5b57\u6bb5\u540d \u65b0\u5b57\u6bb5\u540d \u7c7b\u578b [\u7ea6\u675f];\n\u5220\u9664\u8868\u4e2d\u7684\u5217: alter table \u8868\u540d \u5b57\u6bb5;\n\u4fee\u6539\u8868\u540d: rename table \u65e7\u8868\u540d to \u65b0\u8868\u540d;\n<\/code><\/pre>\n<h3><a id=\"6-2-dml%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C%E8%AF%AD%E8%A8%80\" class=\"anchor\" aria-hidden=\"true\" href=\"#6-2-dml%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C%E8%AF%AD%E8%A8%80\"><span class=\"octicon octicon-link\"><\/span><\/a>6.2 DML\u8bed\u53e5(\u6570\u636e\u5e93\u64cd\u4f5c\u8bed\u8a00)<\/h3>\n<p>\u200b \u5bf9\u6570\u636e\u5e93\u91cc\u7684\u6570\u636e\u8fdb\u884c\u64cd\u4f5c \u589e \u5220 \u6539<\/p>\n<p>\u200b \u5982 update\uff0c insert\uff0c delete<\/p>\n<pre><code>insert into \u8868\u540d(\u5b57\u6bb5\u540d,\u5b57\u6bb5\u540d..) values(\u503c,\u503c,..);\ninsert into \u8868\u540d values(\u503c,\u503c,..);\nupdate \u8868\u540d set \u5b57\u6bb5=\u503c,\u5b57\u6bb5=\u503c..where \u6761\u4ef6 ;\ndelete from \u8868\u540d where \u6761\u4ef6 ;\n\ntruncate delete\u533a\u522b: \n    delete \u5220\u9664\u6570\u636e\u65f6 \u81ea\u52a8\u589e\u957f\u4e0d\u4f1a\u6e05\u9664\n    truncate\u5220\u9664\u6570\u636e\u65f6 \u6e05\u9664\u81ea\u52a8\u589e\u957f \u91cd\u5199\u8ba1\u6570\n<\/code><\/pre>\n<h3><a id=\"6-3-dcl%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%8E%A7%E5%88%B6%E8%AF%AD%E8%A8%80\" class=\"anchor\" aria-hidden=\"true\" href=\"#6-3-dcl%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%8E%A7%E5%88%B6%E8%AF%AD%E8%A8%80\"><span class=\"octicon octicon-link\"><\/span><\/a>6.3 DCL\u8bed\u53e5(\u6570\u636e\u5e93\u63a7\u5236\u8bed\u8a00)<\/h3>\n<p>\u200b \u7528\u4e8e\u8bbe\u7f6e\u7528\u6237\u6743\u9650\u548c\u63a7\u5236\u4e8b\u52a1\u8bed\u53e5<\/p>\n<p>\u200b \u5982 grant&#8230;&#8230;<\/p>\n<h3><a id=\"6-4-dql%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80\" class=\"anchor\" aria-hidden=\"true\" href=\"#6-4-dql%E8%AF%AD%E5%8F%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80\"><span class=\"octicon octicon-link\"><\/span><\/a>6.4 DQL\u8bed\u53e5(\u6570\u636e\u5e93\u67e5\u8be2\u8bed\u8a00)<\/h3>\n<p>\u200b \u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u67e5\u8be2<\/p>\n<pre><code> select distinct * from \u8868\u540d where \u6761\u4ef6 \n     group by \u5206\u7ec4\u5b57\u6bb5 having\u5206\u7ec4\u5b8c\u63a5\u6761\u4ef6\u7b5b\u9009\u6761\u4ef6\n     order by \u6392\u5e8f\n     distinct  \u53bb\u6389\u91cd\u590d\n<\/code><\/pre>\n<h2><a id=\"7%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%A4%87%E4%BB%BD%E4%B8%8E%E6%81%A2%E5%A4%8D\" class=\"anchor\" aria-hidden=\"true\" href=\"#7%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E5%A4%87%E4%BB%BD%E4%B8%8E%E6%81%A2%E5%A4%8D\"><span class=\"octicon octicon-link\"><\/span><\/a>7. \u6570\u636e\u5e93\u7684\u5907\u4efd\u4e0e\u6062\u590d<\/h2>\n<h3><a id=\"7-1%E5%A4%87%E4%BB%BD%E5%91%BD%E4%BB%A4\" class=\"anchor\" aria-hidden=\"true\" href=\"#7-1%E5%A4%87%E4%BB%BD%E5%91%BD%E4%BB%A4\"><span class=\"octicon octicon-link\"><\/span><\/a>7.1 \u5907\u4efd\u547d\u4ee4<\/h3>\n<p>\u200b \u5728mysql\u7684\u5b89\u88c5\u76ee\u5f55\u7684bin\u76ee\u5f55\u4e0b\u6709mysqldump\u547d\u4ee4\uff0c\u53ef\u4ee5\u5b8c\u6210\u5bf9\u6570\u636e\u5e93\u7684\u5907\u4efd\u3002<\/p>\n<p>\u200b \u8bed\u6cd5\uff1amysqldump -u \u7528\u6237\u540d -p \u6570\u636e\u5e93\u540d&gt; \u78c1\u76d8SQL\u6587\u4ef6\u8def\u5f84<\/p>\n<p>\u200b \u7531\u4e8emysqldump\u547d\u4ee4\u4e0d\u662fsql\u547d\u4ee4\uff0c\u9700\u8981\u5728dos\u7a97\u53e3\u4e0b\u4f7f\u7528\u3002<\/p>\n<p>\u200b \u6ce8\u610f\uff1a\u5728\u5907\u4efd\u6570\u636e\u7684\u65f6\u5019\uff0c\u6570\u636e\u5e93\u4e0d\u4f1a\u88ab\u5220\u9664\u3002\u53ef\u4ee5\u624b\u52a8\u5220\u9664\u6570\u636e\u5e93\u3002\u540c\u65f6\u5728\u6062\u590d\u6570\u636e\u7684\u65f6\u5019\uff0c\u4e0d\u4f1a<\/p>\n<p>\u81ea\u52a8\u7684\u7ed9\u6211\u4eec\u521b\u5efa\u6570\u636e\u5e93\uff0c\u4ec5\u4ec5\u53ea\u4f1a\u6062\u590d\u6570\u636e\u5e93\u4e2d\u7684\u8868\u548c\u8868\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<pre><code>\u4f8b\u5982: mysqldump -uroot -p123456 test01 &gt;\/root\/data\/test01.sql\n\u5176\u4e2d: test01\u662f\u6570\u636e\u5e93\u7684\u540d\u5b57\n<\/code><\/pre>\n<h3><a id=\"7-2%E6%81%A2%E5%A4%8D%E5%91%BD%E4%BB%A4\" class=\"anchor\" aria-hidden=\"true\" href=\"#7-2%E6%81%A2%E5%A4%8D%E5%91%BD%E4%BB%A4\"><span class=\"octicon octicon-link\"><\/span><\/a>7.2 \u6062\u590d\u547d\u4ee4<\/h3>\n<p>\u6062\u590d\u6570\u636e\u5e93\uff0c\u9700\u8981\u624b\u52a8\u7684\u5148\u521b\u5efa\u6570\u636e\u5e93\uff1a<\/p>\n<p>\u200b create database heima2;<\/p>\n<p>\u8bed\u6cd5\uff1amysql -u \u7528\u6237\u540d-p \u5bfc\u5165\u5e93\u540d&lt; \u786c\u76d8SQL\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84<\/p>\n<pre><code>\/\/\u6062\u590d\u547d\u4ee4\nmysql -uroot -p123456 itcast&lt;\/root\/data\/menagerie.sql\n<\/code><\/pre>\n<h2><a id=\"8%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2\" class=\"anchor\" aria-hidden=\"true\" href=\"#8%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2\"><span class=\"octicon octicon-link\"><\/span><\/a>8. \u591a\u8868\u67e5\u8be2<\/h2>\n<h3><a id=\"8-1%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E4%BB%8B%E7%BB%8D\" class=\"anchor\" aria-hidden=\"true\" href=\"#8-1%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E4%BB%8B%E7%BB%8D\"><span class=\"octicon octicon-link\"><\/span><\/a>8.1 \u7b1b\u5361\u5c14\u79ef\u4ecb\u7ecd<\/h3>\n<p>\u200b \u7b1b\u5361\u5c14\u4e58\u79ef\u662f\u6307\u5728\u6570\u5b66\u4e2d\uff0c\u4e24\u4e2a\u96c6\u5408X\u548cY\u7684\u7b1b\u5361\u5c13\u79ef\uff08Cartesian product\uff09\uff0c\u53c8\u79f0\u76f4\u79ef\uff0c\u8868\u793a\u4e3aX \u00d7 Y\uff0c\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u662fX\u7684\u6210\u5458\u800c\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\u662fY\u7684\u6240\u6709\u53ef\u80fd\u6709\u5e8f\u5bf9\u7684\u5176\u4e2d\u4e00\u4e2a\u6210\u5458<\/p>\n<p>\u51c6\u5907\u6570\u636e:<\/p>\n<pre><code>create table A(\n  A_ID int primary key auto_increment,\n  A_NAME varchar(20) not null\n);\ninsert into A values(1,'apple');\ninsert into A values(2,'orange');\ninsert into A values(3,'banana');\n\ncreate table B( \n   A_ID int primary key auto_increment,\n   B_PRICE double\n);\ninsert into B values(1,2.30);\ninsert into B values(2,3.50);\ninsert into B values(4,null);\n<\/code><\/pre>\n<p>\u5c55\u793a\u6548\u679c\uff1a<\/p>\n<pre><code>mysql&gt; select * from A,B;\n+------+--------+------+---------+\n| A_ID | A_NAME | A_ID | B_PRICE |\n+------+--------+------+---------+\n|    1 | apple  |    1 |     2.3 |\n|    2 | orange |    1 |     2.3 |\n|    3 | banana |    1 |     2.3 |\n|    1 | apple  |    2 |     3.5 |\n|    2 | orange |    2 |     3.5 |\n|    3 | banana |    2 |     3.5 |\n|    1 | apple  |    4 |    NULL |\n|    2 | orange |    4 |    NULL |\n|    3 | banana |    4 |    NULL |\n+------+--------+------+---------+\n<\/code><\/pre>\n<p>\u4f5c\u7528\uff1a\u7b1b\u5361\u5c14\u79ef\u7684\u6570\u636e\uff0c\u5bf9\u7a0b\u5e8f\u662f\u6ca1\u6709\u610f\u4e49\u7684\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u7b1b\u5361\u5c14\u79ef\u4e2d\u7684\u6570\u636e\u518d\u6b21\u8fdb\u884c\u8fc7\u6ee4\u3002<\/p>\n<p>\u5bf9\u4e8e\u591a\u8868\u67e5\u8be2\u64cd\u4f5c\uff0c\u9700\u8981\u8fc7\u6ee4\u51fa\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\uff0c\u9700\u8981\u628a\u591a\u4e2a\u8868\u8fdb\u884c\u8fde\u63a5\uff0c\u8fde\u63a5\u4e4b\u540e\u9700\u8981\u52a0\u4e0a\u8fc7\u6ee4\u7684\u6761\u4ef6\u3002<\/p>\n<h3><a id=\"8-2%E5%86%85%E8%BF%9E%E6%8E%A5\" class=\"anchor\" aria-hidden=\"true\" href=\"#8-2%E5%86%85%E8%BF%9E%E6%8E%A5\"><span class=\"octicon octicon-link\"><\/span><\/a>8.2 \u5185\u8fde\u63a5:<\/h3>\n<ul>\n<li>\u663e\u793a\u5185\u8fde\u63a5:<\/li>\n<\/ul>\n<pre><code>\u8bed\u6cd5: select * from \u8868\u540d1  inner join \u8868\u540d2 on \u5173\u8054\u6761\u4ef6 ; \n<\/code><\/pre>\n<ul>\n<li>\u9690\u5f0f\u5185\u8fde\u63a5:<\/li>\n<\/ul>\n<pre><code>select \u5217\u540d , \u5217\u540d .... from \u8868\u540d1,\u8868\u540d2 where \u5173\u8054\u6761\u4ef6;\n<\/code><\/pre>\n<h3><a id=\"8-3%E5%A4%96%E8%BF%9E%E6%8E%A5\" class=\"anchor\" aria-hidden=\"true\" href=\"#8-3%E5%A4%96%E8%BF%9E%E6%8E%A5\"><span class=\"octicon octicon-link\"><\/span><\/a>8.3 \u5916\u8fde\u63a5:<\/h3>\n<ul>\n<li>\u5de6\u5916\u8fde\u63a5:\n<ul>\n<li>\u7528\u5de6\u8fb9\u8868\u53bb\u53f3\u8fb9\u8868\u4e2d\u67e5\u8be2\u5bf9\u5e94\u8bb0\u5f55\uff0c\u4e0d\u7ba1\u662f\u5426\u627e\u5230\uff0c\u90fd\u5c06\u663e\u793a\u5de6\u8fb9\u8868\u4e2d\u5168\u90e8\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>\u8bed\u6cd5\uff1aselect * from \u88681 left outer join \u88682 on \u6761\u4ef6;\n<\/code><\/pre>\n<ul>\n<li>\u53f3\u5916\u8fde\u63a5:\n<ul>\n<li>\u7528\u53f3\u8fb9\u8868\u53bb\u5de6\u8fb9\u8868\u67e5\u8be2\u5bf9\u5e94\u8bb0\u5f55\uff0c\u4e0d\u7ba1\u662f\u5426\u627e\u5230\uff0c\u53f3\u8fb9\u8868\u5168\u90e8\u8bb0\u5f55\u90fd\u5c06\u663e\u793a\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>\u8bed\u6cd5\uff1aselect * from \u88681 right outer join \u88682 on \u6761\u4ef6;\n<\/code><\/pre>\n<ul>\n<li>\u5168\u5916\u8fde\u63a5\n<ul>\n<li>\u5de6\u5916\u8fde\u63a5\u548c\u53f3\u5916\u8fde\u63a5\u7684\u7ed3\u679c\u5408\u5e76\uff0c\u5355\u4f1a\u53bb\u6389\u91cd\u590d\u7684\u8bb0\u5f55\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>\u8bed\u6cd5: select * from a full outer join b on a.A_ID = b.A_ID; \n\n\u6ce8\u610f: \u4f46\u662fmysql\u6570\u636e\u5e93\u4e0d\u652f\u6301\u6b64\u8bed\u6cd5\u3002\n<\/code><\/pre>\n<h3><a id=\"8-4%E5%85%B3%E8%81%94%E5%AD%90%E6%9F%A5%E8%AF%A2\" class=\"anchor\" aria-hidden=\"true\" href=\"#8-4%E5%85%B3%E8%81%94%E5%AD%90%E6%9F%A5%E8%AF%A2\"><span class=\"octicon octicon-link\"><\/span><\/a>8.4 \u5173\u8054\u5b50\u67e5\u8be2<\/h3>\n<p>\u200b \u5b50\u67e5\u8be2\uff1a\u628a\u4e00\u4e2asql\u7684\u67e5\u8be2\u7ed3\u679c\u4f5c\u4e3a\u53e6\u5916\u4e00\u4e2a\u67e5\u8be2\u7684\u53c2\u6570\u5b58\u5728\u3002<\/p>\n<ul>\n<li>\n<ol>\n<li>in\u548cexist\u5173\u952e\u8bcd\u7684\u7528\u6cd5<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u5173\u8054\u5b50\u67e5\u8be2\u5176\u4ed6\u7684\u5173\u952e\u5b57\u4f7f\u7528\uff1a<\/p>\n<p>\u56de\u5fc6\uff1aage=23 or age=24\u00a0 \u7b49\u4ef7\u4e8e age in (23,24)<\/p>\n<p>in \u8868\u793a\u6761\u4ef6\u5e94\u8be5\u662f\u5728\u591a\u4e2a\u5217\u503c\u4e2d\u3002<\/p>\n<p>in\uff1a\u4f7f\u7528\u5728where\u540e\u9762\uff0c\u7ecf\u5e38\u8868\u793a\u662f\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u6570\u636e\uff0c\u53ea\u8981\u88ab\u67e5\u8be2\u7684\u6570\u636e\u5728\u8fd9\u4e2a\u5217\u8868\u4e2d\u5b58\u5728\u5373\u53ef\u3002<\/p>\n<\/blockquote>\n<pre><code>mysql&gt; select * from A where A_ID in(1,2,3);\n+------+--------+\n| A_ID | A_NAME |\n+------+--------+\n|    1 | apple  |\n|    2 | orange |\n|    3 | banana |\n+------+--------+\n3 rows in set (0.00 sec)\n\nmysql&gt; select * from A where A_ID =1 or A_ID =2 or A_ID =3;\n+------+--------+\n| A_ID | A_NAME |\n+------+--------+\n|    1 | apple  |\n|    2 | orange |\n|    3 | banana |\n+------+--------+\n\/\/not in\nmysql&gt; select * from A where A_ID not in (1,2,3,4);\nEmpty set (0.00 sec)\n\nmysql&gt; select * from A where A_ID not in (3,4);    \n+------+--------+\n| A_ID | A_NAME |\n+------+--------+\n|    1 | apple  |\n|    2 | orange |\n+------+--------+\n2 rows in set (0.00 sec)\n<\/code><\/pre>\n<blockquote>\n<p>exists\uff1a\u8868\u793a\u5b58\u5728\uff0c\u5f53\u5b50\u67e5\u8be2\u7684\u7ed3\u679c\u5b58\u5728\uff0c\u5c31\u4f1a\u663e\u793a\u4e3b\u67e5\u8be2\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<\/p>\n<\/blockquote>\n<pre><code>mysql&gt; select * from A where exists(select A_ID from B);\n+------+--------+\n| A_ID | A_NAME |\n+------+--------+\n|    1 | apple  |\n|    2 | orange |\n|    3 | banana |\n+------+--------+\n\nmysql&gt; select * from A where not exists(select A_ID from B);\nEmpty set (0.00 sec)\n<\/code><\/pre>\n<ul>\n<li>union \u548cunion all\u4f7f\u7528\u6cd5<\/li>\n<\/ul>\n<blockquote>\n<p><strong>UNION \u8bed\u53e5<\/strong>\uff1a\u7528\u4e8e\u5c06\u4e0d\u540c\u8868\u4e2d\u76f8\u540c\u5217\u4e2d\u67e5\u8be2\u7684\u6570\u636e\u5c55\u793a\u51fa\u6765\uff1b\uff08\u4e0d\u5305\u62ec\u91cd\u590d\u6570\u636e\uff09<\/p>\n<p><strong>UNION ALL \u8bed\u53e5<\/strong>\uff1a\u7528\u4e8e\u5c06\u4e0d\u540c\u8868\u4e2d\u76f8\u540c\u5217\u4e2d\u67e5\u8be2\u7684\u6570\u636e\u5c55\u793a\u51fa\u6765\uff1b\uff08\u5305\u62ec\u91cd\u590d\u6570\u636e\uff09<\/p>\n<\/blockquote>\n<pre><code>mysql&gt; select * from A  union   select * from B;   \n+------+--------+\n| A_ID | A_NAME |\n+------+--------+\n|    1 | apple  |\n|    2 | orange |\n|    3 | banana |\n|    1 | 2.3    |\n|    2 | 3.5    |\n|    4 | NULL   |\n+------+--------+\n6 rows in set (0.00 sec)\n\nmysql&gt; select * from A  union all  select * from B;\n+------+--------+\n| A_ID | A_NAME |\n+------+--------+\n|    1 | apple  |\n|    2 | orange |\n|    3 | banana |\n|    1 | 2.3    |\n|    2 | 3.5    |\n|    4 | NULL   |\n+------+--------+\n<\/code><\/pre>\n<ul>\n<li>case when \u8bed\u53e5<\/li>\n<\/ul>\n<pre><code>case when \u8bed\u53e5\u8bed\u6cd5\u7ed3\u6784\uff1a\n\nCASE sex  \nWHEN '1' THEN '\u7537'  \nWHEN '2' THEN '\u5973'  \nELSE '\u5176\u4ed6' END \n<\/code><\/pre>\n<p>\u51c6\u5907\u6570\u636e:<\/p>\n<pre><code>\/\/\u521b\u5efa\u8868\ncreate table employee(\nempid                   int  ,                                       \ndeptid                  int   ,                                      \nsex                     varchar(20)  ,                                    \nsalary                  double \n);\n\n\/\/\u52a0\u8f7d\u6570\u636e\n1\t10\tfemale\t5500.0\n2\t10\tmale\t4500.0\n3\t20\tfemale\t1900.0\n4\t20\tmale\t4800.0\n5\t40\tfemale\t6500.0\n6\t40\tfemale\t14500.0\n7\t40\tmale\t44500.0\n8\t50\tmale\t6500.0\n9\t50\tmale\t7500.0\n\nload data local infile '\/root\/data\/emp.txt' into table employee ;\n<\/code><\/pre>\n<pre><code>select *,\ncase \nwhen salary &lt; 5000 then &quot;\u4f4e\u7b49\u6536\u5165&quot; \nwhen salary&gt;= 5000 and salary &lt; 10000 then &quot;\u4e2d\u7b49\u6536\u5165&quot;\nwhen salary &gt; 10000 then &quot;\u9ad8\u7b49\u6536\u5165&quot;  \nend  as level,\ncase sex\nwhen &quot;female&quot; then 1 \nwhen &quot;male&quot; then 0\nend as flag \nfrom employee;\n\n\u5982\u4e0b\u4ee3\u7801\u4e3a\u5ba0\u7269\u8868\u7684\u4ee3\u7801:\n\t\n<\/code><\/pre>\n<h2><a id=\"9-mysql%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#9-mysql%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>9. mysql\u6570\u636e\u7c7b\u578b<\/h2>\n<p>\u200b MySQL\u4e2d\u5b9a\u4e49\u6570\u636e\u5b57\u6bb5\u7684\u7c7b\u578b\u5bf9\u4f60\u6570\u636e\u5e93\u7684\u4f18\u5316\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<p>\u200b MySQL\u652f\u6301\u591a\u79cd\u7c7b\u578b\uff0c\u5927\u81f4\u53ef\u4ee5\u5206\u4e3a\u4e09\u7c7b\uff1a\u6570\u503c\u3001\u65e5\u671f\/\u65f6\u95f4\u548c\u5b57\u7b26\u4e32(\u5b57\u7b26)\u7c7b\u578b\u3002<\/p>\n<h3><a id=\"9-1%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#9-1%E6%95%B0%E5%80%BC%E7%B1%BB%E5%9E%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>9.1 \u6570\u503c\u7c7b\u578b<\/h3>\n<p>\u200b MySQL\u652f\u6301\u6240\u6709\u6807\u51c6SQL\u6570\u503c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>\u200b \u8fd9\u4e9b\u7c7b\u578b\u5305\u62ec\u4e25\u683c\u6570\u503c\u6570\u636e\u7c7b\u578b(INTEGER\u3001SMALLINT\u3001DECIMAL\u548cNUMERIC)\uff0c\u4ee5\u53ca\u8fd1\u4f3c\u6570\u503c\u6570\u636e\u7c7b\u578b(FLOAT\u3001REAL\u548cDOUBLE PRECISION)\u3002<\/p>\n<p>\u200b \u5173\u952e\u5b57INT\u662fINTEGER\u7684.\u540c\u4e49\u8bcd\uff0c\u5173\u952e\u5b57DEC\u662fDECIMAL\u7684\u540c\u4e49\u8bcd\u3002<\/p>\n<p>\u200b BIT\u6570\u636e\u7c7b\u578b\u4fdd\u5b58\u4f4d\u5b57\u6bb5\u503c\uff0c\u5e76\u4e14\u652f\u6301MyISAM\u3001MEMORY\u3001InnoDB\u548cBDB\u8868\u3002<\/p>\n<p>\u200b \u4f5c\u4e3aSQL\u6807\u51c6\u7684\u6269\u5c55\uff0cMySQL\u4e5f\u652f\u6301\u6574\u6570\u7c7b\u578bTINYINT\u3001MEDIUMINT\u548cBIGINT\u3002\u4e0b\u9762\u7684\u8868\u663e\u793a\u4e86\u9700\u8981\u7684\u6bcf\u4e2a\u6574\u6570\u7c7b\u578b\u7684\u5b58\u50a8\u548c\u8303\u56f4\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u5927\u5c0f<\/th>\n<th>\u8303\u56f4\uff08\u6709\u7b26\u53f7\uff09<\/th>\n<th>\u8303\u56f4\uff08\u65e0\u7b26\u53f7\uff09<\/th>\n<th>\u7528\u9014<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>TINYINT<\/td>\n<td>1 \u5b57\u8282<\/td>\n<td>(-128\uff0c127)<\/td>\n<td>(0\uff0c255)<\/td>\n<td>\u5c0f\u6574\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>SMALLINT<\/td>\n<td>2 \u5b57\u8282<\/td>\n<td>(-32 768\uff0c32 767)<\/td>\n<td>(0\uff0c65 535)<\/td>\n<td>\u5927\u6574\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMINT<\/td>\n<td>3 \u5b57\u8282<\/td>\n<td>(-8 388 608\uff0c8 388 607)<\/td>\n<td>(0\uff0c16 777 215)<\/td>\n<td>\u5927\u6574\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>INT\u6216INTEGER<\/td>\n<td>4 \u5b57\u8282<\/td>\n<td>(-2 147 483 648\uff0c2 147 483 647)<\/td>\n<td>(0\uff0c4 294 967 295)<\/td>\n<td>\u5927\u6574\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>BIGINT<\/td>\n<td>8 \u5b57\u8282<\/td>\n<td>(-9 233 372 036 854 775 808\uff0c9 223 372 036 854 775 807)<\/td>\n<td>(0\uff0c18 446 744 073 709 551 615)<\/td>\n<td>\u6781\u5927\u6574\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>FLOAT<\/td>\n<td>4 \u5b57\u8282<\/td>\n<td>(-3.402 823 466 E+38\uff0c-1.175 494 351 E-38)\uff0c0\uff0c(1.175 494 351 E-38\uff0c3.402 823 466 351 E+38)<\/td>\n<td>0\uff0c(1.175 494 351 E-38\uff0c3.402 823 466 E+38)<\/td>\n<td>\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>DOUBLE<\/td>\n<td>8 \u5b57\u8282<\/td>\n<td>(-1.797 693 134 862 315 7 E+308\uff0c-2.225 073 858 507 201 4 E-308)\uff0c0\uff0c(2.225 073 858 507 201 4 E-308\uff0c1.797 693 134 862 315 7 E+308)<\/td>\n<td>0\uff0c(2.225 073 858 507 201 4 E-308\uff0c1.797 693 134 862 315 7 E+308)<\/td>\n<td>\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>DECIMAL<\/td>\n<td>\u5bf9DECIMAL(M,D) \uff0c\u5982\u679cM&gt;D\uff0c\u4e3aM+2\u5426\u5219\u4e3aD+2<\/td>\n<td>\u4f9d\u8d56\u4e8eM\u548cD\u7684\u503c<\/td>\n<td>\u4f9d\u8d56\u4e8eM\u548cD\u7684\u503c<\/td>\n<td>\u5c0f\u6570\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>M\u8868\u793a: \u603b\u7684\u6570\u636e\u4e2a\u6570, D\u8868\u793a: \u5c0f\u6570\u70b9\u540e\u7684\u7cbe\u786e\u4f4d\u6570.<\/p>\n<p>DECIMAL(5, 2), \u5e0c\u671b\u83b7\u5f97\u7684\u6570\u5b57\u4e00\u5171\u67095\u4f4d, \u5e76\u8981\u6c42\u5c0f\u6570\u70b9\u4fdd\u7559\u4e24\u4f4d.<\/p>\n<p>11123.54321 &#8211;&gt; 11123.54<\/p>\n<h3><a id=\"9-2%E6%97%A5%E6%9C%9F%E5%92%8C%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#9-2%E6%97%A5%E6%9C%9F%E5%92%8C%E6%97%B6%E9%97%B4%E7%B1%BB%E5%9E%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>9.2 \u65e5\u671f\u548c\u65f6\u95f4\u7c7b\u578b<\/h3>\n<p>\u8868\u793a\u65f6\u95f4\u503c\u7684\u65e5\u671f\u548c\u65f6\u95f4\u7c7b\u578b\u4e3aDATETIME\u3001DATE\u3001TIMESTAMP\u3001TIME\u548cYEAR\u3002<\/p>\n<p>\u6bcf\u4e2a\u65f6\u95f4\u7c7b\u578b\u6709\u4e00\u4e2a\u6709\u6548\u503c\u8303\u56f4\u548c\u4e00\u4e2a&quot;\u96f6&quot;\u503c\uff0c\u5f53\u6307\u5b9a\u4e0d\u5408\u6cd5\u7684MySQL\u4e0d\u80fd\u8868\u793a\u7684\u503c\u65f6\u4f7f\u7528&quot;\u96f6&quot;\u503c\u3002<\/p>\n<p>TIMESTAMP\u7c7b\u578b\u6709\u4e13\u6709\u7684\u81ea\u52a8\u66f4\u65b0\u7279\u6027\uff0c\u5c06\u5728\u540e\u9762\u63cf\u8ff0\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u5927\u5c0f(\u5b57\u8282)<\/th>\n<th>\u8303\u56f4<\/th>\n<th>\u683c\u5f0f<\/th>\n<th>\u7528\u9014<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>DATE<\/td>\n<td>3<\/td>\n<td>1000-01-01\/9999-12-31<\/td>\n<td>YYYY-MM-DD<\/td>\n<td>\u65e5\u671f\u503c<\/td>\n<\/tr>\n<tr>\n<td>TIME<\/td>\n<td>3<\/td>\n<td>&#8216;-838:59:59&#8217;\/&#8217;838:59:59&#8217;<\/td>\n<td>HH:MM:SS<\/td>\n<td>\u65f6\u95f4\u503c\u6216\u6301\u7eed\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>YEAR<\/td>\n<td>1<\/td>\n<td>1901\/2155<\/td>\n<td>YYYY<\/td>\n<td>\u5e74\u4efd\u503c<\/td>\n<\/tr>\n<tr>\n<td>DATETIME<\/td>\n<td>8<\/td>\n<td>1000-01-01 00:00:00\/9999-12-31 23:59:59<\/td>\n<td>YYYY-MM-DD HH:MM:SS<\/td>\n<td>\u6df7\u5408\u65e5\u671f\u548c\u65f6\u95f4\u503c<\/td>\n<\/tr>\n<tr>\n<td>TIMESTAMP<\/td>\n<td>4<\/td>\n<td>1970-01-01 00:00:00\/2038\u7ed3\u675f\u65f6\u95f4\u662f\u7b2c\u00a0<strong>2147483647<\/strong>\u00a0\u79d2\uff0c\u5317\u4eac\u65f6\u95f4\u00a0<strong>2038-1-19 11:14:07<\/strong>\uff0c\u683c\u6797\u5c3c\u6cbb\u65f6\u95f4 2038\u5e741\u670819\u65e5 \u51cc\u6668 03:14:07<\/td>\n<td>YYYYMMDD HHMMSS<\/td>\n<td>\u6df7\u5408\u65e5\u671f\u548c\u65f6\u95f4\u503c\uff0c\u65f6\u95f4\u6233<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><a id=\"9-3%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#9-3%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%B1%BB%E5%9E%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>9.3 \u5b57\u7b26\u4e32\u7c7b\u578b<\/h3>\n<p>\u200b \u5b57\u7b26\u4e32\u7c7b\u578b\u6307CHAR\u3001VARCHAR\u3001BINARY\u3001VARBINARY\u3001BLOB\u3001TEXT\u3001ENUM\u548cSET\u3002\u8be5\u8282\u63cf\u8ff0\u4e86\u8fd9\u4e9b\u7c7b\u578b\u5982\u4f55\u5de5\u4f5c\u4ee5\u53ca\u5982\u4f55\u5728\u67e5\u8be2\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u7c7b\u578b\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u5927\u5c0f<\/th>\n<th>\u7528\u9014<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CHAR<\/td>\n<td>0-255\u5b57\u8282<\/td>\n<td>\u5b9a\u957f\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>VARCHAR<\/td>\n<td>0-65535 \u5b57\u8282<\/td>\n<td>\u53d8\u957f\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>TINYBLOB<\/td>\n<td>0-255\u5b57\u8282<\/td>\n<td>\u4e0d\u8d85\u8fc7 255 \u4e2a\u5b57\u7b26\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>TINYTEXT<\/td>\n<td>0-255\u5b57\u8282<\/td>\n<td>\u77ed\u6587\u672c\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>BLOB<\/td>\n<td>0-65 535\u5b57\u8282<\/td>\n<td>\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u957f\u6587\u672c\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>TEXT<\/td>\n<td>0-65 535\u5b57\u8282<\/td>\n<td>\u957f\u6587\u672c\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMBLOB<\/td>\n<td>0-16 777 215\u5b57\u8282<\/td>\n<td>\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u4e2d\u7b49\u957f\u5ea6\u6587\u672c\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMTEXT<\/td>\n<td>0-16 777 215\u5b57\u8282<\/td>\n<td>\u4e2d\u7b49\u957f\u5ea6\u6587\u672c\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>LONGBLOB<\/td>\n<td>0-4 294 967 295\u5b57\u8282<\/td>\n<td>\u4e8c\u8fdb\u5236\u5f62\u5f0f\u7684\u6781\u5927\u6587\u672c\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>LONGTEXT<\/td>\n<td>0-4 294 967 295\u5b57\u8282<\/td>\n<td>\u6781\u5927\u6587\u672c\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>CHAR \u548c VARCHAR \u7c7b\u578b\u7c7b\u4f3c\uff0c\u4f46\u5b83\u4eec\u4fdd\u5b58\u548c\u68c0\u7d22\u7684\u65b9\u5f0f\u4e0d\u540c\u3002\u5b83\u4eec\u7684\u6700\u5927\u957f\u5ea6\u548c\u662f\u5426\u5c3e\u90e8\u7a7a\u683c\u88ab\u4fdd\u7559\u7b49\u65b9\u9762\u4e5f\u4e0d\u540c\u3002\u5728\u5b58\u50a8\u6216\u68c0\u7d22\u8fc7\u7a0b\u4e2d\u4e0d\u8fdb\u884c\u5927\u5c0f\u5199\u8f6c\u6362\u3002<\/p>\n<p>BINARY \u548c VARBINARY \u7c7b\u4f3c\u4e8e CHAR \u548c VARCHAR\uff0c\u4e0d\u540c\u7684\u662f\u5b83\u4eec\u5305\u542b\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u800c\u4e0d\u8981\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u4eec\u5305\u542b\u5b57\u8282\u5b57\u7b26\u4e32\u800c\u4e0d\u662f\u5b57\u7b26\u5b57\u7b26\u4e32\u3002\u8fd9\u8bf4\u660e\u5b83\u4eec\u6ca1\u6709\u5b57\u7b26\u96c6\uff0c\u5e76\u4e14\u6392\u5e8f\u548c\u6bd4\u8f83\u57fa\u4e8e\u5217\u503c\u5b57\u8282\u7684\u6570\u503c\u503c\u3002<\/p>\n<p>BLOB \u662f\u4e00\u4e2a\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u53ef\u53d8\u6570\u91cf\u7684\u6570\u636e\u3002\u6709 4 \u79cd BLOB \u7c7b\u578b\uff1aTINYBLOB\u3001BLOB\u3001MEDIUMBLOB \u548c LONGBLOB\u3002\u5b83\u4eec\u533a\u522b\u5728\u4e8e\u53ef\u5bb9\u7eb3\u5b58\u50a8\u8303\u56f4\u4e0d\u540c\u3002<\/p>\n<p>\u6709 4 \u79cd TEXT \u7c7b\u578b\uff1aTINYTEXT\u3001TEXT\u3001MEDIUMTEXT \u548c LONGTEXT\u3002\u5bf9\u5e94\u7684\u8fd9 4 \u79cd BLOB \u7c7b\u578b\uff0c\u53ef\u5b58\u50a8\u7684\u6700\u5927\u957f\u5ea6\u4e0d\u540c\uff0c\u53ef\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u3002<\/p>\n<p>MySQL 5.0 \u4ee5\u4e0a\u7684\u7248\u672c\uff1a<\/p>\n<p>1\u3001\u4e00\u4e2a\u6c49\u5b57\u5360\u591a\u5c11\u957f\u5ea6\u4e0e\u7f16\u7801\u6709\u5173\uff1a<\/p>\n<p><strong>UTF\uff0d8<\/strong>\uff1a\u4e00\u4e2a\u6c49\u5b57\uff1d3\u4e2a\u5b57\u8282<\/p>\n<p><strong>GBK<\/strong>\uff1a\u4e00\u4e2a\u6c49\u5b57\uff1d2\u4e2a\u5b57\u8282<\/p>\n<p>2\u3001varchar(n) \u8868\u793a n \u4e2a\u5b57\u7b26\uff0c\u65e0\u8bba\u6c49\u5b57\u548c\u82f1\u6587\uff0cMysql \u90fd\u80fd\u5b58\u5165 n \u4e2a\u5b57\u7b26\uff0c\u4ec5\u662f\u5b9e\u9645\u5b57\u8282\u957f\u5ea6\u6709\u6240\u533a\u522b<\/p>\n<p>3\u3001MySQL \u68c0\u67e5\u957f\u5ea6\uff0c\u53ef\u7528 SQL \u8bed\u8a00\u6765\u67e5\u770b\uff1a<\/p>\n<pre><code>select LENGTH(fieldname) from tablename\n<\/code><\/pre>\n<p>1\u3001\u6574\u578b<\/p>\n<table>\n<thead>\n<tr>\n<th><strong>MySQL\u6570\u636e\u7c7b\u578b<\/strong><\/th>\n<th><strong>\u542b\u4e49\uff08\u6709\u7b26\u53f7\uff09<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>tinyint(m)<\/td>\n<td>1\u4e2a\u5b57\u8282\u00a0 \u8303\u56f4(-128~127)<\/td>\n<\/tr>\n<tr>\n<td>smallint(m)<\/td>\n<td>2\u4e2a\u5b57\u8282\u00a0 \u8303\u56f4(-32768~32767)<\/td>\n<\/tr>\n<tr>\n<td>mediumint(m)<\/td>\n<td>3\u4e2a\u5b57\u8282\u00a0 \u8303\u56f4(-8388608~8388607)<\/td>\n<\/tr>\n<tr>\n<td>int(m)<\/td>\n<td>4\u4e2a\u5b57\u8282\u00a0 \u8303\u56f4(-2147483648~2147483647)<\/td>\n<\/tr>\n<tr>\n<td>bigint(m)<\/td>\n<td>8\u4e2a\u5b57\u8282\u00a0 \u8303\u56f4(+-9.22*10\u768418\u6b21\u65b9)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u53d6\u503c\u8303\u56f4\u5982\u679c\u52a0\u4e86 unsigned\uff0c\u5219\u6700\u5927\u503c\u7ffb\u500d\uff0c\u5982 tinyint unsigned \u7684\u53d6\u503c\u8303\u56f4\u4e3a(0~256)\u3002<\/p>\n<p>int(m) \u91cc\u7684 m \u662f\u8868\u793a SELECT \u67e5\u8be2\u7ed3\u679c\u96c6\u4e2d\u7684\u663e\u793a\u5bbd\u5ea6\uff0c\u5e76\u4e0d\u5f71\u54cd\u5b9e\u9645\u7684\u53d6\u503c\u8303\u56f4\uff0c\u6ca1\u6709\u5f71\u54cd\u5230\u663e\u793a\u7684\u5bbd\u5ea6\uff0c\u4e0d\u77e5\u9053\u8fd9\u4e2a m \u6709\u4ec0\u4e48\u7528\u3002<\/p>\n<p><strong>2\u3001\u6d6e\u70b9\u578b(float \u548c double)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th><strong>MySQL\u6570\u636e\u7c7b\u578b<\/strong><\/th>\n<th><strong>\u542b\u4e49<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>float(m,d)<\/td>\n<td>\u5355\u7cbe\u5ea6\u6d6e\u70b9\u578b\u00a0\u00a0\u00a0 8\u4f4d\u7cbe\u5ea6(4\u5b57\u8282)\u00a0\u00a0\u00a0 \u00a0m\u603b\u4e2a\u6570\uff0cd\u5c0f\u6570\u4f4d<\/td>\n<\/tr>\n<tr>\n<td>double(m,d)<\/td>\n<td>\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u578b\u00a0\u00a0\u00a0 16\u4f4d\u7cbe\u5ea6(8\u5b57\u8282)\u00a0\u00a0 \u00a0m\u603b\u4e2a\u6570\uff0cd\u5c0f\u6570\u4f4d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bbe\u4e00\u4e2a\u5b57\u6bb5\u5b9a\u4e49\u4e3a float(5,3)\uff0c\u5982\u679c\u63d2\u5165\u4e00\u4e2a\u6570 123.45678,\u5b9e\u9645\u6570\u636e\u5e93\u91cc\u5b58\u7684\u662f 123.457\uff0c\u4f46\u603b\u4e2a\u6570\u8fd8\u4ee5\u5b9e\u9645\u4e3a\u51c6\uff0c\u5373 6 \u4f4d\u3002<\/p>\n<p><strong>3\u3001\u5b9a\u70b9\u6570<\/strong><\/p>\n<p>\u6d6e\u70b9\u578b\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u653e\u7684\u662f\u8fd1\u4f3c\u503c\uff0c\u800c\u5b9a\u70b9\u7c7b\u578b\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u653e\u7684\u662f\u7cbe\u786e\u503c\u3002<\/p>\n<p>decimal(m,d) \u53c2\u6570 m&lt;65 \u662f\u603b\u4e2a\u6570\uff0cd&lt;30 \u4e14 d&lt;m \u662f\u5c0f\u6570\u4f4d\u3002<\/p>\n<p>m: \u8868\u793a\u603b\u4e2a\u6570, \u8981\u6c42\u6700\u5927\u503c64.<\/p>\n<p>d: \u8868\u793a\u5c0f\u6570\u4f4d\u6570,<\/p>\n<p>\u200b d&lt;30; \u5c0f\u6570\u4f4d\u6570\u6700\u591a\u4fdd\u7559: 29\u4e2a.<\/p>\n<p><strong>4\u3001\u5b57\u7b26\u4e32(char,varchar,_text)<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th><strong>MySQL\u6570\u636e\u7c7b\u578b<\/strong><\/th>\n<th><strong>\u542b\u4e49<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>char(n)<\/td>\n<td>\u56fa\u5b9a\u957f\u5ea6\uff0c\u6700\u591a255\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>varchar(n)<\/td>\n<td>\u56fa\u5b9a\u957f\u5ea6\uff0c\u6700\u591a65535\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>tinytext<\/td>\n<td>\u53ef\u53d8\u957f\u5ea6\uff0c\u6700\u591a255\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>text<\/td>\n<td>\u53ef\u53d8\u957f\u5ea6\uff0c\u6700\u591a65535\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>mediumtext<\/td>\n<td>\u53ef\u53d8\u957f\u5ea6\uff0c\u6700\u591a2\u768424\u6b21\u65b9-1\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<tr>\n<td>longtext<\/td>\n<td>\u53ef\u53d8\u957f\u5ea6\uff0c\u6700\u591a2\u768432\u6b21\u65b9-1\u4e2a\u5b57\u7b26<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>char \u548c varchar\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>char(n) \u82e5\u5b58\u5165\u5b57\u7b26\u6570\u5c0f\u4e8en\uff0c\u5219\u4ee5\u7a7a\u683c\u8865\u4e8e\u5176\u540e\uff0c\u67e5\u8be2\u4e4b\u65f6\u518d\u5c06\u7a7a\u683c\u53bb\u6389\u3002\u6240\u4ee5 char \u7c7b\u578b\u5b58\u50a8\u7684\u5b57\u7b26\u4e32\u672b\u5c3e\u4e0d\u80fd\u6709\u7a7a\u683c\uff0cvarchar \u4e0d\u9650\u4e8e\u6b64\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>char(n) \u56fa\u5b9a\u957f\u5ea6\uff0cchar(4) \u4e0d\u7ba1\u662f\u5b58\u5165\u51e0\u4e2a\u5b57\u7b26\uff0c\u90fd\u5c06\u5360\u7528 4 \u4e2a\u5b57\u8282\uff0cvarchar \u662f\u5b58\u5165\u7684\u5b9e\u9645\u5b57\u7b26\u6570 +1 \u4e2a\u5b57\u8282\uff08n&lt;=255\uff09\u62162\u4e2a\u5b57\u8282(n&gt;255)\uff0c\u6240\u4ee5 varchar(4),\u5b58\u5165 3 \u4e2a\u5b57\u7b26\u5c06\u5360\u7528 4 \u4e2a\u5b57\u8282\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>char \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u68c0\u7d22\u901f\u5ea6\u8981\u6bd4 varchar \u7c7b\u578b\u7684\u5feb\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>varchar \u548c text\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>varchar( \u53ef\u6307\u5b9a n\uff0ctext \u4e0d\u80fd\u6307\u5b9a\uff0c\u5185\u90e8\u5b58\u50a8 varchar \u662f\u5b58\u5165\u7684\u5b9e\u9645\u5b57\u7b26\u6570 +1 \u4e2a\u5b57\u8282\uff08n&lt;=255\uff09\u6216 2 \u4e2a\u5b57\u8282(n&gt;255)\uff0ctext \u662f\u5b9e\u9645\u5b57\u7b26\u6570 +2 \u4e2a\u5b57\u8282\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>text \u7c7b\u578b\u4e0d\u80fd\u6709\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>varchar \u53ef\u76f4\u63a5\u521b\u5efa\u7d22\u5f15\uff0ctext \u521b\u5efa\u7d22\u5f15\u8981\u6307\u5b9a\u524d\u591a\u5c11\u4e2a\u5b57\u7b26\u3002varchar \u67e5\u8be2\u901f\u5ea6\u5feb\u4e8e text, \u5728\u90fd\u521b\u5efa\u7d22\u5f15\u7684\u60c5\u51b5\u4e0b\uff0ctext \u7684\u7d22\u5f15\u4f3c\u4e4e\u4e0d\u8d77\u4f5c\u7528\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\u603b\u7ed3: \u6548\u7387: char &gt; varchar &gt; text.<\/li>\n<\/ul>\n<p><strong>5.\u4e8c\u8fdb\u5236\u6570\u636e(Blob)<\/strong><\/p>\n<ul>\n<li>\n<ol>\n<li>BLOB\u548ctext\u5b58\u50a8\u65b9\u5f0f\u4e0d\u540c\uff0cTEXT\u4ee5\u6587\u672c\u65b9\u5f0f\u5b58\u50a8\uff0c\u82f1\u6587\u5b58\u50a8\u533a\u5206\u5927\u5c0f\u5199\uff0c\u800cBlob\u662f\u4ee5\u4e8c\u8fdb\u5236\u65b9\u5f0f\u5b58\u50a8\uff0c\u4e0d\u5206\u5927\u5c0f\u5199\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>BLOB\u5b58\u50a8\u7684\u6570\u636e\u53ea\u80fd\u6574\u4f53\u8bfb\u51fa\u3002<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>TEXT\u53ef\u4ee5\u6307\u5b9a\u5b57\u7b26\u96c6\uff0cBLOB\u4e0d\u7528\u6307\u5b9a\u5b57\u7b26\u96c6\u3002<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p><strong>6.\u65e5\u671f\u65f6\u95f4\u7c7b\u578b<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th><strong>MySQL\u6570\u636e\u7c7b\u578b<\/strong><\/th>\n<th><strong>\u542b\u4e49<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>date<\/td>\n<td>\u65e5\u671f &#8216;2008-12-2&#8217;<\/td>\n<\/tr>\n<tr>\n<td>time<\/td>\n<td>\u65f6\u95f4 &#8217;12:25:36&#8242;<\/td>\n<\/tr>\n<tr>\n<td>datetime<\/td>\n<td>\u65e5\u671f\u65f6\u95f4 &#8216;2008-12-2 22:06:44&#8217;<\/td>\n<\/tr>\n<tr>\n<td>timestamp<\/td>\n<td>\u81ea\u52a8\u5b58\u50a8\u8bb0\u5f55\u4fee\u6539\u65f6\u95f4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u82e5\u5b9a\u4e49\u4e00\u4e2a\u5b57\u6bb5\u4e3atimestamp\uff0c\u8fd9\u4e2a\u5b57\u6bb5\u91cc\u7684\u65f6\u95f4\u6570\u636e\u4f1a\u968f\u5176\u4ed6\u5b57\u6bb5\u4fee\u6539\u7684\u65f6\u5019\u81ea\u52a8\u5237\u65b0\uff0c\u6240\u4ee5\u8fd9\u4e2a\u6570\u636e\u7c7b\u578b\u7684\u5b57\u6bb5\u53ef\u4ee5\u5b58\u653e\u8fd9\u6761\u8bb0\u5f55\u6700\u540e\u88ab\u4fee\u6539\u7684\u65f6\u95f4\u3002<\/p>\n<p><strong>\u6570\u636e\u7c7b\u578b\u7684\u5c5e\u6027<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th><strong>MySQL\u5173\u952e\u5b57<\/strong><\/th>\n<th><strong>\u542b\u4e49<\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>NULL<\/td>\n<td>\u6570\u636e\u5217\u53ef\u5305\u542bNULL\u503c<\/td>\n<\/tr>\n<tr>\n<td>NOT NULL<\/td>\n<td>\u6570\u636e\u5217\u4e0d\u5141\u8bb8\u5305\u542bNULL\u503c<\/td>\n<\/tr>\n<tr>\n<td>DEFAULT<\/td>\n<td>\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n<tr>\n<td>PRIMARY KEY<\/td>\n<td>\u4e3b\u952e<\/td>\n<\/tr>\n<tr>\n<td>AUTO_INCREMENT<\/td>\n<td>\u81ea\u52a8\u9012\u589e\uff0c\u9002\u7528\u4e8e\u6574\u6570\u7c7b\u578b<\/td>\n<\/tr>\n<tr>\n<td>UNSIGNED<\/td>\n<td>\u65e0\u7b26\u53f7<\/td>\n<\/tr>\n<tr>\n<td>CHARACTER SET name<\/td>\n<td>\u6307\u5b9a\u4e00\u4e2a\u5b57\u7b26\u96c6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>charset<\/p>\n<h2><a id=\"10-mysql-group-by%E8%AF%AD%E5%8F%A5\" class=\"anchor\" aria-hidden=\"true\" href=\"#10-mysql-group-by%E8%AF%AD%E5%8F%A5\"><span class=\"octicon octicon-link\"><\/span><\/a>10. MySQL GROUP BY \u8bed\u53e5<\/h2>\n<p>GROUP BY \u8bed\u53e5\u6839\u636e\u4e00\u4e2a\u6216\u591a\u4e2a\u5217\u5bf9\u7ed3\u679c\u96c6\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n<p>\u5728\u5206\u7ec4\u7684\u5217\u4e0a\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 COUNT, SUM, AVG,\u7b49\u51fd\u6570\u3002<\/p>\n<p>\u8bed\u6cd5\u7ed3\u6784\uff1a<\/p>\n<pre><code>SELECT column_name, function(column_name)\nFROM table_name\nWHERE column_name operator value\nGROUP BY column_name;\n<\/code><\/pre>\n<p>\u51c6\u5907\u6570\u636e\uff1a<\/p>\n<pre><code>CREATE TABLE `employee_tbl` (\n  `id` int(11) NOT NULL,\n  `name` char(10) NOT NULL DEFAULT '',\n  `date` datetime NOT NULL,\n  `singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '\u767b\u5f55\u6b21\u6570',\n  PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8;\n\n-- ----------------------------\n\nINSERT INTO `employee_tbl` VALUES ('1', '\u5c0f\u660e', '2016-04-22 15:25:33', '1'), ('2', '\u5c0f\u738b', '2016-04-20 15:25:47', '3'), ('3', '\u5c0f\u4e3d', '2016-04-19 15:26:02', '2'), ('4', '\u5c0f\u738b', '2016-04-07 15:26:14', '4'), ('5', '\u5c0f\u660e', '2016-04-11 15:26:40', '4'), ('6', '\u5c0f\u660e', '2016-04-04 15:26:54', '2');\n<\/code><\/pre>\n<pre><code>mysql&gt; SELECT name, COUNT(*) FROM   employee_tbl GROUP BY name;\n+--------+----------+\n| name   | COUNT(*) |\n+--------+----------+\n| \u5c0f\u4e3d |        1 |\n| \u5c0f\u660e |        3 |\n| \u5c0f\u738b |        2 |\n+--------+----------+\n\nmysql&gt; select * from employee_tbl;\n+----+------+---------------------+--------+\n| id | name | date                | singin |\n+----+------+---------------------+--------+\n|  1 | ??   | 2016-04-22 15:25:33 |      1 |\n|  2 | ??   | 2016-04-20 15:25:47 |      3 |\n|  3 | ??   | 2016-04-19 15:26:02 |      2 |\n|  4 | ??   | 2016-04-07 15:26:14 |      4 |\n|  5 | ??   | 2016-04-11 15:26:40 |      4 |\n|  6 | ??   | 2016-04-04 15:26:54 |      2 |\n+----+------+---------------------+--------+\n\nmysql&gt; select * from employee_tbl group by singin;\n+----+------+---------------------+--------+\n| id | name | date                | singin |\n+----+------+---------------------+--------+\n|  1 | ??   | 2016-04-22 15:25:33 |      1 |\n|  3 | ??   | 2016-04-19 15:26:02 |      2 |\n|  2 | ??   | 2016-04-20 15:25:47 |      3 |\n|  4 | ??   | 2016-04-07 15:26:14 |      4 |\n+----+------+---------------------+--------+\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1\u3001group by \u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u53bb\u91cd\u67e5\u8be2\uff0c\u5047\u8bbe\u60f3\u770b\u4e0b\u6709\u54ea\u4e9b\u5458\u5de5\uff0c\u9664\u4e86\u7528 distinct,\u8fd8\u53ef\u4ee5\u7528\uff1a<\/p>\n<pre><code>SELECT name FROM employee_tbl GROUP BY name;\n<\/code><\/pre>\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u5c31\u662f\u6240\u6709\u5458\u5de5\u7684\u540d\u5b57\u3002<\/p>\n<p>2\u3001\u5206\u7ec4\u540e\u7684\u6761\u4ef6\u4f7f\u7528 HAVING \u6765\u9650\u5b9a\uff0cWHERE \u662f\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u6761\u4ef6\u9650\u5236\u3002\u51e0\u4e2a\u5173\u952e\u5b57\u7684\u4f7f\u7528\u987a\u5e8f\u4e3a where \u3001group by \u3001having\u3001order by \uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT name ,sum(singin)  FROM employee_tbl WHERE date&gt;'2015-04-07 15:26:14' GROUP BY name  HAVING sum(*)&gt;5 ORDER BY sum(singin) DESC;\n<\/code><\/pre>\n<h2><a id=\"11%E3%80%81mysql-like%E5%AD%90%E5%8F%A5\" class=\"anchor\" aria-hidden=\"true\" href=\"#11%E3%80%81mysql-like%E5%AD%90%E5%8F%A5\"><span class=\"octicon octicon-link\"><\/span><\/a>11\u3001MySQL LIKE \u5b50\u53e5<\/h2>\n<p>\u6211\u4eec\u77e5\u9053\u5728 MySQL \u4e2d\u4f7f\u7528 SQL SELECT \u547d\u4ee4\u6765\u8bfb\u53d6\u6570\u636e\uff0c \u540c\u65f6\u6211\u4eec\u53ef\u4ee5\u5728 SELECT \u8bed\u53e5\u4e2d\u4f7f\u7528 WHERE \u5b50\u53e5\u6765\u83b7\u53d6\u6307\u5b9a\u7684\u8bb0\u5f55\u3002<\/p>\n<p>WHERE \u5b50\u53e5\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7b49\u53f7\u00a0=\u00a0\u6765\u8bbe\u5b9a\u83b7\u53d6\u6570\u636e\u7684\u6761\u4ef6\uff0c\u5982 &quot;company = &#8216;itcast&quot;\u3002<\/p>\n<p>\u4f46\u662f\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u83b7\u53d6 company \u5b57\u6bb5\u542b\u6709 &quot;it&quot; \u5b57\u7b26\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u8fd9\u65f6\u6211\u4eec\u5c31\u9700\u8981\u5728 WHERE \u5b50\u53e5\u4e2d\u4f7f\u7528 SQL LIKE \u5b50\u53e5\u3002<\/p>\n<p>SQL LIKE \u5b50\u53e5\u4e2d\u4f7f\u7528\u767e\u5206\u53f7\u00a0%\u5b57\u7b26\u6765\u8868\u793a\u4efb\u610f\u5b57\u7b26\u3002<\/p>\n<p>\u5982\u679c\u6ca1\u6709\u4f7f\u7528\u767e\u5206\u53f7\u00a0%, LIKE \u5b50\u53e5\u4e0e\u7b49\u53f7\u00a0=\u00a0\u7684\u6548\u679c\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<p>\u4ee5\u4e0b\u662f SQL SELECT \u8bed\u53e5\u4f7f\u7528 LIKE \u5b50\u53e5\u4ece\u6570\u636e\u8868\u4e2d\u8bfb\u53d6\u6570\u636e\u7684\u901a\u7528\u8bed\u6cd5\uff1a<\/p>\n<pre><code>SELECT field1, field2,...fieldN \nFROM table_name\nWHERE field1 LIKE condition1 [AND|OR] filed2 = 'somevalue'\n<\/code><\/pre>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u5728 WHERE \u5b50\u53e5\u4e2d\u6307\u5b9a\u4efb\u4f55\u6761\u4ef6\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u5728 WHERE \u5b50\u53e5\u4e2d\u4f7f\u7528LIKE\u5b50\u53e5\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528LIKE\u5b50\u53e5\u4ee3\u66ff\u7b49\u53f7\u00a0=\u3002<\/li>\n<li>LIKE \u901a\u5e38\u4e0e\u00a0&#8216; %\u00a0\u548c _ &#8216; \u4e00\u540c\u4f7f\u7528\uff0c\u7c7b\u4f3c\u4e8e\u4e00\u4e2a\u5b57\u7b26\u7684\u6a21\u7cca\u641c\u7d22\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528 AND \u6216\u8005 OR \u6307\u5b9a\u4e00\u4e2a\u6216\u591a\u4e2a\u6761\u4ef6\u3002<\/li>\n<li>\u4f60\u53ef\u4ee5\u5728 DELETE \u6216 UPDATE \u547d\u4ee4\u4e2d\u4f7f\u7528 WHERE&#8230;LIKE \u5b50\u53e5\u6765\u6307\u5b9a\u6761\u4ef6\u3002<\/li>\n<\/ul>\n<pre><code>mysql&gt; select * from pet where species like '%d%';\n+----------+--------+---------+------+------------+------------+\n| name     | owner  | species | sex  | birth      | death      |\n+----------+--------+---------+------+------------+------------+\n| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL       |\n| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL       |\n| Bowser   | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |\n| Chirpy   | Gwen   | bird    | f    | 1998-09-11 | NULL       |\n| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL       |\n+----------+--------+---------+------+------------+------------+\n<\/code><\/pre>\n<h2><a id=\"12-mysql-null%E5%80%BC%E5%A4%84%E7%90%86\" class=\"anchor\" aria-hidden=\"true\" href=\"#12-mysql-null%E5%80%BC%E5%A4%84%E7%90%86\"><span class=\"octicon octicon-link\"><\/span><\/a>12. MySQL NULL \u503c\u5904\u7406<\/h2>\n<p>\u200b \u6211\u4eec\u5df2\u7ecf\u77e5\u9053 MySQL \u4f7f\u7528 SQL SELECT \u547d\u4ee4\u53ca WHERE \u5b50\u53e5\u6765\u8bfb\u53d6\u6570\u636e\u8868\u4e2d\u7684\u6570\u636e,\u4f46\u662f\u5f53\u63d0\u4f9b\u7684\u67e5\u8be2\u6761\u4ef6\u5b57\u6bb5\u4e3a NULL \u65f6\uff0c\u8be5\u547d\u4ee4\u53ef\u80fd\u5c31\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p>\u4e3a\u4e86\u5904\u7406\u8fd9\u79cd\u60c5\u51b5\uff0cMySQL\u63d0\u4f9b\u4e86\u4e09\u5927\u8fd0\u7b97\u7b26:<\/p>\n<ul>\n<li><strong>IS NULL:<\/strong>\u00a0\u5f53\u5217\u7684\u503c\u662f NULL,\u6b64\u8fd0\u7b97\u7b26\u8fd4\u56de true\u3002<\/li>\n<li><strong>IS NOT NULL:<\/strong>\u00a0\u5f53\u5217\u7684\u503c\u4e0d\u4e3a NULL, \u8fd0\u7b97\u7b26\u8fd4\u56de true\u3002<\/li>\n<li><strong>&lt;=&gt;:<\/strong>\u00a0\u6bd4\u8f83\u64cd\u4f5c\u7b26\uff08\u4e0d\u540c\u4e8e=\u8fd0\u7b97\u7b26\uff09\uff0c\u5f53\u6bd4\u8f83\u7684\u7684\u4e24\u4e2a\u503c\u4e3a NULL \u65f6\u8fd4\u56de true\u3002<\/li>\n<\/ul>\n<p>\u5173\u4e8e NULL \u7684\u6761\u4ef6\u6bd4\u8f83\u8fd0\u7b97\u662f\u6bd4\u8f83\u7279\u6b8a\u7684\u3002\u4f60\u4e0d\u80fd\u4f7f\u7528 = NULL \u6216 != NULL \u5728\u5217\u4e2d\u67e5\u627e NULL \u503c \u3002<\/p>\n<p>\u5728 MySQL \u4e2d\uff0cNULL \u503c\u4e0e\u4efb\u4f55\u5176\u5b83\u503c\u7684\u6bd4\u8f83\uff08\u5373\u4f7f\u662f NULL\uff09\u6c38\u8fdc\u8fd4\u56de false\uff0c\u5373 NULL = NULL \u8fd4\u56defalse \u3002<\/p>\n<p>MySQL \u4e2d\u5904\u7406 NULL \u4f7f\u7528 IS NULL \u548c IS NOT NULL \u8fd0\u7b97\u7b26\u3002<\/p>\n<h2><a id=\"13-mysql%E5%85%83%E6%95%B0%E6%8D%AE\" class=\"anchor\" aria-hidden=\"true\" href=\"#13-mysql%E5%85%83%E6%95%B0%E6%8D%AE\"><span class=\"octicon octicon-link\"><\/span><\/a>13. MySQL \u5143\u6570\u636e<\/h2>\n<p>\u4f60\u53ef\u80fd\u60f3\u77e5\u9053MySQL\u4ee5\u4e0b\u4e09\u79cd\u4fe1\u606f\uff1a<\/p>\n<ul>\n<li><strong>\u67e5\u8be2\u7ed3\u679c\u4fe1\u606f\uff1a<\/strong>\u00a0SELECT, UPDATE \u6216 DELETE\u8bed\u53e5\u5f71\u54cd\u7684\u8bb0\u5f55\u6570\u3002<\/li>\n<li><strong>\u6570\u636e\u5e93\u548c\u6570\u636e\u8868\u7684\u4fe1\u606f\uff1a<\/strong>\u00a0\u5305\u542b\u4e86\u6570\u636e\u5e93\u53ca\u6570\u636e\u8868\u7684\u7ed3\u6784\u4fe1\u606f\u3002<\/li>\n<li><strong>MySQL\u670d\u52a1\u5668\u4fe1\u606f\uff1a<\/strong>\u00a0\u5305\u542b\u4e86\u6570\u636e\u5e93\u670d\u52a1\u5668\u7684\u5f53\u524d\u72b6\u6001\uff0c\u7248\u672c\u53f7\u7b49\u3002<\/li>\n<\/ul>\n<p>\u5728MySQL\u7684\u547d\u4ee4\u63d0\u793a\u7b26\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u5bb9\u6613\u7684\u83b7\u53d6\u4ee5\u4e0a\u670d\u52a1\u5668\u4fe1\u606f\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u547d\u4ee4<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>SELECT VERSION( )<\/td>\n<td>\u670d\u52a1\u5668\u7248\u672c\u4fe1\u606f<\/td>\n<\/tr>\n<tr>\n<td>SELECT DATABASE( )<\/td>\n<td>\u5f53\u524d\u6570\u636e\u5e93\u540d (\u6216\u8005\u8fd4\u56de\u7a7a)<\/td>\n<\/tr>\n<tr>\n<td>SELECT USER( )<\/td>\n<td>\u5f53\u524d\u7528\u6237\u540d<\/td>\n<\/tr>\n<tr>\n<td>SHOW STATUS<\/td>\n<td>\u670d\u52a1\u5668\u72b6\u6001<\/td>\n<\/tr>\n<tr>\n<td>SHOW VARIABLES<\/td>\n<td>\u670d\u52a1\u5668\u914d\u7f6e\u53d8\u91cf<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><a id=\"14-mysql%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#14-mysql%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>14 MySQL \u51fd\u6570<\/h2>\n<p>\u200b MySQL \u6709\u5f88\u591a\u5185\u7f6e\u7684\u51fd\u6570\uff0c\u4ee5\u4e0b\u5217\u51fa\u4e86\u8fd9\u4e9b\u51fd\u6570\u7684\u8bf4\u660e\u3002<\/p>\n<h3><a id=\"14-1-mysql%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#14-1-mysql%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>14.1 MySQL \u5b57\u7b26\u4e32\u51fd\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u5b9e\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ASCII(s)<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684 ASCII \u7801\u3002<\/td>\n<td>\u8fd4\u56de CustomerName \u5b57\u6bb5\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u7684 ASCII \u7801\uff1a<code>SELECT ASCII(CustomerName) AS NumCodeOfFirstCharFROM Customers;<\/code><\/td>\n<\/tr>\n<tr>\n<td>CHAR_LENGTH(s)<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u5b57\u7b26\u6570<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 itcast \u7684\u5b57\u7b26\u6570<code>SELECT CHAR_LENGTH(&quot;itcast&quot;) AS LengthOfString;<\/code><\/td>\n<\/tr>\n<tr>\n<td>CHARACTER_LENGTH(s)<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u5b57\u7b26\u6570<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 itcast \u7684\u5b57\u7b26\u6570<code>SELECT CHARACTER_LENGTH(&quot;itcast&quot;) AS LengthOfString;<\/code><\/td>\n<\/tr>\n<tr>\n<td>CONCAT(s1,s2&#8230;sn)<\/td>\n<td>\u5b57\u7b26\u4e32 s1,s2 \u7b49\u591a\u4e2a\u5b57\u7b26\u4e32\u5408\u5e76\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32<\/td>\n<td>\u5408\u5e76\u591a\u4e2a\u5b57\u7b26\u4e32<code>SELECT CONCAT(&quot;SQL &quot;, &quot;itcast &quot;, &quot;Gooogle &quot;, &quot;Facebook&quot;) AS ConcatenatedString;<\/code><\/td>\n<\/tr>\n<tr>\n<td>CONCAT_WS(x, s1,s2&#8230;sn)<\/td>\n<td>\u540c CONCAT(s1,s2,&#8230;) \u51fd\u6570\uff0c\u4f46\u662f\u6bcf\u4e2a\u5b57\u7b26\u4e32\u76f4\u63a5\u8981\u52a0\u4e0a x\uff0cx \u53ef\u4ee5\u662f\u5206\u9694\u7b26<\/td>\n<td>\u5408\u5e76\u591a\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u6dfb\u52a0\u5206\u9694\u7b26\uff1a<code>SELECT CONCAT_WS(&quot;-&quot;, &quot;SQL&quot;, &quot;Tutorial&quot;, &quot;is&quot;, &quot;fun!&quot;)AS ConcatenatedString;<\/code><\/td>\n<\/tr>\n<tr>\n<td>FIELD(s,s1,s2&#8230;)<\/td>\n<td>\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5b57\u7b26\u4e32 s \u5728\u5b57\u7b26\u4e32\u5217\u8868(s1,s2&#8230;)\u4e2d\u7684\u4f4d\u7f6e<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 c \u5728\u5217\u8868\u503c\u4e2d\u7684\u4f4d\u7f6e\uff1a<code>SELECT FIELD(&quot;c&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;);<\/code><\/td>\n<\/tr>\n<tr>\n<td>FIND_IN_SET(s1,s2)<\/td>\n<td>\u8fd4\u56de\u5728\u5b57\u7b26\u4e32s2\u4e2d\u4e0es1\u5339\u914d\u7684\u5b57\u7b26\u4e32\u7684\u4f4d\u7f6e<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 c \u5728\u6307\u5b9a\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff1a<code>SELECT FIND_IN_SET(&quot;c&quot;, &quot;a,b,c,d,e&quot;);<\/code><\/td>\n<\/tr>\n<tr>\n<td>FORMAT(x,n)<\/td>\n<td>\u51fd\u6570\u53ef\u4ee5\u5c06\u6570\u5b57 x \u8fdb\u884c\u683c\u5f0f\u5316 &quot;#,###.##&quot;, \u5c06 x \u4fdd\u7559\u5230\u5c0f\u6570\u70b9\u540e n \u4f4d\uff0c\u6700\u540e\u4e00\u4f4d\u56db\u820d\u4e94\u5165\u3002<\/td>\n<td>\u683c\u5f0f\u5316\u6570\u5b57 &quot;#,###.##&quot; \u5f62\u5f0f\uff1a<code>SELECT FORMAT(250500.5634, 2); -- \u8f93\u51fa 250,500.56<\/code><\/td>\n<\/tr>\n<tr>\n<td>INSERT(s1,x,len,s2)<\/td>\n<td>\u5b57\u7b26\u4e32 s2 \u66ff\u6362 s1 \u7684 x \u4f4d\u7f6e\u5f00\u59cb\u957f\u5ea6\u4e3a len \u7684\u5b57\u7b26\u4e32<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32\u7b2c\u4e00\u4e2a\u4f4d\u7f6e\u5f00\u59cb\u7684 6 \u4e2a\u5b57\u7b26\u66ff\u6362\u4e3a itcast\uff1a<code>SELECT INSERT(&quot;google.com&quot;, 1, 6, &quot;runnob&quot;); -- \u8f93\u51fa\uff1aitcast.com<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOCATE(s1,s)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 s \u4e2d\u83b7\u53d6 s1 \u7684\u5f00\u59cb\u4f4d\u7f6e<\/td>\n<td>\u83b7\u53d6 b \u5728\u5b57\u7b26\u4e32 abc \u4e2d\u7684\u4f4d\u7f6e\uff1a<code>SELECT INSTR('abc','b') -- 2<\/code><\/td>\n<\/tr>\n<tr>\n<td>LCASE(s)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 s \u7684\u6240\u6709\u5b57\u6bcd\u53d8\u6210\u5c0f\u5199\u5b57\u6bcd<\/td>\n<td>\u5b57\u7b26\u4e32 itcast \u8f6c\u6362\u4e3a\u5c0f\u5199\uff1a<code>SELECT LOWER('itcast') -- itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>LEFT(s,n)<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u524d n \u4e2a\u5b57\u7b26<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 itcast \u4e2d\u7684\u524d\u4e24\u4e2a\u5b57\u7b26\uff1a<code>SELECT LEFT('itcast',2) -- it<\/code><\/td>\n<\/tr>\n<tr>\n<td>LEFT(s,n)<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u524d n \u4e2a\u5b57\u7b26<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 abcde \u7684\u524d\u4e24\u4e2a\u5b57\u7b26\uff1a<code>SELECT LEFT('abcde',2) -- ab<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOCATE(s1,s)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 s \u4e2d\u83b7\u53d6 s1 \u7684\u5f00\u59cb\u4f4d\u7f6e<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 abc \u4e2d b \u7684\u4f4d\u7f6e\uff1a<code>SELECT LOCATE('b', 'abc') -- 2<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOWER(s)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 s \u7684\u6240\u6709\u5b57\u6bcd\u53d8\u6210\u5c0f\u5199\u5b57\u6bcd<\/td>\n<td>\u5b57\u7b26\u4e32 itcast \u8f6c\u6362\u4e3a\u5c0f\u5199\uff1a<code>SELECT LOWER('itcast') -- itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>LPAD(s1,len,s2)<\/td>\n<td>\u5728\u5b57\u7b26\u4e32 s1 \u7684\u5f00\u59cb\u5904\u586b\u5145\u5b57\u7b26\u4e32 s2\uff0c\u4f7f\u5b57\u7b26\u4e32\u957f\u5ea6\u8fbe\u5230 len<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 xx \u586b\u5145\u5230 abc \u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u5904\uff1a<code>SELECT LPAD('abc',5,'xx') -- xxabc<\/code><\/td>\n<\/tr>\n<tr>\n<td>LTRIM(s)<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32 s \u5f00\u59cb\u5904\u7684\u7a7a\u683c<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32 itcast\u5f00\u59cb\u5904\u7684\u7a7a\u683c\uff1a<code>SELECT LTRIM(&quot; itcast&quot;) AS LeftTrimmedString;-- itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>MID(s,n,len)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 s \u7684 start \u4f4d\u7f6e\u622a\u53d6\u957f\u5ea6\u4e3a length \u7684\u5b50\u5b57\u7b26\u4e32\uff0c\u540c SUBSTRING(s,n,len)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 itcast \u4e2d\u7684\u7b2c 2 \u4e2a\u4f4d\u7f6e\u622a\u53d6 3\u4e2a \u5b57\u7b26\uff1a<code>SELECT MID(&quot;itcast&quot;, 2, 3) AS ExtractString; -- UNO<\/code><\/td>\n<\/tr>\n<tr>\n<td>POSITION(s1 IN s)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 s \u4e2d\u83b7\u53d6 s1 \u7684\u5f00\u59cb\u4f4d\u7f6e<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 abc \u4e2d b \u7684\u4f4d\u7f6e\uff1a<code>SELECT POSITION('b' in 'abc') -- 2<\/code><\/td>\n<\/tr>\n<tr>\n<td>REPEAT(s,n)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 s \u91cd\u590d n \u6b21<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 itcast \u91cd\u590d\u4e09\u6b21\uff1a<code>SELECT REPEAT('itcast',3) -- itcastitcastitcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>REPLACE(s,s1,s2)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 s2 \u66ff\u4ee3\u5b57\u7b26\u4e32 s \u4e2d\u7684\u5b57\u7b26\u4e32 s1<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 abc \u4e2d\u7684\u5b57\u7b26 a \u66ff\u6362\u4e3a\u5b57\u7b26 x\uff1a<code>SELECT REPLACE('abc','a','x') --xbc<\/code><\/td>\n<\/tr>\n<tr>\n<td>REVERSE(s)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32s\u7684\u987a\u5e8f\u53cd\u8fc7\u6765<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 abc \u7684\u987a\u5e8f\u53cd\u8fc7\u6765\uff1a<code>SELECT REVERSE('abc') -- cba<\/code><\/td>\n<\/tr>\n<tr>\n<td>RIGHT(s,n)<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 s \u7684\u540e n \u4e2a\u5b57\u7b26<\/td>\n<td>\u8fd4\u56de\u5b57\u7b26\u4e32 itcast \u7684\u540e\u4e24\u4e2a\u5b57\u7b26\uff1a<code>SELECT RIGHT('itcast',2) -- ob<\/code><\/td>\n<\/tr>\n<tr>\n<td>RPAD(s1,len,s2)<\/td>\n<td>\u5728\u5b57\u7b26\u4e32 s1 \u7684\u7ed3\u5c3e\u5904\u6dfb\u52a0\u5b57\u7b26\u4e32 s1\uff0c\u4f7f\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u8fbe\u5230 len<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 xx \u586b\u5145\u5230 abc \u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u5904\uff1a<code>SELECT RPAD('abc',5,'xx') -- abcxx<\/code><\/td>\n<\/tr>\n<tr>\n<td>RTRIM(s)<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32 s \u7ed3\u5c3e\u5904\u7684\u7a7a\u683c<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32 itcast \u7684\u672b\u5c3e\u7a7a\u683c\uff1a<code>SELECT RTRIM(&quot;itcast &quot;) AS RightTrimmedString; -- itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>SPACE(n)<\/td>\n<td>\u8fd4\u56de n \u4e2a\u7a7a\u683c<\/td>\n<td>\u8fd4\u56de 10 \u4e2a\u7a7a\u683c\uff1a<code>SELECT SPACE(10);<\/code><\/td>\n<\/tr>\n<tr>\n<td>STRCMP(s1,s2)<\/td>\n<td>\u6bd4\u8f83\u5b57\u7b26\u4e32 s1 \u548c s2\uff0c\u5982\u679c s1 \u4e0e s2 \u76f8\u7b49\u8fd4\u56de 0 \uff0c\u5982\u679c s1&gt;s2 \u8fd4\u56de 1\uff0c\u5982\u679c s1&lt;s2 \u8fd4\u56de -1<\/td>\n<td>\u6bd4\u8f83\u5b57\u7b26\u4e32\uff1a<code>SELECT STRCMP(&quot;itcast&quot;, &quot;itcast&quot;); -- 0<\/code><\/td>\n<\/tr>\n<tr>\n<td>SUBSTR(s, start, length)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 s \u7684 start \u4f4d\u7f6e\u622a\u53d6\u957f\u5ea6\u4e3a length \u7684\u5b50\u5b57\u7b26\u4e32<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 itcast \u4e2d\u7684\u7b2c 2 \u4e2a\u4f4d\u7f6e\u622a\u53d6 3\u4e2a \u5b57\u7b26\uff1a<code>SELECT SUBSTR(&quot;itcast&quot;, 2, 3) AS ExtractString; -- UNO<\/code><\/td>\n<\/tr>\n<tr>\n<td>SUBSTRING(s, start, length)<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 s \u7684 start \u4f4d\u7f6e\u622a\u53d6\u957f\u5ea6\u4e3a length \u7684\u5b50\u5b57\u7b26\u4e32<\/td>\n<td>\u4ece\u5b57\u7b26\u4e32 itcast \u4e2d\u7684\u7b2c 2 \u4e2a\u4f4d\u7f6e\u622a\u53d6 3\u4e2a \u5b57\u7b26\uff1a<code>SELECT SUBSTRING(&quot;itcast&quot;, 2, 3) AS ExtractString; -- UNO<\/code><\/td>\n<\/tr>\n<tr>\n<td>SUBSTRING_INDEX(s, delimiter, number)<\/td>\n<td>\u8fd4\u56de\u4ece\u5b57\u7b26\u4e32 s \u7684\u7b2c number \u4e2a\u51fa\u73b0\u7684\u5206\u9694\u7b26 delimiter \u4e4b\u540e\u7684\u5b50\u4e32\u3002\u5982\u679c number \u662f\u6b63\u6570\uff0c\u8fd4\u56de\u7b2c number \u4e2a\u5b57\u7b26\u5de6\u8fb9\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c number \u662f\u8d1f\u6570\uff0c\u8fd4\u56de\u7b2c(number \u7684\u7edd\u5bf9\u503c(\u4ece\u53f3\u8fb9\u6570))\u4e2a\u5b57\u7b26\u53f3\u8fb9\u7684\u5b57\u7b26\u4e32\u3002<\/td>\n<td><code>SELECT SUBSTRING_INDEX('a*b','*',1) -- aSELECT SUBSTRING_INDEX('a*b','*',-1) -- bSELECT SUBSTRING_INDEX(SUBSTRING_INDEX('a*b*c*d*e','*',3),'*',-1) -- c<\/code><\/td>\n<\/tr>\n<tr>\n<td>TRIM(s)<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32 s \u5f00\u59cb\u548c\u7ed3\u5c3e\u5904\u7684\u7a7a\u683c<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32 itcast \u7684\u9996\u5c3e\u7a7a\u683c\uff1a<code>SELECT TRIM(' itcast ') AS TrimmedString;<\/code><\/td>\n<\/tr>\n<tr>\n<td>UCASE(s)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 itcast \u8f6c\u6362\u4e3a\u5927\u5199\uff1a<code>SELECT UCASE(&quot;itcast&quot;); -- itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>UPPER(s)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5927\u5199<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 itcast \u8f6c\u6362\u4e3a\u5927\u5199\uff1a<code>SELECT UPPER(&quot;itcast&quot;); -- itcast<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><a id=\"14-2-mysql%E6%95%B0%E5%AD%97%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#14-2-mysql%E6%95%B0%E5%AD%97%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>14.2 MySQL \u6570\u5b57\u51fd\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570\u540d<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u5b9e\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ABS(x)<\/td>\n<td>\u8fd4\u56de x \u7684\u7edd\u5bf9\u503c<\/td>\n<td>\u8fd4\u56de -1 \u7684\u7edd\u5bf9\u503c\uff1a<code>SELECT ABS(-1) -- \u8fd4\u56de1<\/code><\/td>\n<\/tr>\n<tr>\n<td>ACOS(x)<\/td>\n<td>\u6c42 x \u7684\u53cd\u4f59\u5f26\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT ACOS(0.25);<\/code><\/td>\n<\/tr>\n<tr>\n<td>ASIN(x)<\/td>\n<td>\u6c42\u53cd\u6b63\u5f26\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT ASIN(0.25);<\/code><\/td>\n<\/tr>\n<tr>\n<td>ATAN(x)<\/td>\n<td>\u6c42\u53cd\u6b63\u5207\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT ATAN(2.5);<\/code><\/td>\n<\/tr>\n<tr>\n<td>ATAN2(n, m)<\/td>\n<td>\u6c42\u53cd\u6b63\u5207\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT ATAN2(-0.8, 2);<\/code><\/td>\n<\/tr>\n<tr>\n<td>AVG(expression)<\/td>\n<td>\u8fd4\u56de\u4e00\u4e2a\u8868\u8fbe\u5f0f\u7684\u5e73\u5747\u503c\uff0cexpression \u662f\u4e00\u4e2a\u5b57\u6bb5<\/td>\n<td>\u8fd4\u56de Products \u8868\u4e2dPrice \u5b57\u6bb5\u7684\u5e73\u5747\u503c\uff1a<code>SELECT AVG(Price) AS AveragePrice FROM Products;<\/code><\/td>\n<\/tr>\n<tr>\n<td>CEIL(x)<\/td>\n<td>\u8fd4\u56de\u5927\u4e8e\u6216\u7b49\u4e8e x \u7684\u6700\u5c0f\u6574\u6570<\/td>\n<td><code>SELECT CEIL(1.5) -- \u8fd4\u56de2<\/code><\/td>\n<\/tr>\n<tr>\n<td>CEILING(x)<\/td>\n<td>\u8fd4\u56de\u5927\u4e8e\u6216\u7b49\u4e8e x \u7684\u6700\u5c0f\u6574\u6570<\/td>\n<td><code>SELECT CEILING(1.5) -- \u8fd4\u56de2<\/code><\/td>\n<\/tr>\n<tr>\n<td>COS(x)<\/td>\n<td>\u6c42\u4f59\u5f26\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT COS(2);<\/code><\/td>\n<\/tr>\n<tr>\n<td>COT(x)<\/td>\n<td>\u6c42\u4f59\u5207\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT COT(6);<\/code><\/td>\n<\/tr>\n<tr>\n<td>COUNT(expression)<\/td>\n<td>\u8fd4\u56de\u67e5\u8be2\u7684\u8bb0\u5f55\u603b\u6570\uff0cexpression \u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u6bb5\u6216\u8005 * \u53f7<\/td>\n<td>\u8fd4\u56de Products \u8868\u4e2d products \u5b57\u6bb5\u603b\u5171\u6709\u591a\u5c11\u6761\u8bb0\u5f55\uff1a<code>SELECT COUNT(ProductID) AS NumberOfProducts FROM Products;<\/code><\/td>\n<\/tr>\n<tr>\n<td>DEGREES(x)<\/td>\n<td>\u5c06\u5f27\u5ea6\u8f6c\u6362\u4e3a\u89d2\u5ea6<\/td>\n<td><code>SELECT DEGREES(3.1415926535898) -- 180<\/code><\/td>\n<\/tr>\n<tr>\n<td>n DIV m<\/td>\n<td>\u6574\u9664\uff0cn \u4e3a\u88ab\u9664\u6570\uff0cm \u4e3a\u9664\u6570<\/td>\n<td>\u8ba1\u7b97 10 \u9664\u4e8e 5\uff1a<code>SELECT 10 DIV 5; -- 2<\/code><\/td>\n<\/tr>\n<tr>\n<td>EXP(x)<\/td>\n<td>\u8fd4\u56de e \u7684 x \u6b21\u65b9<\/td>\n<td>\u8ba1\u7b97 e \u7684\u4e09\u6b21\u65b9\uff1a<code>SELECT EXP(3) -- 20.085536923188<\/code><\/td>\n<\/tr>\n<tr>\n<td>FLOOR(x)<\/td>\n<td>\u8fd4\u56de\u5c0f\u4e8e\u6216\u7b49\u4e8e x \u7684\u6700\u5927\u6574\u6570<\/td>\n<td>\u5c0f\u4e8e\u6216\u7b49\u4e8e 1.5 \u7684\u6574\u6570\uff1a<code>SELECT FLOOR(1.5) -- \u8fd4\u56de1<\/code><\/td>\n<\/tr>\n<tr>\n<td>GREATEST(expr1, expr2, expr3, &#8230;)<\/td>\n<td>\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c<\/td>\n<td>\u8fd4\u56de\u4ee5\u4e0b\u6570\u5b57\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff1a<code>SELECT GREATEST(3, 12, 34, 8, 25); -- 34<\/code>\u8fd4\u56de\u4ee5\u4e0b\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6700\u5927\u503c\uff1a<code>SELECT GREATEST(&quot;Google&quot;, &quot;itcast&quot;, &quot;Apple&quot;); -- itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>LEAST(expr1, expr2, expr3, &#8230;)<\/td>\n<td>\u8fd4\u56de\u5217\u8868\u4e2d\u7684\u6700\u5c0f\u503c<\/td>\n<td>\u8fd4\u56de\u4ee5\u4e0b\u6570\u5b57\u5217\u8868\u4e2d\u7684\u6700\u5c0f\u503c\uff1a<code>SELECT LEAST(3, 12, 34, 8, 25); -- 3<\/code>\u8fd4\u56de\u4ee5\u4e0b\u5b57\u7b26\u4e32\u5217\u8868\u4e2d\u7684\u6700\u5c0f\u503c\uff1a<code>SELECT LEAST(&quot;Google&quot;, &quot;itcast&quot;, &quot;Apple&quot;); -- Apple<\/code><\/td>\n<\/tr>\n<tr>\n<td><a href=\"http:\/\/www.itcast.com\/mysql\/func_mysql_ln.asp\">LN(opens new window)<\/a><\/td>\n<td>\u8fd4\u56de\u6570\u5b57\u7684\u81ea\u7136\u5bf9\u6570<\/td>\n<td>\u8fd4\u56de 2 \u7684\u81ea\u7136\u5bf9\u6570\uff1a<code>SELECT LN(2); -- 0.6931471805599453<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOG(x)<\/td>\n<td>\u8fd4\u56de\u81ea\u7136\u5bf9\u6570(\u4ee5 e \u4e3a\u5e95\u7684\u5bf9\u6570)<\/td>\n<td><code>SELECT LOG(20.085536923188) -- 3<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOG10(x)<\/td>\n<td>\u8fd4\u56de\u4ee5 10 \u4e3a\u5e95\u7684\u5bf9\u6570<\/td>\n<td><code>SELECT LOG10(100) -- 2<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOG2(x)<\/td>\n<td>\u8fd4\u56de\u4ee5 2 \u4e3a\u5e95\u7684\u5bf9\u6570<\/td>\n<td>\u8fd4\u56de\u4ee5 2 \u4e3a\u5e95 6 \u7684\u5bf9\u6570\uff1a<code>SELECT LOG2(6); -- 2.584962500721156<\/code><\/td>\n<\/tr>\n<tr>\n<td>MAX(expression)<\/td>\n<td>\u8fd4\u56de\u5b57\u6bb5 expression \u4e2d\u7684\u6700\u5927\u503c<\/td>\n<td>\u8fd4\u56de\u6570\u636e\u8868 Products \u4e2d\u5b57\u6bb5 Price \u7684\u6700\u5927\u503c\uff1a<code>SELECT MAX(Price) AS LargestPrice FROM Products;<\/code><\/td>\n<\/tr>\n<tr>\n<td>MIN(expression)<\/td>\n<td>\u8fd4\u56de\u5b57\u6bb5 expression \u4e2d\u7684\u6700\u5c0f\u503c<\/td>\n<td>\u8fd4\u56de\u6570\u636e\u8868 Products \u4e2d\u5b57\u6bb5 Price \u7684\u6700\u5c0f\u503c\uff1a<code>SELECT MIN(Price) AS LargestPrice FROM Products;<\/code><\/td>\n<\/tr>\n<tr>\n<td>MOD(x,y)<\/td>\n<td>\u8fd4\u56de x \u9664\u4ee5 y \u4ee5\u540e\u7684\u4f59\u6570<\/td>\n<td>5 \u9664\u4e8e 2 \u7684\u4f59\u6570\uff1a<code>SELECT MOD(5,2) -- 1<\/code><\/td>\n<\/tr>\n<tr>\n<td>PI()<\/td>\n<td>\u8fd4\u56de\u5706\u5468\u7387(3.141593\uff09<\/td>\n<td><code>SELECT PI() --3.141593<\/code><\/td>\n<\/tr>\n<tr>\n<td>POW(x,y)<\/td>\n<td>\u8fd4\u56de x \u7684 y \u6b21\u65b9<\/td>\n<td>2 \u7684 3 \u6b21\u65b9\uff1a<code>SELECT POW(2,3) -- 8<\/code><\/td>\n<\/tr>\n<tr>\n<td>POWER(x,y)<\/td>\n<td>\u8fd4\u56de x \u7684 y \u6b21\u65b9<\/td>\n<td>2 \u7684 3 \u6b21\u65b9\uff1a<code>SELECT POWER(2,3) -- 8<\/code><\/td>\n<\/tr>\n<tr>\n<td>RADIANS(x)<\/td>\n<td>\u5c06\u89d2\u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6<\/td>\n<td>180 \u5ea6\u8f6c\u6362\u4e3a\u5f27\u5ea6\uff1a<code>SELECT RADIANS(180) -- 3.1415926535898<\/code><\/td>\n<\/tr>\n<tr>\n<td>RAND()<\/td>\n<td>\u8fd4\u56de 0 \u5230 1 \u7684\u968f\u673a\u6570<\/td>\n<td><code>SELECT RAND() --0.93099315644334<\/code><\/td>\n<\/tr>\n<tr>\n<td>ROUND(x)<\/td>\n<td>\u8fd4\u56de\u79bb x \u6700\u8fd1\u7684\u6574\u6570<\/td>\n<td><code>SELECT ROUND(1.23456) --1<\/code><\/td>\n<\/tr>\n<tr>\n<td>SIGN(x)<\/td>\n<td>\u8fd4\u56de x \u7684\u7b26\u53f7\uff0cx \u662f\u8d1f\u6570\u30010\u3001\u6b63\u6570\u5206\u522b\u8fd4\u56de -1\u30010 \u548c 1<\/td>\n<td><code>SELECT SIGN(-10) -- (-1)<\/code><\/td>\n<\/tr>\n<tr>\n<td>SIN(x)<\/td>\n<td>\u6c42\u6b63\u5f26\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT SIN(RADIANS(30)) -- 0.5<\/code><\/td>\n<\/tr>\n<tr>\n<td>SQRT(x)<\/td>\n<td>\u8fd4\u56dex\u7684\u5e73\u65b9\u6839<\/td>\n<td>25 \u7684\u5e73\u65b9\u6839\uff1a<code>SELECT SQRT(25) -- 5<\/code><\/td>\n<\/tr>\n<tr>\n<td>SUM(expression)<\/td>\n<td>\u8fd4\u56de\u6307\u5b9a\u5b57\u6bb5\u7684\u603b\u548c<\/td>\n<td>\u8ba1\u7b97 OrderDetails \u8868\u4e2d\u5b57\u6bb5 Quantity \u7684\u603b\u548c\uff1a<code>SELECT SUM(Quantity) AS TotalItemsOrdered FROM OrderDetails;<\/code><\/td>\n<\/tr>\n<tr>\n<td>TAN(x)<\/td>\n<td>\u6c42\u6b63\u5207\u503c(\u53c2\u6570\u662f\u5f27\u5ea6)<\/td>\n<td><code>SELECT TAN(1.75); -- -5.52037992250933<\/code><\/td>\n<\/tr>\n<tr>\n<td>TRUNCATE(x,y)<\/td>\n<td>\u8fd4\u56de\u6570\u503c x \u4fdd\u7559\u5230\u5c0f\u6570\u70b9\u540e y \u4f4d\u7684\u503c\uff08\u4e0e ROUND \u6700\u5927\u7684\u533a\u522b\u662f\u4e0d\u4f1a\u8fdb\u884c\u56db\u820d\u4e94\u5165\uff09<\/td>\n<td><code>SELECT TRUNCATE(1.23456,3) -- 1.234<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><a id=\"14-3-mysql%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#14-3-mysql%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>14.3 MySQL \u65e5\u671f\u51fd\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570\u540d<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u5b9e\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ADDDATE(d,n)<\/td>\n<td>\u8ba1\u7b97\u5176\u5b9e\u65e5\u671f d \u52a0\u4e0a n \u5929\u7684\u65e5\u671f<\/td>\n<td><code>SELECT ADDDATE(&quot;2017-06-15&quot;, INTERVAL 10 DAY);-&gt;2017-06-25<\/code><\/td>\n<\/tr>\n<tr>\n<td>ADDTIME(t,n)<\/td>\n<td>\u65f6\u95f4 t \u52a0\u4e0a n \u79d2\u7684\u65f6\u95f4<\/td>\n<td><code>SELECT ADDTIME('2011-11-11 11:11:11', 5)-&gt;2011-11-11 11:11:16 (\u79d2)<\/code><\/td>\n<\/tr>\n<tr>\n<td>CURDATE()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f<\/td>\n<td><code>SELECT CURDATE();-&gt; 2018-09-19<\/code><\/td>\n<\/tr>\n<tr>\n<td>CURRENT_DATE()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f<\/td>\n<td><code>SELECT CURRENT_DATE();-&gt; 2018-09-19<\/code><\/td>\n<\/tr>\n<tr>\n<td>CURRENT_TIME<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/td>\n<td><code>SELECT CURRENT_TIME();-&gt; 19:59:02<\/code><\/td>\n<\/tr>\n<tr>\n<td>CURRENT_TIMESTAMP()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<td><code>SELECT CURRENT_TIMESTAMP()-&gt; 2018-09-19 20:57:43<\/code><\/td>\n<\/tr>\n<tr>\n<td>CURTIME()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/td>\n<td><code>SELECT CURTIME();-&gt; 19:59:02<\/code><\/td>\n<\/tr>\n<tr>\n<td>DATE()<\/td>\n<td>\u4ece\u65e5\u671f\u6216\u65e5\u671f\u65f6\u95f4\u8868\u8fbe\u5f0f\u4e2d\u63d0\u53d6\u65e5\u671f\u503c<\/td>\n<td><code>SELECT DATE(&quot;2017-06-15&quot;); -&gt; 2017-06-15<\/code><\/td>\n<\/tr>\n<tr>\n<td>DATEDIFF(d1,d2)<\/td>\n<td>\u8ba1\u7b97\u65e5\u671f d1-&gt;d2 \u4e4b\u95f4\u76f8\u9694\u7684\u5929\u6570<\/td>\n<td><code>SELECT DATEDIFF('2001-01-01','2001-02-02')-&gt; -32<\/code><\/td>\n<\/tr>\n<tr>\n<td>DATE_ADD(d\uff0cINTERVAL expr type)<\/td>\n<td>\u8ba1\u7b97\u8d77\u59cb\u65e5\u671f d \u52a0\u4e0a\u4e00\u4e2a\u65f6\u95f4\u6bb5\u540e\u7684\u65e5\u671f<\/td>\n<td><code>SELECT ADDDATE('2011-11-11 11:11:11',1)-&gt; 2011-11-12 11:11:11 (\u9ed8\u8ba4\u662f\u5929)SELECT ADDDATE('2011-11-11 11:11:11', INTERVAL 5 MINUTE)-&gt; 2011-11-11 11:16:11 (TYPE\u7684\u53d6\u503c\u4e0e\u4e0a\u9762\u90a3\u4e2a\u5217\u51fa\u6765\u7684\u51fd\u6570\u7c7b\u4f3c)<\/code><\/td>\n<\/tr>\n<tr>\n<td>DATE_FORMAT(d,f)<\/td>\n<td>\u6309\u8868\u8fbe\u5f0f f\u7684\u8981\u6c42\u663e\u793a\u65e5\u671f d<\/td>\n<td><code>SELECT DATE_FORMAT('2011-11-11 11:11:11','%Y-%m-%d %r')-&gt; 2011-11-11 11:11:11 AM<\/code><\/td>\n<\/tr>\n<tr>\n<td>DATE_SUB(date,INTERVAL expr type)<\/td>\n<td>\u51fd\u6570\u4ece\u65e5\u671f\u51cf\u53bb\u6307\u5b9a\u7684\u65f6\u95f4\u95f4\u9694\u3002<\/td>\n<td>Orders \u8868\u4e2d OrderDate \u5b57\u6bb5\u51cf\u53bb 2 \u5929\uff1a<code>SELECT OrderId,DATE_SUB(OrderDate,INTERVAL 2 DAY) AS OrderPayDateFROM Orders<\/code><\/td>\n<\/tr>\n<tr>\n<td>DAY(d)<\/td>\n<td>\u8fd4\u56de\u65e5\u671f\u503c d \u7684\u65e5\u671f\u90e8\u5206<\/td>\n<td><code>SELECT DAY(&quot;2017-06-15&quot;); -&gt; 15<\/code><\/td>\n<\/tr>\n<tr>\n<td>DAYNAME(d)<\/td>\n<td>\u8fd4\u56de\u65e5\u671f d \u662f\u661f\u671f\u51e0\uff0c\u5982 Monday,Tuesday<\/td>\n<td><code>SELECT DAYNAME('2011-11-11 11:11:11')-&gt;Friday<\/code><\/td>\n<\/tr>\n<tr>\n<td>DAYOFMONTH(d)<\/td>\n<td>\u8ba1\u7b97\u65e5\u671f d \u662f\u672c\u6708\u7684\u7b2c\u51e0\u5929<\/td>\n<td><code>SELECT DAYOFMONTH('2011-11-11 11:11:11')-&gt;11<\/code><\/td>\n<\/tr>\n<tr>\n<td>DAYOFWEEK(d)<\/td>\n<td>\u65e5\u671f d \u4eca\u5929\u662f\u661f\u671f\u51e0\uff0c1 \u661f\u671f\u65e5\uff0c2 \u661f\u671f\u4e00\uff0c\u4ee5\u6b64\u7c7b\u63a8<\/td>\n<td><code>SELECT DAYOFWEEK('2011-11-11 11:11:11')-&gt;6<\/code><\/td>\n<\/tr>\n<tr>\n<td>DAYOFYEAR(d)<\/td>\n<td>\u8ba1\u7b97\u65e5\u671f d \u662f\u672c\u5e74\u7684\u7b2c\u51e0\u5929<\/td>\n<td><code>SELECT DAYOFYEAR('2011-11-11 11:11:11')-&gt;315<\/code><\/td>\n<\/tr>\n<tr>\n<td>EXTRACT(type FROM d)<\/td>\n<td>\u4ece\u65e5\u671f d \u4e2d\u83b7\u53d6\u6307\u5b9a\u7684\u503c\uff0ctype \u6307\u5b9a\u8fd4\u56de\u7684\u503c\u3002 type\u53ef\u53d6\u503c\u4e3a\uff1a MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEARSECOND_MICROSECONDMINUTE_MICROSECONDMINUTE_SECONDHOUR_MICROSECONDHOUR_SECONDHOUR_MINUTEDAY_MICROSECONDDAY_SECONDDAY_MINUTEDAY_HOURYEAR_MONTH<\/td>\n<td><code>SELECT EXTRACT(MINUTE FROM '2011-11-11 11:11:11') -&gt; 11<\/code><\/td>\n<\/tr>\n<tr>\n<td>ROM_DAYS(n)<\/td>\n<td>\u8ba1\u7b97\u4ece 0000 \u5e74 1 \u6708 1 \u65e5\u5f00\u59cb n \u5929\u540e\u7684\u65e5\u671f<\/td>\n<td><code>SELECT FROM_DAYS(1111)-&gt; 0003-01-16<\/code><\/td>\n<\/tr>\n<tr>\n<td>HOUR(t)<\/td>\n<td>\u8fd4\u56de t \u4e2d\u7684\u5c0f\u65f6\u503c<\/td>\n<td><code>SELECT HOUR('1:2:3')-&gt; 1<\/code><\/td>\n<\/tr>\n<tr>\n<td>LAST_DAY(d)<\/td>\n<td>\u8fd4\u56de\u7ed9\u7ed9\u5b9a\u65e5\u671f\u7684\u90a3\u4e00\u6708\u4efd\u7684\u6700\u540e\u4e00\u5929<\/td>\n<td><code>SELECT LAST_DAY(&quot;2017-06-20&quot;);-&gt; 2017-06-30<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOCALTIME()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<td><code>SELECT LOCALTIME()-&gt; 2018-09-19 20:57:43<\/code><\/td>\n<\/tr>\n<tr>\n<td>LOCALTIMESTAMP()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<td><code>SELECT LOCALTIMESTAMP()-&gt; 2018-09-19 20:57:43<\/code><\/td>\n<\/tr>\n<tr>\n<td>MAKEDATE(year, day-of-year)<\/td>\n<td>\u57fa\u4e8e\u7ed9\u5b9a\u53c2\u6570\u5e74\u4efd year \u548c\u6240\u5728\u5e74\u4e2d\u7684\u5929\u6570\u5e8f\u53f7 day-of-year \u8fd4\u56de\u4e00\u4e2a\u65e5\u671f<\/td>\n<td><code>SELECT MAKEDATE(2017, 3);-&gt; 2017-01-03<\/code><\/td>\n<\/tr>\n<tr>\n<td>MAKETIME(hour, minute, second)<\/td>\n<td>\u7ec4\u5408\u65f6\u95f4\uff0c\u53c2\u6570\u5206\u522b\u4e3a\u5c0f\u65f6\u3001\u5206\u949f\u3001\u79d2<\/td>\n<td><code>SELECT MAKETIME(11, 35, 4);-&gt; 11:35:04<\/code><\/td>\n<\/tr>\n<tr>\n<td>MICROSECOND(date)<\/td>\n<td>\u8fd4\u56de\u65e5\u671f\u53c2\u6570\u6240\u5bf9\u5e94\u7684\u6beb\u79d2\u6570<\/td>\n<td><code>SELECT MICROSECOND(&quot;2017-06-20 09:34:00.000023&quot;);-&gt; 23<\/code><\/td>\n<\/tr>\n<tr>\n<td>MINUTE(t)<\/td>\n<td>\u8fd4\u56de t \u4e2d\u7684\u5206\u949f\u503c<\/td>\n<td><code>SELECT MINUTE('1:2:3')-&gt; 2<\/code><\/td>\n<\/tr>\n<tr>\n<td>MONTHNAME(d)<\/td>\n<td>\u8fd4\u56de\u65e5\u671f\u5f53\u4e2d\u7684\u6708\u4efd\u540d\u79f0\uff0c\u5982 Janyary<\/td>\n<td><code>SELECT MONTHNAME('2011-11-11 11:11:11')-&gt; November<\/code><\/td>\n<\/tr>\n<tr>\n<td>MONTH(d)<\/td>\n<td>\u8fd4\u56de\u65e5\u671fd\u4e2d\u7684\u6708\u4efd\u503c\uff0c1 \u5230 12<\/td>\n<td><code>SELECT MONTH('2011-11-11 11:11:11')-&gt;11<\/code><\/td>\n<\/tr>\n<tr>\n<td>NOW()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<td><code>SELECT NOW()-&gt; 2018-09-19 20:57:43<\/code><\/td>\n<\/tr>\n<tr>\n<td>PERIOD_ADD(period, number)<\/td>\n<td>\u4e3a \u5e74-\u6708 \u7ec4\u5408\u65e5\u671f\u6dfb\u52a0\u4e00\u4e2a\u65f6\u6bb5<\/td>\n<td><code>SELECT PERIOD_ADD(201703, 5); -&gt; 201708<\/code><\/td>\n<\/tr>\n<tr>\n<td>PERIOD_DIFF(period1, period2)<\/td>\n<td>\u8fd4\u56de\u4e24\u4e2a\u65f6\u6bb5\u4e4b\u95f4\u7684\u6708\u4efd\u5dee\u503c<\/td>\n<td><code>SELECT PERIOD_DIFF(201710, 201703);-&gt; 7<\/code><\/td>\n<\/tr>\n<tr>\n<td>QUARTER(d)<\/td>\n<td>\u8fd4\u56de\u65e5\u671fd\u662f\u7b2c\u51e0\u5b63\u8282\uff0c\u8fd4\u56de 1 \u5230 4<\/td>\n<td><code>SELECT QUARTER('2011-11-11 11:11:11')-&gt; 4<\/code><\/td>\n<\/tr>\n<tr>\n<td>SECOND(t)<\/td>\n<td>\u8fd4\u56de t \u4e2d\u7684\u79d2\u949f\u503c<\/td>\n<td><code>SELECT SECOND('1:2:3')-&gt; 3<\/code><\/td>\n<\/tr>\n<tr>\n<td>SEC_TO_TIME(s)<\/td>\n<td>\u5c06\u4ee5\u79d2\u4e3a\u5355\u4f4d\u7684\u65f6\u95f4 s \u8f6c\u6362\u4e3a\u65f6\u5206\u79d2\u7684\u683c\u5f0f<\/td>\n<td><code>SELECT SEC_TO_TIME(4320)-&gt; 01:12:00<\/code><\/td>\n<\/tr>\n<tr>\n<td>STR_TO_DATE(string, format_mask)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32\u8f6c\u53d8\u4e3a\u65e5\u671f<\/td>\n<td><code>SELECT STR_TO_DATE(&quot;August 10 2017&quot;, &quot;%M %d %Y&quot;);-&gt; 2017-08-10<\/code><\/td>\n<\/tr>\n<tr>\n<td>SUBDATE(d,n)<\/td>\n<td>\u65e5\u671f d \u51cf\u53bb n \u5929\u540e\u7684\u65e5\u671f<\/td>\n<td><code>SELECT SUBDATE('2011-11-11 11:11:11', 1)-&gt;2011-11-10 11:11:11 (\u9ed8\u8ba4\u662f\u5929)<\/code><\/td>\n<\/tr>\n<tr>\n<td>SUBTIME(t,n)<\/td>\n<td>\u65f6\u95f4 t \u51cf\u53bb n \u79d2\u7684\u65f6\u95f4<\/td>\n<td><code>SELECT SUBTIME('2011-11-11 11:11:11', 5)-&gt;2011-11-11 11:11:06 (\u79d2)<\/code><\/td>\n<\/tr>\n<tr>\n<td>SYSDATE()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<td><code>SELECT SYSDATE()-&gt; 2018-09-19 20:57:43<\/code><\/td>\n<\/tr>\n<tr>\n<td>TIME(expression)<\/td>\n<td>\u63d0\u53d6\u4f20\u5165\u8868\u8fbe\u5f0f\u7684\u65f6\u95f4\u90e8\u5206<\/td>\n<td><code>SELECT TIME(&quot;19:30:10&quot;);-&gt; 19:30:10<\/code><\/td>\n<\/tr>\n<tr>\n<td>TIME_FORMAT(t,f)<\/td>\n<td>\u6309\u8868\u8fbe\u5f0f f \u7684\u8981\u6c42\u663e\u793a\u65f6\u95f4 t<\/td>\n<td><code>SELECT TIME_FORMAT('11:11:11','%r')11:11:11 AM<\/code><\/td>\n<\/tr>\n<tr>\n<td>TIME_TO_SEC(t)<\/td>\n<td>\u5c06\u65f6\u95f4 t \u8f6c\u6362\u4e3a\u79d2<\/td>\n<td><code>SELECT TIME_TO_SEC('1:12:00')-&gt; 4320<\/code><\/td>\n<\/tr>\n<tr>\n<td>TIMEDIFF(time1, time2)<\/td>\n<td>\u8ba1\u7b97\u65f6\u95f4\u5dee\u503c<\/td>\n<td><code>SELECT TIMEDIFF(&quot;13:10:11&quot;, &quot;13:10:10&quot;);-&gt; 00:00:01<\/code><\/td>\n<\/tr>\n<tr>\n<td>TIMESTAMP(expression, interval)<\/td>\n<td>\u5355\u4e2a\u53c2\u6570\u65f6\uff0c\u51fd\u6570\u8fd4\u56de\u65e5\u671f\u6216\u65e5\u671f\u65f6\u95f4\u8868\u8fbe\u5f0f\uff1b\u67092\u4e2a\u53c2\u6570\u65f6\uff0c\u5c06\u53c2\u6570\u52a0\u548c<\/td>\n<td><code>SELECT TIMESTAMP(&quot;2017-07-23&quot;, &quot;13:10:11&quot;);-&gt; 2017-07-23 13:10:11<\/code><\/td>\n<\/tr>\n<tr>\n<td>TO_DAYS(d)<\/td>\n<td>\u8ba1\u7b97\u65e5\u671f d \u8ddd\u79bb 0000 \u5e74 1 \u6708 1 \u65e5\u7684\u5929\u6570<\/td>\n<td><code>SELECT TO_DAYS('0001-01-01 01:01:01')-&gt; 366<\/code><\/td>\n<\/tr>\n<tr>\n<td>WEEK(d)<\/td>\n<td>\u8ba1\u7b97\u65e5\u671f d \u662f\u672c\u5e74\u7684\u7b2c\u51e0\u4e2a\u661f\u671f\uff0c\u8303\u56f4\u662f 0 \u5230 53<\/td>\n<td><code>SELECT WEEK('2011-11-11 11:11:11')-&gt; 45<\/code><\/td>\n<\/tr>\n<tr>\n<td>WEEKDAY(d)<\/td>\n<td>\u65e5\u671f d \u662f\u661f\u671f\u51e0\uff0c0 \u8868\u793a\u661f\u671f\u4e00\uff0c1 \u8868\u793a\u661f\u671f\u4e8c<\/td>\n<td><code>SELECT WEEKDAY(&quot;2017-06-15&quot;);-&gt; 3<\/code><\/td>\n<\/tr>\n<tr>\n<td>WEEKOFYEAR(d)<\/td>\n<td>\u8ba1\u7b97\u65e5\u671f d \u662f\u672c\u5e74\u7684\u7b2c\u51e0\u4e2a\u661f\u671f\uff0c\u8303\u56f4\u662f 0 \u5230 53<\/td>\n<td><code>SELECT WEEKOFYEAR('2011-11-11 11:11:11')-&gt; 45<\/code><\/td>\n<\/tr>\n<tr>\n<td>YEAR(d)<\/td>\n<td>\u8fd4\u56de\u5e74\u4efd<\/td>\n<td><code>SELECT YEAR(&quot;2017-06-15&quot;);-&gt; 2017<\/code><\/td>\n<\/tr>\n<tr>\n<td>YEARWEEK(date, mode)<\/td>\n<td>\u8fd4\u56de\u5e74\u4efd\u53ca\u7b2c\u51e0\u5468\uff080\u523053\uff09\uff0cmode \u4e2d 0 \u8868\u793a\u5468\u5929\uff0c1\u8868\u793a\u5468\u4e00\uff0c\u4ee5\u6b64\u7c7b\u63a8<\/td>\n<td><code>SELECT YEARWEEK(&quot;2017-06-15&quot;);-&gt; 201724<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><a id=\"14-4-mysql%E9%AB%98%E7%BA%A7%E5%87%BD%E6%95%B0\" class=\"anchor\" aria-hidden=\"true\" href=\"#14-4-mysql%E9%AB%98%E7%BA%A7%E5%87%BD%E6%95%B0\"><span class=\"octicon octicon-link\"><\/span><\/a>14.4 MySQL \u9ad8\u7ea7\u51fd\u6570<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570\u540d<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u5b9e\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>BIN(x)<\/td>\n<td>\u8fd4\u56de x \u7684\u4e8c\u8fdb\u5236\u7f16\u7801<\/td>\n<td>15 \u7684 2 \u8fdb\u5236\u7f16\u7801:<code>SELECT BIN(15); -- 1111<\/code><\/td>\n<\/tr>\n<tr>\n<td>BINARY(s)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32 s \u8f6c\u6362\u4e3a\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td><code>SELECT BINARY &quot;itcast&quot;;-&gt; itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>CASE expression WHEN condition1 THEN result1 WHEN condition2 THEN result2 ... WHEN conditionN THEN resultN ELSE resultEND<\/code><\/td>\n<td>CASE \u8868\u793a\u51fd\u6570\u5f00\u59cb\uff0cEND \u8868\u793a\u51fd\u6570\u7ed3\u675f\u3002\u5982\u679c condition1 \u6210\u7acb\uff0c\u5219\u8fd4\u56de result1, \u5982\u679c condition2 \u6210\u7acb\uff0c\u5219\u8fd4\u56de result2\uff0c\u5f53\u5168\u90e8\u4e0d\u6210\u7acb\u5219\u8fd4\u56de result\uff0c\u800c\u5f53\u6709\u4e00\u4e2a\u6210\u7acb\u4e4b\u540e\uff0c\u540e\u9762\u7684\u5c31\u4e0d\u6267\u884c\u4e86\u3002<\/td>\n<td><code>SELECT CASE \u3000\u3000WHEN 1 &gt; 0\u3000\u3000THEN '1 &gt; 0'\u3000\u3000WHEN 2 &gt; 0\u3000\u3000THEN '2 &gt; 0'\u3000\u3000ELSE '3 &gt; 0'\u3000\u3000END-&gt;1 &gt; 0<\/code><\/td>\n<\/tr>\n<tr>\n<td>CAST(x AS type)<\/td>\n<td>\u8f6c\u6362\u6570\u636e\u7c7b\u578b<\/td>\n<td>\u5b57\u7b26\u4e32\u65e5\u671f\u8f6c\u6362\u4e3a\u65e5\u671f\uff1a<code>SELECT CAST(&quot;2017-08-29&quot; AS DATE);-&gt; 2017-08-29<\/code><\/td>\n<\/tr>\n<tr>\n<td>COALESCE(expr1, expr2, &#8230;., expr_n)<\/td>\n<td>\u8fd4\u56de\u53c2\u6570\u4e2d\u7684\u7b2c\u4e00\u4e2a\u975e\u7a7a\u8868\u8fbe\u5f0f\uff08\u4ece\u5de6\u5411\u53f3\uff09<\/td>\n<td><code>SELECT COALESCE(NULL, NULL, NULL, 'itcast.com', NULL, 'google.com');-&gt; itcast.com<\/code><\/td>\n<\/tr>\n<tr>\n<td>CONNECTION_ID()<\/td>\n<td>\u8fd4\u56de\u670d\u52a1\u5668\u7684\u8fde\u63a5\u6570<\/td>\n<td><code>SELECT CONNECTION_ID();-&gt; 4292835<\/code><\/td>\n<\/tr>\n<tr>\n<td>CONV(x,f1,f2)<\/td>\n<td>\u8fd4\u56de f1 \u8fdb\u5236\u6570\u53d8\u6210 f2 \u8fdb\u5236\u6570<\/td>\n<td><code>SELECT CONV(15, 10, 2);-&gt; 1111<\/code><\/td>\n<\/tr>\n<tr>\n<td>CONVERT(s USING cs)<\/td>\n<td>\u51fd\u6570\u5c06\u5b57\u7b26\u4e32 s \u7684\u5b57\u7b26\u96c6\u53d8\u6210 cs<\/td>\n<td><code>SELECT CHARSET('ABC')-&gt;utf-8 SELECT CHARSET(CONVERT('ABC' USING gbk))-&gt;gbk<\/code><\/td>\n<\/tr>\n<tr>\n<td>CURRENT_USER()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u7528\u6237<\/td>\n<td><code>SELECT CURRENT_USER();-&gt; guest@%<\/code><\/td>\n<\/tr>\n<tr>\n<td>DATABASE()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u6570\u636e\u5e93\u540d<\/td>\n<td><code>SELECT DATABASE(); -&gt; itcast<\/code><\/td>\n<\/tr>\n<tr>\n<td>IF(expr,v1,v2)<\/td>\n<td>\u5982\u679c\u8868\u8fbe\u5f0f expr \u6210\u7acb\uff0c\u8fd4\u56de\u7ed3\u679c v1\uff1b\u5426\u5219\uff0c\u8fd4\u56de\u7ed3\u679c v2\u3002<\/td>\n<td><code>SELECT IF(1 &gt; 0,'\u6b63\u786e','\u9519\u8bef') -&gt;\u6b63\u786e<\/code><\/td>\n<\/tr>\n<tr>\n<td><a href=\"http:\/\/www.itcast.com\/mysql\/mysql-func-ifnull.html\">IFNULL(v1,v2)(opens new window)<\/a><\/td>\n<td>\u5982\u679c v1 \u7684\u503c\u4e0d\u4e3a NULL\uff0c\u5219\u8fd4\u56de v1\uff0c\u5426\u5219\u8fd4\u56de v2\u3002<\/td>\n<td><code>SELECT IFNULL(null,'Hello Word')-&gt;Hello Word<\/code><\/td>\n<\/tr>\n<tr>\n<td>ISNULL(expression)<\/td>\n<td>\u5224\u65ad\u8868\u8fbe\u5f0f\u662f\u5426\u4e3a\u7a7a<\/td>\n<td><code>SELECT ISNULL(NULL);-&gt;1<\/code><\/td>\n<\/tr>\n<tr>\n<td>LAST_INSERT_ID()<\/td>\n<td>\u8fd4\u56de\u6700\u8fd1\u751f\u6210\u7684 AUTO_INCREMENT \u503c<\/td>\n<td><code>SELECT LAST_INSERT_ID();-&gt;6<\/code><\/td>\n<\/tr>\n<tr>\n<td>NULLIF(expr1, expr2)<\/td>\n<td>\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u5b57\u7b26\u4e32 expr1 \u4e0e expr2 \u76f8\u7b49 \u8fd4\u56de NULL\uff0c\u5426\u5219\u8fd4\u56de expr1<\/td>\n<td><code>SELECT NULLIF(25, 25);-&gt;<\/code><\/td>\n<\/tr>\n<tr>\n<td>SESSION_USER()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u7528\u6237<\/td>\n<td><code>SELECT SESSION_USER();-&gt; guest@%<\/code><\/td>\n<\/tr>\n<tr>\n<td>SYSTEM_USER()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u7528\u6237<\/td>\n<td><code>SELECT SYSTEM_USER();-&gt; guest@%<\/code><\/td>\n<\/tr>\n<tr>\n<td>USER()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u7528\u6237<\/td>\n<td><code>SELECT USER();-&gt; guest@%<\/code><\/td>\n<\/tr>\n<tr>\n<td>VERSION()<\/td>\n<td>\u8fd4\u56de\u6570\u636e\u5e93\u7684\u7248\u672c\u53f7<\/td>\n<td><code>SELECT VERSION()-&gt; 5.6.34<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><a id=\"15-mysql%E7%B4%A2%E5%BC%95\" class=\"anchor\" aria-hidden=\"true\" href=\"#15-mysql%E7%B4%A2%E5%BC%95\"><span class=\"octicon octicon-link\"><\/span><\/a>15. MySQL \u7d22\u5f15<\/h2>\n<p>MySQL\u7d22\u5f15\u7684\u5efa\u7acb\u5bf9\u4e8eMySQL\u7684\u9ad8\u6548\u8fd0\u884c\u662f\u5f88\u91cd\u8981\u7684\uff0c\u7d22\u5f15\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8MySQL\u7684\u68c0\u7d22\u901f\u5ea6\u3002<\/p>\n<p>\u7d22\u5f15\u5206\u5355\u5217\u7d22\u5f15\u548c\u7ec4\u5408\u7d22\u5f15\u3002\u5355\u5217\u7d22\u5f15\uff0c\u5373\u4e00\u4e2a\u7d22\u5f15\u53ea\u5305\u542b\u5355\u4e2a\u5217\uff0c\u4e00\u4e2a\u8868\u53ef\u4ee5\u6709\u591a\u4e2a\u5355\u5217\u7d22\u5f15\uff0c\u4f46\u8fd9\u4e0d\u662f\u7ec4\u5408\u7d22\u5f15\u3002\u7ec4\u5408\u7d22\u5f15\uff0c\u5373\u4e00\u4e2a\u7d22\u5f15\u5305\u542b\u591a\u4e2a\u5217\u3002<\/p>\n<p>\u521b\u5efa\u7d22\u5f15\u65f6\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u8be5\u7d22\u5f15\u662f\u5e94\u7528\u5728 SQL \u67e5\u8be2\u8bed\u53e5\u7684\u6761\u4ef6(\u4e00\u822c\u4f5c\u4e3a WHERE \u5b50\u53e5\u7684\u6761\u4ef6)\u3002<\/p>\n<p>\u5b9e\u9645\u4e0a\uff0c\u7d22\u5f15\u4e5f\u662f\u4e00\u5f20\u8868\uff0c\u8be5\u8868\u4fdd\u5b58\u4e86\u4e3b\u952e\u4e0e\u7d22\u5f15\u5b57\u6bb5\uff0c\u5e76\u6307\u5411\u5b9e\u4f53\u8868\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4e0a\u9762\u90fd\u5728\u8bf4\u4f7f\u7528\u7d22\u5f15\u7684\u597d\u5904\uff0c\u4f46\u8fc7\u591a\u7684\u4f7f\u7528\u7d22\u5f15\u5c06\u4f1a\u9020\u6210\u6ee5\u7528\u3002\u56e0\u6b64\u7d22\u5f15\u4e5f\u4f1a\u6709\u5b83\u7684\u7f3a\u70b9\uff1a\u867d\u7136\u7d22\u5f15\u5927\u5927\u63d0\u9ad8\u4e86\u67e5\u8be2\u901f\u5ea6\uff0c\u540c\u65f6\u5374\u4f1a\u964d\u4f4e\u66f4\u65b0\u8868\u7684\u901f\u5ea6\uff0c\u5982\u5bf9\u8868\u8fdb\u884cINSERT\u3001UPDATE\u548cDELETE\u3002\u56e0\u4e3a\u66f4\u65b0\u8868\u65f6\uff0cMySQL\u4e0d\u4ec5\u8981\u4fdd\u5b58\u6570\u636e\uff0c\u8fd8\u8981\u4fdd\u5b58\u4e00\u4e0b\u7d22\u5f15\u6587\u4ef6\u3002<\/p>\n<p>\u5efa\u7acb\u7d22\u5f15\u4f1a\u5360\u7528\u78c1\u76d8\u7a7a\u95f4\u7684\u7d22\u5f15\u6587\u4ef6\u3002<\/p>\n<h3><a id=\"15-1%E6%99%AE%E9%80%9A%E7%B4%A2%E5%BC%95\" class=\"anchor\" aria-hidden=\"true\" href=\"#15-1%E6%99%AE%E9%80%9A%E7%B4%A2%E5%BC%95\"><span class=\"octicon octicon-link\"><\/span><\/a>15.1 \u666e\u901a\u7d22\u5f15<\/h3>\n<ul>\n<li>\n<ol>\n<li>\u521b\u5efa\u7d22\u5f15:<\/li>\n<\/ol>\n<p>\u8fd9\u662f\u6700\u57fa\u672c\u7684\u7d22\u5f15\uff0c\u5b83\u6ca1\u6709\u4efb\u4f55\u9650\u5236\u3002\u5b83\u6709\u4ee5\u4e0b\u51e0\u79cd\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code>\u683c\u5f0f: CREATE INDEX indexName ON mytable(username(length)); \n\n\/\/\u521b\u5efa\u7d22\u5f15\n create index id on B(A_ID);\n<\/code><\/pre>\n<p>\u5982\u679c\u662fCHAR\uff0cVARCHAR\u7c7b\u578b\uff0clength\u53ef\u4ee5\u5c0f\u4e8e\u5b57\u6bb5\u5b9e\u9645\u957f\u5ea6\uff1b\u5982\u679c\u662fBLOB\u548cTEXT\u7c7b\u578b\uff0c\u5fc5\u987b\u6307\u5b9a length\u3002<\/p>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u4fee\u6539\u8868\u7ed3\u6784(\u6dfb\u52a0\u7d22\u5f15)<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>\u683c\u5f0f: ALTER table tableName ADD INDEX indexName(columnName)\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u521b\u5efa\u8868\u7684\u65f6\u5019\u76f4\u63a5\u6307\u5b9a<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>CREATE TABLE mytable(  \n \nID INT NOT NULL,   \n \nusername VARCHAR(16) NOT NULL,  \n \nINDEX [indexName] (username(length))  \n \n);\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"4\">\n<li>\u5220\u9664\u7d22\u5f15\u7684\u8bed\u6cd5<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>DROP INDEX [indexName] ON mytable; \n<\/code><\/pre>\n<h3><a id=\"15-2%E5%94%AF%E4%B8%80%E7%B4%A2%E5%BC%95\" class=\"anchor\" aria-hidden=\"true\" href=\"#15-2%E5%94%AF%E4%B8%80%E7%B4%A2%E5%BC%95\"><span class=\"octicon octicon-link\"><\/span><\/a>15.2 \u552f\u4e00\u7d22\u5f15<\/h3>\n<p>\u5b83\u4e0e\u524d\u9762\u7684\u666e\u901a\u7d22\u5f15\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u5c31\u662f\uff1a\u7d22\u5f15\u5217\u7684\u503c\u5fc5\u987b\u552f\u4e00\uff0c\u4f46\u5141\u8bb8\u6709\u7a7a\u503c\u3002\u5982\u679c\u662f\u7ec4\u5408\u7d22\u5f15\uff0c\u5219\u5217\u503c\u7684\u7ec4\u5408\u5fc5\u987b\u552f\u4e00\u3002\u5b83\u6709\u4ee5\u4e0b\u51e0\u79cd\u521b\u5efa\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\n<ol>\n<li>\u521b\u5efa\u7d22\u5f15<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>CREATE UNIQUE INDEX indexName ON mytable(username(length)) \n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"2\">\n<li>\u4fee\u6539\u8868\u7ed3\u6784<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>ALTER table mytable ADD UNIQUE [indexName] (username(length))\n<\/code><\/pre>\n<ul>\n<li>\n<ol start=\"3\">\n<li>\u521b\u5efa\u8868\u7684\u65f6\u5019\u76f4\u63a5\u6307\u5b9a<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>CREATE TABLE mytable(  \n \nID INT NOT NULL,   \n \nusername VARCHAR(16) NOT NULL,  \n \nUNIQUE [indexName] (username(length))  \n);  \n<\/code><\/pre>\n<h3><a id=\"15-3%E4%BD%BF%E7%94%A8-alter%E5%91%BD%E4%BB%A4%E6%B7%BB%E5%8A%A0%E5%92%8C%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95\" class=\"anchor\" aria-hidden=\"true\" href=\"#15-3%E4%BD%BF%E7%94%A8-alter%E5%91%BD%E4%BB%A4%E6%B7%BB%E5%8A%A0%E5%92%8C%E5%88%A0%E9%99%A4%E7%B4%A2%E5%BC%95\"><span class=\"octicon octicon-link\"><\/span><\/a>15.3 \u4f7f\u7528alter\u547d\u4ee4\u6dfb\u52a0\u548c\u5220\u9664\u7d22\u5f15<\/h3>\n<p>\u6709\u56db\u79cd\u65b9\u5f0f\u6765\u6dfb\u52a0\u6570\u636e\u8868\u7684\u7d22\u5f15\uff1a<\/p>\n<ul>\n<li><strong>ALTER TABLE tbl_name ADD PRIMARY KEY (column_list):<\/strong>\u00a0\u8be5\u8bed\u53e5\u6dfb\u52a0\u4e00\u4e2a\u4e3b\u952e\uff0c\u8fd9\u610f\u5473\u7740\u7d22\u5f15\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff0c\u4e14\u4e0d\u80fd\u4e3aNULL\u3002<\/li>\n<li><strong>ALTER TABLE tbl_name ADD UNIQUE index_name (column_list):<\/strong>\u00a0\u8fd9\u6761\u8bed\u53e5\u521b\u5efa\u7d22\u5f15\u7684\u503c\u5fc5\u987b\u662f\u552f\u4e00\u7684\uff08\u9664\u4e86NULL\u5916\uff0cNULL\u53ef\u80fd\u4f1a\u51fa\u73b0\u591a\u6b21\uff09\u3002<\/li>\n<li><strong>ALTER TABLE tbl_name ADD INDEX index_name (column_list):<\/strong>\u00a0\u6dfb\u52a0\u666e\u901a\u7d22\u5f15\uff0c\u7d22\u5f15\u503c\u53ef\u51fa\u73b0\u591a\u6b21\u3002<\/li>\n<li>**ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list)?*\u8be5\u8bed\u53e5\u6307\u5b9a\u4e86\u7d22\u5f15\u4e3a FULLTEXT \uff0c\u7528\u4e8e\u5168\u6587\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u4e3a\u5728\u8868\u4e2d\u6dfb\u52a0\u7d22\u5f15\u3002<\/p>\n<pre><code>mysql&gt; ALTER TABLE testalter_tbl ADD INDEX (c);\n<\/code><\/pre>\n<p>\u4f60\u8fd8\u53ef\u4ee5\u5728 ALTER \u547d\u4ee4\u4e2d\u4f7f\u7528 DROP \u5b50\u53e5\u6765\u5220\u9664\u7d22\u5f15\u3002\u5c1d\u8bd5\u4ee5\u4e0b\u5b9e\u4f8b\u5220\u9664\u7d22\u5f15:<\/p>\n<pre><code>mysql&gt; ALTER TABLE testalter_tbl DROP INDEX c;\n<\/code><\/pre>\n<h3><a id=\"15-4%E4%BD%BF%E7%94%A8-alter%E5%91%BD%E4%BB%A4%E6%B7%BB%E5%8A%A0%E5%92%8C%E5%88%A0%E9%99%A4%E4%B8%BB%E9%94%AE\" class=\"anchor\" aria-hidden=\"true\" href=\"#15-4%E4%BD%BF%E7%94%A8-alter%E5%91%BD%E4%BB%A4%E6%B7%BB%E5%8A%A0%E5%92%8C%E5%88%A0%E9%99%A4%E4%B8%BB%E9%94%AE\"><span class=\"octicon octicon-link\"><\/span><\/a>15.4 \u4f7f\u7528alter\u547d\u4ee4\u6dfb\u52a0\u548c\u5220\u9664\u4e3b\u952e<\/h3>\n<p>\u4e3b\u952e\u53ea\u80fd\u5728\u4e00\u4e2a\u8868\u4e2d\u6dfb\u52a0\u4e00\u4e2a\uff0c\u6dfb\u52a0\u4e3b\u952e\u7d22\u5f15\u65f6\uff0c\u4f60\u9700\u8981\u786e\u4fdd\u8be5\u4e3b\u952e\u9ed8\u8ba4\u4e0d\u4e3a\u7a7a\uff08NOT NULL\uff09\u3002\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code>mysql&gt; ALTER TABLE testalter_tbl ADD PRIMARY KEY (itcast);\n<\/code><\/pre>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 ALTER \u547d\u4ee4\u5220\u9664\u4e3b\u952e\uff1a<\/p>\n<pre><code>mysql&gt; ALTER TABLE testalter_tbl DROP PRIMARY KEY;\n<\/code><\/pre>\n<p>\u5220\u9664\u4e3b\u952e\u65f6\u53ea\u9700\u6307\u5b9aPRIMARY KEY\uff0c\u4f46\u5728\u5220\u9664\u7d22\u5f15\u65f6\uff0c\u4f60\u5fc5\u987b\u77e5\u9053\u7d22\u5f15\u540d\u3002<\/p>\n<h3><a id=\"15-5%E6%98%BE%E7%A4%BA%E7%B4%A2%E5%BC%95%E4%BF%A1%E6%81%AF\" class=\"anchor\" aria-hidden=\"true\" href=\"#15-5%E6%98%BE%E7%A4%BA%E7%B4%A2%E5%BC%95%E4%BF%A1%E6%81%AF\"><span class=\"octicon octicon-link\"><\/span><\/a>15.5 \u663e\u793a\u7d22\u5f15\u4fe1\u606f<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 SHOW INDEX \u547d\u4ee4\u6765\u5217\u51fa\u8868\u4e2d\u7684\u76f8\u5173\u7684\u7d22\u5f15\u4fe1\u606f\u3002\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0 \\G \u6765\u683c\u5f0f\u5316\u8f93\u51fa\u4fe1\u606f\u3002<\/p>\n<p>\u5c1d\u8bd5\u4ee5\u4e0b\u5b9e\u4f8b:<\/p>\n<pre><code>\u683c\u5f0f: SHOW INDEX FROM table_name; \\G\n\n\n\nmysql&gt; show index from B;\n+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+\n| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |\n+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+\n| B     |          0 | PRIMARY  |            1 | A_ID        | A         |           3 |     NULL | NULL   |      | BTREE      |         |               |\n+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+\n<\/code><\/pre>\n<h2><a id=\"16-mysql%E4%BA%8B%E5%8A%A1\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-mysql%E4%BA%8B%E5%8A%A1\"><span class=\"octicon octicon-link\"><\/span><\/a>16. MySQL \u4e8b\u52a1<\/h2>\n<p>MySQL \u4e8b\u52a1\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u64cd\u4f5c\u91cf\u5927\uff0c\u590d\u6742\u5ea6\u9ad8\u7684\u6570\u636e\u3002\u6bd4\u5982\u8bf4\uff0c\u5728\u4eba\u5458\u7ba1\u7406\u7cfb\u7edf\u4e2d\uff0c\u4f60\u5220\u9664\u4e00\u4e2a\u4eba\u5458\uff0c\u4f60\u5373\u9700\u8981\u5220\u9664\u4eba\u5458\u7684\u57fa\u672c\u8d44\u6599\uff0c\u4e5f\u8981\u5220\u9664\u548c\u8be5\u4eba\u5458\u76f8\u5173\u7684\u4fe1\u606f\uff0c\u5982\u4fe1\u7bb1\uff0c\u6587\u7ae0\u7b49\u7b49\uff0c\u8fd9\u6837\uff0c\u8fd9\u4e9b\u6570\u636e\u5e93\u64cd\u4f5c\u8bed\u53e5\u5c31\u6784\u6210\u4e00\u4e2a\u4e8b\u52a1\uff01<\/p>\n<ul>\n<li>\u5728 MySQL \u4e2d\u53ea\u6709\u4f7f\u7528\u4e86 Innodb \u6570\u636e\u5e93\u5f15\u64ce\u7684\u6570\u636e\u5e93\u6216\u8868\u624d\u652f\u6301\u4e8b\u52a1\u3002<\/li>\n<li>\u4e8b\u52a1\u5904\u7406\u53ef\u4ee5\u7528\u6765\u7ef4\u62a4\u6570\u636e\u5e93\u7684\u5b8c\u6574\u6027\uff0c\u4fdd\u8bc1\u6210\u6279\u7684 SQL \u8bed\u53e5\u8981\u4e48\u5168\u90e8\u6267\u884c\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u6267\u884c\u3002<\/li>\n<li>\u4e8b\u52a1\u7528\u6765\u7ba1\u7406 insert,update,delete \u8bed\u53e5<\/li>\n<\/ul>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u4e8b\u52a1\u662f\u5fc5\u987b\u6ee1\u8db34\u4e2a\u6761\u4ef6\uff08ACID\uff09\uff1a\uff1a\u539f\u5b50\u6027\uff08<strong>A<\/strong>tomicity\uff0c\u6216\u79f0\u4e0d\u53ef\u5206\u5272\u6027\uff09\u3001\u4e00\u81f4\u6027\uff08<strong>C<\/strong>onsistency\uff09\u3001\u9694\u79bb\u6027\uff08<strong>I<\/strong>solation\uff0c\u53c8\u79f0\u72ec\u7acb\u6027\uff09\u3001\u6301\u4e45\u6027\uff08<strong>D<\/strong>urability\uff09\u3002<\/p>\n<ul>\n<li>**\u539f\u5b50\u6027\uff1a**\u4e00\u4e2a\u4e8b\u52a1\uff08transaction\uff09\u4e2d\u7684\u6240\u6709\u64cd\u4f5c\uff0c\u8981\u4e48\u5168\u90e8\u5b8c\u6210\uff0c\u8981\u4e48\u5168\u90e8\u4e0d\u5b8c\u6210\uff0c\u5728\u4e2d\u95f4\u67d0\u4e2a\u73af\u8282\u4e0d\u4f1a\u7ed3\u675f\u3002\u4e8b\u52a1\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\u53d1\u751f\u9519\u8bef\uff0c\u4f1a\u88ab\u56de\u6eda\uff08Rollback\uff09\u5230\u4e8b\u52a1\u5f00\u59cb\u524d\u7684\u72b6\u6001\uff0c\u5c31\u50cf\u8fd9\u4e2a\u4e8b\u52a1\u4ece\u6765\u6ca1\u6709\u6267\u884c\u8fc7\u4e00\u6837\u3002<\/li>\n<li>**\u4e00\u81f4\u6027\uff1a**\u5728\u4e8b\u52a1\u5f00\u59cb\u4e4b\u524d\u548c\u4e8b\u52a1\u7ed3\u675f\u4ee5\u540e\uff0c\u6570\u636e\u5e93\u7684\u5b8c\u6574\u6027\u6ca1\u6709\u88ab\u7834\u574f\u3002\u8fd9\u8868\u793a\u5199\u5165\u7684\u8d44\u6599\u5fc5\u987b\u5b8c\u5168\u7b26\u5408\u6240\u6709\u7684\u9884\u8bbe\u89c4\u5219\uff0c\u8fd9\u5305\u542b\u8d44\u6599\u7684\u7cbe\u786e\u5ea6\u3001\u4e32\u8054\u6027\u4ee5\u53ca\u540e\u7eed\u6570\u636e\u5e93\u53ef\u4ee5\u81ea\u53d1\u6027\u5730\u5b8c\u6210\u9884\u5b9a\u7684\u5de5\u4f5c\u3002<\/li>\n<li>**\u9694\u79bb\u6027\uff1a**\u6570\u636e\u5e93\u5141\u8bb8\u591a\u4e2a\u5e76\u53d1\u4e8b\u52a1\u540c\u65f6\u5bf9\u5176\u6570\u636e\u8fdb\u884c\u8bfb\u5199\u548c\u4fee\u6539\u7684\u80fd\u529b\uff0c\u9694\u79bb\u6027\u53ef\u4ee5\u9632\u6b62\u591a\u4e2a\u4e8b\u52a1\u5e76\u53d1\u6267\u884c\u65f6\u7531\u4e8e\u4ea4\u53c9\u6267\u884c\u800c\u5bfc\u81f4\u6570\u636e\u7684\u4e0d\u4e00\u81f4\u3002\u4e8b\u52a1\u9694\u79bb\u5206\u4e3a\u4e0d\u540c\u7ea7\u522b\uff0c\u5305\u62ec\u8bfb\u672a\u63d0\u4ea4\uff08Read uncommitted\uff09\u3001\u8bfb\u63d0\u4ea4\uff08read committed\uff09\u3001\u53ef\u91cd\u590d\u8bfb\uff08repeatable read\uff09\u548c\u4e32\u884c\u5316\uff08Serializable\uff09\u3002<\/li>\n<li>**\u6301\u4e45\u6027\uff1a**\u4e8b\u52a1\u5904\u7406\u7ed3\u675f\u540e\uff0c\u5bf9\u6570\u636e\u7684\u4fee\u6539\u5c31\u662f\u6c38\u4e45\u7684\uff0c\u5373\u4fbf\u7cfb\u7edf\u6545\u969c\u4e5f\u4e0d\u4f1a\u4e22\u5931\u3002<\/li>\n<\/ul>\n<h3><a id=\"16-1%E4%BA%8B%E5%8A%A1%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-1%E4%BA%8B%E5%8A%A1%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5%EF%BC%9A\"><span class=\"octicon octicon-link\"><\/span><\/a>16.1 \u4e8b\u52a1\u63a7\u5236\u8bed\u53e5\uff1a<\/h3>\n<ul>\n<li>BEGIN\u6216START TRANSACTION\uff1b\u663e\u5f0f\u5730\u5f00\u542f\u4e00\u4e2a\u4e8b\u52a1\uff1b<\/li>\n<li>COMMIT\uff1b\u4e5f\u53ef\u4ee5\u4f7f\u7528COMMIT WORK\uff0c\u4e0d\u8fc7\u4e8c\u8005\u662f\u7b49\u4ef7\u7684\u3002COMMIT\u4f1a\u63d0\u4ea4\u4e8b\u52a1\uff0c\u5e76\u4f7f\u5df2\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u7684\u6240\u6709\u4fee\u6539\u6210\u4e3a\u6c38\u4e45\u6027\u7684\uff1b<\/li>\n<li>ROLLBACK\uff1b\u6709\u53ef\u4ee5\u4f7f\u7528ROLLBACK WORK\uff0c\u4e0d\u8fc7\u4e8c\u8005\u662f\u7b49\u4ef7\u7684\u3002\u56de\u6eda\u4f1a\u7ed3\u675f\u7528\u6237\u7684\u4e8b\u52a1\uff0c\u5e76\u64a4\u9500\u6b63\u5728\u8fdb\u884c\u7684\u6240\u6709\u672a\u63d0\u4ea4\u7684\u4fee\u6539\uff1b<\/li>\n<li>SAVEPOINT identifier\uff1bSAVEPOINT\u5141\u8bb8\u5728\u4e8b\u52a1\u4e2d\u521b\u5efa\u4e00\u4e2a\u4fdd\u5b58\u70b9\uff0c\u4e00\u4e2a\u4e8b\u52a1\u4e2d\u53ef\u4ee5\u6709\u591a\u4e2aSAVEPOINT\uff1b<\/li>\n<li>RELEASE SAVEPOINT identifier\uff1b\u5220\u9664\u4e00\u4e2a\u4e8b\u52a1\u7684\u4fdd\u5b58\u70b9\uff0c\u5f53\u6ca1\u6709\u6307\u5b9a\u7684\u4fdd\u5b58\u70b9\u65f6\uff0c\u6267\u884c\u8be5\u8bed\u53e5\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff1b<\/li>\n<li>ROLLBACK TO identifier\uff1b\u628a\u4e8b\u52a1\u56de\u6eda\u5230\u6807\u8bb0\u70b9\uff1b<\/li>\n<li>SET TRANSACTION\uff1b\u7528\u6765\u8bbe\u7f6e\u4e8b\u52a1\u7684\u9694\u79bb\u7ea7\u522b\u3002InnoDB\u5b58\u50a8\u5f15\u64ce\u63d0\u4f9b\u4e8b\u52a1\u7684\u9694\u79bb\u7ea7\u522b\u6709READ UNCOMMITTED\u3001READ COMMITTED\u3001REPEATABLE READ\u548cSERIALIZABLE\u3002<\/li>\n<\/ul>\n<h3><a id=\"16-2-mysql%E4%BA%8B%E5%8A%A1%E5%A4%84%E7%90%86%E4%B8%BB%E8%A6%81%E6%9C%89%E4%B8%A4%E7%A7%8D%E6%96%B9%E6%B3%95%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-2-mysql%E4%BA%8B%E5%8A%A1%E5%A4%84%E7%90%86%E4%B8%BB%E8%A6%81%E6%9C%89%E4%B8%A4%E7%A7%8D%E6%96%B9%E6%B3%95%EF%BC%9A\"><span class=\"octicon octicon-link\"><\/span><\/a>16.2 MYSQL \u4e8b\u52a1\u5904\u7406\u4e3b\u8981\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<\/h3>\n<p>1\u3001\u7528 BEGIN, ROLLBACK, COMMIT\u6765\u5b9e\u73b0<\/p>\n<ul>\n<li><strong>BEGIN<\/strong>\u00a0\u5f00\u59cb\u4e00\u4e2a\u4e8b\u52a1<\/li>\n<li><strong>ROLLBACK<\/strong>\u00a0\u4e8b\u52a1\u56de\u6eda<\/li>\n<li><strong>COMMIT<\/strong>\u00a0\u4e8b\u52a1\u786e\u8ba4<\/li>\n<\/ul>\n<p>2\u3001\u76f4\u63a5\u7528 SET \u6765\u6539\u53d8 MySQL \u7684\u81ea\u52a8\u63d0\u4ea4\u6a21\u5f0f:<\/p>\n<ul>\n<li><strong>SET AUTOCOMMIT=0<\/strong>\u00a0\u7981\u6b62\u81ea\u52a8\u63d0\u4ea4<\/li>\n<li><strong>SET AUTOCOMMIT=1<\/strong>\u00a0\u5f00\u542f\u81ea\u52a8\u63d0\u4ea4<\/li>\n<\/ul>\n<p>16.3\u3001\u4e8b\u52a1\u5e76\u53d1\u64cd\u4f5c\u51fa\u73b0\u51e0\u79cd\u95ee\u9898<\/p>\n<p>\u200b \u6240\u8c13\u4e8b\u52a1\uff0c\u662f\u7528\u6237\u5b9a\u4e49\u7684\u4e00\u4e2a\u6570\u636e\u5e93\u64cd\u4f5c\u5e8f\u5217\uff0c\u662f\u6570\u636e\u5e93\u73af\u5883\u4e2d\u7684\u903b\u8f91\u5de5\u4f5c\u5355\u5143\uff0c\u662f\u4e00\u4e2a\u4e0d\u53ef\u5206\u5272\u7684\u6574\u4f53\u3002<\/p>\n<p>\u4e8b\u52a1\u7684\u8fd9\u4e2a4\u4e2a\u7279\u6027\u7b80\u79f0\u4e3aACID\u7279\u6027\uff0c\u4e8b\u52a1ACID\u7279\u6027\u53ef\u80fd\u906d\u5230\u7834\u574f\u7684\u56e0\u7d20\u6709\uff1a<\/p>\n<p>\u2460\u591a\u4e2a\u4e8b\u52a1\u5e76\u53d1\u6267\u884c\uff0c\u4e0d\u540c\u4e8b\u52a1\u7684\u64cd\u4f5c\u4ea4\u53c9\u6267\u884c\uff1b<\/p>\n<p>\u2461\u4e8b\u52a1\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u88ab\u5f3a\u884c\u7ec8\u6b62\u3002<\/p>\n<p>\u5982\u4f55\u4fdd\u8bc1\u5728\u591a\u4e2a\u4e8b\u52a1\u5e76\u53d1\u6267\u884c\u7684\u8fc7\u7a0b\u4e2d\u4e0d\u53d1\u751f\u4e0a\u8ff0\u7684\u4e24\u79cd\u60c5\u51b5\uff0c\u662f\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u5e76\u53d1\u63a7\u5236\u7684\u4e3b\u8981\u8d23\u4efb\u3002<\/p>\n<h3><a id=\"16-4%E3%80%81%E4%B8%A2%E5%A4%B1%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-4%E3%80%81%E4%B8%A2%E5%A4%B1%E4%BF%AE%E6%94%B9%E6%95%B0%E6%8D%AE\"><span class=\"octicon octicon-link\"><\/span><\/a>16.4\u3001\u4e22\u5931\u4fee\u6539\u6570\u636e<\/h3>\n<p>\u4e3e\u4f8b\uff1a\u94f6\u884c\u5361\u6709100\u5143\uff0c\u4e8b\u52a1A\u53d610\u5143\uff0c\u4e8b\u52a1B\u53d610\u5143\uff0c\u4e8b\u52a1AB\u4e24\u4eba\u540c\u65f6\u53d6\u94b1\uff0c\u521d\u59cb\u503c\u90fd\u662f100<\/p>\n<h4><a id=\"16-4-1%E3%80%81%E8%AF%BB%E2%80%9C%E8%84%8F%E2%80%9D%E6%95%B0%E6%8D%AE\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-4-1%E3%80%81%E8%AF%BB%E2%80%9C%E8%84%8F%E2%80%9D%E6%95%B0%E6%8D%AE\"><span class=\"octicon octicon-link\"><\/span><\/a>16.4.1\u3001\u8bfb\u201c\u810f\u201d\u6570\u636e<\/h4>\n<p>\u6570\u636e\u5e93\u6280\u672f\u4e2d\uff0c\u5982\u679c\u6b63\u5e38\u63d0\u4ea4\u7684\u4e8b\u52a1A\u4f7f\u7528\u4e86\u4e8b\u52a1B\u672a\u63d0\u4ea4\u7684\u64a4\u9500\u6570\u636e\uff0c\u8fd9\u79cd\u6570\u636e\u6210\u4e3a\u201c\u810f\u6570\u636e\u201d\uff0c\u4f1a\u9020\u6210\u6570\u636e\u7684\u810f\u8bfb\u548c\u6c61\u8bfb\u3002<\/p>\n<h4><a id=\"16-4-2%E3%80%81%E4%B8%8D%E4%B8%80%E8%87%B4%E5%88%86%E6%9E%90\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-4-2%E3%80%81%E4%B8%8D%E4%B8%80%E8%87%B4%E5%88%86%E6%9E%90\"><span class=\"octicon octicon-link\"><\/span><\/a>16.4.2\u3001\u4e0d\u4e00\u81f4\u5206\u6790<\/h4>\n<p>\u9020\u6210\u8fd9\u79cd\u6570\u636e\u4e0d\u4e00\u81f4\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5e76\u53d1\u6267\u884c\u7684\u4e24\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u4e00\u4e2a\u4e8b\u52a1\u5728\u8bfb\u53d6\u6570\u636e\u65f6\uff0c\u53e6\u4e00\u4e2a\u4e8b\u52a1\u6b63\u5728\u4fee\u6539\u540c\u4e00\u4e2a\u6570\u636e\u3002\u8fd9\u6837\u5c31\u53ef\u80fd\u5bfc\u81f4\u4e24\u4e2a\u4e8b\u52a1\u7684\u76f8\u4e92\u5e72\u6270\u53ca\u201c\u8bfb\u201d\u4e8b\u52a1\u7684\u9519\u8bef\u6267\u884c\u7ed3\u679c\u3002<\/p>\n<h3><a id=\"16-5%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93%E5%B9%B6%E5%8F%91%E7%9A%84%E6%8E%A7%E5%88%B6%EF%BC%88%E4%BA%86%E8%A7%A3%E9%83%A8%E5%88%86%EF%BC%89\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-5%E3%80%81%E6%95%B0%E6%8D%AE%E5%BA%93%E5%B9%B6%E5%8F%91%E7%9A%84%E6%8E%A7%E5%88%B6%EF%BC%88%E4%BA%86%E8%A7%A3%E9%83%A8%E5%88%86%EF%BC%89\"><span class=\"octicon octicon-link\"><\/span><\/a>16.5\u3001\u6570\u636e\u5e93\u5e76\u53d1\u7684\u63a7\u5236\uff08\u4e86\u89e3\u90e8\u5206\uff09<\/h3>\n<h4><a id=\"16-5-1%E3%80%81%E5%B9%B6%E5%8F%91%E8%B0%83%E5%BA%A6%E7%9A%84%E5%8F%AF%E4%B8%B2%E8%A1%8C%E5%8C%96\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-5-1%E3%80%81%E5%B9%B6%E5%8F%91%E8%B0%83%E5%BA%A6%E7%9A%84%E5%8F%AF%E4%B8%B2%E8%A1%8C%E5%8C%96\"><span class=\"octicon octicon-link\"><\/span><\/a>16.5.1\u3001\u5e76\u53d1\u8c03\u5ea6\u7684\u53ef\u4e32\u884c\u5316<\/h4>\n<p>\u53ef\u4e32\u884c\u5316\u51c6\u5219\uff1a\u591a\u4e2a\u4e8b\u52a1\u7684\u5e76\u53d1\u6267\u884c\u65f6\u6b63\u786e\u7684\uff0c\u5f53\u4e14\u4ec5\u5f53\u5176\u7ed3\u679c\u6309\u67d0\u4e00\u6b21\u5e8f\u4e32\u884c\u6267\u884c\u5b83\u4eec\u65f6\u7684\u7ed3\u679c\u76f8\u540c\uff0c\u8fd9\u79cd\u8c03\u5ea6\u7b56\u7565\u79f0\u4e3a\u53ef\u4e32\u884c\u5316\u8c03\u5ea6\u3002\u53ef\u4e32\u884c\u5316\u662f\u5e76\u53d1\u4e8b\u52a1\u6b63\u786e\u6027\u7684\u51c6\u5219\uff0c\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5e76\u53d1\u8c03\u5ea6\uff0c\u5f53\u4e14\u4ec5\u5f53\u5b83\u662f\u53ef\u4e32\u884c\u5316\u7684\uff0c\u624d\u8ba4\u4e3a\u662f\u6b63\u786e\u7684\u3002<\/p>\n<h4><a id=\"16-5-2%E3%80%81%E5%B0%81%E9%94%81\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-5-2%E3%80%81%E5%B0%81%E9%94%81\"><span class=\"octicon octicon-link\"><\/span><\/a>16.5.2\u3001\u5c01\u9501<\/h4>\n<p>\u200b \u5c01\u9501\u662f\u5b9e\u73b0\u5e76\u53d1\u63a7\u5236\u7684\u975e\u5e38\u91cd\u8981\u7684\u6280\u672f\u3002\u5c01\u9501\u662f\u6307\u67d0\u4e8b\u52a1\u5728\u5bf9\u67d0\u6570\u636e\u5bf9\u8c61\u8fdb\u884c\u64cd\u4f5c\u4ee5\u524d\uff0c\u5148\u8bf7\u6c42\u7cfb\u7edf\u5bf9\u5176\u52a0\u9501\uff0c\u6210\u529f\u52a0\u9501\u4e4b\u540e\u8be5\u4e8b\u52a1\u5c31\u5bf9\u8be5\u6570\u636e\u5bf9\u8c61\u6709\u4e86\u63a7\u5236\u6743\uff0c\u53ea\u6709\u8be5\u4e8b\u52a1\u5bf9\u5176\u8fdb\u884c\u89e3\u9501\u4e4b\u540e\uff0c\u5176\u4ed6\u7684\u4e8b\u52a1\u624d\u80fd\u66f4\u65b0\u5b83\uff0cDBMS\u6709\u4e24\u79cd\u9501\uff1a<\/p>\n<p>\u2460\u00a0 \u6392\u5b83\u9501\uff08\u4e5f\u79f0\u4f5cX\u9501\uff09 \uff1a \u53ef\u8bfb\u53ef\u5199<\/p>\n<p>\u200b \u5982\u679c\u4e8b\u52a1T\u5728\u5bf9\u67d0\u4e2a\u6570\u636e\u5bf9\u8c61\u5b9e\u65bd\u4e86X\u9501\uff0c\u90a3\u4e48\u5176\u4ed6\u7684\u4e8b\u52a1\u5fc5\u987b\u8981\u7b49\u5230T\u4e8b\u52a1\u63a5\u89e6\u5bf9\u8be5\u6570\u636e\u5bf9\u8c61\u7684X\u9501\u4e4b\u540e\uff0c\u624d\u80fd\u5bf9\u8fd9\u4e2a\u6570\u636e\u8fdb\u884c\u52a0\u9501\u3002<\/p>\n<p>\u2461\u00a0 \u5171\u4eab\u9501\uff08\u4e5f\u79f0\u4f5cS\u9501\uff09 \uff1a\u53ea\u80fd\u8fdb\u884c\u8bfb\u53d6\u5de5\u4f5c<\/p>\n<p>\u200b \u5982\u679c\u4e8b\u52a1T\u5728\u5bf9\u67d0\u4e2a\u6570\u636e\u5bf9\u8c61\u5b9e\u65bd\u4e86S\u9501\uff0c\u90a3\u4e48\u5176\u4ed6\u7684\u4e8b\u52a1\u4e5f\u80fd\u5bf9\u8be5\u6570\u636e\u5bf9\u8c61\u5b9e\u65bdS\u9501\uff0c\u4f46\u662f\u5bf9\u8fd9\u4e2a\u6570\u636e\u5bf9\u8c61\u65bd\u52a0\u7684\u6240\u6709S\u9501\u90fd\u91ca\u653e\u4e4b\u524d\u4e0d\u5141\u8bb8\u4efb\u4f55\u4e8b\u52a1\u5bf9\u8be5\u6570\u636e\u5bf9\u8c61\u5b9e\u65bdX\u9501\u3002<\/p>\n<h4><a id=\"16-5-3%E3%80%81%E6%AD%BB%E9%94%81\" class=\"anchor\" aria-hidden=\"true\" href=\"#16-5-3%E3%80%81%E6%AD%BB%E9%94%81\"><span class=\"octicon octicon-link\"><\/span><\/a>16.5.3\u3001\u6b7b\u9501<\/h4>\n<p>\u5c01\u9501\u6280\u672f\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u5e76\u53d1\u64cd\u4f5c\u5f15\u8d77\u7684\u4e0d\u4e00\u81f4\u9519\u8bef\uff0c\u4f46\u4e5f\u4f1a\u4ea7\u751f\u5176\u4ed6\u7684\u4e00\u4e9b\u95ee\u9898\uff0c\u6d3b\u9501\u548c\u6b7b\u9501\u3002<\/p>\n<p>\u2460\u00a0 \u6d3b\u9501<\/p>\n<p>\u5982\u679c\u67d0\u4e2a\u4e8b\u52a1\u5904\u5728\u6c38\u8fdc\u7b49\u5f85\u7684\u72b6\u6001\uff0c\u5f97\u4e0d\u5230\u5c01\u9501\u7684\u673a\u4f1a\uff0c\u8fd9\u79cd\u73b0\u8c61\u4e3a\u6d3b\u9501\uff0c\u907f\u514d\u8fd9\u79cd\u9501\u6700\u597d\u7684\u65b9\u6cd5\u5c31\u662f\u91c7\u7528\u5148\u6765\u5148\u670d\u52a1\u7684\u7b56\u7565\u3002<\/p>\n<p>\u2461\u00a0 \u6b7b\u9501<\/p>\n<p>\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u4e8b\u52a1\u90fd\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u6bcf\u4e2a\u4e8b\u52a1\u90fd\u5728\u7b49\u5f85\u5bf9\u65b9\u4e8b\u52a1\u63a5\u89e6\u5c01\u9501\uff0c\u5b83\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u4e0b\u53bb\uff0c\u8fd9\u6837\u4efb\u4f55\u4e8b\u52a1\u90fd\u5904\u4e8e\u7b49\u5f85\u72b6\u6001\u800c\u65e0\u6cd5\u7ee7\u7eed\u6267\u884c\u7684\u73b0\u8c61\u79f0\u4e3a\u6b7b\u9501<\/p>\n<p>\u89e3\u51b3\u6b7b\u9501\u95ee\u9898\u65b9\u6cd5\u6709\u4e24\u7c7b\uff1a<\/p>\n<p>A\u3001 \u6b7b\u9501\u7684\u9884\u9632<\/p>\n<p>B\u3001 \u6b7b\u9501\u7684\u8bca\u65ad\u4e0e\u9884\u9632<\/p>\n<h2><a id=\"17%E6%95%B0%E6%8D%AE%E5%BA%93%E7%BC%96%E7%A0%81\" class=\"anchor\" aria-hidden=\"true\" href=\"#17%E6%95%B0%E6%8D%AE%E5%BA%93%E7%BC%96%E7%A0%81\"><span class=\"octicon octicon-link\"><\/span><\/a>17. \u6570\u636e\u5e93\u7f16\u7801<\/h2>\n<ul>\n<li>\n<ol>\n<li>\u67e5\u770bmysql\u7f16\u7801<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<pre><code>mysql&gt; show variables like 'character%';\n+--------------------------+----------------------------+\n| Variable_name            | Value                      |\n+--------------------------+----------------------------+\n| character_set_client     | utf8                       |\n| character_set_connection | utf8                       |\n| character_set_database   | latin1                     |\n| character_set_filesystem | binary                     |\n| character_set_results    | utf8                       |\n| character_set_server     | latin1                     |\n| character_set_system     | utf8                       |\n| character_sets_dir       | \/usr\/share\/mysql\/charsets\/ |\n+--------------------------+----------------------------+\n<\/code><\/pre>\n<ul>\n<li>s2) \u8bbe\u7f6emysql\u7f16\u7801<\/li>\n<\/ul>\n<pre><code># vi \/etc\/my.cnf\n\u5982\u4e0b(\u5c11\u8865)\uff1a\n\n[mysqld]\ncharacter-set-server=utf8 \ncollation-server=utf8_general_ci \nsql_mode='NO_ENGINE_SUBSTITUTION'\n\n[mysql]\ndefault-character-set=utf8\n\n\u91cd\u542fmysql\n\n# service mysql restart\n\u518d\u6b21\u67e5\u770b\u7f16\u7801\uff1a\n\n# mysql -uroot -p\nmysql&gt; show variables like 'character%';\n+--------------------------+----------------------------+\n| Variable_name | Value |\n+--------------------------+----------------------------+\n| character_set_client | utf8 |\n| character_set_connection | utf8 |\n| character_set_database | utf8 |\n| character_set_filesystem | binary |\n| character_set_results | utf8 |\n| character_set_server | utf8 |\n| character_set_system | utf8 |\n| character_sets_dir | \/usr\/share\/mysql\/charsets\/ |\n+--------------------------+----------------------------+\n8 rows in set (0.00 sec)\n<\/code><\/pre>\n<h2><a id=\"18-mysql%E7%9A%84%E7%9B%AE%E5%BD%95%E5%8F%8A%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6\" class=\"anchor\" aria-hidden=\"true\" href=\"#18-mysql%E7%9A%84%E7%9B%AE%E5%BD%95%E5%8F%8A%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6\"><span class=\"octicon octicon-link\"><\/span><\/a>18. mysql \u7684\u76ee\u5f55\u53ca\u914d\u7f6e\u6587\u4ef6<\/h2>\n<ul>\n<li>\n<ol>\n<li>\/etc\/my.cnf : \u8fd9\u662fmysql\u7684\u4e3b\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>\/var\/lib\/mysql : mysql\u4e2d\u6570\u636e\u5e93\u53ca\u5176\u6570\u636e\u5b58\u50a8\u7684\u76ee\u5f55<\/li>\n<\/ol>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qn.fivedata.cn\/1554735026813.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/qn.fivedata.cn\/1554735026813.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>\/var\/log\/mysql(\u6216\u8005\u76f4\u63a5\u5728\/var\/lib\/mysql) : \u6570\u636e\u5e93\u7684\u65e5\u5fd7\u8f93\u51fa\u5b58\u653e\u4f4d\u7f6e<\/li>\n<\/ol>\n<\/li>\n<li>\n<ol start=\"4\">\n<li>\u67e5\u770b\u7aef\u53e3\u3002netstat -nltp \u770b\u662f\u5426\u80fd\u627e\u52303306\u7684\u7aef\u53e3<\/li>\n<\/ol>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/qn.fivedata.cn\/1554735467166.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/qn.fivedata.cn\/1554735467166.png?imageView2\/0\/q\/75%7Cwatermark\/2\/text\/5YWs5LyX5Y-377ya5LqU5YiG6ZKf5a2m5aSn5pWw5o2u\/font\/5b6u6L2v6ZuF6buR\/fontsize\/280\/fill\/I0M3MDgwOA==\/dissolve\/100\/gravity\/NorthWest\/dx\/10\/dy\/10\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<\/li>\n<\/ul>\n<h2><a id=\"19%E6%95%B0%E6%8D%AE%E5%BA%93%E5%B8%B8%E7%94%A8%E6%9C%AF%E8%AF%AD\" class=\"anchor\" aria-hidden=\"true\" href=\"#19%E6%95%B0%E6%8D%AE%E5%BA%93%E5%B8%B8%E7%94%A8%E6%9C%AF%E8%AF%AD\"><span class=\"octicon octicon-link\"><\/span><\/a>19. \u6570\u636e\u5e93\u5e38\u7528\u672f\u8bed<\/h2>\n<h3><a id=\"19-1%E6%95%B0%E6%8D%AE%E6%A8%A1%E5%9E%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#19-1%E6%95%B0%E6%8D%AE%E6%A8%A1%E5%9E%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>19.1 \u6570\u636e\u6a21\u578b<\/h3>\n<p>\u200b \u6570\u636e\u6a21\u578b\uff08Data model\uff09\u662f\u6570\u636e\u5e93\u7ed3\u6784\u7684\u57fa\u7840\uff0c\u662f\u7528\u6765\u63cf\u8ff0\u6570\u636e\u7684\u4e00\u7ec4\u6982\u5ff5\u548c\u5b9a\u4e49\uff0c\u6570\u636e\u6a21\u578b\u4e3b\u8981\u6709\u4e09\u4e2a\u8981\u7d20\uff1a\u6570\u636e\u7ed3\u6784\u3001\u6570\u636e\u64cd\u4f5c\u3001\u6570\u636e\u7ea6\u675f\u6761\u4ef6\u3002<\/p>\n<p>\u6570\u636e\u7ed3\u6784\uff1a\u5bf9\u8c61\u7c7b\u578b\u7684\u96c6\u5408\uff0c\u662f\u5bf9\u9759\u6001\u5c5e\u6027\u7684\u63cf\u8ff0\u3002<\/p>\n<p>\u6570\u636e\u64cd\u4f5c\uff1a\u662f\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u5404\u79cd\u5bf9\u8c61\u6027\u6570\u636e\uff0c\u5141\u8bb8\u6267\u884c\u7684\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u5982\u589e\u5220\u6539\u67e5\u7b49\uff1b\u6570\u636e\u64cd\u4f5c\u662f\u5bf9\u7cfb\u7edf\u52a8\u6001\u70ed\u6027\u7684\u63cf\u8ff0\u3002<\/p>\n<p>\u6570\u636e\u7684\u7ea6\u675f\u6761\u4ef6\uff1a\u662f\u4e00\u7ec4\u5b8c\u6574\u6027\u89c4\u5219\u7684\u96c6\u5408\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5bf9\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u5fc5\u987b\u5148\u9075\u5faa\u7279\u5b9a\u7684\u8bed\u4e49\u7ea6\u675f\u6761\u4ef6\u3002\u6bd4\u5982\uff1a\u6027\u522b\u53ea\u80fd\u53d6\u00a0 \u201c\u7537\u201d\u6216\u8005\u201c\u5973\u201d\u4e2d\u7684\u4e4b\u4e00\u3002\u8003\u8bd5\u6210\u7ee9\uff1a\uff08\u6ee1\u5206100\uff09\u53ea\u80fd\u662f0-100\u7684\u6570\u503c\u3002<\/p>\n<h3><a id=\"19-2%E6%95%B0%E6%8D%AE%E5%BA%93\" class=\"anchor\" aria-hidden=\"true\" href=\"#19-2%E6%95%B0%E6%8D%AE%E5%BA%93\"><span class=\"octicon octicon-link\"><\/span><\/a>19.2 \u6570\u636e\u5e93<\/h3>\n<p>\u200b \u6570\u636e\u5e93\uff08database\uff09\u662f\u957f\u671f\u5b58\u50a8\u5728\u8ba1\u7b97\u673a\u5916\u5b58\u4e0a\u6709\u7ed3\u6784\uff0c\u53ef\u5171\u4eab\u7684\u6570\u636e\u96c6\u5408\uff1b\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u6309\u7167\u4e00\u5b9a\u7684\u6570\u636e\u6a21\u578b\u63cf\u8ff0\u3001\u7ec4\u7ec7\u548c\u5b58\u50a8\uff0c\u5177\u6709\u8f83\u5c0f\u7684\u5197\u4f59\u5ea6\uff0c\u8f83\u9ad8\u7684\u6570\u636e\u72ec\u7acb\u6027\u548c\u53ef\u6269\u5c55\u6027\uff0c\u5e76\u53ef\u4ee5\u4e3a\u591a\u4e2a\u7528\u6237\u5171\u4eab\u3002<\/p>\n<p>\u5e38\u89c1\u6570\u636e\u5e93\u5206\u7c7b\uff1a<\/p>\n<p>\u200b \u5173\u7cfb\u578b\u6570\u636e\u5e93\uff1a\u5982\uff1aMySQL\uff0coracle\uff0csqlserver<\/p>\n<p>\u200b \u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff1a\u5982\uff1aredis\uff0chbase\u7b49<\/p>\n<h3><a id=\"19-3%E6%95%B0%E6%8D%AE%E5%BA%93%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F\" class=\"anchor\" aria-hidden=\"true\" href=\"#19-3%E6%95%B0%E6%8D%AE%E5%BA%93%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F\"><span class=\"octicon octicon-link\"><\/span><\/a>19.3 \u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf<\/h3>\n<p>\u200b \u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff08database managerment system\uff0c DBMS\uff09\u662f\u6307\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u7ba1\u7406\u7684\u8f6f\u4ef6\u7cfb\u7edf\uff0c\u662f\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u6838\u5fc3\u7ec4\u6210\u90e8\u5206\u3002\u6570\u636e\u5e93\u7684\u4e00\u5207\u64cd\u4f5c\uff0c\u5982\u589e\u5220\u6539\u67e5\u4ee5\u53ca\u5404\u79cd\u63a7\u5236\uff0c\u90fd\u662f\u901a\u8fc7DBMS\u8fdb\u884c\u7684\u3002<\/p>\n<p>\u5177\u6709\u4ee5\u4e0b4\u4e2a\u57fa\u672c\u7684\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\n<p>1\u3001\u6570\u636e\u5b9a\u4e49\u529f\u80fd<\/p>\n<p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7DBMS\u63d0\u4f9b\u7684\u6570\u636e\u5b9a\u4e49\u8bed\u8a00\u5bf9\u6570\u636e\u5e93\u7684\u6570\u636e\u8fdb\u884c\u5b9a\u4e49\u3002<\/p>\n<\/li>\n<li>\n<p>2\u3001\u6570\u636e\u64cd\u7eb5\u529f\u80fd<\/p>\n<p>\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u64cd\u7eb5\u8bed\u8a00\u5b9e\u73b0\u5bf9\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\u64cd\u4f5c<\/p>\n<\/li>\n<li>\n<p>3\u3001\u6570\u636e\u5e93\u8fd0\u884c\u7ba1\u7406<\/p>\n<p>\u7ba1\u7406\u6570\u636e\u5e93\u7684\u8fd0\u884c\u662fDBMS\u8fd0\u884c\u65f6\u7684\u6838\u5fc3\u5de5\u4f5c\u3002\u6240\u6709\u8bbf\u95ee\u6570\u636e\u5e93\u7684\u64cd\u4f5c\u90fd\u8981\u5728DBMS\u7684\u7edf\u4e00\u7ba1\u7406\u4e0b\u8fdb\u884c\uff0c\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u3001\u5b8c\u6574\u6027\u3001\u4e00\u81f4\u6027\u4ee5\u53ca\u591a\u7528\u6237\u5bf9\u6570\u636e\u5e93\u7684\u5e76\u53d1\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p>4\u3001\u6570\u636e\u5e93\u7684\u5efa\u7acb\u548c\u7ef4\u62a4<\/p>\n<p>\u5efa\u7acb\u6570\u636e\u5e93\uff0c\u5305\u62ec\u6570\u636e\u5e93\u521d\u59cb\u6570\u636e\u7684\u8f93\u5165\u4e0e\u6570\u636e\u8f6c\u6362\u7b49\u3002\u7ef4\u62a4\u6570\u636e\u5e93\uff0c\u5305\u62ec\u6570\u636e\u5e93\u7684\u8f6c\u50a8\u4e0e\u6062\u590d\uff0c\u6570\u636e\u5e93\u7684\u91cd\u7ec4\u7ec7\uff0c\u6027\u80fd\u76d1\u63a7\u548c\u5206\u6790\u3002<\/p>\n<\/li>\n<\/ul>\n<h3><a id=\"19-4%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B3%BB%E7%BB%9F%E7%9B%B8%E5%85%B3%E7%AE%A1%E7%90%86%E4%BA%BA%E5%91%98\" class=\"anchor\" aria-hidden=\"true\" href=\"#19-4%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B3%BB%E7%BB%9F%E7%9B%B8%E5%85%B3%E7%AE%A1%E7%90%86%E4%BA%BA%E5%91%98\"><span class=\"octicon octicon-link\"><\/span><\/a>19.4 \u6570\u636e\u5e93\u7cfb\u7edf\u76f8\u5173\u7ba1\u7406\u4eba\u5458<\/h3>\n<p>\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u76f8\u5173\u4eba\u5458\u662f\u6570\u636e\u5e93\u7cfb\u7edf\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u5177\u4f53\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u7684\u4e09\u7c7b\u4eba\u5458<\/p>\n<ul>\n<li>\n<p>1\u3001\u6570\u636e\u5e93\u7ba1\u7406\u5458(DBA)<\/p>\n<p>\u804c\u8d23\uff1a\u8d1f\u8d23\u6570\u636e\u5e93\u7684\u5efa\u7acb\u3001\u4f7f\u7528\u3001\u7ef4\u62a4\u7684\u4e13\u95e8\u4eba\u5458<\/p>\n<\/li>\n<li>\n<p>2\u3001\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u4eba\u5458<\/p>\n<p>\u804c\u8d23\uff1a\u5f00\u53d1\u6570\u636e\u5e93\u5e94\u7528\u7a0b\u5e8f\u7684\u4eba\u5458\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u7684\u6240\u6709\u529f\u80fd\u3002<\/p>\n<\/li>\n<li>\n<p>3\u3001\u6700\u7ec8\u7528\u6237<\/p>\n<p>\u804c\u8d23\uff1a\u4e00\u822c\u6765\u8bf4\uff0c\u662f\u901a\u8fc7\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u6570\u636e\u5e93\u7684\u4eba\u5458\uff0c\u6700\u7ec8\u7528\u6237\u65e0\u9700\u81ea\u5df1\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<\/li>\n<\/ul>\n<h3><a id=\"19-5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B3%BB%E7%BB%9F\" class=\"anchor\" aria-hidden=\"true\" href=\"#19-5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%B3%BB%E7%BB%9F\"><span class=\"octicon octicon-link\"><\/span><\/a>19.5 \u6570\u636e\u5e93\u7cfb\u7edf<\/h3>\n<p>\u200b \u6570\u636e\u5e93\u7cfb\u7edf\uff08database system DBS\uff09\u662f\u7531\u786c\u4ef6\u7cfb\u7edf\uff0c\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u5e94\u7528\u7a0b\u5e8f\uff0c\u6570\u636e\u5e93\u7cfb\u7edf\u76f8\u5173\u4eba\u5458\u6784\u6210\u7684\u4eba-\u673a\u7cfb\u7edf\uff0c\u662f\u6307\u6709\u6570\u636e\u5e93\u7684\u6574\u4e2a\u8ba1\u7b97\u673a\u7cfb\u7edf\u3002<\/p>\n<p>\u8bf4\u660e\uff1a\u5728\u8bb8\u591a\u573a\u5408\u4e0b\uff0c\u6570\u636e\u5e93\uff0c\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\uff0c\u6570\u636e\u5e93\u7cfb\u7edf\u4e0d\u505a\u4e25\u683c\u533a\u5206\uff1b<\/p>\n<h2><a id=\"20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1%E6%B5%81%E7%A8%8B\" class=\"anchor\" aria-hidden=\"true\" href=\"#20%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1%E6%B5%81%E7%A8%8B\"><span class=\"octicon octicon-link\"><\/span><\/a>20 \u6570\u636e\u5e93\u8bbe\u8ba1\u6d41\u7a0b<\/h2>\n<pre><code>\u4e00\u822c\u5bf9\u4e8e\u6574\u4e2a\u6570\u636e\u5e93\u7684\u8bbe\u7f6e\u5206\u4e3a5\u5927\u90e8\u5206\n<\/code><\/pre>\n<ul>\n<li>\n<ol>\n<li>\u9700\u6c42\u5206\u6790:<\/li>\n<\/ol>\n<ul>\n<li>\u660e\u786e\u7528\u6237\u9700\u6c42\uff0c\u5230\u5e95\u505a\u4ec0\u4e48\uff1f<\/li>\n<\/ul>\n<\/li>\n<li>\n<ol start=\"2\">\n<li>\u6982\u5ff5\u6a21\u578b\u8bbe\u8ba1:<\/li>\n<\/ol>\n<ul>\n<li>\n<p>\u8be5\u9636\u6bb5\u662f\u6574\u4e2a\u6570\u636e\u5e93\u8bbe\u8ba1\u7684\u5173\u952e\uff0c\u5b83\u901a\u8fc7\u5bf9\u7528\u6237\u9700\u6c42\u8fdb\u884c\u7efc\u5408\u3001\u5f52\u7eb3\u4e0e\u62bd\u8c61\u3002\u4e3b\u8981\u662f\u901a\u8fc7E-R\u56fe\u8868\u793a<\/p>\n<ul>\n<li>\u4f18\u70b9:\n<ul>\n<li>\u7b80\u6d01\u660e\u4e86, \u63cf\u8ff0\u4e86\u5404\u4e2a\u4e4b\u95f4\u7684\u903b\u8f91\u5173\u7cfb<\/li>\n<li>\u72ec\u7acb\u4e8e\u8ba1\u7b97\u673a\u4e0e\u5177\u4f53\u7684RDBMS\u65e0\u5173\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code>E-R\u6a21\u578b\u7684\u57fa\u672c\u5143\u7d20:\n\nA\u3001\u5b9e\u4f53\uff08Entity\uff09 \u5982\uff1a\u5b66\u751f\n\nB\u3001\u5c5e\u6027\uff08attribute\uff09\u5982\uff1a\u59d3\u540d\n\nC\u3001\u952e\u7801\uff08key\uff09\u5982\uff1a\u8eab\u4efd\u8bc1\u53f7\u7801\uff1b\n\nD\u3001\u5173\u7cfb\uff08relationship\uff09\u5982\uff1a\u4e24\u4e2a\u5b9e\u4f53\u4e4b\u95f4\u7684\u5173\u7cfb\n\n\ta)\u00a0\u00a0\u4e00\u5bf9\u4e00\uff081:1\uff09\uff1a \u4e00\u4e2a\u4eba\u4e00\u4e2a\u8eab\u4efd\u8bc1\u53f7\u7801\uff1b\u4e00\u4e2a\u5b66\u6821\u4e00\u4e2a\u6821\u957f\n\n\tb)\u00a0\u00a0\u4e00\u5bf9\u591a\uff081\uff1an\uff09\uff1a\u5b66\u6821\u548c\u8001\u5e08\u7684\u5173\u7cfb\n\n\tc)\u00a0\u00a0\u591a\u5bf9\u591a\uff08n\uff1an\uff09\uff1a\u5b66\u751f\u9009\u8bfe\uff0c\u4e00\u4e2a\u5b66\u751f\u53ef\u4ee5\u9009\u62e9\u591a\u95e8\u8bfe\u7a0b\uff0c\u4e00\u95e8\u8bfe\u7a0b\u8bfe\u88ab\u591a\u540d\u5b66\u751f\u8fdb\u884c\u9009\u4fee\u3002\n\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<ol start=\"3\">\n<li>\u903b\u8f91\u6a21\u578b\u8bbe\u8ba1: \u8be5\u9636\u6bb5\u4f1a\u6d89\u53ca\u5230\u66f4\u591a\u7684\u6982\u5ff5\uff0c\u65b9\u6cd5\uff0c\u7406\u8bba\u3002<\/li>\n<\/ol>\n<ul>\n<li>\u4e0e\u5177\u4f53\u7684\u6570\u636e\u5e93\u76f8\u5173,\u53cd\u6620\u51fa\u4e1a\u52a1\u90e8\u95e8\u7684\u9700\u6c42<\/li>\n<li>\u89c4\u8303\u5316\u5904\u7406\uff0c\u5c3d\u53ef\u80fd\u7684\u6d88\u9664\u5173\u7cfb\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u7684\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<ol start=\"4\">\n<li>\u6570\u636e\u5e93\u5b9e\u65bd:<\/li>\n<\/ol>\n<ul>\n<li>\u521b\u5efa\u6570\u636e\u5e93\uff0c\u5b9a\u4e49\u6570\u636e\u5e93\u7ed3\u6784\uff0c\u7ec4\u7ec7\u6570\u636e\u5165\u5e93\uff0c\u8c03\u8bd5\u6570\u636e\u5e93\u5e76\u8fdb\u884c\u6570\u636e\u5e93\u7684\u8bd5\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<ol start=\"5\">\n<li>\u6570\u636e\u5e93\u7684\u8fd0\u884c\u548c\u7ef4\u62a4:<\/li>\n<\/ol>\n<ul>\n<li>\u6570\u636e\u5e93\u6b63\u5f0f\u8fd0\u884c\u4e4b\u540e\uff0c\u5bf9\u6570\u636e\u5e93\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u5176\u8fdb\u884c\u8bc4\u4ef7\uff0c\u8c03\u6574\uff0c\u4fee\u6539\uff0c\u8c03\u4f18\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2><a id=\"21%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1%E9%81%B5%E5%BE%AA%E7%9A%84%E5%8E%9F%E5%88%99\" class=\"anchor\" aria-hidden=\"true\" href=\"#21%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1%E9%81%B5%E5%BE%AA%E7%9A%84%E5%8E%9F%E5%88%99\"><span class=\"octicon octicon-link\"><\/span><\/a>21 \u6570\u636e\u5e93\u8bbe\u8ba1\u9075\u5faa\u7684\u539f\u5219<\/h2>\n<h3><a id=\"21-1%E8%8C%83%E5%BC%8F%E6%A6%82%E5%BF%B5\" class=\"anchor\" aria-hidden=\"true\" href=\"#21-1%E8%8C%83%E5%BC%8F%E6%A6%82%E5%BF%B5\"><span class=\"octicon octicon-link\"><\/span><\/a>21.1 \u8303\u5f0f\u6982\u5ff5<\/h3>\n<p>\u6982\u5ff5\uff1a\u8303\u5f0f\u5c31\u662f\u7b26\u5408\u67d0\u4e00\u89c4\u8303\u7ea7\u522b\u7684\u5173\u7cfb\u6a21\u5f0f\u7684\u96c6\u5408\u3002\u5171\u67097\u79cd\u8303\u5f0f\uff1a<\/p>\n<p>1NF \u22832NF\u22833NF\u2283BCNF\u22834NF\u22835NF\u22836NF<\/p>\n<h3><a id=\"21-2%E7%AC%AC%E4%B8%80%E8%8C%83%E5%BC%8F-first-normal-form\" class=\"anchor\" aria-hidden=\"true\" href=\"#21-2%E7%AC%AC%E4%B8%80%E8%8C%83%E5%BC%8F-first-normal-form\"><span class=\"octicon octicon-link\"><\/span><\/a>21.2 \u7b2c\u4e00\u8303\u5f0f(First Normal Form)<\/h3>\n<p>\u5982\u679c\u4e00\u4e2a\u5173\u7cfb\u6a21\u5f0fR\u7684\u6240\u6709\u5c5e\u6027\u90fd\u662f\u4e0d\u53ef\u5206\u5272\u7684\u57fa\u672c\u6570\u636e\u9879\uff0c\u5219\u8fd9\u4e2a\u5173\u7cfb\u5c5e\u4e8e\u7b2c\u4e00\u8303\u5f0f\u3002<\/p>\n<p>\u4e3e\u4f8b\u8bf4\u660e\uff1a(\u5b66\u751f\u9009\u8bfe\uff1a\u5b66\u53f7\uff0c\u59d3\u540d\uff0c\u7cfb\u522b\uff0c\u7cfb\u90e8\u5730\u5740\uff0c\u8bfe\u7a0b\u540d\u79f0\uff0c\u8bfe\u7a0b\u6210\u7ee9)<\/p>\n<p>Student(s_no,s_name,s_dept,s_location,s_course_name,s_grade)<\/p>\n<p>\u6ce8\uff1a1NF\u662f\u5173\u7cfb\u6a21\u5f0f\u5e94\u5177\u5907\u7684\u6700\u8d77\u7801\u7684\u6761\u4ef6\uff0c\u5982\u679c\u6570\u636e\u5e93\u8bbe\u8ba1\u4e0d\u80fd\u6ee1\u8db3\u7b2c\u4e00\u8303\u5f0f\uff0c\u5c31\u4e0d\u80fd\u79f0\u4f5c\u662f\u5173\u7cfb\u6a21\u5f0f\uff1b\u5173\u7cfb\u6570\u636e\u5e93\u8bbe\u8ba1\u7814\u7a76\u7684\u5173\u7cfb\u89c4\u8303\u5316\u662f\u57281NF\u57fa\u7840\u4e4b\u4e0a\u8fdb\u884c\u7684\u3002<\/p>\n<h3><a id=\"21-3%E7%AC%AC%E4%BA%8C%E8%8C%83%E5%BC%8F-second-normal-form\" class=\"anchor\" aria-hidden=\"true\" href=\"#21-3%E7%AC%AC%E4%BA%8C%E8%8C%83%E5%BC%8F-second-normal-form\"><span class=\"octicon octicon-link\"><\/span><\/a>21.3 \u7b2c\u4e8c\u8303\u5f0f(Second Normal Form)<\/h3>\n<p>\u5b9a\u4e49\uff1a\u82e5\u5173\u7cfb\u6a21\u5f0fR\u5c5e\u4e8e\u7b2c\u4e00\u8303\u5f0f\uff0c\u4e14\u6bcf\u4e2a\u975e\u4e3b\u5c5e\u6027\u90fd\u662f\u5b8c\u5168\u51fd\u6570\u4f9d\u8d56\u4e8e\u4e3b\u952e\uff0c\u5219R\u5c5e\u4e8e\u7b2c\u4e8c\u8303\u5f0f\u3002<\/p>\n<p>\u8bf4\u660e\uff1a\u4ece2NF\u7684\u5b9a\u4e49\u53ef\u4ee5\u770b\u51fa\uff0c\u4ece2NF\u5f00\u59cb\u8ba8\u8bba\u7684\u662f\u4e3b\u952e\u548c\u975e\u4e3b\u5c5e\u6027\u4e4b\u95f4\u7684\u51fd\u6570\u4f9d\u8d56\u5173\u7cfb\uff0c\u6240\u4ee5\u5206\u6790\u5173\u7cfb\u6a21\u5f0f\u662f\u5c5e\u4e8e2NF\uff0c\u9996\u5148\u6307\u660e\u5173\u7cfb\u6a21\u5f0f\u7684\u4e3b\u952e\uff0c\u7136\u540e\u5728\u8ba8\u8bba\u975e\u4e3b\u5c5e\u6027\u548c\u4e3b\u952e\u4e4b\u95f4\u7684\u51fd\u6570\u4f9d\u8d56\u5173\u7cfb\u3002<\/p>\n<p>\u4f8b\u5982\uff1a\u9009\u8bfe\u5173\u7cfb\u6a21\u5f0f<\/p>\n<p>SC\uff08s_no,c_no,score\uff09\u4e2d\uff0c\u4e3b\u952e\u4e3a\uff08s_no,c_no\uff09\uff0c\u800c\u975e\u4e3b\u5c5e\u6027score\u4e0e\u4e3b\u952e\u4e4b\u95f4\u4e0d\u5b58\u5728\u90e8\u5206\u51fd\u6570\u4f9d\u8d56\u5173\u7cfb\uff0c\u6240\u4ee5\u5173\u7cfb\u6a21\u5f0fSC\u5c5e\u4e8e2NF<\/p>\n<h3><a id=\"21-4%E7%AC%AC%E4%B8%89%E8%8C%83%E5%BC%8F-third-normal-form\" class=\"anchor\" aria-hidden=\"true\" href=\"#21-4%E7%AC%AC%E4%B8%89%E8%8C%83%E5%BC%8F-third-normal-form\"><span class=\"octicon octicon-link\"><\/span><\/a>21.4 \u7b2c\u4e09\u8303\u5f0f(Third Normal Form)<\/h3>\n<p>\u5b9a\u4e49\uff1a\u82e5\u5173\u7cfb\u6a21\u5f0fR\u5c5e\u4e8e\u7b2c\u4e00\u8303\u5f0f\uff0c\u4e14\u6bcf\u4e2a\u975e\u4e3b\u5c5e\u6027\u90fd\u4e0d\u4f20\u9012\u51fd\u6570\u4f9d\u8d56\u4e8e\u4e3b\u952e\uff0c\u5219R\u5c5e\u4e8e\u7b2c\u4e09\u8303\u5f0f\u3002<\/p>\n<p>\u8bf4\u660e\uff1a3NF\u8bf4\u660e\u7684\u662f\u975e\u4e3b\u5c5e\u6027\u548c\u4e3b\u952e\u4e4b\u95f4\u7684\u51fd\u6570\u4f9d\u8d56\u5173\u7cfb<\/p>\n<p>\u4f8b\u5982\uff1a\u9009\u8bfe\u5173\u7cfb\u6a21\u5f0f<\/p>\n<p>SC\uff08s_no,c_no,score\uff09\u4e2d,\u7531\u4e8e\u9664\u4e86\u4e3b\u952e\u4e4b\u5916\uff0c\u53ea\u6709\u4e00\u4e2a\u975e\u4e3b\u5c5e\u6027score\uff0c\u6240\u4ee5score\u4e0d\u53ef\u80fd\u6784\u6210\u4e0e\u4e3b\u952e\u4e4b\u95f4\u7684\u4f20\u9012\u51fd\u6570\u4f9d\u8d56\uff0c\u6240\u4ee5SC\u5c5e\u4e8e3NF<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1. \u6570\u636e\u5e93\u57fa\u672c\u4ecb\u7ecd 1.1 \u4ec0\u4e48\u662f\u6570\u636e\u5e93 \u200b \u6570\u636e\u5e93: \u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u7684\u4ed3\u5e93. \u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\u7cfb\u7edf, \u53ea\u662f\u9700\u8981\u8bbf\u95ee\u8fd9\u4e2a\u6587\u4ef6\u7cfb\u7edf\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u8bed\u53e5(sql)\u8fdb\u884c\u64cd\u4f5c. 1.2 \u6570\u636e\u5e93\u7684\u4f5c\u7528 \u200b \u6211\u4eec\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u7684\u65f6\u5019\uff0c\u7a0b\u5e8f\u4e2d\u7684\u6240\u6709\u6570\u636e\uff0c\u6700\u540e\u90fd\u9700\u8981\u4fdd\u5b58\u5230\u4e13\u4e1a\u8f6f\u4ef6\u4e2d\u3002 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1703"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1703"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1703\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}