mirror of
https://github.com/jaywcjlove/reference.git
synced 2025-06-17 12:41:20 +08:00
doc: update docs/mysql.md #940 3e732813c8
This commit is contained in:
114
docs/mysql.html
114
docs/mysql.html
@ -39,7 +39,7 @@
|
||||
<p>本备忘单旨在快速理解 <a href="https://mysql.com">MySQL</a> 所涉及的主要概念,提供了最常用的SQL语句,供您参考。</p>
|
||||
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
|
||||
<path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
|
||||
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#登录mysql">登录MySQL</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用的">常用的</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#数据库-database">数据库 Database</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#表-table">表 Table</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#proccess">Proccess</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看-mysql-信息">查看 MySQL 信息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#退出mysql会话">退出MySQL会话</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#备份">备份</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#错误处理-error-handling">错误处理 Error Handling</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mysql-示例">MySQL 示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理表格">管理表格</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从表中查询数据">从表中查询数据</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从多个表查询">从多个表查询</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-sql-约束">使用 SQL 约束</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改数据">修改数据</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理视图">管理视图</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理触发器">管理触发器</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#when">WHEN</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#event">EVENT</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#trigger_type">TRIGGER_TYPE</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理索引">管理索引</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mysql-数据类型">MySQL 数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#strings">Strings</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#date--time">Date & time</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#numeric">Numeric</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#聚合函数">聚合函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数学函数">数学函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#日期和时间函数">日期和时间函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串函数">字符串函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#高级函数">高级函数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
|
||||
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#登录mysql">登录MySQL</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用的">常用的</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#数据库-database">数据库 Database</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#表-table">表 Table</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#proccess">Proccess</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#查看-mysql-信息">查看 MySQL 信息</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#退出mysql会话">退出MySQL会话</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#备份">备份</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#恢复备份">恢复备份</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#错误处理error-handling">错误处理(Error Handling)</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mysql-示例">MySQL 示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理表格">管理表格</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从表中查询数据">从表中查询数据</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从多个表查询">从多个表查询</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-sql-约束">使用 SQL 约束</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改数据">修改数据</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理视图">管理视图</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理触发器">管理触发器</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#when">WHEN</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#event">EVENT</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#trigger_type">TRIGGER_TYPE</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#管理索引">管理索引</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#mysql-数据类型">MySQL 数据类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#strings">Strings</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#date--time">Date & time</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#numeric">Numeric</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#聚合函数">聚合函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数学函数">数学函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#日期和时间函数">日期和时间函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串函数">字符串函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#高级函数">高级函数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
|
||||
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=row-span-3-->
|
||||
<p>MySQL 为关系型数据库(Relational Database Management System),一个关系型数据库由一个或数个表格组成,如下所示的一个表格</p>
|
||||
@ -169,18 +169,83 @@
|
||||
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">mysql<span class="token operator">></span> <span class="token builtin class-name">exit</span>
|
||||
</span></code></pre>
|
||||
<p>退出 <code>quit;</code> 或 <code>\q;</code> 一样的效果</p>
|
||||
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="备份"><a aria-hidden="true" tabindex="-1" href="#备份"><span class="icon icon-link"></span></a>备份</h3><div class="wrap-body">
|
||||
<p>创建备份</p>
|
||||
<pre class="language-sql"><code class="language-sql code-highlight"><span class="code-line">mysqldump <span class="token operator">-</span>u <span class="token keyword">user</span> <span class="token operator">-</span>p db_name <span class="token operator">></span> db<span class="token punctuation">.</span><span class="token keyword">sql</span>
|
||||
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="备份"><a aria-hidden="true" tabindex="-1" href="#备份"><span class="icon icon-link"></span></a>备份</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=col-span-2-->
|
||||
<p>备份特定表</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name table1 table2 <span class="token operator">></span> tables_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>备份多个数据库</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> <span class="token parameter variable">--databases</span> db1 db2 <span class="token operator">></span> multi_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>备份所有数据库</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> --all-databases <span class="token operator">></span> all_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>备份时压缩</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator">|</span> <span class="token function">gzip</span> <span class="token operator">></span> db_backup.sql.gz
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>导出不带架构的数据库</p>
|
||||
<pre class="wrap-text "><code class="language-shell code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name --no-data<span class="token operator">=</span>true --add-drop-table<span class="token operator">=</span>false <span class="token operator">></span> db.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>恢复备份</p>
|
||||
<pre class="language-shell"><code class="language-shell code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator"><</span> db.sql
|
||||
<p>仅导出数据</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> --no-create-info db_name <span class="token operator">></span> only_data.sql
|
||||
</span></code></pre>
|
||||
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="错误处理-error-handling"><a aria-hidden="true" tabindex="-1" href="#错误处理-error-handling"><span class="icon icon-link"></span></a>错误处理 Error Handling</h3><div class="wrap-body">
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>仅导出结构</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> --no-data db_name <span class="token operator">></span> only_schema.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>导出时忽略某些表</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysqldump <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name --ignore-table<span class="token operator">=</span>db_name.table1 --ignore-table<span class="token operator">=</span>db_name.table2 <span class="token operator">></span> partial.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="恢复备份"><a aria-hidden="true" tabindex="-1" href="#恢复备份"><span class="icon icon-link"></span></a>恢复备份</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=row-span-2-->
|
||||
<p>恢复单个数据库备份</p>
|
||||
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator"><</span> db_backup.sql
|
||||
</span></code></pre>
|
||||
<p>恢复多个数据库(带 <code>--databases</code> 选项备份的)</p>
|
||||
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> <span class="token operator"><</span> multi_backup.sql
|
||||
</span></code></pre>
|
||||
<p>恢复所有数据库(使用 <code>--all-databases</code> 备份的)</p>
|
||||
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> <span class="token operator"><</span> all_backup.sql
|
||||
</span></code></pre>
|
||||
<p>从 gzip 压缩的备份恢复</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">gunzip <span class="token operator"><</span> db_backup.sql.gz <span class="token operator">|</span> mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name
|
||||
</span><span class="code-line">
|
||||
</span><span class="code-line"><span class="token comment"># 或:</span>
|
||||
</span><span class="code-line">
|
||||
</span><span class="code-line">zcat db_backup.sql.gz <span class="token operator">|</span> mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>恢复单张表(从 <code>mysqldump</code> 单表导出文件)</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator"><</span> table1_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>先创建数据库再导入(如果备份中不包含 CREATE DATABASE)</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> <span class="token parameter variable">-e</span> <span class="token string">"CREATE DATABASE IF NOT EXISTS db_name;"</span>
|
||||
</span><span class="code-line">
|
||||
</span><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator"><</span> db_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>恢复指定字符集(防止乱码)</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysql --default-character-set<span class="token operator">=</span>utf8mb4 <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator"><</span> db_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>恢复时跳过某些错误(如重复键)</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> <span class="token parameter variable">--force</span> db_name <span class="token operator"><</span> db_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
<p>恢复到远程主机数据库</p>
|
||||
<pre class="wrap-text "><code class="language-bash code-highlight"><span class="code-line">mysql <span class="token parameter variable">-h</span> remote_host <span class="token parameter variable">-u</span> user <span class="token parameter variable">-p</span> db_name <span class="token operator"><</span> db_backup.sql
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text -->
|
||||
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="错误处理error-handling"><a aria-hidden="true" tabindex="-1" href="#错误处理error-handling"><span class="icon icon-link"></span></a>错误处理(Error Handling)</h3><div class="wrap-body">
|
||||
<!--rehype:wrap-class=col-span-2-->
|
||||
|
||||
|
||||
|
||||
@ -214,7 +279,40 @@
|
||||
|
||||
|
||||
|
||||
<table><thead><tr><th align="left">语句</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>SHOW ERRORS;</code></td><td align="left">显示最近的错误信息</td></tr><tr><td align="left"><code>SHOW WARNINGS;</code></td><td align="left">显示最近的警告信息</td></tr><tr><td align="left"><code>SHOW COUNT(*) ERRORS;</code></td><td align="left">返回最近语句的错误数量</td></tr><tr><td align="left"><code>SHOW COUNT(*) WARNINGS;</code></td><td align="left">返回最近语句的警告数量</td></tr><tr><td align="left"><code>EXPLAIN</code></td><td align="left">分析 SQL 查询的执行计划,排查问题</td></tr><tr><td align="left"><code>SHOW ENGINE INNODB STATUS;</code></td><td align="left">显示 InnoDB 引擎状态,包括死锁等错误信息</td></tr></tbody></table>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<table class="left-align"><thead><tr><th align="left">语句</th><th align="left">说明</th></tr></thead><tbody><tr><td align="left"><code>SHOW ERRORS;</code></td><td align="left">显示最近的错误</td></tr><tr><td align="left"><code>SHOW WARNINGS;</code></td><td align="left">显示最近的警告</td></tr><tr><td align="left"><code>SHOW COUNT(*) ERRORS;</code></td><td align="left">显示错误数量</td></tr><tr><td align="left"><code>SHOW COUNT(*) WARNINGS;</code></td><td align="left">显示警告数量</td></tr><tr><td align="left"><code>EXPLAIN SELECT ...;</code></td><td align="left">分析查询执行计划</td></tr><tr><td align="left"><code>SHOW ENGINE INNODB STATUS;</code></td><td align="left">查看 InnoDB 状态和死锁信息</td></tr><tr><td align="left"><code>SHOW PROFILE;</code></td><td align="left">显示语句的资源消耗(需开启 profiling)</td></tr><tr><td align="left"><code>SHOW PROFILES;</code></td><td align="left">显示所有已记录的 profiling 数据</td></tr><tr><td align="left"><code>SHOW PROCESSLIST;</code></td><td align="left">查看当前线程,排查长时间运行或阻塞的语句</td></tr><tr><td align="left"><code>SHOW STATUS LIKE 'Last_error%';</code></td><td align="left">查看上次语句执行的错误信息</td></tr><tr><td align="left"><code>SHOW VARIABLES LIKE 'log_%';</code></td><td align="left">查看错误日志相关配置</td></tr><tr><td align="left"><code>SHOW BINARY LOGS;</code></td><td align="left">查看二进制日志,排查事务或复制异常</td></tr><tr><td align="left"><code>SHOW SLAVE STATUS\G</code></td><td align="left">查看主从复制错误(用于主从复制场景)</td></tr><tr><td align="left"><code>SHOW MASTER STATUS;</code></td><td align="left">查看主库状态,辅助分析复制问题</td></tr></tbody></table>
|
||||
<!--rehype:className=left-align-->
|
||||
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="mysql-示例"><a aria-hidden="true" tabindex="-1" href="#mysql-示例"><span class="icon icon-link"></span></a>MySQL 示例</h2><div class="wrap-body">
|
||||
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="管理表格"><a aria-hidden="true" tabindex="-1" href="#管理表格"><span class="icon icon-link"></span></a>管理表格</h3><div class="wrap-body">
|
||||
<p>创建一个包含三列的新表</p>
|
||||
|
Reference in New Issue
Block a user