主键和唯一的区别: 主键:唯一、非空、只能一个 唯一:唯一、可空、多个
外键: 1. 从表设置外键关系 2. 主从表类型一致/兼容 3. 主表关联键一般为主键或唯一 4. 必须对应主表数据,删除先删除从表再删除主表 </code></pre>
<h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">DQL 数据查询语言</span><span class="suffix"></span></h2> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">常量、表达式、函数</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 常量值</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>*<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表达式</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">VERSION</span>(); <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 函数</span> <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">别名</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>+<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">2</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">number</span>; <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">去重</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DISTINCT</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`table`</span> <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">+号</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>+<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">2</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 数字相加</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>+<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'123'</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 字符串会强转成数字非数字转为0</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span> + <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">Null</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 与Null返回Null</span> <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">字符串连接 <code>concat</code></span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'a'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'b'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'c'</span>); <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 字符串拼接</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 拼接字段</span> <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">条件查询</span><span class="suffix" style="display: none;"></span></h4> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content">条件表达式</span><span class="suffix" style="display: none;"></span></h5> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;"><</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">></code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">>=</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;"><=</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">!=</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;"><></code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;"><=></code></p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 等于</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Bruce'</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 安全等于 可查 Null</span> <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span><=><span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Bruce'</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 大于</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> * <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span> > <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">60</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 小于</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> * <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span> <= <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">60</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 不等于 </span> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># != 不建议</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> * <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span> <> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">60</span>; <span/></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content">逻辑表达式</span><span class="suffix" style="display: none;"></span></h5> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">&&</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">||</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">!</code><br> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">AND</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">OR</code> <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">NOT</code></p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 且查询</span> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 不建议 &&</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Bruce'</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Ernst'</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 或</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Bruce'</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">OR</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'K_ing'</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Bruce'</span> <span/></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content">模糊查询</span><span class="suffix" style="display: none;"></span></h5> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>like</code> 模糊查询</span><span class="suffix" style="display: none;"></span></h6> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">%</code>:任意多个字符 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">_</code>: 任意单个字符 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">\</code>: 转义</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 包含 a</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">like</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%a%'</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 定义转义字符</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">like</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%$_%'</span> ESCAPE <span class="hljs-string" style="color: #c41a16; line-height: 26px;">' <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>between and</code> 范围</span><span class="suffix" style="display: none;"></span></h6> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">在什么到什么之间,包含前后</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查找100 到200 之间的数据</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employee_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BETWEEN</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">100</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>; <span/></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>in</code> 属于</span><span class="suffix" style="display: none;"></span></h6> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">查询是否属于某些列表中的某一个</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询是否属于某些列表中的某一个</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IN</span> (<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'SH_CLERK'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'AD_ASST'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'AD_VP'</span>); <span/></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>is null</code> or <code>is not null</code> 是否为Null</span><span class="suffix" style="display: none;"></span></h6> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询字段为空的</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IS</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询不为空的</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IS</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>; <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>; <span/></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code><=></code> 安全等于</span><span class="suffix" style="display: none;"></span></h6> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">既可以判断NULL 又可以判断数值</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span/> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <=> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>; <span/></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>order by</code> 排序</span><span class="suffix" style="display: none;"></span></h5> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">ASC</code>升序 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">DESC</code>降序 默认 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">ASC</code></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ASC</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 升序</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DESC</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 降序</span> <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IFNULL</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">0</span>) + <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IFNULL</span>(manager_id,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">0</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> money,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`money`</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表达式别名降序</span> <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LENGTH</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">len</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">len</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 按函数</span> <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DESC</span>, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employee_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ASC</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 多个排序条件</span> <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">分组查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 每个工种的总工资</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SUM</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`money`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`money`</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 每个工种的最高工资</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询邮箱包含a的工种的最高工资</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> email <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%a%'</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询邮箱包含a的工种的最高工资大于1万的 HAVING</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> email <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%a%'</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span> > <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10000</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询名称长度一样的大于5个人的数据</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LENGTH</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span>, <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COUNT</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span> > <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">5</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 多字段分组</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AVG</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`avg`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">连接查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;">分类: 按年代划分: sql92标准:只支持内连接 sql99标准【推荐】:支持内连接+外来连接(左外、右外)+交叉连接 按功能划分: 内连接: 等值连接 非等值连接 自连接 外连接: 左外连接 右外连接 全外连接 交叉连接 </code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">sql92标准</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 等值连接</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> e.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,j.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_title`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`j`</span>.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> e,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`jobs`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> j <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`e`</span>.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`j`</span>.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span>; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非等值连接</span> <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 自连接</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> e.last_name,m.employee_id,e.manager_id,m.last_name <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> e, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> m <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`e`</span>.employee_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`m`</span>.manager_id <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">sql99标准</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 等值连接</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_name`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> departments <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span> = departments.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span> <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 复杂的等值连接</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> department_name,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COUNT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'*'</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">count</span>,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">max</span>,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">min</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">min</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> departments <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.department_id = departments.department_id <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> last_name <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%o%'</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> department_name <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BETWEEN</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">2</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span> <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 多表等值连接</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> last_name,department_name,job_title <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> departments <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> departments.department_id =employees.department_id <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> jobs <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.job_id = jobs.job_id; <span/> <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">子查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;">分类: 按出现位置: select 后面: 只支持标量子查询 form 后面 表子查询 where 和having后面 * 标量子查询 * 列子查询 * 行子查询 exists后面 表子查询1按结果集的行列数不同: 2 标量子查询(结果集只有一行一列) 3 列子查询(结果集只有一列多行) 4 行子查询(结果集只有多列一行) 5 表子查询(结果集一般多行多列)
</code></pre>
<h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">where 和having后面</span><span class="suffix" style="display: none;"></span></h4> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">特点: 1. 放在小括号内 2. 放在条件右侧 3. 标量子查询:配合单行操作符。列子查询:配合多行操作符。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询工资比Abel高的人</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> salary > ( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> salary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> last_name = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Abel'</span> <span/>); <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># job_id与141号员工一样且工资大于143号员工的员工</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> last_name,salary,employee_id <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> job_id = ( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> job_id <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> employee_id = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">141</span> <span/>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> salary > ( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> salary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> employee_id = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">143</span> <span/>) <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询比50号部门最低工资高的部门的最低工资</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> department_id, <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COUNT</span>(*),<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MIN</span>(salary) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> minsalary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> department_id <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> minsalary > (<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MIN</span>(salary) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> minsalary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> department_id = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">50</span>) <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> minsalary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DESC</span>; <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">分页查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIMIT</span> (page<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">-1</span>)*<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">size</span>,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">size</span>; <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIMIT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>; <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">联合查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 无</span> <span/></code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">DML 数据操作语言</span><span class="suffix"></span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">插入</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs(job_id,job_title,min_salary,max_salary) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">VALUES</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'p_a'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'捡垃圾1'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span>); <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">VALUES</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'p_a1'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'捡垃圾1'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span>), <span/>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'p_a2'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'捡垃圾2'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span>); <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces'</span>,job_title=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">"123"</span> <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces1'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'444'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span> <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">修改</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 简单修改</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> jobs <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> job_title = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'2222'</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces1'</span> <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改多表</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> jobs <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.job_id = jobs.job_id <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> jobs.job_title = <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(jobs.job_title,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'22222'</span>),employees.job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces1'</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> jobs.job_title = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Public Accountant12322222'</span> <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">删除</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 单表删除</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DELETE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> jobs <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces'</span> <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 清空整表</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TRUNCATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> ttt; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 多表删除</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DELETE</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> jobs <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> jobs.job_id = employees.job_id <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> jobs.job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'SA_MAN'</span> <span/></code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">DDL 数据定义语言</span><span class="suffix"></span></h2> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;">1. 库的管理 创建\修改\删除 2. 表的管理 创建\修改\删除创建: create 修改: alter 删除: drop </code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">库管理</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 创建 IF NOT EXISTS 进行容错 CHARACTER SET 字符集</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> books <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHARACTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> utf8; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改 不能该了</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">RENAME</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> books <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TO</span> newbook; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 更改字符集</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> books <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHARACTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> gbk; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 库的删除</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> books; <span/> <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">表的管理</span><span class="suffix" style="display: none;"></span></h3> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">创建表</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">USE</span> books; <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 表的创建</span> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;">/* <span/>CREATE TABLE 表名( <span/> 列名 列类型([长度]) [约束], <span/> 列名 列类型([长度]) [约束], <span/> ... <span/>) <span/>*/</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> book ( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span/> b_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>), <span/> price <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DOUBLE</span>, <span/> author_id <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span/> publish_date DATETIME <span/>); <span/> <span/>DESC book; <span/> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span/> au_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>), <span/> nation <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>) <span/>); <span/> <span/>DESC author; <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">表的修改</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;">/* <span/>修改列: <span/>ALTER TABLE 表名 [CHANGE|MODIFY|ADD|DROP] COLUMN 列名 类型|约束; <span/>修改表名: <span/>ALTER TABLE 表名 RENAME TO 新表名; <span/>*/</span> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改列名</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> book <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CHANGE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> publish_date publishDate DATETIME; <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改列类型约束</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> book <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MODIFY</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> publishDate <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">TIMESTAMP</span>; <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 添加新列</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ADD</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> annual <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DOUBLE</span>; <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 删除</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> annual; <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改表名</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">RENAME</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TO</span> authers; <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">表的删除</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> authers; <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">表的复制</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 复制表结构</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> copy <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> author; <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 复制整个表</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> copy2 <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> author; <span/></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">约束</span><span class="suffix" style="display: none;"></span></h3> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">添加表时添加约束</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 列级约束</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span> <span/> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span> <span/> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DEFAULT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'男'</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span> <span/> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span> <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span> <span/>); <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 表级约束</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span> <span/> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>), <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span> <span/> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) , <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span> <span/> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span> <span/> majorid <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span/> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> pk PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>),<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> uq <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span>(seat),<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> fk_stuiinfo_major <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>)<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 外键</span> <span/>); <span/> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 通用建议约束</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> stuinfo; <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span> <span/> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span> <span/> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DEFAULT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'男'</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span> <span/> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span> <span/> majorid <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span/> <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表级约束</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> fk_stuiinfo_major <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>)<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 外键</span> <span/>); <span/> <span/> <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">修改表时的约束</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改列约束</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MODIFY</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>; <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 表级约束</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ADD</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>); <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ADD</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>); <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 删除外键</span> <span/><span class="hljs-comment" style="color: #007400; line-height: 26px;"># ALTER TABLE stuinfo DROP PRIMARY KEY;</span> <span/></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">标识列(自增长列)</span><span class="suffix" style="display: none;"></span></h4> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">AUTO_INCREMENT</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo( <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span> AUTO_INCREMENT, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span> <span/> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span> <span/> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DEFAULT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'男'</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span> <span/> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span> <span/> majorid <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span/> <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表级约束</span> <span/> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> fk_stuiinfo_major <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>)<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 外键</span> <span/>); <span/></code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">TCL 事务控制语言</span><span class="suffix"></span></h2> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">一个或一组sql语句组成一个执行单位,要么全部执行,要么全部失败。</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">ACID: A: 原子性 C: 一致性 I: 隔离性 D: 持久性</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: block; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 提交表示完成,回滚代表异常</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">set</span> autocommit = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">0</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 关闭自动事务</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">START</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TRANSACTION</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 开始事务</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> stu_name = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'12232'</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">3</span>; <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SAVEPOINT</span> a; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 保存节点</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> stu_name = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'12332'</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">5</span>; <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ROLLBACK</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 回滚事务</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ROLLBACK</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TO</span> a; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 回滚事务到指定节点</span> <span/><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COMMIT</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 提交事务 </span> <span/></code></pre> <p id="nice-suffix-juejin-container" class="nice-suffix-juejin-container" data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin-top: 20px !important;">本文使用 <a href="https://mdnice.com/?from=juejin" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">mdnice</a> 排版</p></section></span>; <span/> <span/></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>between and</code> 范围</span><span class="suffix" style="display: none;"></span></h6> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">在什么到什么之间,包含前后</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查找100 到200 之间的数据</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <br> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employee_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BETWEEN</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">100</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>;<br></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>in</code> 属于</span><span class="suffix" style="display: none;"></span></h6> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">查询是否属于某些列表中的某一个</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询是否属于某些列表中的某一个</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <br> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IN</span> (<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'SH_CLERK'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'AD_ASST'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'AD_VP'</span>);<br></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>is null</code> or <code>is not null</code> 是否为Null</span><span class="suffix" style="display: none;"></span></h6> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询字段为空的</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span><br> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IS</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询不为空的</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span><br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IS</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>;<br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span><br> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>;<br></code></pre> <h6 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code><=></code> 安全等于</span><span class="suffix" style="display: none;"></span></h6> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">既可以判断NULL 又可以判断数值</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span><br> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span> <=> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>;<br></code></pre> <h5 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 16px;"><span class="prefix" style="display: none;"></span><span class="content"><code>order by</code> 排序</span><span class="suffix" style="display: none;"></span></h5> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;"><code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">ASC</code>升序 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">DESC</code>降序 默认 <code style="font-size: 14px; word-wrap: break-word; padding: 2px 4px; border-radius: 4px; margin: 0 2px; background-color: rgba(27,31,35,.05); font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; word-break: break-all; color: #595959;">ASC</code></p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ASC</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 升序</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DESC</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 降序</span><br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IFNULL</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`commission_pct`</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">0</span>) + <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IFNULL</span>(manager_id,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">0</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> money,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`money`</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表达式别名降序</span><br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LENGTH</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">len</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">len</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 按函数</span><br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DESC</span>, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employee_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ASC</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 多个排序条件</span><br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">分组查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 每个工种的总工资</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SUM</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`money`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`money`</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 每个工种的最高工资</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询邮箱包含a的工种的最高工资</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> email <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%a%'</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询邮箱包含a的工种的最高工资大于1万的 HAVING</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> email <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%a%'</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span> > <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10000</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`max`</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询名称长度一样的大于5个人的数据</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LENGTH</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span>, <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COUNT</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`name`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span> > <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">5</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 多字段分组</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AVG</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`avg`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span><br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">连接查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;">分类: 按年代划分: sql92标准:只支持内连接 sql99标准【推荐】:支持内连接+外来连接(左外、右外)+交叉连接 按功能划分: 内连接: 等值连接 非等值连接 自连接 外连接: 左外连接 右外连接 全外连接 交叉连接 </code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">sql92标准</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 等值连接</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> e.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`first_name`</span>,j.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_title`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`j`</span>.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> e,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`jobs`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">as</span> j<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`e`</span>.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span> = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`j`</span>.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`job_id`</span>;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非等值连接</span><br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 自连接</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> e.last_name,m.employee_id,e.manager_id,m.last_name <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> e, <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> m<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`e`</span>.employee_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`m`</span>.manager_id<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">sql99标准</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 等值连接</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`last_name`</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_name`</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> departments<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span> = departments.<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`department_id`</span><br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 复杂的等值连接</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> department_name,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COUNT</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'*'</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">count</span>,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MAX</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">max</span>,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">min</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">`salary`</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">min</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> departments<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.department_id = departments.department_id<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> last_name <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'%o%'</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> department_name<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BETWEEN</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">2</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`count`</span><br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 多表等值连接</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> last_name,department_name,job_title<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> departments<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> departments.department_id =employees.department_id<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> jobs<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.job_id = jobs.job_id;<br><br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">子查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;">分类: 按出现位置: select 后面: 只支持标量子查询 form 后面 表子查询 where 和having后面 * 标量子查询 * 列子查询 * 行子查询 exists后面 表子查询1按结果集的行列数不同: 2 标量子查询(结果集只有一行一列) 3 列子查询(结果集只有一列多行) 4 行子查询(结果集只有多列一行) 5 表子查询(结果集一般多行多列)
</code></pre>
<h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">where 和having后面</span><span class="suffix" style="display: none;"></span></h4> <p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px;">特点: 1. 放在小括号内 2. 放在条件右侧 3. 标量子查询:配合单行操作符。列子查询:配合多行操作符。</p> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询工资比Abel高的人</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> salary > (<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> salary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> last_name = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Abel'</span><br>);<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># job_id与141号员工一样且工资大于143号员工的员工</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> last_name,salary,employee_id <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> job_id = (<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> job_id <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> employee_id = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">141</span><br>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AND</span> salary > (<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> salary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> employee_id = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">143</span><br>)<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 查询比50号部门最低工资高的部门的最低工资</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> department_id, <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COUNT</span>(*),<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MIN</span>(salary) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> minsalary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">GROUP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> department_id<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">HAVING</span> minsalary > (<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MIN</span>(salary) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">AS</span> minsalary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> department_id = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">50</span>)<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ORDER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">BY</span> minsalary <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DESC</span>;<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">分页查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> *<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIMIT</span> (page<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">-1</span>)*<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">size</span>,<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">size</span>;<br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> *<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">`employees`</span> <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIMIT</span> <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>;<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">联合查询</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 无</span><br></code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">DML 数据操作语言</span><span class="suffix"></span></h2> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">插入</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs(job_id,job_title,min_salary,max_salary) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">VALUES</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'p_a'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'捡垃圾1'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span>);<br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">VALUES</span>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'p_a1'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'捡垃圾1'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span>),<br>(<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'p_a2'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'捡垃圾2'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span>);<br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces'</span>,job_title=<span class="hljs-string" style="color: #c41a16; line-height: 26px;">"123"</span><br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INSERT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INTO</span> jobs <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces1'</span>,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'444'</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">200</span>,<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">6000</span><br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">修改</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 简单修改</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> jobs<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> job_title = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'2222'</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces1'</span><br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改多表</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> jobs<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> employees.job_id = jobs.job_id<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> jobs.job_title = <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONCAT</span>(jobs.job_title,<span class="hljs-string" style="color: #c41a16; line-height: 26px;">'22222'</span>),employees.job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces1'</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> jobs.job_title = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'Public Accountant12322222'</span><br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">删除</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 单表删除</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DELETE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> jobs<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'ces'</span><br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 清空整表</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TRUNCATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> ttt;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 多表删除</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DELETE</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> employees<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">INNER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">JOIN</span> jobs<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ON</span> jobs.job_id = employees.job_id<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> jobs.job_id = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'SA_MAN'</span><br></code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">DDL 数据定义语言</span><span class="suffix"></span></h2> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;">1. 库的管理 创建\修改\删除 2. 表的管理 创建\修改\删除创建: create 修改: alter 删除: drop </code></pre>
<h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">库管理</span><span class="suffix" style="display: none;"></span></h3> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 创建 IF NOT EXISTS 进行容错 CHARACTER SET 字符集</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> books <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHARACTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> utf8;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改 不能该了</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">RENAME</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> books <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TO</span> newbook;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 更改字符集</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> books <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHARACTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> gbk;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 库的删除</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DATABASE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> books;<br><br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">表的管理</span><span class="suffix" style="display: none;"></span></h3> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">创建表</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">USE</span> books;<br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 表的创建</span><br><span class="hljs-comment" style="color: #007400; line-height: 26px;">/*<br>CREATE TABLE 表名(<br> 列名 列类型([长度]) [约束],<br> 列名 列类型([长度]) [约束],<br> ...<br>)<br>*/</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> book (<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>,<br> b_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>),<br> price <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DOUBLE</span>,<br> author_id <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>,<br> publish_date DATETIME<br>);<br><br>DESC book;<br><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author(<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>,<br> au_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>),<br> nation <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">10</span>)<br>);<br><br>DESC author;<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">表的修改</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;">/*<br>修改列:<br>ALTER TABLE 表名 [CHANGE|MODIFY|ADD|DROP] COLUMN 列名 类型|约束;<br>修改表名:<br>ALTER TABLE 表名 RENAME TO 新表名;<br>*/</span><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改列名</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> book <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CHANGE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> publish_date publishDate DATETIME;<br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改列类型约束</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> book <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MODIFY</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> publishDate <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">TIMESTAMP</span>;<br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 添加新列</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ADD</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> annual <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DOUBLE</span>;<br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 删除</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> annual;<br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改表名</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> author <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">RENAME</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TO</span> authers;<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">表的删除</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> authers;<br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">表的复制</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 复制表结构</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> copy <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">LIKE</span> author;<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 复制整个表</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> copy2 <br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SELECT</span> * <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FROM</span> author;<br></code></pre> <h3 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; color: black; font-size: 16px; font-weight: bold; text-align: center;"><span class="prefix" style="display: none;"></span><span class="content" style="border-bottom: 2px solid #DEC6FB; color: #595959;">约束</span><span class="suffix" style="display: none;"></span></h3> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">添加表时添加约束</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 列级约束</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo(<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span><br> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span><br> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DEFAULT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'男'</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span><br> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span> <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span><br>);<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 表级约束</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo(<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span><br> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>), <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span><br> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) , <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span><br> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span><br> majorid <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>,<br> <br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> pk PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>),<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span><br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> uq <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span>(seat),<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span><br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> fk_stuiinfo_major <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>)<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 外键</span><br>);<br><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 通用建议约束</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DROP</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">IF</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">EXISTS</span> stuinfo;<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo(<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span><br> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span><br> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DEFAULT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'男'</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span><br> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span><br> majorid <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>,<br> <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表级约束</span><br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> fk_stuiinfo_major <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>)<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 外键</span><br>);<br><br><br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">修改表时的约束</span><span class="suffix" style="display: none;"></span></h4> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 修改列约束</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">MODIFY</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COLUMN</span> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>; <br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 表级约束</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ADD</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>);<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ALTER</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ADD</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>);<br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 删除外键</span><br><span class="hljs-comment" style="color: #007400; line-height: 26px;"># ALTER TABLE stuinfo DROP PRIMARY KEY;</span><br></code></pre> <h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">标识列(自增长列)</span><span class="suffix" style="display: none;"></span></h4> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">AUTO_INCREMENT</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CREATE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TABLE</span> stuinfo(<br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> PRIMARY <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span> AUTO_INCREMENT, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 主键</span><br> stu_name <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">VARCHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">20</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">NOT</span> <span class="hljs-literal" style="color: #aa0d91; line-height: 26px;">NULL</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 非空</span><br> gender <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">CHAR</span>(<span class="hljs-number" style="color: #1c00cf; line-height: 26px;">1</span>) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">DEFAULT</span> <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'男'</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 默认</span><br> seat <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UNIQUE</span>, <span class="hljs-comment" style="color: #007400; line-height: 26px;"># 唯一</span><br> majorid <span class="hljs-built_in" style="color: #5c2699; line-height: 26px;">INT</span>,<br> <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 表级约束</span><br> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">CONSTRAINT</span> fk_stuiinfo_major <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">FOREIGN</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">KEY</span>(majorid) <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">REFERENCES</span> major(<span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span>)<span class="hljs-comment" style="color: #007400; line-height: 26px;"># 外键</span><br>);<br></code></pre> <h2 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 22px; text-align: left; margin: 20px 10px 0px 0px;"><span class="prefix" style="display: none;"></span><span class="content" style="font-size: 18px; font-weight: bold; display: inline-block; padding-left: 10px; border-left: 5px solid #DEC6FB; color: #595959;">TCL 事务控制语言</span><span class="suffix"></span></h2> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">一个或一组sql语句组成一个执行单位,要么全部执行,要么全部失败。</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <blockquote data-tool="mdnice编辑器" style="display: block; font-size: 0.9em; overflow: auto; overflow-scrolling: touch; padding-top: 10px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; margin-bottom: 20px; margin-top: 20px; text-size-adjust: 100%; line-height: 1.55em; font-weight: 400; border-radius: 6px; color: #595959; font-style: normal; text-align: left; box-sizing: inherit; border-left: none; border: 1px solid #DEC6FB; background: #F6EEFF;"><span style="color: #DEC6FB; font-size: 34px; line-height: 1; font-weight: 700;">❝</span> <p style="padding-top: 8px; padding-bottom: 8px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin: 0px; line-height: 26px; color: #595959;">ACID: A: 原子性 C: 一致性 I: 隔离性 D: 持久性</p> <span style="float: right; color: #DEC6FB;">❞</span></blockquote> <pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://imgkr.cn-bj.ufileos.com/97e4eed2-a992-4976-acf0-ccb6fb34d308.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #fff; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: black; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #fff; border-radius: 5px;"><span class="hljs-comment" style="color: #007400; line-height: 26px;"># 提交表示完成,回滚代表异常</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">set</span> autocommit = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">0</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 关闭自动事务</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">START</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TRANSACTION</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 开始事务</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> stu_name = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'12232'</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">3</span>;<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SAVEPOINT</span> a; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 保存节点</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">UPDATE</span> stuinfo <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">SET</span> stu_name = <span class="hljs-string" style="color: #c41a16; line-height: 26px;">'12332'</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">WHERE</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">id</span> = <span class="hljs-number" style="color: #1c00cf; line-height: 26px;">5</span>;<br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ROLLBACK</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 回滚事务</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">ROLLBACK</span> <span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">TO</span> a; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 回滚事务到指定节点</span><br><span class="hljs-keyword" style="color: #aa0d91; line-height: 26px;">COMMIT</span>; <span class="hljs-comment" style="color: #007400; line-height: 26px;">-- 提交事务 </span><br></code></pre> <p id="nice-suffix-juejin-container" class="nice-suffix-juejin-container" data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; line-height: 26px; color: #595959; margin: 10px 0px; letter-spacing: 2px; font-size: 14px; word-spacing: 2px; margin-top: 20px !important;">本文使用 <a href="https://mdnice.com/?from=juejin" style="text-decoration: none; word-wrap: break-word; color: #664D9D; font-weight: normal; border-bottom: 1px solid #664D9D;">mdnice</a> 排版</p></section>