{"id":107,"date":"2023-03-29T14:40:00","date_gmt":"2023-03-29T06:40:00","guid":{"rendered":"https:\/\/solost.cn\/?p=107"},"modified":"2023-04-24T09:36:43","modified_gmt":"2023-04-24T01:36:43","slug":"207","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=107","title":{"rendered":"MySQL\u7b14\u8bb0"},"content":{"rendered":"<h1>\u57fa\u7840\u7bc7<\/h1>\n<h2>\u901a\u7528\u8bed\u6cd5\u53ca\u5206\u7c7b<\/h2>\n<ul>\n<li>DDL: \u6570\u636e\u5b9a\u4e49\u8bed\u8a00\uff0c\u7528\u6765\u5b9a\u4e49\u6570\u636e\u5e93\u5bf9\u8c61\uff08\u6570\u636e\u5e93\u3001\u8868\u3001\u5b57\u6bb5\uff09<\/li>\n<li>DML: \u6570\u636e\u64cd\u4f5c\u8bed\u8a00\uff0c\u7528\u6765\u5bf9\u6570\u636e\u5e93\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u589e\u5220\u6539<\/li>\n<li>DQL: \u6570\u636e\u67e5\u8be2\u8bed\u8a00\uff0c\u7528\u6765\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u8868\u7684\u8bb0\u5f55<\/li>\n<li>DCL: \u6570\u636e\u63a7\u5236\u8bed\u8a00\uff0c\u7528\u6765\u521b\u5efa\u6570\u636e\u5e93\u7528\u6237\u3001\u63a7\u5236\u6570\u636e\u5e93\u7684\u63a7\u5236\u6743\u9650<\/li>\n<\/ul>\n<h3>DDL\uff08\u6570\u636e\u5b9a\u4e49\u8bed\u8a00\uff09<\/h3>\n<p>\u6570\u636e\u5b9a\u4e49\u8bed\u8a00<\/p>\n<h4>\u6570\u636e\u5e93\u64cd\u4f5c<\/h4>\n<p>\u67e5\u8be2\u6240\u6709\u6570\u636e\u5e93\uff1a<br \/><code>SHOW DATABASES;<\/code><br \/>\u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\uff1a<br \/><code>SELECT DATABASE();<\/code><br \/>\u521b\u5efa\u6570\u636e\u5e93\uff1a<br \/><code>CREATE DATABASE [ IF NOT EXISTS ] \u6570\u636e\u5e93\u540d [ DEFAULT CHARSET \u5b57\u7b26\u96c6] [COLLATE \u6392\u5e8f\u89c4\u5219 ];<\/code><br \/>\u5220\u9664\u6570\u636e\u5e93\uff1a<br \/><code>DROP DATABASE [ IF EXISTS ] \u6570\u636e\u5e93\u540d;<\/code><br \/>\u4f7f\u7528\u6570\u636e\u5e93\uff1a<br \/><code>USE \u6570\u636e\u5e93\u540d;<\/code><\/p>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>UTF8\u5b57\u7b26\u96c6\u957f\u5ea6\u4e3a3\u5b57\u8282\uff0c\u6709\u4e9b\u7b26\u53f7\u53604\u5b57\u8282\uff0c\u6240\u4ee5\u63a8\u8350\u7528utf8mb4\u5b57\u7b26\u96c6<\/li>\n<\/ul>\n<h4>\u8868\u64cd\u4f5c<\/h4>\n<p>\u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\u6240\u6709\u8868\uff1a<br \/><code>SHOW TABLES;<\/code><br \/>\u67e5\u8be2\u8868\u7ed3\u6784\uff1a<br \/><code>DESC \u8868\u540d;<\/code><br \/>\u67e5\u8be2\u6307\u5b9a\u8868\u7684\u5efa\u8868\u8bed\u53e5\uff1a<br \/><code>SHOW CREATE TABLE \u8868\u540d;<\/code><\/p>\n<p>\u521b\u5efa\u8868\uff1a<\/p>\n<pre><code class=\"lang-mysql\">CREATE TABLE \u8868\u540d(\n    \u5b57\u6bb51 \u5b57\u6bb51\u7c7b\u578b [COMMENT \u5b57\u6bb51\u6ce8\u91ca],\n    \u5b57\u6bb52 \u5b57\u6bb52\u7c7b\u578b [COMMENT \u5b57\u6bb52\u6ce8\u91ca],\n    \u5b57\u6bb53 \u5b57\u6bb53\u7c7b\u578b [COMMENT \u5b57\u6bb53\u6ce8\u91ca],\n    ...\n    \u5b57\u6bb5n \u5b57\u6bb5n\u7c7b\u578b [COMMENT \u5b57\u6bb5n\u6ce8\u91ca]\n)[ COMMENT \u8868\u6ce8\u91ca ];<\/code><\/pre>\n<p><strong>\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u540e\u9762\u6ca1\u6709\u9017\u53f7<\/strong><\/p>\n<p>\u6dfb\u52a0\u5b57\u6bb5\uff1a<br \/><code>ALTER TABLE \u8868\u540d ADD \u5b57\u6bb5\u540d \u7c7b\u578b(\u957f\u5ea6) [COMMENT \u6ce8\u91ca] [\u7ea6\u675f];<\/code><br \/>\u4f8b\uff1a<code>ALTER TABLE emp ADD nickname varchar(20) COMMENT &#039;\u6635\u79f0&#039;;<\/code><\/p>\n<p>\u4fee\u6539\u6570\u636e\u7c7b\u578b\uff1a<br \/><code>ALTER TABLE \u8868\u540d MODIFY \u5b57\u6bb5\u540d \u65b0\u6570\u636e\u7c7b\u578b(\u957f\u5ea6);<\/code><br \/>\u4fee\u6539\u5b57\u6bb5\u540d\u548c\u5b57\u6bb5\u7c7b\u578b\uff1a<br \/><code>ALTER TABLE \u8868\u540d CHANGE \u65e7\u5b57\u6bb5\u540d \u65b0\u5b57\u6bb5\u540d \u7c7b\u578b(\u957f\u5ea6) [COMMENT \u6ce8\u91ca] [\u7ea6\u675f];<\/code><br \/>\u4f8b\uff1a\u5c06emp\u8868\u7684nickname\u5b57\u6bb5\u4fee\u6539\u4e3ausername\uff0c\u7c7b\u578b\u4e3avarchar(30)<br \/><code>ALTER TABLE emp CHANGE nickname username varchar(30) COMMENT &#039;\u6635\u79f0&#039;;<\/code><\/p>\n<p>\u5220\u9664\u5b57\u6bb5\uff1a<br \/><code>ALTER TABLE \u8868\u540d DROP \u5b57\u6bb5\u540d;<\/code><\/p>\n<p>\u4fee\u6539\u8868\u540d\uff1a<br \/><code>ALTER TABLE \u8868\u540d RENAME TO \u65b0\u8868\u540d<\/code><\/p>\n<p>\u5220\u9664\u8868\uff1a<br \/><code>DROP TABLE [IF EXISTS] \u8868\u540d;<\/code><br \/>\u5220\u9664\u8868\uff0c\u5e76\u91cd\u65b0\u521b\u5efa\u8be5\u8868\uff1a<br \/><code>TRUNCATE TABLE \u8868\u540d;<\/code><\/p>\n<h3>DML\uff08\u6570\u636e\u64cd\u4f5c\u8bed\u8a00\uff09<\/h3>\n<h4>\u6dfb\u52a0\u6570\u636e<\/h4>\n<p>\u6307\u5b9a\u5b57\u6bb5\uff1a<br \/><code>INSERT INTO \u8868\u540d (\u5b57\u6bb5\u540d1, \u5b57\u6bb5\u540d2, ...) VALUES (\u503c1, \u503c2, ...);<\/code><br \/>\u5168\u90e8\u5b57\u6bb5\uff1a<br \/><code>INSERT INTO \u8868\u540d VALUES (\u503c1, \u503c2, ...);<\/code><\/p>\n<p>\u6279\u91cf\u6dfb\u52a0\u6570\u636e\uff1a<br \/><code>INSERT INTO \u8868\u540d (\u5b57\u6bb5\u540d1, \u5b57\u6bb5\u540d2, ...) VALUES (\u503c1, \u503c2, ...), (\u503c1, \u503c2, ...), (\u503c1, \u503c2, ...);<\/code><br \/><code>INSERT INTO \u8868\u540d VALUES (\u503c1, \u503c2, ...), (\u503c1, \u503c2, ...), (\u503c1, \u503c2, ...);<\/code><\/p>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\u5b57\u7b26\u4e32\u548c\u65e5\u671f\u7c7b\u578b\u6570\u636e\u5e94\u8be5\u5305\u542b\u5728\u5f15\u53f7\u4e2d<\/li>\n<li>\u63d2\u5165\u7684\u6570\u636e\u5927\u5c0f\u5e94\u8be5\u5728\u5b57\u6bb5\u7684\u89c4\u5b9a\u8303\u56f4\u5185<\/li>\n<\/ul>\n<h4>\u66f4\u65b0\u548c\u5220\u9664\u6570\u636e<\/h4>\n<p>\u4fee\u6539\u6570\u636e\uff1a<br \/><code>UPDATE \u8868\u540d SET \u5b57\u6bb5\u540d1 = \u503c1, \u5b57\u6bb5\u540d2 = \u503c2, ... [ WHERE \u6761\u4ef6 ];<\/code><br \/>\u4f8b\uff1a<br \/><code>UPDATE emp SET name = &#039;Jack&#039; WHERE id = 1;<\/code><\/p>\n<p>\u5220\u9664\u6570\u636e\uff1a<br \/><code>DELETE FROM \u8868\u540d [ WHERE \u6761\u4ef6 ];<\/code><\/p>\n<h3>DQL\uff08\u6570\u636e\u67e5\u8be2\u8bed\u8a00\uff09<\/h3>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"lang-mysql\">SELECT\n    \u5b57\u6bb5\u5217\u8868\nFROM\n    \u8868\u540d\u5b57\u6bb5\nWHERE\n    \u6761\u4ef6\u5217\u8868\nGROUP BY\n    \u5206\u7ec4\u5b57\u6bb5\u5217\u8868\nHAVING\n    \u5206\u7ec4\u540e\u7684\u6761\u4ef6\u5217\u8868\nORDER BY\n    \u6392\u5e8f\u5b57\u6bb5\u5217\u8868\nLIMIT\n    \u5206\u9875\u53c2\u6570<\/code><\/pre>\n<h4>\u57fa\u7840\u67e5\u8be2<\/h4>\n<p>\u67e5\u8be2\u591a\u4e2a\u5b57\u6bb5\uff1a<br \/><code>SELECT \u5b57\u6bb51, \u5b57\u6bb52, \u5b57\u6bb53, ... FROM \u8868\u540d;<\/code><br \/><code>SELECT * FROM \u8868\u540d;<\/code><\/p>\n<p>\u8bbe\u7f6e\u522b\u540d\uff1a<br \/><code>SELECT \u5b57\u6bb51 [ AS \u522b\u540d1 ], \u5b57\u6bb52 [ AS \u522b\u540d2 ], \u5b57\u6bb53 [ AS \u522b\u540d3 ], ... FROM \u8868\u540d;<\/code><br \/><code>SELECT \u5b57\u6bb51 [ \u522b\u540d1 ], \u5b57\u6bb52 [ \u522b\u540d2 ], \u5b57\u6bb53 [ \u522b\u540d3 ], ... FROM \u8868\u540d;<\/code><\/p>\n<p>\u53bb\u9664\u91cd\u590d\u8bb0\u5f55\uff1a<br \/><code>SELECT DISTINCT \u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d;<\/code><\/p>\n<p>\u8f6c\u4e49\uff1a<br \/><code>SELECT * FROM \u8868\u540d WHERE name LIKE &#039;\/_\u5f20\u4e09&#039; ESCAPE &#039;\/&#039;<\/code><br \/>\/ \u4e4b\u540e\u7684\\_\u4e0d\u4f5c\u4e3a\u901a\u914d\u7b26<\/p>\n<h4>\u6761\u4ef6\u67e5\u8be2<\/h4>\n<p>\u8bed\u6cd5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d WHERE \u6761\u4ef6\u5217\u8868;<\/code><\/p>\n<p>\u6761\u4ef6\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>&gt;<\/td>\n<td>\u5927\u4e8e<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td>\u5927\u4e8e\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td>&lt;<\/td>\n<td>\u5c0f\u4e8e<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td>\u5c0f\u4e8e\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td>=<\/td>\n<td>\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td>&lt;&gt; \u6216 !=<\/td>\n<td>\u4e0d\u7b49\u4e8e<\/td>\n<\/tr>\n<tr>\n<td>BETWEEN ... AND ...<\/td>\n<td>\u5728\u67d0\u4e2a\u8303\u56f4\u5185\uff08\u542b\u6700\u5c0f\u3001\u6700\u5927\u503c\uff09<\/td>\n<\/tr>\n<tr>\n<td>IN(...)<\/td>\n<td>\u5728in\u4e4b\u540e\u7684\u5217\u8868\u4e2d\u7684\u503c\uff0c\u591a\u9009\u4e00<\/td>\n<\/tr>\n<tr>\n<td>LIKE \u5360\u4f4d\u7b26<\/td>\n<td>\u6a21\u7cca\u5339\u914d\uff08\\_\u5339\u914d\u5355\u4e2a\u5b57\u7b26\uff0c%\u5339\u914d\u4efb\u610f\u4e2a\u5b57\u7b26\uff09<\/td>\n<\/tr>\n<tr>\n<td>IS NULL<\/td>\n<td>\u662fNULL<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<thead>\n<tr>\n<th>\u903b\u8f91\u8fd0\u7b97\u7b26<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>AND \u6216 &&<\/td>\n<td>\u5e76\u4e14\uff08\u591a\u4e2a\u6761\u4ef6\u540c\u65f6\u6210\u7acb\uff09<\/td>\n<\/tr>\n<tr>\n<td>OR \u6216&#124;&#124;<\/td>\n<td>\u6216\u8005\uff08\u591a\u4e2a\u6761\u4ef6\u4efb\u610f\u4e00\u4e2a\u6210\u7acb\uff09<\/td>\n<\/tr>\n<tr>\n<td>NOT \u6216 !<\/td>\n<td>\u975e\uff0c\u4e0d\u662f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u5e74\u9f84\u7b49\u4e8e30\nselect * from employee where age = 30;\n-- \u5e74\u9f84\u5c0f\u4e8e30\nselect * from employee where age &lt; 30;\n-- \u5c0f\u4e8e\u7b49\u4e8e\nselect * from employee where age &lt;= 30;\n-- \u6ca1\u6709\u8eab\u4efd\u8bc1\nselect * from employee where idcard is null or idcard = &#039;&#039;;\n-- \u6709\u8eab\u4efd\u8bc1\nselect * from employee where idcard;\nselect * from employee where idcard is not null;\n-- \u4e0d\u7b49\u4e8e\nselect * from employee where age != 30;\n-- \u5e74\u9f84\u572820\u523030\u4e4b\u95f4\nselect * from employee where age between 20 and 30;\nselect * from employee where age &gt;= 20 and age &lt;= 30;\n-- \u4e0b\u9762\u8bed\u53e5\u4e0d\u62a5\u9519\uff0c\u4f46\u67e5\u4e0d\u5230\u4efb\u4f55\u4fe1\u606f\nselect * from employee where age between 30 and 20;\n-- \u6027\u522b\u4e3a\u5973\u4e14\u5e74\u9f84\u5c0f\u4e8e30\nselect * from employee where age &lt; 30 and gender = &#039;\u5973&#039;;\n-- \u5e74\u9f84\u7b49\u4e8e25\u621630\u621635\nselect * from employee where age = 25 or age = 30 or age = 35;\nselect * from employee where age in (25, 30, 35);\n-- \u59d3\u540d\u4e3a\u4e24\u4e2a\u5b57\nselect * from employee where name like &#039;__&#039;;\n-- \u8eab\u4efd\u8bc1\u6700\u540e\u4e3aX\nselect * from employee where idcard like &#039;%X&#039;;<\/code><\/pre>\n<h4>\u805a\u5408\u67e5\u8be2\uff08\u805a\u5408\u51fd\u6570\uff09<\/h4>\n<p>\u5e38\u89c1\u805a\u5408\u51fd\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>count<\/td>\n<td>\u7edf\u8ba1\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>max<\/td>\n<td>\u6700\u5927\u503c<\/td>\n<\/tr>\n<tr>\n<td>min<\/td>\n<td>\u6700\u5c0f\u503c<\/td>\n<\/tr>\n<tr>\n<td>avg<\/td>\n<td>\u5e73\u5747\u503c<\/td>\n<\/tr>\n<tr>\n<td>sum<\/td>\n<td>\u6c42\u548c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8bed\u6cd5\uff1a<br \/><code>SELECT \u805a\u5408\u51fd\u6570(\u5b57\u6bb5\u5217\u8868) FROM \u8868\u540d;<\/code><br \/>\u4f8b\uff1a<br \/><code>SELECT count(id) from employee where workaddress = &quot;\u5e7f\u4e1c\u7701&quot;;<\/code><\/p>\n<h4>\u5206\u7ec4\u67e5\u8be2<\/h4>\n<p>\u8bed\u6cd5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d [ WHERE \u6761\u4ef6 ] GROUP BY \u5206\u7ec4\u5b57\u6bb5\u540d [ HAVING \u5206\u7ec4\u540e\u7684\u8fc7\u6ee4\u6761\u4ef6 ];<\/code><\/p>\n<p>where \u548c having \u7684\u533a\u522b\uff1a<\/p>\n<ul>\n<li>\u6267\u884c\u65f6\u673a\u4e0d\u540c\uff1awhere\u662f\u5206\u7ec4\u4e4b\u524d\u8fdb\u884c\u8fc7\u6ee4\uff0c\u4e0d\u6ee1\u8db3where\u6761\u4ef6\u4e0d\u53c2\u4e0e\u5206\u7ec4\uff1bhaving\u662f\u5206\u7ec4\u540e\u5bf9\u7ed3\u679c\u8fdb\u884c\u8fc7\u6ee4\u3002<\/li>\n<li>\u5224\u65ad\u6761\u4ef6\u4e0d\u540c\uff1awhere\u4e0d\u80fd\u5bf9\u805a\u5408\u51fd\u6570\u8fdb\u884c\u5224\u65ad\uff0c\u800chaving\u53ef\u4ee5\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u6839\u636e\u6027\u522b\u5206\u7ec4\uff0c\u7edf\u8ba1\u7537\u6027\u548c\u5973\u6027\u6570\u91cf\uff08\u53ea\u663e\u793a\u5206\u7ec4\u6570\u91cf\uff0c\u4e0d\u663e\u793a\u54ea\u4e2a\u662f\u7537\u54ea\u4e2a\u662f\u5973\uff09\nselect count(*) from employee group by gender;\n-- \u6839\u636e\u6027\u522b\u5206\u7ec4\uff0c\u7edf\u8ba1\u7537\u6027\u548c\u5973\u6027\u6570\u91cf\nselect gender, count(*) from employee group by gender;\n-- \u6839\u636e\u6027\u522b\u5206\u7ec4\uff0c\u7edf\u8ba1\u7537\u6027\u548c\u5973\u6027\u7684\u5e73\u5747\u5e74\u9f84\nselect gender, avg(age) from employee group by gender;\n-- \u5e74\u9f84\u5c0f\u4e8e45\uff0c\u5e76\u6839\u636e\u5de5\u4f5c\u5730\u5740\u5206\u7ec4\nselect workaddress, count(*) from employee where age &lt; 45 group by workaddress;\n-- \u5e74\u9f84\u5c0f\u4e8e45\uff0c\u5e76\u6839\u636e\u5de5\u4f5c\u5730\u5740\u5206\u7ec4\uff0c\u83b7\u53d6\u5458\u5de5\u6570\u91cf\u5927\u4e8e\u7b49\u4e8e3\u7684\u5de5\u4f5c\u5730\u5740\nselect workaddress, count(*) address_count from employee where age &lt; 45 group by workaddress having address_count &gt;= 3;<\/code><\/pre>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\u6267\u884c\u987a\u5e8f\uff1awhere &gt; \u805a\u5408\u51fd\u6570 &gt; having<\/li>\n<li>\u5206\u7ec4\u4e4b\u540e\uff0c\u67e5\u8be2\u7684\u5b57\u6bb5\u4e00\u822c\u4e3a\u805a\u5408\u51fd\u6570\u548c\u5206\u7ec4\u5b57\u6bb5\uff0c\u67e5\u8be2\u5176\u4ed6\u5b57\u6bb5\u65e0\u4efb\u4f55\u610f\u4e49<\/li>\n<\/ul>\n<h4>\u6392\u5e8f\u67e5\u8be2<\/h4>\n<p>\u8bed\u6cd5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d ORDER BY \u5b57\u6bb51 \u6392\u5e8f\u65b9\u5f0f1, \u5b57\u6bb52 \u6392\u5e8f\u65b9\u5f0f2;<\/code><\/p>\n<p>\u6392\u5e8f\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>ASC: \u5347\u5e8f\uff08\u9ed8\u8ba4\uff09<\/li>\n<li>DESC: \u964d\u5e8f<\/li>\n<\/ul>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u6839\u636e\u5e74\u9f84\u5347\u5e8f\u6392\u5e8f\nSELECT * FROM employee ORDER BY age ASC;\nSELECT * FROM employee ORDER BY age;\n-- \u4e24\u5b57\u6bb5\u6392\u5e8f\uff0c\u6839\u636e\u5e74\u9f84\u5347\u5e8f\u6392\u5e8f\uff0c\u5165\u804c\u65f6\u95f4\u964d\u5e8f\u6392\u5e8f\nSELECT * FROM employee ORDER BY age ASC, entrydate DESC;<\/code><\/pre>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<p>\u5982\u679c\u662f\u591a\u5b57\u6bb5\u6392\u5e8f\uff0c\u5f53\u7b2c\u4e00\u4e2a\u5b57\u6bb5\u503c\u76f8\u540c\u65f6\uff0c\u624d\u4f1a\u6839\u636e\u7b2c\u4e8c\u4e2a\u5b57\u6bb5\u8fdb\u884c\u6392\u5e8f<\/p>\n<h4>\u5206\u9875\u67e5\u8be2<\/h4>\n<p>\u8bed\u6cd5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d LIMIT \u8d77\u59cb\u7d22\u5f15, \u67e5\u8be2\u8bb0\u5f55\u6570;<\/code><\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u7b2c\u4e00\u9875\u6570\u636e\uff0c\u5c55\u793a10\u6761\nSELECT * FROM employee LIMIT 0, 10;\n-- \u67e5\u8be2\u7b2c\u4e8c\u9875\nSELECT * FROM employee LIMIT 10, 10;<\/code><\/pre>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\u8d77\u59cb\u7d22\u5f15\u4ece0\u5f00\u59cb\uff0c\u8d77\u59cb\u7d22\u5f15 = \uff08\u67e5\u8be2\u9875\u7801 - 1\uff09 * \u6bcf\u9875\u663e\u793a\u8bb0\u5f55\u6570<\/li>\n<li>\u5206\u9875\u67e5\u8be2\u662f\u6570\u636e\u5e93\u7684\u65b9\u8a00\uff0c\u4e0d\u540c\u6570\u636e\u5e93\u6709\u4e0d\u540c\u5b9e\u73b0\uff0cMySQL\u662fLIMIT<\/li>\n<li>\u5982\u679c\u67e5\u8be2\u7684\u662f\u7b2c\u4e00\u9875\u6570\u636e\uff0c\u8d77\u59cb\u7d22\u5f15\u53ef\u4ee5\u7701\u7565\uff0c\u76f4\u63a5\u7b80\u5199 LIMIT 10<\/li>\n<\/ul>\n<h4>DQL\u6267\u884c\u987a\u5e8f<\/h4>\n<p>FROM -&gt; WHERE -&gt; GROUP BY -&gt; SELECT -&gt; ORDER BY -&gt; LIMIT<\/p>\n<h3>DCL<\/h3>\n<h4>\u7ba1\u7406\u7528\u6237<\/h4>\n<p>\u67e5\u8be2\u7528\u6237\uff1a<\/p>\n<pre><code class=\"lang-mysql\">USE mysql;\nSELECT * FROM user;<\/code><\/pre>\n<p>\u521b\u5efa\u7528\u6237:<br \/><code>CREATE USER &#039;\u7528\u6237\u540d&#039;@&#039;\u4e3b\u673a\u540d&#039; IDENTIFIED BY &#039;\u5bc6\u7801&#039;;<\/code><\/p>\n<p>\u4fee\u6539\u7528\u6237\u5bc6\u7801\uff1a<br \/><code>ALTER USER &#039;\u7528\u6237\u540d&#039;@&#039;\u4e3b\u673a\u540d&#039; IDENTIFIED WITH mysql_native_password BY &#039;\u65b0\u5bc6\u7801&#039;;<\/code><\/p>\n<p>\u5220\u9664\u7528\u6237\uff1a<br \/><code>DROP USER &#039;\u7528\u6237\u540d&#039;@&#039;\u4e3b\u673a\u540d&#039;;<\/code><\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u521b\u5efa\u7528\u6237test\uff0c\u53ea\u80fd\u5728\u5f53\u524d\u4e3b\u673alocalhost\u8bbf\u95ee\ncreate user &#039;test&#039;@&#039;localhost&#039; identified by &#039;123456&#039;;\n-- \u521b\u5efa\u7528\u6237test\uff0c\u80fd\u5728\u4efb\u610f\u4e3b\u673a\u8bbf\u95ee\ncreate user &#039;test&#039;@&#039;%&#039; identified by &#039;123456&#039;;\ncreate user &#039;test&#039; identified by &#039;123456&#039;;\n-- \u4fee\u6539\u5bc6\u7801\nalter user &#039;test&#039;@&#039;localhost&#039; identified with mysql_native_password by &#039;1234&#039;;\n-- \u5220\u9664\u7528\u6237\ndrop user &#039;test&#039;@&#039;localhost&#039;;<\/code><\/pre>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\u4e3b\u673a\u540d\u53ef\u4ee5\u4f7f\u7528 % \u901a\u914d<\/li>\n<\/ul>\n<h4>\u6743\u9650\u63a7\u5236<\/h4>\n<p>\u5e38\u7528\u6743\u9650\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u6743\u9650<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ALL, ALL PRIVILEGES<\/td>\n<td>\u6240\u6709\u6743\u9650<\/td>\n<\/tr>\n<tr>\n<td>SELECT<\/td>\n<td>\u67e5\u8be2\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>INSERT<\/td>\n<td>\u63d2\u5165\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>UPDATE<\/td>\n<td>\u4fee\u6539\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>DELETE<\/td>\n<td>\u5220\u9664\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>ALTER<\/td>\n<td>\u4fee\u6539\u8868<\/td>\n<\/tr>\n<tr>\n<td>DROP<\/td>\n<td>\u5220\u9664\u6570\u636e\u5e93\/\u8868\/\u89c6\u56fe<\/td>\n<\/tr>\n<tr>\n<td>CREATE<\/td>\n<td>\u521b\u5efa\u6570\u636e\u5e93\/\u8868<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u591a\u6743\u9650\u8bf7\u770b<a href=\"#\u6743\u9650\u4e00\u89c8\u8868\" title=\"\u6743\u9650\u4e00\u89c8\u8868\">\u6743\u9650\u4e00\u89c8\u8868<\/a><\/p>\n<p>\u67e5\u8be2\u6743\u9650\uff1a<br \/><code>SHOW GRANTS FOR &#039;\u7528\u6237\u540d&#039;@&#039;\u4e3b\u673a\u540d&#039;;<\/code><\/p>\n<p>\u6388\u4e88\u6743\u9650\uff1a<br \/><code>GRANT \u6743\u9650\u5217\u8868 ON \u6570\u636e\u5e93\u540d.\u8868\u540d TO &#039;\u7528\u6237\u540d&#039;@&#039;\u4e3b\u673a\u540d&#039;;<\/code><\/p>\n<p>\u64a4\u9500\u6743\u9650\uff1a<br \/><code>REVOKE \u6743\u9650\u5217\u8868 ON \u6570\u636e\u5e93\u540d.\u8868\u540d FROM &#039;\u7528\u6237\u540d&#039;@&#039;\u4e3b\u673a\u540d&#039;;<\/code><\/p>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\u591a\u4e2a\u6743\u9650\u7528\u9017\u53f7\u5206\u9694<\/li>\n<li>\u6388\u6743\u65f6\uff0c\u6570\u636e\u5e93\u540d\u548c\u8868\u540d\u53ef\u4ee5\u7528 * \u8fdb\u884c\u901a\u914d\uff0c\u4ee3\u8868\u6240\u6709<\/li>\n<\/ul>\n<h2>\u51fd\u6570<\/h2>\n<ul>\n<li>\u5b57\u7b26\u4e32\u51fd\u6570<\/li>\n<li>\u6570\u503c\u51fd\u6570<\/li>\n<li>\u65e5\u671f\u51fd\u6570<\/li>\n<li>\u6d41\u7a0b\u51fd\u6570<\/li>\n<\/ul>\n<h3>\u5b57\u7b26\u4e32\u51fd\u6570<\/h3>\n<p>\u5e38\u7528\u51fd\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CONCAT(s1, s2, ..., sn)<\/td>\n<td>\u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u5c06s1, s2, ..., sn\u62fc\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>LOWER(str)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32\u5168\u90e8\u8f6c\u4e3a\u5c0f\u5199<\/td>\n<\/tr>\n<tr>\n<td>UPPER(str)<\/td>\n<td>\u5c06\u5b57\u7b26\u4e32\u5168\u90e8\u8f6c\u4e3a\u5927\u5199<\/td>\n<\/tr>\n<tr>\n<td>LPAD(str, n, pad)<\/td>\n<td>\u5de6\u586b\u5145\uff0c\u7528\u5b57\u7b26\u4e32pad\u5bf9str\u7684\u5de6\u8fb9\u8fdb\u884c\u586b\u5145\uff0c\u8fbe\u5230n\u4e2a\u5b57\u7b26\u4e32\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>RPAD(str, n, pad)<\/td>\n<td>\u53f3\u586b\u5145\uff0c\u7528\u5b57\u7b26\u4e32pad\u5bf9str\u7684\u53f3\u8fb9\u8fdb\u884c\u586b\u5145\uff0c\u8fbe\u5230n\u4e2a\u5b57\u7b26\u4e32\u957f\u5ea6<\/td>\n<\/tr>\n<tr>\n<td>TRIM(str)<\/td>\n<td>\u53bb\u6389\u5b57\u7b26\u4e32\u5934\u90e8\u548c\u5c3e\u90e8\u7684\u7a7a\u683c<\/td>\n<\/tr>\n<tr>\n<td>SUBSTRING(str, start, len)<\/td>\n<td>\u8fd4\u56de\u4ece\u5b57\u7b26\u4e32str\u4ecestart\u4f4d\u7f6e\u8d77\u7684len\u4e2a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td>REPLACE(column, source, replace)<\/td>\n<td>\u66ff\u6362\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u62fc\u63a5\nSELECT CONCAT(&#039;Hello&#039;, &#039;World&#039;);\n-- \u5c0f\u5199\nSELECT LOWER(&#039;Hello&#039;);\n-- \u5927\u5199\nSELECT UPPER(&#039;Hello&#039;);\n-- \u5de6\u586b\u5145\nSELECT LPAD(&#039;01&#039;, 5, &#039;-&#039;);\n-- \u53f3\u586b\u5145\nSELECT RPAD(&#039;01&#039;, 5, &#039;-&#039;);\n-- \u53bb\u9664\u7a7a\u683c\nSELECT TRIM(&#039; Hello World &#039;);\n-- \u5207\u7247\uff08\u8d77\u59cb\u7d22\u5f15\u4e3a1\uff09\nSELECT SUBSTRING(&#039;Hello World&#039;, 1, 5);<\/code><\/pre>\n<h3>\u6570\u503c\u51fd\u6570<\/h3>\n<p>\u5e38\u89c1\u51fd\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CEIL(x)<\/td>\n<td>\u5411\u4e0a\u53d6\u6574<\/td>\n<\/tr>\n<tr>\n<td>FLOOR(x)<\/td>\n<td>\u5411\u4e0b\u53d6\u6574<\/td>\n<\/tr>\n<tr>\n<td>MOD(x, y)<\/td>\n<td>\u8fd4\u56dex\/y\u7684\u6a21<\/td>\n<\/tr>\n<tr>\n<td>RAND()<\/td>\n<td>\u8fd4\u56de0~1\u5185\u7684\u968f\u673a\u6570<\/td>\n<\/tr>\n<tr>\n<td>ROUND(x, y)<\/td>\n<td>\u6c42\u53c2\u6570x\u7684\u56db\u820d\u4e94\u5165\u503c\uff0c\u4fdd\u7559y\u4f4d\u5c0f\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u65e5\u671f\u51fd\u6570<\/h3>\n<p>\u5e38\u7528\u51fd\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CURDATE()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f<\/td>\n<\/tr>\n<tr>\n<td>CURTIME()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>NOW()<\/td>\n<td>\u8fd4\u56de\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td>YEAR(date)<\/td>\n<td>\u83b7\u53d6\u6307\u5b9adate\u7684\u5e74\u4efd<\/td>\n<\/tr>\n<tr>\n<td>MONTH(date)<\/td>\n<td>\u83b7\u53d6\u6307\u5b9adate\u7684\u6708\u4efd<\/td>\n<\/tr>\n<tr>\n<td>DAY(date)<\/td>\n<td>\u83b7\u53d6\u6307\u5b9adate\u7684\u65e5\u671f<\/td>\n<\/tr>\n<tr>\n<td>DATE_ADD(date, INTERVAL expr type)<\/td>\n<td>\u8fd4\u56de\u4e00\u4e2a\u65e5\u671f\/\u65f6\u95f4\u503c\u52a0\u4e0a\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694expr\u540e\u7684\u65f6\u95f4\u503c<\/td>\n<\/tr>\n<tr>\n<td>DATEDIFF(date1, date2)<\/td>\n<td>\u8fd4\u56de\u8d77\u59cb\u65f6\u95f4date1\u548c\u7ed3\u675f\u65f6\u95f4date2\u4e4b\u95f4\u7684\u5929\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- DATE_ADD\nSELECT DATE_ADD(NOW(), INTERVAL 70 YEAR);<\/code><\/pre>\n<h3>\u6d41\u7a0b\u51fd\u6570<\/h3>\n<p>\u5e38\u7528\u51fd\u6570\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>IF(value, t, f)<\/td>\n<td>\u5982\u679cvalue\u4e3atrue\uff0c\u5219\u8fd4\u56det\uff0c\u5426\u5219\u8fd4\u56def<\/td>\n<\/tr>\n<tr>\n<td>IFNULL(value1, value2)<\/td>\n<td>\u5982\u679cvalue1\u4e0d\u4e3a\u7a7a\uff0c\u8fd4\u56devalue1\uff0c\u5426\u5219\u8fd4\u56devalue2<\/td>\n<\/tr>\n<tr>\n<td>CASE WHEN [ val1 ] THEN [ res1 ] ... ELSE [ default ] END<\/td>\n<td>\u5982\u679cval1\u4e3atrue\uff0c\u8fd4\u56deres1\uff0c... \u5426\u5219\u8fd4\u56dedefault\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n<tr>\n<td>CASE [ expr ] WHEN [ val1 ] THEN [ res1 ] ... ELSE [ default ] END<\/td>\n<td>\u5982\u679cexpr\u7684\u503c\u7b49\u4e8eval1\uff0c\u8fd4\u56deres1\uff0c... \u5426\u5219\u8fd4\u56dedefault\u9ed8\u8ba4\u503c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">select\n    name,\n    (case when age &gt; 30 then &#039;\u4e2d\u5e74&#039; else &#039;\u9752\u5e74&#039; end)\nfrom employee;\nselect\n    name,\n    (case workaddress when &#039;\u5317\u4eac\u5e02&#039; then &#039;\u4e00\u7ebf\u57ce\u5e02&#039; when &#039;\u4e0a\u6d77\u5e02&#039; then &#039;\u4e00\u7ebf\u57ce\u5e02&#039; else &#039;\u4e8c\u7ebf\u57ce\u5e02&#039; end) as &#039;\u5de5\u4f5c\u5730\u5740&#039;\nfrom employee;<\/code><\/pre>\n<h2>\u7ea6\u675f<\/h2>\n<p>\u5206\u7c7b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7ea6\u675f<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u5173\u952e\u5b57<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u975e\u7a7a\u7ea6\u675f<\/td>\n<td>\u9650\u5236\u8be5\u5b57\u6bb5\u7684\u6570\u636e\u4e0d\u80fd\u4e3anull<\/td>\n<td>NOT NULL<\/td>\n<\/tr>\n<tr>\n<td>\u552f\u4e00\u7ea6\u675f<\/td>\n<td>\u4fdd\u8bc1\u8be5\u5b57\u6bb5\u7684\u6240\u6709\u6570\u636e\u90fd\u662f\u552f\u4e00\u3001\u4e0d\u91cd\u590d\u7684<\/td>\n<td>UNIQUE<\/td>\n<\/tr>\n<tr>\n<td>\u4e3b\u952e\u7ea6\u675f<\/td>\n<td>\u4e3b\u952e\u662f\u4e00\u884c\u6570\u636e\u7684\u552f\u4e00\u6807\u8bc6\uff0c\u8981\u6c42\u975e\u7a7a\u4e14\u552f\u4e00<\/td>\n<td>PRIMARY KEY<\/td>\n<\/tr>\n<tr>\n<td>\u9ed8\u8ba4\u7ea6\u675f<\/td>\n<td>\u4fdd\u5b58\u6570\u636e\u65f6\uff0c\u5982\u679c\u672a\u6307\u5b9a\u8be5\u5b57\u6bb5\u7684\u503c\uff0c\u5219\u91c7\u7528\u9ed8\u8ba4\u503c<\/td>\n<td>DEFAULT<\/td>\n<\/tr>\n<tr>\n<td>\u68c0\u67e5\u7ea6\u675f\uff088.0.1\u7248\u672c\u540e\uff09<\/td>\n<td>\u4fdd\u8bc1\u5b57\u6bb5\u503c\u6ee1\u8db3\u67d0\u4e00\u4e2a\u6761\u4ef6<\/td>\n<td>CHECK<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u952e\u7ea6\u675f<\/td>\n<td>\u7528\u6765\u8ba9\u4e24\u5f20\u56fe\u7684\u6570\u636e\u4e4b\u95f4\u5efa\u7acb\u8fde\u63a5\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027<\/td>\n<td>FOREIGN KEY<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ea6\u675f\u662f\u4f5c\u7528\u4e8e\u8868\u4e2d\u5b57\u6bb5\u4e0a\u7684\uff0c\u53ef\u4ee5\u518d\u521b\u5efa\u8868\/\u4fee\u6539\u8868\u7684\u65f6\u5019\u6dfb\u52a0\u7ea6\u675f\u3002<\/p>\n<h3>\u5e38\u7528\u7ea6\u675f<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7ea6\u675f\u6761\u4ef6<\/th>\n<th>\u5173\u952e\u5b57<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u4e3b\u952e<\/td>\n<td>PRIMARY KEY<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u52a8\u589e\u957f<\/td>\n<td>AUTO_INCREMENT<\/td>\n<\/tr>\n<tr>\n<td>\u4e0d\u4e3a\u7a7a<\/td>\n<td>NOT NULL<\/td>\n<\/tr>\n<tr>\n<td>\u552f\u4e00<\/td>\n<td>UNIQUE<\/td>\n<\/tr>\n<tr>\n<td>\u903b\u8f91\u6761\u4ef6<\/td>\n<td>CHECK<\/td>\n<\/tr>\n<tr>\n<td>\u9ed8\u8ba4\u503c<\/td>\n<td>DEFAULT<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">create table user(\n    id int primary key auto_increment,\n    name varchar(10) not null unique,\n    age int check(age &gt; 0 and age &lt; 120),\n    status char(1) default &#039;1&#039;,\n    gender char(1)\n);<\/code><\/pre>\n<h3>\u5916\u952e\u7ea6\u675f<\/h3>\n<p>\u6dfb\u52a0\u5916\u952e\uff1a<\/p>\n<pre><code class=\"lang-mysql\">CREATE TABLE \u8868\u540d(\n    \u5b57\u6bb5\u540d \u5b57\u6bb5\u7c7b\u578b,\n    ...\n    [CONSTRAINT] [\u5916\u952e\u540d\u79f0] FOREIGN KEY(\u5916\u952e\u5b57\u6bb5\u540d) REFERENCES \u4e3b\u8868(\u4e3b\u8868\u5217\u540d)\n);\nALTER TABLE \u8868\u540d ADD CONSTRAINT \u5916\u952e\u540d\u79f0 FOREIGN KEY (\u5916\u952e\u5b57\u6bb5\u540d) REFERENCES \u4e3b\u8868(\u4e3b\u8868\u5217\u540d);\n\n-- \u4f8b\u5b50\nalter table emp add constraint fk_emp_dept_id foreign key(dept_id) references dept(id);<\/code><\/pre>\n<p>\u5220\u9664\u5916\u952e\uff1a<br \/><code>ALTER TABLE \u8868\u540d DROP FOREIGN KEY \u5916\u952e\u540d;<\/code><\/p>\n<h4>\u5220\u9664\/\u66f4\u65b0\u884c\u4e3a<\/h4>\n<table>\n<thead>\n<tr>\n<th>\u884c\u4e3a<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>NO ACTION<\/td>\n<td>\u5f53\u5728\u7236\u8868\u4e2d\u5220\u9664\/\u66f4\u65b0\u5bf9\u5e94\u8bb0\u5f55\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u8be5\u8bb0\u5f55\u662f\u5426\u6709\u5bf9\u5e94\u5916\u952e\uff0c\u5982\u679c\u6709\u5219\u4e0d\u5141\u8bb8\u5220\u9664\/\u66f4\u65b0\uff08\u4e0eRESTRICT\u4e00\u81f4\uff09<\/td>\n<\/tr>\n<tr>\n<td>RESTRICT<\/td>\n<td>\u5f53\u5728\u7236\u8868\u4e2d\u5220\u9664\/\u66f4\u65b0\u5bf9\u5e94\u8bb0\u5f55\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u8be5\u8bb0\u5f55\u662f\u5426\u6709\u5bf9\u5e94\u5916\u952e\uff0c\u5982\u679c\u6709\u5219\u4e0d\u5141\u8bb8\u5220\u9664\/\u66f4\u65b0\uff08\u4e0eNO ACTION\u4e00\u81f4\uff09<\/td>\n<\/tr>\n<tr>\n<td>CASCADE<\/td>\n<td>\u5f53\u5728\u7236\u8868\u4e2d\u5220\u9664\/\u66f4\u65b0\u5bf9\u5e94\u8bb0\u5f55\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u8be5\u8bb0\u5f55\u662f\u5426\u6709\u5bf9\u5e94\u5916\u952e\uff0c\u5982\u679c\u6709\u5219\u4e5f\u5220\u9664\/\u66f4\u65b0\u5916\u952e\u5728\u5b50\u8868\u4e2d\u7684\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td>SET NULL<\/td>\n<td>\u5f53\u5728\u7236\u8868\u4e2d\u5220\u9664\/\u66f4\u65b0\u5bf9\u5e94\u8bb0\u5f55\u65f6\uff0c\u9996\u5148\u68c0\u67e5\u8be5\u8bb0\u5f55\u662f\u5426\u6709\u5bf9\u5e94\u5916\u952e\uff0c\u5982\u679c\u6709\u5219\u8bbe\u7f6e\u5b50\u8868\u4e2d\u8be5\u5916\u952e\u503c\u4e3anull\uff08\u8981\u6c42\u8be5\u5916\u952e\u5141\u8bb8\u4e3anull\uff09<\/td>\n<\/tr>\n<tr>\n<td>SET DEFAULT<\/td>\n<td>\u7236\u8868\u6709\u53d8\u66f4\u65f6\uff0c\u5b50\u8868\u5c06\u5916\u952e\u8bbe\u4e3a\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff08Innodb\u4e0d\u652f\u6301\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u66f4\u6539\u5220\u9664\/\u66f4\u65b0\u884c\u4e3a\uff1a<br \/><code>ALTER TABLE \u8868\u540d ADD CONSTRAINT \u5916\u952e\u540d\u79f0 FOREIGN KEY (\u5916\u952e\u5b57\u6bb5) REFERENCES \u4e3b\u8868\u540d(\u4e3b\u8868\u5b57\u6bb5\u540d) ON UPDATE \u884c\u4e3a ON DELETE \u884c\u4e3a;<\/code><\/p>\n<h2>\u591a\u8868\u67e5\u8be2<\/h2>\n<h3>\u591a\u8868\u5173\u7cfb<\/h3>\n<ul>\n<li>\u4e00\u5bf9\u591a\uff08\u591a\u5bf9\u4e00\uff09<\/li>\n<li>\u591a\u5bf9\u591a<\/li>\n<li>\u4e00\u5bf9\u4e00<\/li>\n<\/ul>\n<h4>\u4e00\u5bf9\u591a<\/h4>\n<p>\u6848\u4f8b\uff1a\u90e8\u95e8\u4e0e\u5458\u5de5<br \/>\u5173\u7cfb\uff1a\u4e00\u4e2a\u90e8\u95e8\u5bf9\u5e94\u591a\u4e2a\u5458\u5de5\uff0c\u4e00\u4e2a\u5458\u5de5\u5bf9\u5e94\u4e00\u4e2a\u90e8\u95e8<br \/>\u5b9e\u73b0\uff1a\u5728\u591a\u7684\u4e00\u65b9\u5efa\u7acb\u5916\u952e\uff0c\u6307\u5411\u4e00\u7684\u4e00\u65b9\u7684\u4e3b\u952e<\/p>\n<h4>\u591a\u5bf9\u591a<\/h4>\n<p>\u6848\u4f8b\uff1a\u5b66\u751f\u4e0e\u8bfe\u7a0b<br \/>\u5173\u7cfb\uff1a\u4e00\u4e2a\u5b66\u751f\u53ef\u4ee5\u9009\u591a\u95e8\u8bfe\u7a0b\uff0c\u4e00\u95e8\u8bfe\u7a0b\u4e5f\u53ef\u4ee5\u4f9b\u591a\u4e2a\u5b66\u751f\u9009\u4fee<br \/>\u5b9e\u73b0\uff1a\u5efa\u7acb\u7b2c\u4e09\u5f20\u4e2d\u95f4\u8868\uff0c\u4e2d\u95f4\u8868\u81f3\u5c11\u5305\u542b\u4e24\u4e2a\u5916\u952e\uff0c\u5206\u522b\u5173\u8054\u4e24\u65b9\u4e3b\u952e<\/p>\n<h4>\u4e00\u5bf9\u4e00<\/h4>\n<p>\u6848\u4f8b\uff1a\u7528\u6237\u4e0e\u7528\u6237\u8be6\u60c5<br \/>\u5173\u7cfb\uff1a\u4e00\u5bf9\u4e00\u5173\u7cfb\uff0c\u591a\u7528\u4e8e\u5355\u8868\u62c6\u5206\uff0c\u5c06\u4e00\u5f20\u8868\u7684\u57fa\u7840\u5b57\u6bb5\u653e\u5728\u4e00\u5f20\u8868\u4e2d\uff0c\u5176\u4ed6\u8be6\u60c5\u5b57\u6bb5\u653e\u5728\u53e6\u4e00\u5f20\u8868\u4e2d\uff0c\u4ee5\u63d0\u5347\u64cd\u4f5c\u6548\u7387<br \/>\u5b9e\u73b0\uff1a\u5728\u4efb\u610f\u4e00\u65b9\u52a0\u5165\u5916\u952e\uff0c\u5173\u8054\u53e6\u5916\u4e00\u65b9\u7684\u4e3b\u952e\uff0c\u5e76\u4e14\u8bbe\u7f6e\u5916\u952e\u4e3a\u552f\u4e00\u7684\uff08UNIQUE\uff09<\/p>\n<h3>\u67e5\u8be2<\/h3>\n<p>\u5408\u5e76\u67e5\u8be2\uff08\u7b1b\u5361\u5c14\u79ef\uff0c\u4f1a\u5c55\u793a\u6240\u6709\u7ec4\u5408\u7ed3\u679c\uff09\uff1a<br \/><code>select * from employee, dept;<\/code><\/p>\n<blockquote><p>\u7b1b\u5361\u5c14\u79ef\uff1a\u4e24\u4e2a\u96c6\u5408A\u96c6\u5408\u548cB\u96c6\u5408\u7684\u6240\u6709\u7ec4\u5408\u60c5\u51b5\uff08\u5728\u591a\u8868\u67e5\u8be2\u65f6\uff0c\u9700\u8981\u6d88\u9664\u65e0\u6548\u7684\u7b1b\u5361\u5c14\u79ef\uff09<\/p><\/blockquote>\n<p>\u6d88\u9664\u65e0\u6548\u7b1b\u5361\u5c14\u79ef\uff1a<br \/><code>select * from employee, dept where employee.dept = dept.id;<\/code><\/p>\n<h3>\u5185\u8fde\u63a5\u67e5\u8be2<\/h3>\n<p>\u5185\u8fde\u63a5\u67e5\u8be2\u7684\u662f\u4e24\u5f20\u8868\u4ea4\u96c6\u7684\u90e8\u5206<\/p>\n<p>\u9690\u5f0f\u5185\u8fde\u63a5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u88681, \u88682 WHERE \u6761\u4ef6 ...;<\/code><\/p>\n<p>\u663e\u5f0f\u5185\u8fde\u63a5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u88681 [ INNER ] JOIN \u88682 ON \u8fde\u63a5\u6761\u4ef6 ...;<\/code><\/p>\n<p>\u663e\u5f0f\u6027\u80fd\u6bd4\u9690\u5f0f\u9ad8<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u5458\u5de5\u59d3\u540d\uff0c\u53ca\u5173\u8054\u7684\u90e8\u95e8\u7684\u540d\u79f0\n-- \u9690\u5f0f\nselect e.name, d.name from employee as e, dept as d where e.dept = d.id;\n-- \u663e\u5f0f\nselect e.name, d.name from employee as e inner join dept as d on e.dept = d.id;<\/code><\/pre>\n<h3>\u5916\u8fde\u63a5\u67e5\u8be2<\/h3>\n<p>\u5de6\u5916\u8fde\u63a5\uff1a<br \/>\u67e5\u8be2\u5de6\u8868\u6240\u6709\u6570\u636e\uff0c\u4ee5\u53ca\u4e24\u5f20\u8868\u4ea4\u96c6\u90e8\u5206\u6570\u636e<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u88681 LEFT [ OUTER ] JOIN \u88682 ON \u6761\u4ef6 ...;<\/code><br \/>\u76f8\u5f53\u4e8e\u67e5\u8be2\u88681\u7684\u6240\u6709\u6570\u636e\uff0c\u5305\u542b\u88681\u548c\u88682\u4ea4\u96c6\u90e8\u5206\u6570\u636e<\/p>\n<p>\u53f3\u5916\u8fde\u63a5\uff1a<br \/>\u67e5\u8be2\u53f3\u8868\u6240\u6709\u6570\u636e\uff0c\u4ee5\u53ca\u4e24\u5f20\u8868\u4ea4\u96c6\u90e8\u5206\u6570\u636e<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u88681 RIGHT [ OUTER ] JOIN \u88682 ON \u6761\u4ef6 ...;<\/code><\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u5de6\nselect e.*, d.name from employee as e left outer join dept as d on e.dept = d.id;\nselect d.name, e.* from dept d left outer join emp e on e.dept = d.id;  -- \u8fd9\u6761\u8bed\u53e5\u4e0e\u4e0b\u9762\u7684\u8bed\u53e5\u6548\u679c\u4e00\u6837\n-- \u53f3\nselect d.name, e.* from employee as e right outer join dept as d on e.dept = d.id;<\/code><\/pre>\n<p>\u5de6\u8fde\u63a5\u53ef\u4ee5\u67e5\u8be2\u5230\u6ca1\u6709dept\u7684employee\uff0c\u53f3\u8fde\u63a5\u53ef\u4ee5\u67e5\u8be2\u5230\u6ca1\u6709employee\u7684dept<\/p>\n<h3>\u81ea\u8fde\u63a5\u67e5\u8be2<\/h3>\n<p>\u5f53\u524d\u8868\u4e0e\u81ea\u8eab\u7684\u8fde\u63a5\u67e5\u8be2\uff0c\u81ea\u8fde\u63a5\u5fc5\u987b\u4f7f\u7528\u8868\u522b\u540d<\/p>\n<p>\u8bed\u6cd5\uff1a<br \/><code>SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868A \u522b\u540dA JOIN \u8868A \u522b\u540dB ON \u6761\u4ef6 ...;<\/code><\/p>\n<p>\u81ea\u8fde\u63a5\u67e5\u8be2\uff0c\u53ef\u4ee5\u662f\u5185\u8fde\u63a5\u67e5\u8be2\uff0c\u4e5f\u53ef\u4ee5\u662f\u5916\u8fde\u63a5\u67e5\u8be2<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u5458\u5de5\u53ca\u5176\u6240\u5c5e\u9886\u5bfc\u7684\u540d\u5b57\nselect a.name, b.name from employee a, employee b where a.manager = b.id;\n-- \u6ca1\u6709\u9886\u5bfc\u7684\u4e5f\u67e5\u8be2\u51fa\u6765\nselect a.name, b.name from employee a left join employee b on a.manager = b.id;<\/code><\/pre>\n<h3>\u8054\u5408\u67e5\u8be2 union, union all<\/h3>\n<p>\u628a\u591a\u6b21\u67e5\u8be2\u7684\u7ed3\u679c\u5408\u5e76\uff0c\u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u67e5\u8be2\u96c6<\/p>\n<p>\u8bed\u6cd5\uff1a<\/p>\n<pre><code class=\"lang-mysql\">SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868A ...\nUNION [ALL]\nSELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868B ...<\/code><\/pre>\n<h4>\u6ce8\u610f\u4e8b\u9879<\/h4>\n<ul>\n<li>UNION ALL \u4f1a\u6709\u91cd\u590d\u7ed3\u679c\uff0cUNION \u4e0d\u4f1a<\/li>\n<li>\u8054\u5408\u67e5\u8be2\u6bd4\u4f7f\u7528or\u6548\u7387\u9ad8\uff0c\u4e0d\u4f1a\u4f7f\u7d22\u5f15\u5931\u6548<\/li>\n<\/ul>\n<h3>\u5b50\u67e5\u8be2<\/h3>\n<p>SQL\u8bed\u53e5\u4e2d\u5d4c\u5957SELECT\u8bed\u53e5\uff0c\u79f0\u8c13\u5d4c\u5957\u67e5\u8be2\uff0c\u53c8\u79f0\u5b50\u67e5\u8be2\u3002<br \/><code>SELECT * FROM t1 WHERE column1 = ( SELECT column1 FROM t2);<\/code><br \/><strong>\u5b50\u67e5\u8be2\u5916\u90e8\u7684\u8bed\u53e5\u53ef\u4ee5\u662f INSERT \/ UPDATE \/ DELETE \/ SELECT \u7684\u4efb\u4f55\u4e00\u4e2a<\/strong><\/p>\n<p>\u6839\u636e\u5b50\u67e5\u8be2\u7ed3\u679c\u53ef\u4ee5\u5206\u4e3a\uff1a<\/p>\n<ul>\n<li>\u6807\u91cf\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u5355\u4e2a\u503c\uff09<\/li>\n<li>\u5217\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u4e00\u5217\uff09<\/li>\n<li>\u884c\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u4e00\u884c\uff09<\/li>\n<li>\u8868\u5b50\u67e5\u8be2\uff08\u5b50\u67e5\u8be2\u7ed3\u679c\u4e3a\u591a\u884c\u591a\u5217\uff09<\/li>\n<\/ul>\n<p>\u6839\u636e\u5b50\u67e5\u8be2\u4f4d\u7f6e\u53ef\u5206\u4e3a\uff1a<\/p>\n<ul>\n<li>WHERE \u4e4b\u540e<\/li>\n<li>FROM \u4e4b\u540e<\/li>\n<li>SELECT \u4e4b\u540e<\/li>\n<\/ul>\n<h4>\u6807\u91cf\u5b50\u67e5\u8be2<\/h4>\n<p>\u5b50\u67e5\u8be2\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u5355\u4e2a\u503c\uff08\u6570\u5b57\u3001\u5b57\u7b26\u4e32\u3001\u65e5\u671f\u7b49\uff09\u3002<br \/>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1a- &lt; &gt; &gt; &gt;= &lt; &lt;=<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u9500\u552e\u90e8\u6240\u6709\u5458\u5de5\nselect id from dept where name = &#039;\u9500\u552e\u90e8&#039;;\n-- \u6839\u636e\u9500\u552e\u90e8\u90e8\u95e8ID\uff0c\u67e5\u8be2\u5458\u5de5\u4fe1\u606f\nselect * from employee where dept = 4;\n-- \u5408\u5e76\uff08\u5b50\u67e5\u8be2\uff09\nselect * from employee where dept = (select id from dept where name = &#039;\u9500\u552e\u90e8&#039;);\n\n-- \u67e5\u8be2xxx\u5165\u804c\u4e4b\u540e\u7684\u5458\u5de5\u4fe1\u606f\nselect * from employee where entrydate &gt; (select entrydate from employee where name = &#039;xxx&#039;);<\/code><\/pre>\n<h4>\u5217\u5b50\u67e5\u8be2<\/h4>\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u5217\uff08\u53ef\u4ee5\u662f\u591a\u884c\uff09\u3002<\/p>\n<p>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u64cd\u4f5c\u7b26<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>IN<\/td>\n<td>\u5728\u6307\u5b9a\u7684\u96c6\u5408\u8303\u56f4\u5185\uff0c\u591a\u9009\u4e00<\/td>\n<\/tr>\n<tr>\n<td>NOT IN<\/td>\n<td>\u4e0d\u5728\u6307\u5b9a\u7684\u96c6\u5408\u8303\u56f4\u5185<\/td>\n<\/tr>\n<tr>\n<td>ANY<\/td>\n<td>\u5b50\u67e5\u8be2\u8fd4\u56de\u5217\u8868\u4e2d\uff0c\u6709\u4efb\u610f\u4e00\u4e2a\u6ee1\u8db3\u5373\u53ef<\/td>\n<\/tr>\n<tr>\n<td>SOME<\/td>\n<td>\u4e0eANY\u7b49\u540c\uff0c\u4f7f\u7528SOME\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528ANY<\/td>\n<\/tr>\n<tr>\n<td>ALL<\/td>\n<td>\u5b50\u67e5\u8be2\u8fd4\u56de\u5217\u8868\u7684\u6240\u6709\u503c\u90fd\u5fc5\u987b\u6ee1\u8db3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u9500\u552e\u90e8\u548c\u5e02\u573a\u90e8\u7684\u6240\u6709\u5458\u5de5\u4fe1\u606f\nselect * from employee where dept in (select id from dept where name = &#039;\u9500\u552e\u90e8&#039; or name = &#039;\u5e02\u573a\u90e8&#039;);\n-- \u67e5\u8be2\u6bd4\u8d22\u52a1\u90e8\u6240\u6709\u4eba\u5de5\u8d44\u90fd\u9ad8\u7684\u5458\u5de5\u4fe1\u606f\nselect * from employee where salary &gt; all(select salary from employee where dept = (select id from dept where name = &#039;\u8d22\u52a1\u90e8&#039;));\n-- \u67e5\u8be2\u6bd4\u7814\u53d1\u90e8\u4efb\u610f\u4e00\u4eba\u5de5\u8d44\u9ad8\u7684\u5458\u5de5\u4fe1\u606f\nselect * from employee where salary &gt; any (select salary from employee where dept = (select id from dept where name = &#039;\u7814\u53d1\u90e8&#039;));<\/code><\/pre>\n<h4>\u884c\u5b50\u67e5\u8be2<\/h4>\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u4e00\u884c\uff08\u53ef\u4ee5\u662f\u591a\u5217\uff09\u3002<br \/>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1a=, &lt;, &gt;, IN, NOT IN<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u4e0exxx\u7684\u85aa\u8d44\u53ca\u76f4\u5c5e\u9886\u5bfc\u76f8\u540c\u7684\u5458\u5de5\u4fe1\u606f\nselect * from employee where (salary, manager) = (12500, 1);\nselect * from employee where (salary, manager) = (select salary, manager from employee where name = &#039;xxx&#039;);<\/code><\/pre>\n<h4>\u8868\u5b50\u67e5\u8be2<\/h4>\n<p>\u8fd4\u56de\u7684\u7ed3\u679c\u662f\u591a\u884c\u591a\u5217<br \/>\u5e38\u7528\u64cd\u4f5c\u7b26\uff1aIN<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u4e0exxx1\uff0cxxx2\u7684\u804c\u4f4d\u548c\u85aa\u8d44\u76f8\u540c\u7684\u5458\u5de5\nselect * from employee where (job, salary) in (select job, salary from employee where name = &#039;xxx1&#039; or name = &#039;xxx2&#039;);\n-- \u67e5\u8be2\u5165\u804c\u65e5\u671f\u662f2006-01-01\u4e4b\u540e\u7684\u5458\u5de5\uff0c\u53ca\u5176\u90e8\u95e8\u4fe1\u606f\nselect e.*, d.* from (select * from employee where entrydate &gt; &#039;2006-01-01&#039;) as e left join dept as d on e.dept = d.id;<\/code><\/pre>\n<h2>\u4e8b\u52a1<\/h2>\n<p>\u4e8b\u52a1\u662f\u4e00\u7ec4\u64cd\u4f5c\u7684\u96c6\u5408\uff0c\u4e8b\u52a1\u4f1a\u628a\u6240\u6709\u64cd\u4f5c\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u4e00\u8d77\u5411\u7cfb\u7edf\u63d0\u4ea4\u6216\u64a4\u9500\u64cd\u4f5c\u8bf7\u6c42\uff0c\u5373\u8fd9\u4e9b\u64cd\u4f5c\u8981\u4e48\u540c\u65f6\u6210\u529f\uff0c\u8981\u4e48\u540c\u65f6\u5931\u8d25\u3002<\/p>\n<p>\u57fa\u672c\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- 1. \u67e5\u8be2\u5f20\u4e09\u8d26\u6237\u4f59\u989d\nselect * from account where name = &#039;\u5f20\u4e09&#039;;\n-- 2. \u5c06\u5f20\u4e09\u8d26\u6237\u4f59\u989d-1000\nupdate account set money = money - 1000 where name = &#039;\u5f20\u4e09&#039;;\n-- \u6b64\u8bed\u53e5\u51fa\u9519\u540e\u5f20\u4e09\u94b1\u51cf\u5c11\u4f46\u662f\u674e\u56db\u94b1\u6ca1\u6709\u589e\u52a0\n\u6a21\u62dfsql\u8bed\u53e5\u9519\u8bef\n-- 3. \u5c06\u674e\u56db\u8d26\u6237\u4f59\u989d+1000\nupdate account set money = money + 1000 where name = &#039;\u674e\u56db&#039;;\n\n-- \u67e5\u770b\u4e8b\u52a1\u63d0\u4ea4\u65b9\u5f0f\nSELECT @@AUTOCOMMIT;\n-- \u8bbe\u7f6e\u4e8b\u52a1\u63d0\u4ea4\u65b9\u5f0f\uff0c1\u4e3a\u81ea\u52a8\u63d0\u4ea4\uff0c0\u4e3a\u624b\u52a8\u63d0\u4ea4\uff0c\u8be5\u8bbe\u7f6e\u53ea\u5bf9\u5f53\u524d\u4f1a\u8bdd\u6709\u6548\nSET @@AUTOCOMMIT = 0;\n-- \u63d0\u4ea4\u4e8b\u52a1\nCOMMIT;\n-- \u56de\u6eda\u4e8b\u52a1\nROLLBACK;\n\n-- \u8bbe\u7f6e\u624b\u52a8\u63d0\u4ea4\u540e\u4e0a\u9762\u4ee3\u7801\u6539\u4e3a\uff1a\nselect * from account where name = &#039;\u5f20\u4e09&#039;;\nupdate account set money = money - 1000 where name = &#039;\u5f20\u4e09&#039;;\nupdate account set money = money + 1000 where name = &#039;\u674e\u56db&#039;;\ncommit;<\/code><\/pre>\n<p>\u64cd\u4f5c\u65b9\u5f0f\u4e8c\uff1a<\/p>\n<p>\u5f00\u542f\u4e8b\u52a1\uff1a<br \/><code>START TRANSACTION \u6216 BEGIN TRANSACTION;<\/code><br \/>\u63d0\u4ea4\u4e8b\u52a1\uff1a<br \/><code>COMMIT;<\/code><br \/>\u56de\u6eda\u4e8b\u52a1\uff1a<br \/><code>ROLLBACK;<\/code><\/p>\n<p>\u64cd\u4f5c\u5b9e\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-mysql\">start transaction;\nselect * from account where name = &#039;\u5f20\u4e09&#039;;\nupdate account set money = money - 1000 where name = &#039;\u5f20\u4e09&#039;;\nupdate account set money = money + 1000 where name = &#039;\u674e\u56db&#039;;\ncommit;<\/code><\/pre>\n<h3>\u56db\u5927\u7279\u6027ACID<\/h3>\n<ul>\n<li>\u539f\u5b50\u6027(Atomicity)\uff1a\u4e8b\u52a1\u662f\u4e0d\u53ef\u5206\u5272\u7684\u6700\u5c0f\u64cd\u4f5c\u4f46\u613f\uff0c\u8981\u4e48\u5168\u90e8\u6210\u529f\uff0c\u8981\u4e48\u5168\u90e8\u5931\u8d25<\/li>\n<li>\u4e00\u81f4\u6027(Consistency)\uff1a\u4e8b\u52a1\u5b8c\u6210\u65f6\uff0c\u5fc5\u987b\u4f7f\u6240\u6709\u6570\u636e\u90fd\u4fdd\u6301\u4e00\u81f4\u72b6\u6001<\/li>\n<li>\u9694\u79bb\u6027(Isolation)\uff1a\u6570\u636e\u5e93\u7cfb\u7edf\u63d0\u4f9b\u7684\u9694\u79bb\u673a\u5236\uff0c\u4fdd\u8bc1\u4e8b\u52a1\u5728\u4e0d\u53d7\u5916\u90e8\u5e76\u53d1\u64cd\u4f5c\u5f71\u54cd\u7684\u72ec\u7acb\u73af\u5883\u4e0b\u8fd0\u884c<\/li>\n<li>\u6301\u4e45\u6027(Durability)\uff1a\u4e8b\u52a1\u4e00\u65e6\u63d0\u4ea4\u6216\u56de\u6eda\uff0c\u5b83\u5bf9\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u7684\u6539\u53d8\u5c31\u662f\u6c38\u4e45\u7684<\/li>\n<\/ul>\n<h3>\u5e76\u53d1\u4e8b\u52a1<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u95ee\u9898<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u810f\u8bfb<\/td>\n<td>\u4e00\u4e2a\u4e8b\u52a1\u8bfb\u5230\u53e6\u4e00\u4e2a\u4e8b\u52a1\u8fd8\u6ca1\u63d0\u4ea4\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>\u4e0d\u53ef\u91cd\u590d\u8bfb<\/td>\n<td>\u4e00\u4e2a\u4e8b\u52a1\u5148\u540e\u8bfb\u53d6\u540c\u4e00\u6761\u8bb0\u5f55\uff0c\u4f46\u4e24\u6b21\u8bfb\u53d6\u7684\u6570\u636e\u4e0d\u540c<\/td>\n<\/tr>\n<tr>\n<td>\u5e7b\u8bfb<\/td>\n<td>\u4e00\u4e2a\u4e8b\u52a1\u6309\u7167\u6761\u4ef6\u67e5\u8be2\u6570\u636e\u65f6\uff0c\u6ca1\u6709\u5bf9\u5e94\u7684\u6570\u636e\u884c\uff0c\u4f46\u662f\u518d\u63d2\u5165\u6570\u636e\u65f6\uff0c\u53c8\u53d1\u73b0\u8fd9\u884c\u6570\u636e\u5df2\u7ecf\u5b58\u5728<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote><p>\u8fd9\u4e09\u4e2a\u95ee\u9898\u7684\u8be6\u7ec6\u6f14\u793a\uff1a<a href=\"https:\/\/www.bilibili.com\/video\/BV1Kr4y1i7ru?p=55cd\">https:\/\/www.bilibili.com\/video\/BV1Kr4y1i7ru?p=55cd<\/a><\/p><\/blockquote>\n<p>\u5e76\u53d1\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u9694\u79bb\u7ea7\u522b<\/th>\n<th>\u810f\u8bfb<\/th>\n<th>\u4e0d\u53ef\u91cd\u590d\u8bfb<\/th>\n<th>\u5e7b\u8bfb<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Read uncommitted<\/td>\n<td>\u221a<\/td>\n<td>\u221a<\/td>\n<td>\u221a<\/td>\n<\/tr>\n<tr>\n<td>Read committed<\/td>\n<td>\u00d7<\/td>\n<td>\u221a<\/td>\n<td>\u221a<\/td>\n<\/tr>\n<tr>\n<td>Repeatable Read(\u9ed8\u8ba4)<\/td>\n<td>\u00d7<\/td>\n<td>\u00d7<\/td>\n<td>\u221a<\/td>\n<\/tr>\n<tr>\n<td>Serializable<\/td>\n<td>\u00d7<\/td>\n<td>\u00d7<\/td>\n<td>\u00d7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u221a\u8868\u793a\u5728\u5f53\u524d\u9694\u79bb\u7ea7\u522b\u4e0b\u8be5\u95ee\u9898\u4f1a\u51fa\u73b0<\/li>\n<li>Serializable \u6027\u80fd\u6700\u4f4e\uff1bRead uncommitted \u6027\u80fd\u6700\u9ad8\uff0c\u6570\u636e\u5b89\u5168\u6027\u6700\u5dee<\/li>\n<\/ul>\n<p>\u67e5\u770b\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff1a<br \/><code>SELECT @@TRANSACTION_ISOLATION;<\/code><br \/>\u8bbe\u7f6e\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\uff1a<br \/><code>SET [ SESSION | GLOBAL ] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE };<\/code><br \/>SESSION \u662f\u4f1a\u8bdd\u7ea7\u522b\uff0c\u8868\u793a\u53ea\u9488\u5bf9\u5f53\u524d\u4f1a\u8bdd\u6709\u6548\uff0cGLOBAL \u8868\u793a\u5bf9\u6240\u6709\u4f1a\u8bdd\u6709\u6548<\/p>\n<h1>\u8fdb\u9636\u7bc7<\/h1>\n<h2>\u5b58\u50a8\u5f15\u64ce<\/h2>\n<p>MySQL\u4f53\u7cfb\u7ed3\u6784\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/MySQL\u4f53\u7cfb\u7ed3\u6784_20220315034329549927.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FMySQL%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84_20220315034329549927.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FMySQL%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84_20220315034329549927.png\"\"><br \/><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/MySQL\u4f53\u7cfb\u7ed3\u6784\u5c42\u7ea7\u542b\u4e49_20220315034359342837.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FMySQL%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84%E5%B1%82%E7%BA%A7%E5%90%AB%E4%B9%89_20220315034359342837.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FMySQL%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84%E5%B1%82%E7%BA%A7%E5%90%AB%E4%B9%89_20220315034359342837.png\"\"><\/p>\n<p>\u5b58\u50a8\u5f15\u64ce\u5c31\u662f\u5b58\u50a8\u6570\u636e\u3001\u5efa\u7acb\u7d22\u5f15\u3001\u66f4\u65b0\/\u67e5\u8be2\u6570\u636e\u7b49\u6280\u672f\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u5b58\u50a8\u5f15\u64ce\u662f\u57fa\u4e8e\u8868\u800c\u4e0d\u662f\u57fa\u4e8e\u5e93\u7684\uff0c\u6240\u4ee5\u5b58\u50a8\u5f15\u64ce\u4e5f\u53ef\u4ee5\u88ab\u79f0\u4e3a\u8868\u5f15\u64ce\u3002<br \/>\u9ed8\u8ba4\u5b58\u50a8\u5f15\u64ce\u662fInnoDB\u3002<\/p>\n<p>\u76f8\u5173\u64cd\u4f5c\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u67e5\u8be2\u5efa\u8868\u8bed\u53e5\nshow create table account;\n-- \u5efa\u8868\u65f6\u6307\u5b9a\u5b58\u50a8\u5f15\u64ce\nCREATE TABLE \u8868\u540d(\n    ...\n) ENGINE=INNODB;\n-- \u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u652f\u6301\u7684\u5b58\u50a8\u5f15\u64ce\nshow engines;<\/code><\/pre>\n<h3>InnoDB<\/h3>\n<p>InnoDB \u662f\u4e00\u79cd\u517c\u987e\u9ad8\u53ef\u9760\u6027\u548c\u9ad8\u6027\u80fd\u7684\u901a\u7528\u5b58\u50a8\u5f15\u64ce\uff0c\u5728 MySQL 5.5 \u4e4b\u540e\uff0cInnoDB \u662f\u9ed8\u8ba4\u7684 MySQL \u5f15\u64ce\u3002<\/p>\n<p>\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>DML \u64cd\u4f5c\u9075\u5faa ACID \u6a21\u578b\uff0c\u652f\u6301<strong>\u4e8b\u52a1<\/strong><\/li>\n<li><strong>\u884c\u7ea7\u9501<\/strong>\uff0c\u63d0\u9ad8\u5e76\u53d1\u8bbf\u95ee\u6027\u80fd<\/li>\n<li>\u652f\u6301<strong>\u5916\u952e<\/strong>\u7ea6\u675f\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6b63\u786e\u6027<\/li>\n<\/ul>\n<p>\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>xxx.ibd: xxx\u4ee3\u8868\u8868\u540d\uff0cInnoDB \u5f15\u64ce\u7684\u6bcf\u5f20\u8868\u90fd\u4f1a\u5bf9\u5e94\u8fd9\u6837\u4e00\u4e2a\u8868\u7a7a\u95f4\u6587\u4ef6\uff0c\u5b58\u50a8\u8be5\u8868\u7684\u8868\u7ed3\u6784\uff08frm\u3001sdi\uff09\u3001\u6570\u636e\u548c\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p>\u53c2\u6570\uff1ainnodb_file_per_table\uff0c\u51b3\u5b9a\u591a\u5f20\u8868\u5171\u4eab\u4e00\u4e2a\u8868\u7a7a\u95f4\u8fd8\u662f\u6bcf\u5f20\u8868\u5bf9\u5e94\u4e00\u4e2a\u8868\u7a7a\u95f4<\/p>\n<p>\u77e5\u8bc6\u70b9\uff1a<\/p>\n<p>\u67e5\u770b Mysql \u53d8\u91cf\uff1a<br \/><code>show variables like &#039;innodb_file_per_table&#039;;<\/code><\/p>\n<p>\u4eceidb\u6587\u4ef6\u63d0\u53d6\u8868\u7ed3\u6784\u6570\u636e\uff1a<br \/>\uff08\u5728cmd\u8fd0\u884c\uff09<br \/><code>ibd2sdi xxx.ibd<\/code><\/p>\n<p>InnoDB \u903b\u8f91\u5b58\u50a8\u7ed3\u6784\uff1a<br \/><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/\u903b\u8f91\u5b58\u50a8\u7ed3\u6784_20220316030616590001.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E9%80%BB%E8%BE%91%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84_20220316030616590001.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E9%80%BB%E8%BE%91%E5%AD%98%E5%82%A8%E7%BB%93%E6%9E%84_20220316030616590001.png\"\"><\/p>\n<h3>MyISAM<\/h3>\n<p>MyISAM \u662f MySQL \u65e9\u671f\u7684\u9ed8\u8ba4\u5b58\u50a8\u5f15\u64ce\u3002<\/p>\n<p>\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>\u4e0d\u652f\u6301\u4e8b\u52a1\uff0c\u4e0d\u652f\u6301\u5916\u952e<\/li>\n<li>\u652f\u6301\u8868\u9501\uff0c\u4e0d\u652f\u6301\u884c\u9501<\/li>\n<li>\u8bbf\u95ee\u901f\u5ea6\u5feb<\/li>\n<\/ul>\n<p>\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>xxx.sdi: \u5b58\u50a8\u8868\u7ed3\u6784\u4fe1\u606f<\/li>\n<li>xxx.MYD: \u5b58\u50a8\u6570\u636e<\/li>\n<li>xxx.MYI: \u5b58\u50a8\u7d22\u5f15<\/li>\n<\/ul>\n<h3>Memory<\/h3>\n<p>Memory \u5f15\u64ce\u7684\u8868\u6570\u636e\u662f\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\uff0c\u53d7\u786c\u4ef6\u95ee\u9898\u3001\u65ad\u7535\u95ee\u9898\u7684\u5f71\u54cd\uff0c\u53ea\u80fd\u5c06\u8fd9\u4e9b\u8868\u4f5c\u4e3a\u4e34\u65f6\u8868\u6216\u7f13\u5b58\u4f7f\u7528\u3002<\/p>\n<p>\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>\u5b58\u653e\u5728\u5185\u5b58\u4e2d\uff0c\u901f\u5ea6\u5feb<\/li>\n<li>hash\u7d22\u5f15\uff08\u9ed8\u8ba4\uff09<\/li>\n<\/ul>\n<p>\u6587\u4ef6\uff1a<\/p>\n<ul>\n<li>xxx.sdi: \u5b58\u50a8\u8868\u7ed3\u6784\u4fe1\u606f<\/li>\n<\/ul>\n<h3>\u5b58\u50a8\u5f15\u64ce\u7279\u70b9<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7279\u70b9<\/th>\n<th>InnoDB<\/th>\n<th>MyISAM<\/th>\n<th>Memory<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u5b58\u50a8\u9650\u5236<\/td>\n<td>64TB<\/td>\n<td>\u6709<\/td>\n<td>\u6709<\/td>\n<\/tr>\n<tr>\n<td>\u4e8b\u52a1\u5b89\u5168<\/td>\n<td>\u652f\u6301<\/td>\n<td>-<\/td>\n<td>-<\/td>\n<\/tr>\n<tr>\n<td>\u9501\u673a\u5236<\/td>\n<td>\u884c\u9501<\/td>\n<td>\u8868\u9501<\/td>\n<td>\u8868\u9501<\/td>\n<\/tr>\n<tr>\n<td>B+tree\u7d22\u5f15<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>Hash\u7d22\u5f15<\/td>\n<td>-<\/td>\n<td>-<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u5168\u6587\u7d22\u5f15<\/td>\n<td>\u652f\u6301\uff085.6\u7248\u672c\u4e4b\u540e\uff09<\/td>\n<td>\u652f\u6301<\/td>\n<td>-<\/td>\n<\/tr>\n<tr>\n<td>\u7a7a\u95f4\u4f7f\u7528<\/td>\n<td>\u9ad8<\/td>\n<td>\u4f4e<\/td>\n<td>N\/A<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u4f7f\u7528<\/td>\n<td>\u9ad8<\/td>\n<td>\u4f4e<\/td>\n<td>\u4e2d\u7b49<\/td>\n<\/tr>\n<tr>\n<td>\u6279\u91cf\u63d2\u5165\u901f\u5ea6<\/td>\n<td>\u4f4e<\/td>\n<td>\u9ad8<\/td>\n<td>\u9ad8<\/td>\n<\/tr>\n<tr>\n<td>\u652f\u6301\u5916\u952e<\/td>\n<td>\u652f\u6301<\/td>\n<td>-<\/td>\n<td>-<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5b58\u50a8\u5f15\u64ce\u7684\u9009\u62e9<\/h3>\n<p>\u5728\u9009\u62e9\u5b58\u50a8\u5f15\u64ce\u65f6\uff0c\u5e94\u8be5\u6839\u636e\u5e94\u7528\u7cfb\u7edf\u7684\u7279\u70b9\u9009\u62e9\u5408\u9002\u7684\u5b58\u50a8\u5f15\u64ce\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u5e94\u7528\u7cfb\u7edf\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u591a\u79cd\u5b58\u50a8\u5f15\u64ce\u8fdb\u884c\u7ec4\u5408\u3002<\/p>\n<ul>\n<li>InnoDB: \u5982\u679c\u5e94\u7528\u5bf9\u4e8b\u7269\u7684\u5b8c\u6574\u6027\u6709\u6bd4\u8f83\u9ad8\u7684\u8981\u6c42\uff0c\u5728\u5e76\u53d1\u6761\u4ef6\u4e0b\u8981\u6c42\u6570\u636e\u7684\u4e00\u81f4\u6027\uff0c\u6570\u636e\u64cd\u4f5c\u9664\u4e86\u63d2\u5165\u548c\u67e5\u8be2\u4e4b\u5916\uff0c\u8fd8\u5305\u542b\u5f88\u591a\u7684\u66f4\u65b0\u3001\u5220\u9664\u64cd\u4f5c\uff0c\u5219 InnoDB \u662f\u6bd4\u8f83\u5408\u9002\u7684\u9009\u62e9<\/li>\n<li>MyISAM: \u5982\u679c\u5e94\u7528\u662f\u4ee5\u8bfb\u64cd\u4f5c\u548c\u63d2\u5165\u64cd\u4f5c\u4e3a\u4e3b\uff0c\u53ea\u6709\u5f88\u5c11\u7684\u66f4\u65b0\u548c\u5220\u9664\u64cd\u4f5c\uff0c\u5e76\u4e14\u5bf9\u4e8b\u52a1\u7684\u5b8c\u6574\u6027\u3001\u5e76\u53d1\u6027\u8981\u6c42\u4e0d\u9ad8\uff0c\u90a3\u8fd9\u4e2a\u5b58\u50a8\u5f15\u64ce\u662f\u975e\u5e38\u5408\u9002\u7684\u3002<\/li>\n<li>Memory: \u5c06\u6240\u6709\u6570\u636e\u4fdd\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u8bbf\u95ee\u901f\u5ea6\u5feb\uff0c\u901a\u5e38\u7528\u4e8e\u4e34\u65f6\u8868\u53ca\u7f13\u5b58\u3002Memory \u7684\u7f3a\u9677\u662f\u5bf9\u8868\u7684\u5927\u5c0f\u6709\u9650\u5236\uff0c\u592a\u5927\u7684\u8868\u65e0\u6cd5\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u800c\u4e14\u65e0\u6cd5\u4fdd\u969c\u6570\u636e\u7684\u5b89\u5168\u6027<\/li>\n<\/ul>\n<p>\u7535\u5546\u4e2d\u7684\u8db3\u8ff9\u548c\u8bc4\u8bba\u9002\u5408\u4f7f\u7528 MyISAM \u5f15\u64ce\uff0c\u7f13\u5b58\u9002\u5408\u4f7f\u7528 Memory \u5f15\u64ce\u3002<\/p>\n<h2>\u6027\u80fd\u5206\u6790<\/h2>\n<h3>\u67e5\u770b\u6267\u884c\u9891\u6b21<\/h3>\n<p>\u67e5\u770b\u5f53\u524d\u6570\u636e\u5e93\u7684 INSERT, UPDATE, DELETE, SELECT \u8bbf\u95ee\u9891\u6b21\uff1a<br \/><code>SHOW GLOBAL STATUS LIKE &#039;Com_______&#039;;<\/code> \u6216\u8005 <code>SHOW SESSION STATUS LIKE &#039;Com_______&#039;;<\/code><br \/>\u4f8b\uff1a<code>show global status like &#039;Com_______&#039;<\/code><\/p>\n<h3>\u6162\u67e5\u8be2\u65e5\u5fd7<\/h3>\n<p>\u6162\u67e5\u8be2\u65e5\u5fd7\u8bb0\u5f55\u4e86\u6240\u6709\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u53c2\u6570\uff08long_query_time\uff0c\u5355\u4f4d\uff1a\u79d2\uff0c\u9ed8\u8ba410\u79d2\uff09\u7684\u6240\u6709SQL\u8bed\u53e5\u7684\u65e5\u5fd7\u3002<br \/>MySQL\u7684\u6162\u67e5\u8be2\u65e5\u5fd7\u9ed8\u8ba4\u6ca1\u6709\u5f00\u542f\uff0c\u9700\u8981\u5728MySQL\u7684\u914d\u7f6e\u6587\u4ef6\uff08\/etc\/my.cnf\uff09\u4e2d\u914d\u7f6e\u5982\u4e0b\u4fe1\u606f\uff1a<\/p>\n<h1>\u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7\u5f00\u5173<\/h1>\n<p>slow_query_log=1<\/p>\n<h1>\u8bbe\u7f6e\u6162\u67e5\u8be2\u65e5\u5fd7\u7684\u65f6\u95f4\u4e3a2\u79d2\uff0cSQL\u8bed\u53e5\u6267\u884c\u65f6\u95f4\u8d85\u8fc72\u79d2\uff0c\u5c31\u4f1a\u89c6\u4e3a\u6162\u67e5\u8be2\uff0c\u8bb0\u5f55\u6162\u67e5\u8be2\u65e5\u5fd7<\/h1>\n<p>long_query_time=2<br \/>\u66f4\u6539\u540e\u8bb0\u5f97\u91cd\u542fMySQL\u670d\u52a1\uff0c\u65e5\u5fd7\u6587\u4ef6\u4f4d\u7f6e\uff1a\/var\/lib\/mysql\/localhost-slow.log<\/p>\n<p>\u67e5\u770b\u6162\u67e5\u8be2\u65e5\u5fd7\u5f00\u5173\u72b6\u6001\uff1a<br \/><code>show variables like &#039;slow_query_log&#039;;<\/code><\/p>\n<h3>profile<\/h3>\n<p>show profile \u80fd\u5728\u505aSQL\u4f18\u5316\u65f6\u5e2e\u6211\u4eec\u4e86\u89e3\u65f6\u95f4\u90fd\u8017\u8d39\u5728\u54ea\u91cc\u3002\u901a\u8fc7 have_profiling \u53c2\u6570\uff0c\u80fd\u770b\u5230\u5f53\u524d MySQL \u662f\u5426\u652f\u6301 profile \u64cd\u4f5c\uff1a<br \/><code>SELECT @@have_profiling;<\/code><br \/>profiling \u9ed8\u8ba4\u5173\u95ed\uff0c\u53ef\u4ee5\u901a\u8fc7set\u8bed\u53e5\u5728session\/global\u7ea7\u522b\u5f00\u542f profiling\uff1a<br \/><code>SET profiling = 1;<\/code><br \/>\u67e5\u770b\u6240\u6709\u8bed\u53e5\u7684\u8017\u65f6\uff1a<br \/><code>show profiles;<\/code><br \/>\u67e5\u770b\u6307\u5b9aquery_id\u7684SQL\u8bed\u53e5\u5404\u4e2a\u9636\u6bb5\u7684\u8017\u65f6\uff1a<br \/><code>show profile for query query_id;<\/code><br \/>\u67e5\u770b\u6307\u5b9aquery_id\u7684SQL\u8bed\u53e5CPU\u7684\u4f7f\u7528\u60c5\u51b5<br \/><code>show profile cpu for query query_id;<\/code><\/p>\n<h3>explain<\/h3>\n<p>EXPLAIN \u6216\u8005 DESC \u547d\u4ee4\u83b7\u53d6 MySQL \u5982\u4f55\u6267\u884c SELECT \u8bed\u53e5\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u5728 SELECT \u8bed\u53e5\u6267\u884c\u8fc7\u7a0b\u4e2d\u8868\u5982\u4f55\u8fde\u63a5\u548c\u8fde\u63a5\u7684\u987a\u5e8f\u3002<br \/>\u8bed\u6cd5\uff1a<\/p>\n<h1>\u76f4\u63a5\u5728select\u8bed\u53e5\u4e4b\u524d\u52a0\u4e0a\u5173\u952e\u5b57 explain \/ desc<\/h1>\n<p>EXPLAIN SELECT \u5b57\u6bb5\u5217\u8868 FROM \u8868\u540d HWERE \u6761\u4ef6;<\/p>\n<p>EXPLAIN \u5404\u5b57\u6bb5\u542b\u4e49\uff1a<\/p>\n<ul>\n<li>id\uff1aselect \u67e5\u8be2\u7684\u5e8f\u5217\u53f7\uff0c\u8868\u793a\u67e5\u8be2\u4e2d\u6267\u884c select \u5b50\u53e5\u6216\u8005\u64cd\u4f5c\u8868\u7684\u987a\u5e8f\uff08id\u76f8\u540c\uff0c\u6267\u884c\u987a\u5e8f\u4ece\u4e0a\u5230\u4e0b\uff1bid\u4e0d\u540c\uff0c\u503c\u8d8a\u5927\u8d8a\u5148\u6267\u884c\uff09<\/li>\n<li>select_type\uff1a\u8868\u793a SELECT \u7684\u7c7b\u578b\uff0c\u5e38\u89c1\u53d6\u503c\u6709 SIMPLE\uff08\u7b80\u5355\u8868\uff0c\u5373\u4e0d\u9002\u7528\u8868\u8fde\u63a5\u6216\u8005\u5b50\u67e5\u8be2\uff09\u3001PRIMARY\uff08\u4e3b\u67e5\u8be2\uff0c\u5373\u5916\u5c42\u7684\u67e5\u8be2\uff09\u3001UNION\uff08UNION\u4e2d\u7684\u7b2c\u4e8c\u4e2a\u6216\u8005\u540e\u9762\u7684\u67e5\u8be2\u8bed\u53e5\uff09\u3001SUBQUERY\uff08SELECT\/WHERE\u4e4b\u540e\u5305\u542b\u4e86\u5b50\u67e5\u8be2\uff09\u7b49<\/li>\n<li>type\uff1a\u8868\u793a\u8fde\u63a5\u7c7b\u578b\uff0c\u6027\u80fd\u7531\u597d\u5230\u5dee\u7684\u8fde\u63a5\u7c7b\u578b\u4e3a NULL\u3001system\u3001const\u3001eq_ref\u3001ref\u3001range\u3001index\u3001all<\/li>\n<li>possible_key\uff1a\u53ef\u80fd\u5e94\u7528\u5728\u8fd9\u5f20\u8868\u4e0a\u7684\u7d22\u5f15\uff0c\u4e00\u4e2a\u6216\u591a\u4e2a<\/li>\n<li>Key\uff1a\u5b9e\u9645\u4f7f\u7528\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e3a NULL\uff0c\u5219\u6ca1\u6709\u4f7f\u7528\u7d22\u5f15<\/li>\n<li>Key_len\uff1a\u8868\u793a\u7d22\u5f15\u4e2d\u4f7f\u7528\u7684\u5b57\u8282\u6570\uff0c\u8be5\u503c\u4e3a\u7d22\u5f15\u5b57\u6bb5\u6700\u5927\u53ef\u80fd\u957f\u5ea6\uff0c\u5e76\u975e\u5b9e\u9645\u4f7f\u7528\u957f\u5ea6\uff0c\u5728\u4e0d\u635f\u5931\u7cbe\u786e\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u957f\u5ea6\u8d8a\u77ed\u8d8a\u597d<\/li>\n<li>rows\uff1aMySQL\u8ba4\u4e3a\u5fc5\u987b\u8981\u6267\u884c\u7684\u884c\u6570\uff0c\u5728InnoDB\u5f15\u64ce\u7684\u8868\u4e2d\uff0c\u662f\u4e00\u4e2a\u4f30\u8ba1\u503c\uff0c\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u51c6\u786e\u7684<\/li>\n<li>filtered\uff1a\u8868\u793a\u8fd4\u56de\u7ed3\u679c\u7684\u884c\u6570\u5360\u9700\u8bfb\u53d6\u884c\u6570\u7684\u767e\u5206\u6bd4\uff0cfiltered\u7684\u503c\u8d8a\u5927\u8d8a\u597d<\/li>\n<\/ul>\n<h2>\u7d22\u5f15<\/h2>\n<p>\u7d22\u5f15\u662f\u5e2e\u52a9 MySQL <strong>\u9ad8\u6548\u83b7\u53d6\u6570\u636e<\/strong>\u7684<strong>\u6570\u636e\u7ed3\u6784\uff08\u6709\u5e8f\uff09<\/strong>\u3002\u5728\u6570\u636e\u4e4b\u5916\uff0c\u6570\u636e\u5e93\u7cfb\u7edf\u8fd8\u7ef4\u62a4\u7740\u6ee1\u8db3\u7279\u5b9a\u67e5\u627e\u7b97\u6cd5\u7684\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4ee5\u67d0\u79cd\u65b9\u5f0f\u5f15\u7528\uff08\u6307\u5411\uff09\u6570\u636e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u8fd9\u4e9b\u6570\u636e\u7ed3\u6784\u4e0a\u5b9e\u73b0\u9ad8\u7ea7\u67e5\u8be2\u7b97\u6cd5\uff0c\u8fd9\u79cd\u6570\u636e\u7ed3\u6784\u5c31\u662f\u7d22\u5f15\u3002<\/p>\n<p>\u4f18\u7f3a\u70b9\uff1a<\/p>\n<p>\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u63d0\u9ad8\u6570\u636e\u68c0\u7d22\u6548\u7387\uff0c\u964d\u4f4e\u6570\u636e\u5e93\u7684IO\u6210\u672c<\/li>\n<li>\u901a\u8fc7\u7d22\u5f15\u5217\u5bf9\u6570\u636e\u8fdb\u884c\u6392\u5e8f\uff0c\u964d\u4f4e\u6570\u636e\u6392\u5e8f\u7684\u6210\u672c\uff0c\u964d\u4f4eCPU\u7684\u6d88\u8017<\/li>\n<\/ul>\n<p>\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li>\u7d22\u5f15\u5217\u4e5f\u662f\u8981\u5360\u7528\u7a7a\u95f4\u7684<\/li>\n<li>\u7d22\u5f15\u5927\u5927\u63d0\u9ad8\u4e86\u67e5\u8be2\u6548\u7387\uff0c\u4f46\u964d\u4f4e\u4e86\u66f4\u65b0\u7684\u901f\u5ea6\uff0c\u6bd4\u5982 INSERT\u3001UPDATE\u3001DELETE<\/li>\n<\/ul>\n<h3>\u7d22\u5f15\u7ed3\u6784<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u7d22\u5f15\u7ed3\u6784<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>B+Tree<\/td>\n<td>\u6700\u5e38\u89c1\u7684\u7d22\u5f15\u7c7b\u578b\uff0c\u5927\u90e8\u5206\u5f15\u64ce\u90fd\u652f\u6301B+\u6811\u7d22\u5f15<\/td>\n<\/tr>\n<tr>\n<td>Hash<\/td>\n<td>\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u662f\u7528\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u53ea\u6709\u7cbe\u786e\u5339\u914d\u7d22\u5f15\u5217\u7684\u67e5\u8be2\u624d\u6709\u6548\uff0c\u4e0d\u652f\u6301\u8303\u56f4\u67e5\u8be2<\/td>\n<\/tr>\n<tr>\n<td>R-Tree(\u7a7a\u95f4\u7d22\u5f15)<\/td>\n<td>\u7a7a\u95f4\u7d22\u5f15\u662f MyISAM \u5f15\u64ce\u7684\u4e00\u4e2a\u7279\u6b8a\u7d22\u5f15\u7c7b\u578b\uff0c\u4e3b\u8981\u7528\u4e8e\u5730\u7406\u7a7a\u95f4\u6570\u636e\u7c7b\u578b\uff0c\u901a\u5e38\u4f7f\u7528\u8f83\u5c11<\/td>\n<\/tr>\n<tr>\n<td>Full-Text(\u5168\u6587\u7d22\u5f15)<\/td>\n<td>\u662f\u4e00\u79cd\u901a\u8fc7\u5efa\u7acb\u5012\u6392\u7d22\u5f15\uff0c\u5feb\u901f\u5339\u914d\u6587\u6863\u7684\u65b9\u5f0f\uff0c\u7c7b\u4f3c\u4e8e Lucene, Solr, ES<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<thead>\n<tr>\n<th>\u7d22\u5f15<\/th>\n<th>InnoDB<\/th>\n<th>MyISAM<\/th>\n<th>Memory<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>B+Tree\u7d22\u5f15<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>Hash\u7d22\u5f15<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>R-Tree\u7d22\u5f15<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>Full-text<\/td>\n<td>5.6\u7248\u672c\u540e\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>B-Tree<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/\u4e8c\u53c9\u6811_20220316153214227108.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E4%BA%8C%E5%8F%89%E6%A0%91_20220316153214227108.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E4%BA%8C%E5%8F%89%E6%A0%91_20220316153214227108.png\"\"><\/p>\n<p>\u4e8c\u53c9\u6811\u7684\u7f3a\u70b9\u53ef\u4ee5\u7528\u7ea2\u9ed1\u6811\u6765\u89e3\u51b3\uff1a<br \/><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/\u7ea2\u9ed1\u6811_20220316163142686602.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E7%BA%A2%E9%BB%91%E6%A0%91_20220316163142686602.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E7%BA%A2%E9%BB%91%E6%A0%91_20220316163142686602.png\"\"><br \/>\u7ea2\u9ed1\u6811\u4e5f\u5b58\u5728\u5927\u6570\u636e\u91cf\u60c5\u51b5\u4e0b\uff0c\u5c42\u7ea7\u8f83\u6df1\uff0c\u68c0\u7d22\u901f\u5ea6\u6162\u7684\u95ee\u9898\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528 B-Tree \u7ed3\u6784\u3002<br \/>B-Tree (\u591a\u8def\u5e73\u8861\u67e5\u627e\u6811) \u4ee5\u4e00\u68f5\u6700\u5927\u5ea6\u6570\uff08max-degree\uff0c\u6307\u4e00\u4e2a\u8282\u70b9\u7684\u5b50\u8282\u70b9\u4e2a\u6570\uff09\u4e3a5\uff085\u9636\uff09\u7684 b-tree \u4e3a\u4f8b\uff08\u6bcf\u4e2a\u8282\u70b9\u6700\u591a\u5b58\u50a84\u4e2akey\uff0c5\u4e2a\u6307\u9488\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/B-Tree\u7ed3\u6784_20220316163813441163.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FB-Tree%E7%BB%93%E6%9E%84_20220316163813441163.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FB-Tree%E7%BB%93%E6%9E%84_20220316163813441163.png\"\"><\/p>\n<blockquote><p>B-Tree \u7684\u6570\u636e\u63d2\u5165\u8fc7\u7a0b\u52a8\u753b\u53c2\u7167\uff1a<a href=\"https:\/\/www.bilibili.com\/video\/BV1Kr4y1i7ru?p=68\">https:\/\/www.bilibili.com\/video\/BV1Kr4y1i7ru?p=68<\/a><br \/>\u6f14\u793a\u5730\u5740\uff1a<a href=\"https:\/\/www.cs.usfca.edu\/~galles\/visualization\/BTree.html\">https:\/\/www.cs.usfca.edu\/~galles\/visualization\/BTree.html<\/a><\/p><\/blockquote>\n<h4>B+Tree<\/h4>\n<p>\u7ed3\u6784\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/B+Tree\u7ed3\u6784\u56fe_20220316170700591277.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FB%2BTree%E7%BB%93%E6%9E%84%E5%9B%BE_20220316170700591277.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FB%2BTree%E7%BB%93%E6%9E%84%E5%9B%BE_20220316170700591277.png\"\"><\/p>\n<blockquote><p>\u6f14\u793a\u5730\u5740\uff1a<a href=\"https:\/\/www.cs.usfca.edu\/~galles\/visualization\/BPlusTree.html\">https:\/\/www.cs.usfca.edu\/~galles\/visualization\/BPlusTree.html<\/a><\/p><\/blockquote>\n<p>\u4e0e B-Tree \u7684\u533a\u522b\uff1a<\/p>\n<ul>\n<li>\u6240\u6709\u7684\u6570\u636e\u90fd\u4f1a\u51fa\u73b0\u5728\u53f6\u5b50\u8282\u70b9<\/li>\n<li>\u53f6\u5b50\u8282\u70b9\u5f62\u6210\u4e00\u4e2a\u5355\u5411\u94fe\u8868<\/li>\n<\/ul>\n<p>MySQL \u7d22\u5f15\u6570\u636e\u7ed3\u6784\u5bf9\u7ecf\u5178\u7684 B+Tree \u8fdb\u884c\u4e86\u4f18\u5316\u3002\u5728\u539f B+Tree \u7684\u57fa\u7840\u4e0a\uff0c\u589e\u52a0\u4e00\u4e2a\u6307\u5411\u76f8\u90bb\u53f6\u5b50\u8282\u70b9\u7684\u94fe\u8868\u6307\u9488\uff0c\u5c31\u5f62\u6210\u4e86\u5e26\u6709\u987a\u5e8f\u6307\u9488\u7684 B+Tree\uff0c\u63d0\u9ad8\u533a\u95f4\u8bbf\u95ee\u7684\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/\u7ed3\u6784\u56fe_20220316171730865611.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E7%BB%93%E6%9E%84%E5%9B%BE_20220316171730865611.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E7%BB%93%E6%9E%84%E5%9B%BE_20220316171730865611.png\"\"><\/p>\n<h4>Hash<\/h4>\n<p>\u54c8\u5e0c\u7d22\u5f15\u5c31\u662f\u91c7\u7528\u4e00\u5b9a\u7684hash\u7b97\u6cd5\uff0c\u5c06\u952e\u503c\u6362\u7b97\u6210\u65b0\u7684hash\u503c\uff0c\u6620\u5c04\u5230\u5bf9\u5e94\u7684\u69fd\u4f4d\u4e0a\uff0c\u7136\u540e\u5b58\u50a8\u5728hash\u8868\u4e2d\u3002<br \/>\u5982\u679c\u4e24\u4e2a\uff08\u6216\u591a\u4e2a\uff09\u952e\u503c\uff0c\u6620\u5c04\u5230\u4e00\u4e2a\u76f8\u540c\u7684\u69fd\u4f4d\u4e0a\uff0c\u4ed6\u4eec\u5c31\u4ea7\u751f\u4e86hash\u51b2\u7a81\uff08\u4e5f\u79f0\u4e3ahash\u78b0\u649e\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u94fe\u8868\u6765\u89e3\u51b3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/Hash\u7d22\u5f15\u539f\u7406\u56fe_20220317143226150679.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FHash%E7%B4%A2%E5%BC%95%E5%8E%9F%E7%90%86%E5%9B%BE_20220317143226150679.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2FHash%E7%B4%A2%E5%BC%95%E5%8E%9F%E7%90%86%E5%9B%BE_20220317143226150679.png\"\"><\/p>\n<p>\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>Hash\u7d22\u5f15\u53ea\u80fd\u7528\u4e8e\u5bf9\u7b49\u6bd4\u8f83\uff08=\u3001in\uff09\uff0c\u4e0d\u652f\u6301\u8303\u56f4\u67e5\u8be2\uff08betwwn\u3001&gt;\u3001&lt;\u3001...\uff09<\/li>\n<li>\u65e0\u6cd5\u5229\u7528\u7d22\u5f15\u5b8c\u6210\u6392\u5e8f\u64cd\u4f5c<\/li>\n<li>\u67e5\u8be2\u6548\u7387\u9ad8\uff0c\u901a\u5e38\u53ea\u9700\u8981\u4e00\u6b21\u68c0\u7d22\u5c31\u53ef\u4ee5\u4e86\uff0c\u6548\u7387\u901a\u5e38\u8981\u9ad8\u4e8e B+Tree \u7d22\u5f15<\/li>\n<\/ul>\n<p>\u5b58\u50a8\u5f15\u64ce\u652f\u6301\uff1a<\/p>\n<ul>\n<li>Memory<\/li>\n<li>InnoDB: \u5177\u6709\u81ea\u9002\u5e94hash\u529f\u80fd\uff0chash\u7d22\u5f15\u662f\u5b58\u50a8\u5f15\u64ce\u6839\u636e B+Tree \u7d22\u5f15\u5728\u6307\u5b9a\u6761\u4ef6\u4e0b\u81ea\u52a8\u6784\u5efa\u7684<\/li>\n<\/ul>\n<h4>\u9762\u8bd5\u9898<\/h4>\n<ol>\n<li>\u4e3a\u4ec0\u4e48 InnoDB \u5b58\u50a8\u5f15\u64ce\u9009\u62e9\u4f7f\u7528 B+Tree \u7d22\u5f15\u7ed3\u6784\uff1f<\/li>\n<\/ol>\n<ul>\n<li>\u76f8\u5bf9\u4e8e\u4e8c\u53c9\u6811\uff0c\u5c42\u7ea7\u66f4\u5c11\uff0c\u641c\u7d22\u6548\u7387\u9ad8<\/li>\n<li>\u5bf9\u4e8e B-Tree\uff0c\u65e0\u8bba\u662f\u53f6\u5b50\u8282\u70b9\u8fd8\u662f\u975e\u53f6\u5b50\u8282\u70b9\uff0c\u90fd\u4f1a\u4fdd\u5b58\u6570\u636e\uff0c\u8fd9\u6837\u5bfc\u81f4\u4e00\u9875\u4e2d\u5b58\u50a8\u7684\u952e\u503c\u51cf\u5c11\uff0c\u6307\u9488\u4e5f\u8ddf\u7740\u51cf\u5c11\uff0c\u8981\u540c\u6837\u4fdd\u5b58\u5927\u91cf\u6570\u636e\uff0c\u53ea\u80fd\u589e\u52a0\u6811\u7684\u9ad8\u5ea6\uff0c\u5bfc\u81f4\u6027\u80fd\u964d\u4f4e<\/li>\n<li>\u76f8\u5bf9\u4e8e Hash \u7d22\u5f15\uff0cB+Tree \u652f\u6301\u8303\u56f4\u5339\u914d\u53ca\u6392\u5e8f\u64cd\u4f5c<\/li>\n<\/ul>\n<h3>\u7d22\u5f15\u5206\u7c7b<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u5206\u7c7b<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u7279\u70b9<\/th>\n<th>\u5173\u952e\u5b57<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u4e3b\u952e\u7d22\u5f15<\/td>\n<td>\u9488\u5bf9\u4e8e\u8868\u4e2d\u4e3b\u952e\u521b\u5efa\u7684\u7d22\u5f15<\/td>\n<td>\u9ed8\u8ba4\u81ea\u52a8\u521b\u5efa\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a<\/td>\n<td>PRIMARY<\/td>\n<\/tr>\n<tr>\n<td>\u552f\u4e00\u7d22\u5f15<\/td>\n<td>\u907f\u514d\u540c\u4e00\u4e2a\u8868\u4e2d\u67d0\u6570\u636e\u5217\u4e2d\u7684\u503c\u91cd\u590d<\/td>\n<td>\u53ef\u4ee5\u6709\u591a\u4e2a<\/td>\n<td>UNIQUE<\/td>\n<\/tr>\n<tr>\n<td>\u5e38\u89c4\u7d22\u5f15<\/td>\n<td>\u5feb\u901f\u5b9a\u4f4d\u7279\u5b9a\u6570\u636e<\/td>\n<td>\u53ef\u4ee5\u6709\u591a\u4e2a<\/td>\n<td> <\/td>\n<\/tr>\n<tr>\n<td>\u5168\u6587\u7d22\u5f15<\/td>\n<td>\u5168\u6587\u7d22\u5f15\u67e5\u627e\u7684\u662f\u6587\u672c\u4e2d\u7684\u5173\u952e\u8bcd\uff0c\u800c\u4e0d\u662f\u6bd4\u8f83\u7d22\u5f15\u4e2d\u7684\u503c<\/td>\n<td>\u53ef\u4ee5\u6709\u591a\u4e2a<\/td>\n<td>FULLTEXT<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728 InnoDB \u5b58\u50a8\u5f15\u64ce\u4e2d\uff0c\u6839\u636e\u7d22\u5f15\u7684\u5b58\u50a8\u5f62\u5f0f\uff0c\u53c8\u53ef\u4ee5\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u5206\u7c7b<\/th>\n<th>\u542b\u4e49<\/th>\n<th>\u7279\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u805a\u96c6\u7d22\u5f15(Clustered Index)<\/td>\n<td>\u5c06\u6570\u636e\u5b58\u50a8\u4e0e\u7d22\u5f15\u653e\u4e00\u5757\uff0c\u7d22\u5f15\u7ed3\u6784\u7684\u53f6\u5b50\u8282\u70b9\u4fdd\u5b58\u4e86\u884c\u6570\u636e<\/td>\n<td>\u5fc5\u987b\u6709\uff0c\u800c\u4e14\u53ea\u6709\u4e00\u4e2a<\/td>\n<\/tr>\n<tr>\n<td>\u4e8c\u7ea7\u7d22\u5f15(Secondary Index)<\/td>\n<td>\u5c06\u6570\u636e\u4e0e\u7d22\u5f15\u5206\u5f00\u5b58\u50a8\uff0c\u7d22\u5f15\u7ed3\u6784\u7684\u53f6\u5b50\u8282\u70b9\u5173\u8054\u7684\u662f\u5bf9\u5e94\u7684\u4e3b\u952e<\/td>\n<td>\u53ef\u4ee5\u5b58\u5728\u591a\u4e2a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6f14\u793a\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/\u539f\u7406\u56fe_20220318194454880073.png\" alt=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E5%8E%9F%E7%90%86%E5%9B%BE_20220318194454880073.png\"\" title=\" title=\"https:\/\/amarantos.oss-cn-hangzhou.aliyuncs.com\/typecho\/2023%2F04%2F24%2F%E5%8E%9F%E7%90%86%E5%9B%BE_20220318194454880073.png\"\"><br \/><img decoding=\"async\" src=\"https:\/\/dhc.pythonanywhere.com\/media\/editor\/\u6f14\u793a\u56fe_20220319215403721066.png\" alt=\" title=\"\u6f14\u793a\u56fe\"\" title=\" title=\"\u6f14\u793a\u56fe\"\"><\/p>\n<p>\u805a\u96c6\u7d22\u5f15\u9009\u53d6\u89c4\u5219\uff1a<\/p>\n<ul>\n<li>\u5982\u679c\u5b58\u5728\u4e3b\u952e\uff0c\u4e3b\u952e\u7d22\u5f15\u5c31\u662f\u805a\u96c6\u7d22\u5f15<\/li>\n<li>\u5982\u679c\u4e0d\u5b58\u5728\u4e3b\u952e\uff0c\u5c06\u4f7f\u7528\u7b2c\u4e00\u4e2a\u552f\u4e00(UNIQUE)\u7d22\u5f15\u4f5c\u4e3a\u805a\u96c6\u7d22\u5f15<\/li>\n<li>\u5982\u679c\u8868\u6ca1\u6709\u4e3b\u952e\u6216\u6ca1\u6709\u5408\u9002\u7684\u552f\u4e00\u7d22\u5f15\uff0c\u5219 InnoDB \u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a rowid \u4f5c\u4e3a\u9690\u85cf\u7684\u805a\u96c6\u7d22\u5f15<\/li>\n<\/ul>\n<h4>\u601d\u8003\u9898<\/h4>\n<p>1. \u4ee5\u4e0b SQL \u8bed\u53e5\uff0c\u54ea\u4e2a\u6267\u884c\u6548\u7387\u9ad8\uff1f\u4e3a\u4ec0\u4e48\uff1f<\/p>\n<pre><code class=\"lang-mysql\">select * from user where id = 10;\nselect * from user where name = &#039;Arm&#039;;\n-- \u5907\u6ce8\uff1aid\u4e3a\u4e3b\u952e\uff0cname\u5b57\u6bb5\u521b\u5efa\u7684\u6709\u7d22\u5f15<\/code><\/pre>\n<p>\u7b54\uff1a\u7b2c\u4e00\u6761\u8bed\u53e5\uff0c\u56e0\u4e3a\u7b2c\u4e8c\u6761\u9700\u8981\u56de\u8868\u67e5\u8be2\uff0c\u76f8\u5f53\u4e8e\u4e24\u4e2a\u6b65\u9aa4\u3002<\/p>\n<p>2. InnoDB \u4e3b\u952e\u7d22\u5f15\u7684 B+Tree \u9ad8\u5ea6\u4e3a\u591a\u5c11\uff1f<\/p>\n<p>\u7b54\uff1a\u5047\u8bbe\u4e00\u884c\u6570\u636e\u5927\u5c0f\u4e3a1k\uff0c\u4e00\u9875\u4e2d\u53ef\u4ee5\u5b58\u50a816\u884c\u8fd9\u6837\u7684\u6570\u636e\u3002InnoDB \u7684\u6307\u9488\u5360\u75286\u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u4e3b\u952e\u5047\u8bbe\u4e3abigint\uff0c\u5360\u7528\u5b57\u8282\u6570\u4e3a8.<br \/>\u53ef\u5f97\u516c\u5f0f\uff1a<code>n * 8 + (n + 1) * 6 = 16 * 1024<\/code>\uff0c\u5176\u4e2d 8 \u8868\u793a bigint \u5360\u7528\u7684\u5b57\u8282\u6570\uff0cn \u8868\u793a\u5f53\u524d\u8282\u70b9\u5b58\u50a8\u7684key\u7684\u6570\u91cf\uff0c(n + 1) \u8868\u793a\u6307\u9488\u6570\u91cf\uff08\u6bd4key\u591a\u4e00\u4e2a\uff09\u3002\u7b97\u51fan\u7ea6\u4e3a1170\u3002<\/p>\n<p>\u5982\u679c\u6811\u7684\u9ad8\u5ea6\u4e3a2\uff0c\u90a3\u4e48\u4ed6\u80fd\u5b58\u50a8\u7684\u6570\u636e\u91cf\u5927\u6982\u4e3a\uff1a<code>1171 * 16 = 18736<\/code>\uff1b<br \/>\u5982\u679c\u6811\u7684\u9ad8\u5ea6\u4e3a3\uff0c\u90a3\u4e48\u4ed6\u80fd\u5b58\u50a8\u7684\u6570\u636e\u91cf\u5927\u6982\u4e3a\uff1a<code>1171 * 1171 * 16 = 21939856<\/code>\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5982\u679c\u6709\u6210\u5343\u4e0a\u4e07\u7684\u6570\u636e\uff0c\u90a3\u4e48\u5c31\u8981\u8003\u8651\u5206\u8868\uff0c\u6d89\u53ca\u8fd0\u7ef4\u7bc7\u77e5\u8bc6\u3002<\/p>\n<h3>\u8bed\u6cd5<\/h3>\n<p>\u521b\u5efa\u7d22\u5f15\uff1a<br \/><code>CREATE [ UNIQUE | FULLTEXT ] INDEX index_name ON table_name (index_col_name, ...);<\/code><br \/>\u5982\u679c\u4e0d\u52a0 CREATE \u540e\u9762\u4e0d\u52a0\u7d22\u5f15\u7c7b\u578b\u53c2\u6570\uff0c\u5219\u521b\u5efa\u7684\u662f\u5e38\u89c4\u7d22\u5f15<\/p>\n<p>\u67e5\u770b\u7d22\u5f15\uff1a<br \/><code>SHOW INDEX FROM table_name;<\/code><\/p>\n<p>\u5220\u9664\u7d22\u5f15\uff1a<br \/><code>DROP INDEX index_name ON table_name;<\/code><\/p>\n<p>\u6848\u4f8b\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- name\u5b57\u6bb5\u4e3a\u59d3\u540d\u5b57\u6bb5\uff0c\u8be5\u5b57\u6bb5\u7684\u503c\u53ef\u80fd\u4f1a\u91cd\u590d\uff0c\u4e3a\u8be5\u5b57\u6bb5\u521b\u5efa\u7d22\u5f15\ncreate index idx_user_name on tb_user(name);\n-- phone\u624b\u673a\u53f7\u5b57\u6bb5\u7684\u503c\u975e\u7a7a\uff0c\u4e14\u552f\u4e00\uff0c\u4e3a\u8be5\u5b57\u6bb5\u521b\u5efa\u552f\u4e00\u7d22\u5f15\ncreate unique index idx_user_phone on tb_user (phone);\n-- \u4e3aprofession, age, status\u521b\u5efa\u8054\u5408\u7d22\u5f15\ncreate index idx_user_pro_age_stat on tb_user(profession, age, status);\n-- \u4e3aemail\u5efa\u7acb\u5408\u9002\u7684\u7d22\u5f15\u6765\u63d0\u5347\u67e5\u8be2\u6548\u7387\ncreate index idx_user_email on tb_user(email);\n\n-- \u5220\u9664\u7d22\u5f15\ndrop index idx_user_email on tb_user;<\/code><\/pre>\n<h3>\u4f7f\u7528\u89c4\u5219<\/h3>\n<h4>\u6700\u5de6\u524d\u7f00\u6cd5\u5219<\/h4>\n<p>\u5982\u679c\u7d22\u5f15\u5173\u8054\u4e86\u591a\u5217\uff08\u8054\u5408\u7d22\u5f15\uff09\uff0c\u8981\u9075\u5b88\u6700\u5de6\u524d\u7f00\u6cd5\u5219\uff0c\u6700\u5de6\u524d\u7f00\u6cd5\u5219\u6307\u7684\u662f\u67e5\u8be2\u4ece\u7d22\u5f15\u7684\u6700\u5de6\u5217\u5f00\u59cb\uff0c\u5e76\u4e14\u4e0d\u8df3\u8fc7\u7d22\u5f15\u4e2d\u7684\u5217\u3002<br \/>\u5982\u679c\u8df3\u8dc3\u67d0\u4e00\u5217\uff0c\u7d22\u5f15\u5c06\u90e8\u5206\u5931\u6548\uff08\u540e\u9762\u7684\u5b57\u6bb5\u7d22\u5f15\u5931\u6548\uff09\u3002<\/p>\n<p>\u8054\u5408\u7d22\u5f15\u4e2d\uff0c\u51fa\u73b0\u8303\u56f4\u67e5\u8be2\uff08&lt;, &gt;\uff09\uff0c\u8303\u56f4\u67e5\u8be2\u53f3\u4fa7\u7684\u5217\u7d22\u5f15\u5931\u6548\u3002\u53ef\u4ee5\u7528&gt;=\u6216\u8005&lt;=\u6765\u89c4\u907f\u7d22\u5f15\u5931\u6548\u95ee\u9898\u3002<\/p>\n<h4>\u7d22\u5f15\u5931\u6548\u60c5\u51b5<\/h4>\n<ol>\n<li>\u5728\u7d22\u5f15\u5217\u4e0a\u8fdb\u884c\u8fd0\u7b97\u64cd\u4f5c\uff0c\u7d22\u5f15\u5c06\u5931\u6548\u3002\u5982\uff1a<code>explain select * from tb_user where substring(phone, 10, 2) = &#039;15&#039;;<\/code><\/li>\n<li>\u5b57\u7b26\u4e32\u7c7b\u578b\u5b57\u6bb5\u4f7f\u7528\u65f6\uff0c\u4e0d\u52a0\u5f15\u53f7\uff0c\u7d22\u5f15\u5c06\u5931\u6548\u3002\u5982\uff1a<code>explain select * from tb_user where phone = 17799990015;<\/code>\uff0c\u6b64\u5904phone\u7684\u503c\u6ca1\u6709\u52a0\u5f15\u53f7<\/li>\n<li>\u6a21\u7cca\u67e5\u8be2\u4e2d\uff0c\u5982\u679c\u4ec5\u4ec5\u662f\u5c3e\u90e8\u6a21\u7cca\u5339\u914d\uff0c\u7d22\u5f15\u4e0d\u4f1a\u662f\u5931\u6548\uff1b\u5982\u679c\u662f\u5934\u90e8\u6a21\u7cca\u5339\u914d\uff0c\u7d22\u5f15\u5931\u6548\u3002\u5982\uff1a<code>explain select * from tb_user where profession like &#039;%\u5de5\u7a0b&#039;;<\/code>\uff0c\u524d\u540e\u90fd\u6709 % \u4e5f\u4f1a\u5931\u6548\u3002<\/li>\n<li>\u7528 or \u5206\u5272\u5f00\u7684\u6761\u4ef6\uff0c\u5982\u679c or \u5176\u4e2d\u4e00\u4e2a\u6761\u4ef6\u7684\u5217\u6ca1\u6709\u7d22\u5f15\uff0c\u90a3\u4e48\u6d89\u53ca\u7684\u7d22\u5f15\u90fd\u4e0d\u4f1a\u88ab\u7528\u5230\u3002<\/li>\n<li>\u5982\u679c MySQL \u8bc4\u4f30\u4f7f\u7528\u7d22\u5f15\u6bd4\u5168\u8868\u66f4\u6162\uff0c\u5219\u4e0d\u4f7f\u7528\u7d22\u5f15\u3002<\/li>\n<\/ol>\n<h4>SQL \u63d0\u793a<\/h4>\n<p>\u662f\u4f18\u5316\u6570\u636e\u5e93\u7684\u4e00\u4e2a\u91cd\u8981\u624b\u6bb5\uff0c\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f\u5728SQL\u8bed\u53e5\u4e2d\u52a0\u5165\u4e00\u4e9b\u4eba\u4e3a\u7684\u63d0\u793a\u6765\u8fbe\u5230\u4f18\u5316\u64cd\u4f5c\u7684\u76ee\u7684\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u4f7f\u7528\u7d22\u5f15\uff1a<br \/><code>explain select * from tb_user use index(idx_user_pro) where profession=&quot;\u8f6f\u4ef6\u5de5\u7a0b&quot;;<\/code><br \/>\u4e0d\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15\uff1a<br \/><code>explain select * from tb_user ignore index(idx_user_pro) where profession=&quot;\u8f6f\u4ef6\u5de5\u7a0b&quot;;<\/code><br \/>\u5fc5\u987b\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15\uff1a<br \/><code>explain select * from tb_user force index(idx_user_pro) where profession=&quot;\u8f6f\u4ef6\u5de5\u7a0b&quot;;<\/code><\/p>\n<p>use \u662f\u5efa\u8bae\uff0c\u5b9e\u9645\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15 MySQL \u8fd8\u4f1a\u81ea\u5df1\u6743\u8861\u8fd0\u884c\u901f\u5ea6\u53bb\u66f4\u6539\uff0cforce\u5c31\u662f\u65e0\u8bba\u5982\u4f55\u90fd\u5f3a\u5236\u4f7f\u7528\u8be5\u7d22\u5f15\u3002<\/p>\n<h4>\u8986\u76d6\u7d22\u5f15&\u56de\u8868\u67e5\u8be2<\/h4>\n<p>\u5c3d\u91cf\u4f7f\u7528\u8986\u76d6\u7d22\u5f15\uff08\u67e5\u8be2\u4f7f\u7528\u4e86\u7d22\u5f15\uff0c\u5e76\u4e14\u9700\u8981\u8fd4\u56de\u7684\u5217\uff0c\u5728\u8be5\u7d22\u5f15\u4e2d\u5df2\u7ecf\u5168\u90e8\u80fd\u627e\u5230\uff09\uff0c\u51cf\u5c11 select *\u3002<\/p>\n<p>explain \u4e2d extra \u5b57\u6bb5\u542b\u4e49\uff1a<br \/><code>using index condition<\/code>\uff1a\u67e5\u627e\u4f7f\u7528\u4e86\u7d22\u5f15\uff0c\u4f46\u662f\u9700\u8981\u56de\u8868\u67e5\u8be2\u6570\u636e<br \/><code>using where; using index;<\/code>\uff1a\u67e5\u627e\u4f7f\u7528\u4e86\u7d22\u5f15\uff0c\u4f46\u662f\u9700\u8981\u7684\u6570\u636e\u90fd\u5728\u7d22\u5f15\u5217\u4e2d\u80fd\u627e\u5230\uff0c\u6240\u4ee5\u4e0d\u9700\u8981\u56de\u8868\u67e5\u8be2<\/p>\n<p>\u5982\u679c\u5728\u805a\u96c6\u7d22\u5f15\u4e2d\u76f4\u63a5\u80fd\u627e\u5230\u5bf9\u5e94\u7684\u884c\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u884c\u6570\u636e\uff0c\u53ea\u9700\u8981\u4e00\u6b21\u67e5\u8be2\uff0c\u54ea\u6015\u662fselect *\uff1b\u5982\u679c\u5728\u8f85\u52a9\u7d22\u5f15\u4e2d\u627e\u805a\u96c6\u7d22\u5f15\uff0c\u5982<code>select id, name from xxx where name=&#039;xxx&#039;;<\/code>\uff0c\u4e5f\u53ea\u9700\u8981\u901a\u8fc7\u8f85\u52a9\u7d22\u5f15(name)\u67e5\u627e\u5230\u5bf9\u5e94\u7684id\uff0c\u8fd4\u56dename\u548cname\u7d22\u5f15\u5bf9\u5e94\u7684id\u5373\u53ef\uff0c\u53ea\u9700\u8981\u4e00\u6b21\u67e5\u8be2\uff1b\u5982\u679c\u662f\u901a\u8fc7\u8f85\u52a9\u7d22\u5f15\u67e5\u627e\u5176\u4ed6\u5b57\u6bb5\uff0c\u5219\u9700\u8981\u56de\u8868\u67e5\u8be2\uff0c\u5982<code>select id, name, gender from xxx where name=&#039;xxx&#039;;<\/code><\/p>\n<p>\u6240\u4ee5\u5c3d\u91cf\u4e0d\u8981\u7528<code>select *<\/code>\uff0c\u5bb9\u6613\u51fa\u73b0\u56de\u8868\u67e5\u8be2\uff0c\u964d\u4f4e\u6548\u7387\uff0c\u9664\u975e\u6709\u8054\u5408\u7d22\u5f15\u5305\u542b\u4e86\u6240\u6709\u5b57\u6bb5<\/p>\n<p>\u9762\u8bd5\u9898\uff1a\u4e00\u5f20\u8868\uff0c\u6709\u56db\u4e2a\u5b57\u6bb5\uff08id, username, password, status\uff09\uff0c\u7531\u4e8e\u6570\u636e\u91cf\u5927\uff0c\u9700\u8981\u5bf9\u4ee5\u4e0bSQL\u8bed\u53e5\u8fdb\u884c\u4f18\u5316\uff0c\u8be5\u5982\u4f55\u8fdb\u884c\u624d\u662f\u6700\u4f18\u65b9\u6848\uff1a<br \/><code>select id, username, password from tb_user where username=&#039;itcast&#039;;<\/code><\/p>\n<p>\u89e3\uff1a\u7ed9username\u548cpassword\u5b57\u6bb5\u5efa\u7acb\u8054\u5408\u7d22\u5f15\uff0c\u5219\u4e0d\u9700\u8981\u56de\u8868\u67e5\u8be2\uff0c\u76f4\u63a5\u8986\u76d6\u7d22\u5f15<\/p>\n<h4>\u524d\u7f00\u7d22\u5f15<\/h4>\n<p>\u5f53\u5b57\u6bb5\u7c7b\u578b\u4e3a\u5b57\u7b26\u4e32\uff08varchar, text\u7b49\uff09\u65f6\uff0c\u6709\u65f6\u5019\u9700\u8981\u7d22\u5f15\u5f88\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u8fd9\u4f1a\u8ba9\u7d22\u5f15\u53d8\u5f97\u5f88\u5927\uff0c\u67e5\u8be2\u65f6\uff0c\u6d6a\u8d39\u5927\u91cf\u7684\u78c1\u76d8IO\uff0c\u5f71\u54cd\u67e5\u8be2\u6548\u7387\uff0c\u6b64\u65f6\u53ef\u4ee5\u53ea\u964d\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\u524d\u7f00\uff0c\u5efa\u7acb\u7d22\u5f15\uff0c\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u8282\u7ea6\u7d22\u5f15\u7a7a\u95f4\uff0c\u4ece\u800c\u63d0\u9ad8\u7d22\u5f15\u6548\u7387\u3002<\/p>\n<p>\u8bed\u6cd5\uff1a<code>create index idx_xxxx on table_name(columnn(n));<\/code><br \/>\u524d\u7f00\u957f\u5ea6\uff1a\u53ef\u4ee5\u6839\u636e\u7d22\u5f15\u7684\u9009\u62e9\u6027\u6765\u51b3\u5b9a\uff0c\u800c\u9009\u62e9\u6027\u662f\u6307\u4e0d\u91cd\u590d\u7684\u7d22\u5f15\u503c\uff08\u57fa\u6570\uff09\u548c\u6570\u636e\u8868\u7684\u8bb0\u5f55\u603b\u6570\u7684\u6bd4\u503c\uff0c\u7d22\u5f15\u9009\u62e9\u6027\u8d8a\u9ad8\u5219\u67e5\u8be2\u6548\u7387\u8d8a\u9ad8\uff0c\u552f\u4e00\u7d22\u5f15\u7684\u9009\u62e9\u6027\u662f1\uff0c\u8fd9\u662f\u6700\u597d\u7684\u7d22\u5f15\u9009\u62e9\u6027\uff0c\u6027\u80fd\u4e5f\u662f\u6700\u597d\u7684\u3002<br \/>\u6c42\u9009\u62e9\u6027\u516c\u5f0f\uff1a<\/p>\n<pre><code class=\"lang-mysql\">select count(distinct email) \/ count(*) from tb_user;\nselect count(distinct substring(email, 1, 5)) \/ count(*) from tb_user;<\/code><\/pre>\n<p>show index \u91cc\u9762\u7684sub_part\u53ef\u4ee5\u770b\u5230\u63a5\u53d6\u7684\u957f\u5ea6<\/p>\n<h4>\u5355\u5217\u7d22\u5f15&\u8054\u5408\u7d22\u5f15<\/h4>\n<p>\u5355\u5217\u7d22\u5f15\uff1a\u5373\u4e00\u4e2a\u7d22\u5f15\u53ea\u5305\u542b\u5355\u4e2a\u5217<br \/>\u8054\u5408\u7d22\u5f15\uff1a\u5373\u4e00\u4e2a\u7d22\u5f15\u5305\u542b\u4e86\u591a\u4e2a\u5217<br \/>\u5728\u4e1a\u52a1\u573a\u666f\u4e2d\uff0c\u5982\u679c\u5b58\u5728\u591a\u4e2a\u67e5\u8be2\u6761\u4ef6\uff0c\u8003\u8651\u9488\u5bf9\u4e8e\u67e5\u8be2\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15\u65f6\uff0c\u5efa\u8bae\u5efa\u7acb\u8054\u5408\u7d22\u5f15\uff0c\u800c\u975e\u5355\u5217\u7d22\u5f15\u3002<\/p>\n<p>\u5355\u5217\u7d22\u5f15\u60c5\u51b5\uff1a<br \/><code>explain select id, phone, name from tb_user where phone = &#039;17799990010&#039; and name = &#039;\u97e9\u4fe1&#039;;<\/code><br \/>\u8fd9\u53e5\u53ea\u4f1a\u7528\u5230phone\u7d22\u5f15\u5b57\u6bb5<\/p>\n<h5>\u6ce8\u610f\u4e8b\u9879<\/h5>\n<ul>\n<li>\u591a\u6761\u4ef6\u8054\u5408\u67e5\u8be2\u65f6\uff0cMySQL\u4f18\u5316\u5668\u4f1a\u8bc4\u4f30\u54ea\u4e2a\u5b57\u6bb5\u7684\u7d22\u5f15\u6548\u7387\u66f4\u9ad8\uff0c\u4f1a\u9009\u62e9\u8be5\u7d22\u5f15\u5b8c\u6210\u672c\u6b21\u67e5\u8be2<\/li>\n<\/ul>\n<h3>\u8bbe\u8ba1\u539f\u5219<\/h3>\n<ol>\n<li>\u9488\u5bf9\u4e8e\u6570\u636e\u91cf\u8f83\u5927\uff0c\u4e14\u67e5\u8be2\u6bd4\u8f83\u9891\u7e41\u7684\u8868\u5efa\u7acb\u7d22\u5f15<\/li>\n<li>\u9488\u5bf9\u4e8e\u5e38\u4f5c\u4e3a\u67e5\u8be2\u6761\u4ef6\uff08where\uff09\u3001\u6392\u5e8f\uff08order by\uff09\u3001\u5206\u7ec4\uff08group by\uff09\u64cd\u4f5c\u7684\u5b57\u6bb5\u5efa\u7acb\u7d22\u5f15<\/li>\n<li>\u5c3d\u91cf\u9009\u62e9\u533a\u5206\u5ea6\u9ad8\u7684\u5217\u4f5c\u4e3a\u7d22\u5f15\uff0c\u5c3d\u91cf\u5efa\u7acb\u552f\u4e00\u7d22\u5f15\uff0c\u533a\u5206\u5ea6\u8d8a\u9ad8\uff0c\u4f7f\u7528\u7d22\u5f15\u7684\u6548\u7387\u8d8a\u9ad8<\/li>\n<li>\u5982\u679c\u662f\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5b57\u6bb5\uff0c\u5b57\u6bb5\u957f\u5ea6\u8f83\u957f\uff0c\u53ef\u4ee5\u9488\u5bf9\u4e8e\u5b57\u6bb5\u7684\u7279\u70b9\uff0c\u5efa\u7acb\u524d\u7f00\u7d22\u5f15<\/li>\n<li>\u5c3d\u91cf\u4f7f\u7528\u8054\u5408\u7d22\u5f15\uff0c\u51cf\u5c11\u5355\u5217\u7d22\u5f15\uff0c\u67e5\u8be2\u65f6\uff0c\u8054\u5408\u7d22\u5f15\u5f88\u591a\u65f6\u5019\u53ef\u4ee5\u8986\u76d6\u7d22\u5f15\uff0c\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\uff0c\u907f\u514d\u56de\u8868\uff0c\u63d0\u9ad8\u67e5\u8be2\u6548\u7387<\/li>\n<li>\u8981\u63a7\u5236\u7d22\u5f15\u7684\u6570\u91cf\uff0c\u7d22\u5f15\u5e76\u4e0d\u662f\u591a\u591a\u76ca\u5584\uff0c\u7d22\u5f15\u8d8a\u591a\uff0c\u7ef4\u62a4\u7d22\u5f15\u7ed3\u6784\u7684\u4ee3\u4ef7\u5c31\u8d8a\u5927\uff0c\u4f1a\u5f71\u54cd\u589e\u5220\u6539\u7684\u6548\u7387<\/li>\n<li>\u5982\u679c\u7d22\u5f15\u5217\u4e0d\u80fd\u5b58\u50a8NULL\u503c\uff0c\u8bf7\u5728\u521b\u5efa\u8868\u65f6\u4f7f\u7528NOT NULL\u7ea6\u675f\u5b83\u3002\u5f53\u4f18\u5316\u5668\u77e5\u9053\u6bcf\u5217\u662f\u5426\u5305\u542bNULL\u503c\u65f6\uff0c\u5b83\u53ef\u4ee5\u66f4\u597d\u5730\u786e\u5b9a\u54ea\u4e2a\u7d22\u5f15\u6700\u6709\u6548\u5730\u7528\u4e8e\u67e5\u8be2<\/li>\n<\/ol>\n<h2>SQL \u4f18\u5316<\/h2>\n<h3>\u63d2\u5165\u6570\u636e<\/h3>\n<p>\u666e\u901a\u63d2\u5165\uff1a<\/p>\n<ol>\n<li>\u91c7\u7528\u6279\u91cf\u63d2\u5165\uff08\u4e00\u6b21\u63d2\u5165\u7684\u6570\u636e\u4e0d\u5efa\u8bae\u8d85\u8fc71000\u6761\uff09<\/li>\n<li>\u624b\u52a8\u63d0\u4ea4\u4e8b\u52a1<\/li>\n<li>\u4e3b\u952e\u987a\u5e8f\u63d2\u5165<\/li>\n<\/ol>\n<p>\u5927\u6279\u91cf\u63d2\u5165\uff1a<br \/>\u5982\u679c\u4e00\u6b21\u6027\u9700\u8981\u63d2\u5165\u5927\u6279\u91cf\u6570\u636e\uff0c\u4f7f\u7528insert\u8bed\u53e5\u63d2\u5165\u6027\u80fd\u8f83\u4f4e\uff0c\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528MySQL\u6570\u636e\u5e93\u63d0\u4f9b\u7684load\u6307\u4ee4\u63d2\u5165\u3002<\/p>\n<pre><code class=\"lang-mysql\"># \u5ba2\u6237\u7aef\u8fde\u63a5\u670d\u52a1\u7aef\u65f6\uff0c\u52a0\u4e0a\u53c2\u6570 --local-infile\uff08\u8fd9\u4e00\u884c\u5728bash\/cmd\u754c\u9762\u8f93\u5165\uff09\nmysql --local-infile -u root -p\n# \u8bbe\u7f6e\u5168\u5c40\u53c2\u6570local_infile\u4e3a1\uff0c\u5f00\u542f\u4ece\u672c\u5730\u52a0\u8f7d\u6587\u4ef6\u5bfc\u5165\u6570\u636e\u7684\u5f00\u5173\nset global local_infile = 1;\nselect @@local_infile;\n# \u6267\u884cload\u6307\u4ee4\u5c06\u51c6\u5907\u597d\u7684\u6570\u636e\uff0c\u52a0\u8f7d\u5230\u8868\u7ed3\u6784\u4e2d\nload data local infile &#039;\/root\/sql1.log&#039; into table &#039;tb_user&#039; fields terminated by &#039;,&#039; lines terminated by &#039;\\n&#039;;<\/code><\/pre>\n<h3>\u4e3b\u952e\u4f18\u5316<\/h3>\n<p>\u6570\u636e\u7ec4\u7ec7\u65b9\u5f0f\uff1a\u5728InnoDB\u5b58\u50a8\u5f15\u64ce\u4e2d\uff0c\u8868\u6570\u636e\u90fd\u662f\u6839\u636e\u4e3b\u952e\u987a\u5e8f\u7ec4\u7ec7\u5b58\u653e\u7684\uff0c\u8fd9\u79cd\u5b58\u50a8\u65b9\u5f0f\u7684\u8868\u79f0\u4e3a\u7d22\u5f15\u7ec4\u7ec7\u8868\uff08Index organized table, IOT\uff09<\/p>\n<p>\u9875\u5206\u88c2\uff1a\u9875\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u4e5f\u53ef\u4ee5\u586b\u5145\u4e00\u822c\uff0c\u4e5f\u53ef\u4ee5\u586b\u5145100%\uff0c\u6bcf\u4e2a\u9875\u5305\u542b\u4e862-N\u884c\u6570\u636e\uff08\u5982\u679c\u4e00\u884c\u6570\u636e\u8fc7\u5927\uff0c\u4f1a\u884c\u6ea2\u51fa\uff09\uff0c\u6839\u636e\u4e3b\u952e\u6392\u5217\u3002<br \/>\u9875\u5408\u5e76\uff1a\u5f53\u5220\u9664\u4e00\u884c\u8bb0\u5f55\u65f6\uff0c\u5b9e\u9645\u4e0a\u8bb0\u5f55\u5e76\u6ca1\u6709\u88ab\u7269\u7406\u5220\u9664\uff0c\u53ea\u662f\u8bb0\u5f55\u88ab\u6807\u8bb0\uff08flaged\uff09\u4e3a\u5220\u9664\u5e76\u4e14\u5b83\u7684\u7a7a\u95f4\u53d8\u5f97\u5141\u8bb8\u88ab\u5176\u4ed6\u8bb0\u5f55\u58f0\u660e\u4f7f\u7528\u3002\u5f53\u9875\u4e2d\u5220\u9664\u7684\u8bb0\u5f55\u5230\u8fbe MERGE_THRESHOLD\uff08\u9ed8\u8ba4\u4e3a\u9875\u768450%\uff09\uff0cInnoDB\u4f1a\u5f00\u59cb\u5bfb\u627e\u6700\u9760\u8fd1\u7684\u9875\uff08\u524d\u540e\uff09\u770b\u770b\u662f\u5426\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u9875\u5408\u5e76\u4ee5\u4f18\u5316\u7a7a\u95f4\u4f7f\u7528\u3002<\/p>\n<p>MERGE_THRESHOLD\uff1a\u5408\u5e76\u9875\u7684\u9608\u503c\uff0c\u53ef\u4ee5\u81ea\u5df1\u8bbe\u7f6e\uff0c\u5728\u521b\u5efa\u8868\u6216\u521b\u5efa\u7d22\u5f15\u65f6\u6307\u5b9a<\/p>\n<blockquote><p>\u6587\u5b57\u8bf4\u660e\u4e0d\u591f\u6e05\u6670\u660e\u4e86\uff0c\u5177\u4f53\u53ef\u4ee5\u770b\u89c6\u9891\u91cc\u7684PPT\u6f14\u793a\u8fc7\u7a0b\uff1a<a href=\"https:\/\/www.bilibili.com\/video\/BV1Kr4y1i7ru?p=90\">https:\/\/www.bilibili.com\/video\/BV1Kr4y1i7ru?p=90<\/a><\/p><\/blockquote>\n<p>\u4e3b\u952e\u8bbe\u8ba1\u539f\u5219\uff1a<\/p>\n<ul>\n<li>\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u7684\u60c5\u51b5\u4e0b\uff0c\u5c3d\u91cf\u964d\u4f4e\u4e3b\u952e\u7684\u957f\u5ea6<\/li>\n<li>\u63d2\u5165\u6570\u636e\u65f6\uff0c\u5c3d\u91cf\u9009\u62e9\u987a\u5e8f\u63d2\u5165\uff0c\u9009\u62e9\u4f7f\u7528 AUTO_INCREMENT \u81ea\u589e\u4e3b\u952e<\/li>\n<li>\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528 UUID \u505a\u4e3b\u952e\u6216\u8005\u662f\u5176\u4ed6\u7684\u81ea\u7136\u4e3b\u952e\uff0c\u5982\u8eab\u4efd\u8bc1\u53f7<\/li>\n<li>\u4e1a\u52a1\u64cd\u4f5c\u65f6\uff0c\u907f\u514d\u5bf9\u4e3b\u952e\u7684\u4fee\u6539<\/li>\n<\/ul>\n<h3>order by\u4f18\u5316<\/h3>\n<ol>\n<li>Using filesort\uff1a\u901a\u8fc7\u8868\u7684\u7d22\u5f15\u6216\u5168\u8868\u626b\u63cf\uff0c\u8bfb\u53d6\u6ee1\u8db3\u6761\u4ef6\u7684\u6570\u636e\u884c\uff0c\u7136\u540e\u5728\u6392\u5e8f\u7f13\u51b2\u533a sort buffer \u4e2d\u5b8c\u6210\u6392\u5e8f\u64cd\u4f5c\uff0c\u6240\u6709\u4e0d\u662f\u901a\u8fc7\u7d22\u5f15\u76f4\u63a5\u8fd4\u56de\u6392\u5e8f\u7ed3\u679c\u7684\u6392\u5e8f\u90fd\u53eb FileSort \u6392\u5e8f<\/li>\n<li>Using index\uff1a\u901a\u8fc7\u6709\u5e8f\u7d22\u5f15\u987a\u5e8f\u626b\u63cf\u76f4\u63a5\u8fd4\u56de\u6709\u5e8f\u6570\u636e\uff0c\u8fd9\u79cd\u60c5\u51b5\u5373\u4e3a using index\uff0c\u4e0d\u9700\u8981\u989d\u5916\u6392\u5e8f\uff0c\u64cd\u4f5c\u6548\u7387\u9ad8<\/li>\n<\/ol>\n<p>\u5982\u679corder by\u5b57\u6bb5\u5168\u90e8\u4f7f\u7528\u5347\u5e8f\u6392\u5e8f\u6216\u8005\u964d\u5e8f\u6392\u5e8f\uff0c\u5219\u90fd\u4f1a\u8d70\u7d22\u5f15\uff0c\u4f46\u662f\u5982\u679c\u4e00\u4e2a\u5b57\u6bb5\u5347\u5e8f\u6392\u5e8f\uff0c\u53e6\u4e00\u4e2a\u5b57\u6bb5\u964d\u5e8f\u6392\u5e8f\uff0c\u5219\u4e0d\u4f1a\u8d70\u7d22\u5f15\uff0cexplain\u7684extra\u4fe1\u606f\u663e\u793a\u7684\u662f<code>Using index, Using filesort<\/code>\uff0c\u5982\u679c\u8981\u4f18\u5316\u6389Using filesort\uff0c\u5219\u9700\u8981\u53e6\u5916\u518d\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\uff0c\u5982\uff1a<code>create index idx_user_age_phone_ad on tb_user(age asc, phone desc);<\/code>\uff0c\u6b64\u65f6\u4f7f\u7528<code>select id, age, phone from tb_user order by age asc, phone desc;<\/code>\u4f1a\u5168\u90e8\u8d70\u7d22\u5f15<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<ul>\n<li>\u6839\u636e\u6392\u5e8f\u5b57\u6bb5\u5efa\u7acb\u5408\u9002\u7684\u7d22\u5f15\uff0c\u591a\u5b57\u6bb5\u6392\u5e8f\u65f6\uff0c\u4e5f\u9075\u5faa\u6700\u5de6\u524d\u7f00\u6cd5\u5219<\/li>\n<li>\u5c3d\u91cf\u4f7f\u7528\u8986\u76d6\u7d22\u5f15<\/li>\n<li>\u591a\u5b57\u6bb5\u6392\u5e8f\uff0c\u4e00\u4e2a\u5347\u5e8f\u4e00\u4e2a\u964d\u5e8f\uff0c\u6b64\u65f6\u9700\u8981\u6ce8\u610f\u8054\u5408\u7d22\u5f15\u5728\u521b\u5efa\u65f6\u7684\u89c4\u5219\uff08ASC\/DESC\uff09<\/li>\n<li>\u5982\u679c\u4e0d\u53ef\u907f\u514d\u51fa\u73b0filesort\uff0c\u5927\u6570\u636e\u91cf\u6392\u5e8f\u65f6\uff0c\u53ef\u4ee5\u9002\u5f53\u589e\u5927\u6392\u5e8f\u7f13\u51b2\u533a\u5927\u5c0f sort_buffer_size\uff08\u9ed8\u8ba4256k\uff09<\/li>\n<\/ul>\n<h3>group by\u4f18\u5316<\/h3>\n<ul>\n<li>\u5728\u5206\u7ec4\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u6765\u63d0\u9ad8\u6548\u7387<\/li>\n<li>\u5206\u7ec4\u64cd\u4f5c\u65f6\uff0c\u7d22\u5f15\u7684\u4f7f\u7528\u4e5f\u662f\u6ee1\u8db3\u6700\u5de6\u524d\u7f00\u6cd5\u5219\u7684<\/li>\n<\/ul>\n<p>\u5982\u7d22\u5f15\u4e3a<code>idx_user_pro_age_stat<\/code>\uff0c\u5219\u53e5\u5f0f\u53ef\u4ee5\u662f<code>select ... where profession order by age<\/code>\uff0c\u8fd9\u6837\u4e5f\u7b26\u5408\u6700\u5de6\u524d\u7f00\u6cd5\u5219<\/p>\n<h3>limit\u4f18\u5316<\/h3>\n<p>\u5e38\u89c1\u7684\u95ee\u9898\u5982<code>limit 2000000, 10<\/code>\uff0c\u6b64\u65f6\u9700\u8981 MySQL \u6392\u5e8f\u524d2000000\u6761\u8bb0\u5f55\uff0c\u4f46\u4ec5\u4ec5\u8fd4\u56de2000000 - 2000010\u7684\u8bb0\u5f55\uff0c\u5176\u4ed6\u8bb0\u5f55\u4e22\u5f03\uff0c\u67e5\u8be2\u6392\u5e8f\u7684\u4ee3\u4ef7\u975e\u5e38\u5927\u3002<br \/>\u4f18\u5316\u65b9\u6848\uff1a\u4e00\u822c\u5206\u9875\u67e5\u8be2\u65f6\uff0c\u901a\u8fc7\u521b\u5efa\u8986\u76d6\u7d22\u5f15\u80fd\u591f\u6bd4\u8f83\u597d\u5730\u63d0\u9ad8\u6027\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u8986\u76d6\u7d22\u5f15\u52a0\u5b50\u67e5\u8be2\u5f62\u5f0f\u8fdb\u884c\u4f18\u5316<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"lang-mysql\">-- \u6b64\u8bed\u53e5\u8017\u65f6\u5f88\u957f\nselect * from tb_sku limit 9000000, 10;\n-- \u901a\u8fc7\u8986\u76d6\u7d22\u5f15\u52a0\u5feb\u901f\u5ea6\uff0c\u76f4\u63a5\u901a\u8fc7\u4e3b\u952e\u7d22\u5f15\u8fdb\u884c\u6392\u5e8f\u53ca\u67e5\u8be2\nselect id from tb_sku order by id limit 9000000, 10;\n-- \u4e0b\u9762\u7684\u8bed\u53e5\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a MySQL \u4e0d\u652f\u6301 in \u91cc\u9762\u4f7f\u7528 limit\n-- select * from tb_sku where id in (select id from tb_sku order by id limit 9000000, 10);\n-- \u901a\u8fc7\u8fde\u8868\u67e5\u8be2\u5373\u53ef\u5b9e\u73b0\u7b2c\u4e00\u53e5\u7684\u6548\u679c\uff0c\u5e76\u4e14\u80fd\u8fbe\u5230\u7b2c\u4e8c\u53e5\u7684\u901f\u5ea6\nselect * from tb_sku as s, (select id from tb_sku order by id limit 9000000, 10) as a where s.id = a.id;<\/code><\/pre>\n<h3>count\u4f18\u5316<\/h3>\n<p>MyISAM \u5f15\u64ce\u628a\u4e00\u4e2a\u8868\u7684\u603b\u884c\u6570\u5b58\u5728\u4e86\u78c1\u76d8\u4e0a\uff0c\u56e0\u6b64\u6267\u884c count(*) \u7684\u65f6\u5019\u4f1a\u76f4\u63a5\u8fd4\u56de\u8fd9\u4e2a\u6570\uff0c\u6548\u7387\u5f88\u9ad8\uff08\u524d\u63d0\u662f\u4e0d\u9002\u7528where\uff09\uff1b<br \/>InnoDB \u5728\u6267\u884c count(*) \u65f6\uff0c\u9700\u8981\u628a\u6570\u636e\u4e00\u884c\u4e00\u884c\u5730\u4ece\u5f15\u64ce\u91cc\u9762\u8bfb\u51fa\u6765\uff0c\u7136\u540e\u7d2f\u8ba1\u8ba1\u6570\u3002<br \/>\u4f18\u5316\u65b9\u6848\uff1a\u81ea\u5df1\u8ba1\u6570\uff0c\u5982\u521b\u5efakey-value\u8868\u5b58\u50a8\u5728\u5185\u5b58\u6216\u786c\u76d8\uff0c\u6216\u8005\u662f\u7528redis<\/p>\n<p>count\u7684\u51e0\u79cd\u7528\u6cd5\uff1a<\/p>\n<ul>\n<li>\u5982\u679ccount\u51fd\u6570\u7684\u53c2\u6570\uff08count\u91cc\u9762\u5199\u7684\u90a3\u4e2a\u5b57\u6bb5\uff09\u4e0d\u662fNULL\uff08\u5b57\u6bb5\u503c\u4e0d\u4e3aNULL\uff09\uff0c\u7d2f\u8ba1\u503c\u5c31\u52a0\u4e00\uff0c\u6700\u540e\u8fd4\u56de\u7d2f\u8ba1\u503c<\/li>\n<li>\u7528\u6cd5\uff1acount(*)\u3001count(\u4e3b\u952e)\u3001count(\u5b57\u6bb5)\u3001count(1)<\/li>\n<li>count(\u4e3b\u952e)\u8ddfcount(*)\u4e00\u6837\uff0c\u56e0\u4e3a\u4e3b\u952e\u4e0d\u80fd\u4e3a\u7a7a\uff1bcount(\u5b57\u6bb5)\u53ea\u8ba1\u7b97\u5b57\u6bb5\u503c\u4e0d\u4e3aNULL\u7684\u884c\uff1bcount(1)\u5f15\u64ce\u4f1a\u4e3a\u6bcf\u884c\u6dfb\u52a0\u4e00\u4e2a1\uff0c\u7136\u540e\u5c31count\u8fd9\u4e2a1\uff0c\u8fd4\u56de\u7ed3\u679c\u4e5f\u8ddfcount(*)\u4e00\u6837\uff1bcount(null)\u8fd4\u56de0<\/li>\n<\/ul>\n<p>\u5404\u79cd\u7528\u6cd5\u7684\u6027\u80fd\uff1a<\/p>\n<ul>\n<li>count(\u4e3b\u952e)\uff1aInnoDB\u5f15\u64ce\u4f1a\u904d\u5386\u6574\u5f20\u8868\uff0c\u628a\u6bcf\u884c\u7684\u4e3b\u952eid\u503c\u90fd\u53d6\u51fa\u6765\uff0c\u8fd4\u56de\u7ed9\u670d\u52a1\u5c42\uff0c\u670d\u52a1\u5c42\u62ff\u5230\u4e3b\u952e\u540e\uff0c\u76f4\u63a5\u6309\u884c\u8fdb\u884c\u7d2f\u52a0\uff08\u4e3b\u952e\u4e0d\u53ef\u80fd\u4e3a\u7a7a\uff09<\/li>\n<li>count(\u5b57\u6bb5)\uff1a\u6ca1\u6709not null\u7ea6\u675f\u7684\u8bdd\uff0cInnoDB\u5f15\u64ce\u4f1a\u904d\u5386\u6574\u5f20\u8868\u628a\u6bcf\u4e00\u884c\u7684\u5b57\u6bb5\u503c\u90fd\u53d6\u51fa\u6765\uff0c\u8fd4\u56de\u7ed9\u670d\u52a1\u5c42\uff0c\u670d\u52a1\u5c42\u5224\u65ad\u662f\u5426\u4e3anull\uff0c\u4e0d\u4e3anull\uff0c\u8ba1\u6570\u7d2f\u52a0\uff1b\u6709not null\u7ea6\u675f\u7684\u8bdd\uff0cInnoDB\u5f15\u64ce\u4f1a\u904d\u5386\u6574\u5f20\u8868\u628a\u6bcf\u4e00\u884c\u7684\u5b57\u6bb5\u503c\u90fd\u53d6\u51fa\u6765\uff0c\u8fd4\u56de\u7ed9\u670d\u52a1\u5c42\uff0c\u76f4\u63a5\u6309\u884c\u8fdb\u884c\u7d2f\u52a0<\/li>\n<li>count(1)\uff1aInnoDB \u5f15\u64ce\u904d\u5386\u6574\u5f20\u8868\uff0c\u4f46\u4e0d\u53d6\u503c\u3002\u670d\u52a1\u5c42\u5bf9\u4e8e\u8fd4\u56de\u7684\u6bcf\u4e00\u5c42\uff0c\u653e\u4e00\u4e2a\u6570\u5b57 1 \u8fdb\u53bb\uff0c\u76f4\u63a5\u6309\u884c\u8fdb\u884c\u7d2f\u52a0<\/li>\n<li>count(*)\uff1aInnoDB \u5f15\u64ce\u5e76\u4e0d\u4f1a\u628a\u5168\u90e8\u5b57\u6bb5\u53d6\u51fa\u6765\uff0c\u800c\u662f\u4e13\u95e8\u505a\u4e86\u4f18\u5316\uff0c\u4e0d\u53d6\u503c\uff0c\u670d\u52a1\u5c42\u76f4\u63a5\u6309\u884c\u8fdb\u884c\u7d2f\u52a0<\/li>\n<\/ul>\n<p>\u6309\u6548\u7387\u6392\u5e8f\uff1acount(\u5b57\u6bb5) &lt; count(\u4e3b\u952e) &lt; count(1) &lt; count(*)\uff0c\u6240\u4ee5\u5c3d\u91cf\u4f7f\u7528 count(*)<\/p>\n<h3>update\u4f18\u5316\uff08\u907f\u514d\u884c\u9501\u5347\u7ea7\u4e3a\u8868\u9501\uff09<\/h3>\n<p>InnoDB \u7684\u884c\u9501\u662f\u9488\u5bf9\u7d22\u5f15\u52a0\u7684\u9501\uff0c\u4e0d\u662f\u9488\u5bf9\u8bb0\u5f55\u52a0\u7684\u9501\uff0c\u5e76\u4e14\u8be5\u7d22\u5f15\u4e0d\u80fd\u5931\u6548\uff0c\u5426\u5219\u4f1a\u4ece\u884c\u9501\u5347\u7ea7\u4e3a\u8868\u9501\u3002<\/p>\n<p>\u5982\u4ee5\u4e0b\u4e24\u6761\u8bed\u53e5\uff1a<br \/><code>update student set no = &#039;123&#039; where id = 1;<\/code>\uff0c\u8fd9\u53e5\u7531\u4e8eid\u6709\u4e3b\u952e\u7d22\u5f15\uff0c\u6240\u4ee5\u53ea\u4f1a\u9501\u8fd9\u4e00\u884c\uff1b<br \/><code>update student set no = &#039;123&#039; where name = &#039;test&#039;;<\/code>\uff0c\u8fd9\u53e5\u7531\u4e8ename\u6ca1\u6709\u7d22\u5f15\uff0c\u6240\u4ee5\u4f1a\u628a\u6574\u5f20\u8868\u90fd\u9501\u4f4f\u8fdb\u884c\u6570\u636e\u66f4\u65b0\uff0c\u89e3\u51b3\u65b9\u6cd5\u662f\u7ed9name\u5b57\u6bb5\u6dfb\u52a0\u7d22\u5f15<\/p>\n<h1>\u6570\u636e\u7c7b\u578b<\/h1>\n<h2>\u6574\u578b<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b\u540d\u79f0<\/th>\n<th>\u53d6\u503c\u8303\u56f4<\/th>\n<th>\u5927\u5c0f<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>TINYINT<\/td>\n<td>-128\u301c127<\/td>\n<td>1\u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>SMALLINT<\/td>\n<td>-32768\u301c32767<\/td>\n<td>2\u4e2a\u5b87\u8282<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMINT<\/td>\n<td>-8388608\u301c8388607<\/td>\n<td>3\u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>INT (INTEGHR)<\/td>\n<td>-2147483648\u301c2147483647<\/td>\n<td>4\u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>BIGINT<\/td>\n<td>-9223372036854775808\u301c9223372036854775807<\/td>\n<td>8\u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u65e0\u7b26\u53f7\u5728\u6570\u636e\u7c7b\u578b\u540e\u52a0 unsigned \u5173\u952e\u5b57\u3002<\/p>\n<h2>\u6d6e\u70b9\u578b<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b\u540d\u79f0<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u5b58\u50a8\u9700\u6c42<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>FLOAT<\/td>\n<td>\u5355\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td>\n<td>4 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>DOUBLE<\/td>\n<td>\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td>\n<td>8 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>DECIMAL (M, D)\uff0cDEC<\/td>\n<td>\u538b\u7f29\u7684\u201c\u4e25\u683c\u201d\u5b9a\u70b9\u6570<\/td>\n<td>M+2 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u65e5\u671f\u548c\u65f6\u95f4<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b\u540d\u79f0<\/th>\n<th>\u65e5\u671f\u683c\u5f0f<\/th>\n<th>\u65e5\u671f\u8303\u56f4<\/th>\n<th>\u5b58\u50a8\u9700\u6c42<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>YEAR<\/td>\n<td>YYYY<\/td>\n<td>1901 ~ 2155<\/td>\n<td>1 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>TIME<\/td>\n<td>HH:MM:SS<\/td>\n<td>-838:59:59 ~ 838:59:59<\/td>\n<td>3 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>DATE<\/td>\n<td>YYYY-MM-DD<\/td>\n<td>1000-01-01 ~ 9999-12-3<\/td>\n<td>3 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>DATETIME<\/td>\n<td>YYYY-MM-DD HH:MM:SS<\/td>\n<td>1000-01-01 00:00:00 ~ 9999-12-31 23:59:59<\/td>\n<td>8 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>TIMESTAMP<\/td>\n<td>YYYY-MM-DD HH:MM:SS<\/td>\n<td>1980-01-01 00:00:01 UTC ~ 2040-01-19 03:14:07 UTC<\/td>\n<td>4 \u4e2a\u5b57\u8282<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u5b57\u7b26\u4e32<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b\u540d\u79f0<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u5b58\u50a8\u9700\u6c42<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>CHAR(M)<\/td>\n<td>\u56fa\u5b9a\u957f\u5ea6\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>M \u5b57\u8282\uff0c1&lt;=M&lt;=255<\/td>\n<\/tr>\n<tr>\n<td>VARCHAR(M)<\/td>\n<td>\u53d8\u957f\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>L+1\u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt; = M\u548c 1&lt;=M&lt;=255<\/td>\n<\/tr>\n<tr>\n<td>TINYTEXT<\/td>\n<td>\u975e\u5e38\u5c0f\u7684\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>L+1\u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^8<\/td>\n<\/tr>\n<tr>\n<td>TEXT<\/td>\n<td>\u5c0f\u7684\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>L+2\u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^16<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMTEXT<\/td>\n<td>\u4e2d\u7b49\u5927\u5c0f\u7684\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>L+3\u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^24<\/td>\n<\/tr>\n<tr>\n<td>LONGTEXT<\/td>\n<td>\u5927\u7684\u975e\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>L+4\u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^32<\/td>\n<\/tr>\n<tr>\n<td>ENUM<\/td>\n<td>\u679a\u4e3e\u7c7b\u578b\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u679a\u4e3e\u5b57\u7b26\u4e32\u503c<\/td>\n<td>1\u62162\u4e2a\u5b57\u8282\uff0c\u53d6\u51b3\u4e8e\u679a\u4e3e\u503c\u7684\u6570\u76ee (\u6700\u5927\u503c\u4e3a65535)<\/td>\n<\/tr>\n<tr>\n<td>SET<\/td>\n<td>\u4e00\u4e2a\u8bbe\u7f6e\uff0c\u5b57\u7b26\u4e32\u5bf9\u8c61\u53ef\u4ee5\u6709\u96f6\u4e2a\u6216 \u591a\u4e2aSET\u6210\u5458<\/td>\n<td>1\u30012\u30013\u30014\u62168\u4e2a\u5b57\u8282\uff0c\u53d6\u51b3\u4e8e\u96c6\u5408 \u6210\u5458\u7684\u6570\u91cf\uff08\u6700\u591a64\u4e2a\u6210\u5458\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e8c\u8fdb\u5236\u7c7b\u578b<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b\u540d\u79f0<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u5b58\u50a8\u9700\u6c42<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>BIT(M)<\/td>\n<td>\u4f4d\u5b57\u6bb5\u7c7b\u578b<\/td>\n<td>\u5927\u7ea6 (M+7)\/8 \u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>BINARY(M)<\/td>\n<td>\u56fa\u5b9a\u957f\u5ea6\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>M \u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>VARBINARY (M)<\/td>\n<td>\u53ef\u53d8\u957f\u5ea6\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32<\/td>\n<td>M+1 \u5b57\u8282<\/td>\n<\/tr>\n<tr>\n<td>TINYBLOB (M)<\/td>\n<td>\u975e\u5e38\u5c0f\u7684BLOB<\/td>\n<td>L+1 \u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^8<\/td>\n<\/tr>\n<tr>\n<td>BLOB (M)<\/td>\n<td>\u5c0f BLOB<\/td>\n<td>L+2 \u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^16<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMBLOB (M)<\/td>\n<td>\u4e2d\u7b49\u5927\u5c0f\u7684BLOB<\/td>\n<td>L+3 \u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^24<\/td>\n<\/tr>\n<tr>\n<td>LONGBLOB (M)<\/td>\n<td>\u975e\u5e38\u5927\u7684BLOB<\/td>\n<td>L+4 \u5b57\u8282\uff0c\u5728\u6b64\uff0cL&lt;2^32<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>\u6743\u9650\u4e00\u89c8\u8868<\/h1>\n<blockquote><p>\u5177\u4f53\u6743\u9650\u7684\u4f5c\u7528\u8be6\u89c1<a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html\" title=\"\u5b98\u65b9\u6587\u6863\">\u5b98\u65b9\u6587\u6863<\/a><\/p><\/blockquote>\n<p>GRANT \u548c REVOKE \u5141\u8bb8\u7684\u9759\u6001\u6743\u9650<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Privilege<\/th>\n<th align=\"left\">Grant Table Column<\/th>\n<th align=\"left\">Context<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_all\"><code>ALL [PRIVILEGES]<\/code><\/a><\/td>\n<td align=\"left\">Synonym for \u201call privileges\u201d<\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_alter\"><code>ALTER<\/code><\/a><\/td>\n<td align=\"left\"><code>Alter_priv<\/code><\/td>\n<td align=\"left\">Tables<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_alter-routine\"><code>ALTER ROUTINE<\/code><\/a><\/td>\n<td align=\"left\"><code>Alter_routine_priv<\/code><\/td>\n<td align=\"left\">Stored routines<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create\"><code>CREATE<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_priv<\/code><\/td>\n<td align=\"left\">Databases, tables, or indexes<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create-role\"><code>CREATE ROLE<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_role_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create-routine\"><code>CREATE ROUTINE<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_routine_priv<\/code><\/td>\n<td align=\"left\">Stored routines<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create-tablespace\"><code>CREATE TABLESPACE<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_tablespace_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create-temporary-tables\"><code>CREATE TEMPORARY TABLES<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_tmp_table_priv<\/code><\/td>\n<td align=\"left\">Tables<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create-user\"><code>CREATE USER<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_user_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_create-view\"><code>CREATE VIEW<\/code><\/a><\/td>\n<td align=\"left\"><code>Create_view_priv<\/code><\/td>\n<td align=\"left\">Views<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_delete\"><code>DELETE<\/code><\/a><\/td>\n<td align=\"left\"><code>Delete_priv<\/code><\/td>\n<td align=\"left\">Tables<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_drop\"><code>DROP<\/code><\/a><\/td>\n<td align=\"left\"><code>Drop_priv<\/code><\/td>\n<td align=\"left\">Databases, tables, or views<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_drop-role\"><code>DROP ROLE<\/code><\/a><\/td>\n<td align=\"left\"><code>Drop_role_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_event\"><code>EVENT<\/code><\/a><\/td>\n<td align=\"left\"><code>Event_priv<\/code><\/td>\n<td align=\"left\">Databases<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_execute\"><code>EXECUTE<\/code><\/a><\/td>\n<td align=\"left\"><code>Execute_priv<\/code><\/td>\n<td align=\"left\">Stored routines<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_file\"><code>FILE<\/code><\/a><\/td>\n<td align=\"left\"><code>File_priv<\/code><\/td>\n<td align=\"left\">File access on server host<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_grant-option\"><code>GRANT OPTION<\/code><\/a><\/td>\n<td align=\"left\"><code>Grant_priv<\/code><\/td>\n<td align=\"left\">Databases, tables, or stored routines<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_index\"><code>INDEX<\/code><\/a><\/td>\n<td align=\"left\"><code>Index_priv<\/code><\/td>\n<td align=\"left\">Tables<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_insert\"><code>INSERT<\/code><\/a><\/td>\n<td align=\"left\"><code>Insert_priv<\/code><\/td>\n<td align=\"left\">Tables or columns<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_lock-tables\"><code>LOCK TABLES<\/code><\/a><\/td>\n<td align=\"left\"><code>Lock_tables_priv<\/code><\/td>\n<td align=\"left\">Databases<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_process\"><code>PROCESS<\/code><\/a><\/td>\n<td align=\"left\"><code>Process_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_proxy\"><code>PROXY<\/code><\/a><\/td>\n<td align=\"left\">See<code>proxies_priv<\/code> table<\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_references\"><code>REFERENCES<\/code><\/a><\/td>\n<td align=\"left\"><code>References_priv<\/code><\/td>\n<td align=\"left\">Databases or tables<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_reload\"><code>RELOAD<\/code><\/a><\/td>\n<td align=\"left\"><code>Reload_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_replication-client\"><code>REPLICATION CLIENT<\/code><\/a><\/td>\n<td align=\"left\"><code>Repl_client_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_replication-slave\"><code>REPLICATION SLAVE<\/code><\/a><\/td>\n<td align=\"left\"><code>Repl_slave_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_select\"><code>SELECT<\/code><\/a><\/td>\n<td align=\"left\"><code>Select_priv<\/code><\/td>\n<td align=\"left\">Tables or columns<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_show-databases\"><code>SHOW DATABASES<\/code><\/a><\/td>\n<td align=\"left\"><code>Show_db_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_show-view\"><code>SHOW VIEW<\/code><\/a><\/td>\n<td align=\"left\"><code>Show_view_priv<\/code><\/td>\n<td align=\"left\">Views<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_shutdown\"><code>SHUTDOWN<\/code><\/a><\/td>\n<td align=\"left\"><code>Shutdown_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_super\"><code>SUPER<\/code><\/a><\/td>\n<td align=\"left\"><code>Super_priv<\/code><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_trigger\"><code>TRIGGER<\/code><\/a><\/td>\n<td align=\"left\"><code>Trigger_priv<\/code><\/td>\n<td align=\"left\">Tables<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_update\"><code>UPDATE<\/code><\/a><\/td>\n<td align=\"left\"><code>Update_priv<\/code><\/td>\n<td align=\"left\">Tables or columns<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_usage\"><code>USAGE<\/code><\/a><\/td>\n<td align=\"left\">Synonym for \u201cno privileges\u201d<\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>GRANT \u548c REVOKE \u5141\u8bb8\u7684\u52a8\u6001\u6743\u9650<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">Privilege<\/th>\n<th align=\"left\">Context<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_application-password-admin\"><code>APPLICATION_PASSWORD_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Dual password administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_audit-abort-exempt\"><code>AUDIT_ABORT_EXEMPT<\/code><\/a><\/td>\n<td align=\"left\">Allow queries blocked by audit log filter<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_audit-admin\"><code>AUDIT_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Audit log administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_authentication-policy-admin\"><code>AUTHENTICATION_POLICY_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Authentication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_backup-admin\"><code>BACKUP_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Backup administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_binlog-admin\"><code>BINLOG_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Backup and Replication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_binlog-encryption-admin\"><code>BINLOG_ENCRYPTION_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Backup and Replication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_clone-admin\"><code>CLONE_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Clone administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_connection-admin\"><code>CONNECTION_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_encryption-key-admin\"><code>ENCRYPTION_KEY_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_firewall-admin\"><code>FIREWALL_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Firewall administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_firewall-exempt\"><code>FIREWALL_EXEMPT<\/code><\/a><\/td>\n<td align=\"left\">Firewall administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_firewall-user\"><code>FIREWALL_USER<\/code><\/a><\/td>\n<td align=\"left\">Firewall administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_flush-optimizer-costs\"><code>FLUSH_OPTIMIZER_COSTS<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_flush-status\"><code>FLUSH_STATUS<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_flush-tables\"><code>FLUSH_TABLES<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_flush-user-resources\"><code>FLUSH_USER_RESOURCES<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_group-replication-admin\"><code>GROUP_REPLICATION_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Replication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_group-replication-stream\"><code>GROUP_REPLICATION_STREAM<\/code><\/a><\/td>\n<td align=\"left\">Replication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_innodb-redo-log-archive\"><code>INNODB_REDO_LOG_ARCHIVE<\/code><\/a><\/td>\n<td align=\"left\">Redo log archiving administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_ndb-stored-user\"><code>NDB_STORED_USER<\/code><\/a><\/td>\n<td align=\"left\">NDB Cluster<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_passwordless-user-admin\"><code>PASSWORDLESS_USER_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Authentication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_persist-ro-variables-admin\"><code>PERSIST_RO_VARIABLES_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_replication-applier\"><code>REPLICATION_APPLIER<\/code><\/a><\/td>\n<td align=\"left\"><code>PRIVILEGE_CHECKS_USER<\/code> for a replication channel<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_replication-slave-admin\"><code>REPLICATION_SLAVE_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Replication administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_resource-group-admin\"><code>RESOURCE_GROUP_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Resource group administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_resource-group-user\"><code>RESOURCE_GROUP_USER<\/code><\/a><\/td>\n<td align=\"left\">Resource group administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_role-admin\"><code>ROLE_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_session-variables-admin\"><code>SESSION_VARIABLES_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_set-user-id\"><code>SET_USER_ID<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_show-routine\"><code>SHOW_ROUTINE<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_system-user\"><code>SYSTEM_USER<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_system-variables-admin\"><code>SYSTEM_VARIABLES_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_table-encryption-admin\"><code>TABLE_ENCRYPTION_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_version-token-admin\"><code>VERSION_TOKEN_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<tr>\n<td align=\"left\"><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/privileges-provided.html#priv_xa-recover-admin\"><code>XA_RECOVER_ADMIN<\/code><\/a><\/td>\n<td align=\"left\">Server administration<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h1>\u56fe\u5f62\u5316\u754c\u9762\u5de5\u5177<\/h1>\n<ul>\n<li>Workbench(\u514d\u8d39): <a href=\"http:\/\/dev.mysql.com\/downloads\/workbench\/\">http:\/\/dev.mysql.com\/downloads\/workbench\/<\/a><\/li>\n<li>navicat(\u6536\u8d39\uff0c\u8bd5\u7528\u724830\u5929): <a href=\"https:\/\/www.navicat.com\/en\/download\/navicat-for-mysql\">https:\/\/www.navicat.com\/en\/download\/navicat-for-mysql<\/a><\/li>\n<li>Sequel Pro(\u5f00\u6e90\u514d\u8d39\uff0c\u4ec5\u652f\u6301Mac OS): <a href=\"http:\/\/www.sequelpro.com\/\">http:\/\/www.sequelpro.com\/<\/a><\/li>\n<li>HeidiSQL(\u514d\u8d39): <a href=\"http:\/\/www.heidisql.com\/\">http:\/\/www.heidisql.com\/<\/a><\/li>\n<li>phpMyAdmin(\u514d\u8d39): <a href=\"https:\/\/www.phpmyadmin.net\/\">https:\/\/www.phpmyadmin.net\/<\/a><\/li>\n<li>SQLyog: <a href=\"https:\/\/sqlyog.en.softonic.com\/\">https:\/\/sqlyog.en.softonic.com\/<\/a><\/li>\n<\/ul>\n<h1>\u5b89\u88c5<\/h1>\n<h1>\u5c0f\u6280\u5de7<\/h1>\n<ol>\n<li>\u5728SQL\u8bed\u53e5\u4e4b\u540e\u52a0\u4e0a<code>\\G<\/code>\u4f1a\u5c06\u7ed3\u679c\u7684\u8868\u683c\u5f62\u5f0f\u8f6c\u6362\u6210\u884c\u6587\u672c\u5f62\u5f0f<\/li>\n<li>\u67e5\u770bMysql\u6570\u636e\u5e93\u5360\u7528\u7a7a\u95f4\uff1a<\/li>\n<\/ol>\n<pre><code class=\"lang-mysql\">SELECT table_schema &quot;Database Name&quot;\n     , SUM(data_length + index_length) \/ (1024 * 1024) &quot;Database Size in MB&quot;\nFROM information_schema.TABLES\nGROUP BY table_schema;<\/code><\/pre>\n<h1>\u540e\u7eed\u5185\u5bb9<\/h1>\n<p><strong>\u5728\u6b64\u611f\u8c22B\u7ad9\u540c\u6837\u7231\u5b66\u4e60\u7684\u540c\u5b66 @\u5b88\u5fc3-\u4eba \u63d0\u4f9b\u7684\u540e\u7eed\u8bfe\u7a0b\u7b14\u8bb0\uff0c\u5927\u5bb6\u6709\u6761\u4ef6\u4e00\u5b9a\u8981\u53bb\u7ed9\u4e2astar\uff0c\u4f60\u7684\u6bcf\u4e00\u4e2astar\u548c\u70b9\u8d5e\u90fd\u662f\u6211\u4eec\u524d\u8fdb\u7684\u52a8\u529b<\/strong><br \/><strong><a href=\"https:\/\/github.com\/Buildings-Lei\/mysql_note\/blob\/main\/README.md\">https:\/\/github.com\/Buildings-Lei\/mysql_note\/blob\/main\/README.md<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u7840\u7bc7 \u901a\u7528\u8bed\u6cd5\u53ca\u5206\u7c7b DDL: \u6570\u636e\u5b9a\u4e49\u8bed\u8a00\uff0c\u7528\u6765\u5b9a\u4e49\u6570\u636e\u5e93\u5bf9\u8c61\uff08\u6570\u636e\u5e93\u3001\u8868\u3001\u5b57\u6bb5\uff09 DML: \u6570\u636e\u64cd\u4f5c\u8bed\u8a00\uff0c\u7528\u6765\u5bf9\u6570\u636e\u5e93\u8868\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u589e\u5220\u6539 DQL: \u6570\u636e\u67e5\u8be2\u8bed\u8a00\uff0c\u7528\u6765\u67e5\u8be2\u6570\u636e\u5e93\u4e2d\u8868\u7684\u8bb0\u5f55 DCL: \u6570\u636e\u63a7\u5236\u8bed\u8a00\uff0c\u7528\u6765\u521b\u5efa\u6570\u636e\u5e93\u7528\u6237\u3001\u63a7\u5236\u6570\u636e\u5e93\u7684\u63a7\u5236\u6743\u9650 DDL\uff08\u6570\u636e\u5b9a\u4e49\u8bed\u8a00\uff09 \u6570\u636e\u5b9a\u4e49\u8bed\u8a00 \u6570\u636e\u5e93\u64cd\u4f5c \u67e5\u8be2\u6240\u6709\u6570\u636e\u5e93\uff1aSHOW DATABASES;\u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\uff1aSELECT DATABASE();\u521b\u5efa\u6570\u636e\u5e93\uff1aCREATE DATABASE [ IF NOT EXISTS ] \u6570\u636e\u5e93\u540d [ DEFAULT CHARSET \u5b57\u7b26\u96c6] [COLLATE \u6392\u5e8f\u89c4\u5219 ];\u5220\u9664\u6570\u636e\u5e93\uff1aDROP DATABASE [ IF EXISTS ] \u6570\u636e\u5e93\u540d;\u4f7f\u7528\u6570\u636e\u5e93\uff1aUSE \u6570\u636e\u5e93\u540d; \u6ce8\u610f\u4e8b\u9879 UTF8\u5b57\u7b26\u96c6\u957f\u5ea6\u4e3a3\u5b57\u8282\uff0c\u6709\u4e9b\u7b26\u53f7\u53604\u5b57\u8282\uff0c\u6240\u4ee5\u63a8\u8350\u7528utf8mb4\u5b57\u7b26\u96c6 \u8868\u64cd\u4f5c \u67e5\u8be2\u5f53\u524d\u6570\u636e\u5e93\u6240\u6709\u8868\uff1aSHOW TABLES;\u67e5\u8be2\u8868\u7ed3\u6784\uff1aDESC \u8868\u540d;\u67e5\u8be2\u6307\u5b9a\u8868\u7684\u5efa\u8868\u8bed\u53e5\uff1aSHOW CREATE TABLE \u8868\u540d; \u521b\u5efa\u8868\uff1a CREATE TABLE \u8868\u540d( \u5b57\u6bb51 \u5b57\u6bb51\u7c7b\u578b [COMMENT \u5b57\u6bb51\u6ce8\u91ca], \u5b57\u6bb52 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-107","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=107"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/107\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}