mirror of
https://github.com/JasonsGong/JasonsGong.github.io.git
synced 2024-11-24 12:49:36 +08:00
742 lines
561 KiB
HTML
742 lines
561 KiB
HTML
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>MySql进阶教程 | The Blog</title><meta name="author" content="Jason"><meta name="copyright" content="Jason"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="全部的PDF笔记: https://qingling.icu/posts/50465.html 原视频地址: 黑马程序员 MySQL数据库入门到精通,从mysql安装到mysql高级、mysql优化全囊括 一.Mysql入门1.SQL全称 Structured Query Language,结构化查询语言。操作关系型数据库的编程语言,定义了一套操作关系型数据库统一标准 。 1.1 SQL通用语法">
|
||
<meta property="og:type" content="article">
|
||
<meta property="og:title" content="MySql进阶教程">
|
||
<meta property="og:url" content="https://qingling.icu/posts/39654.html">
|
||
<meta property="og:site_name" content="The Blog">
|
||
<meta property="og:description" content="全部的PDF笔记: https://qingling.icu/posts/50465.html 原视频地址: 黑马程序员 MySQL数据库入门到精通,从mysql安装到mysql高级、mysql优化全囊括 一.Mysql入门1.SQL全称 Structured Query Language,结构化查询语言。操作关系型数据库的编程语言,定义了一套操作关系型数据库统一标准 。 1.1 SQL通用语法">
|
||
<meta property="og:locale" content="zh_CN">
|
||
<meta property="og:image" content="https://qingling.icu/img/9.png">
|
||
<meta property="article:published_time" content="2023-08-28T09:15:10.000Z">
|
||
<meta property="article:modified_time" content="2024-05-10T05:31:11.633Z">
|
||
<meta property="article:author" content="Jason">
|
||
<meta property="article:tag" content="Mysql">
|
||
<meta name="twitter:card" content="summary">
|
||
<meta name="twitter:image" content="https://qingling.icu/img/9.png"><link rel="shortcut icon" href="/img/%E5%9B%BE%E6%A0%87.png"><link rel="canonical" href="https://qingling.icu/posts/39654.html"><link rel="preconnect" href="//fastly.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="/cdn/icon/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="/cdn/css/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
|
||
root: '/',
|
||
algolia: undefined,
|
||
localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容:${query}","hits_stats":"共找到 ${hits} 篇文章"}},
|
||
translate: undefined,
|
||
noticeOutdate: undefined,
|
||
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":400},
|
||
copy: {
|
||
success: '复制成功',
|
||
error: '复制错误',
|
||
noSupport: '浏览器不支持'
|
||
},
|
||
relativeDate: {
|
||
homepage: true,
|
||
post: true
|
||
},
|
||
runtime: '天',
|
||
dateSuffix: {
|
||
just: '刚刚',
|
||
min: '分钟前',
|
||
hour: '小时前',
|
||
day: '天前',
|
||
month: '个月前'
|
||
},
|
||
copyright: undefined,
|
||
lightbox: 'mediumZoom',
|
||
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#006650","bgDark":"#006650","position":"top-center"},
|
||
source: {
|
||
justifiedGallery: {
|
||
js: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
|
||
css: 'https://fastly.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
|
||
}
|
||
},
|
||
isPhotoFigcaption: false,
|
||
islazyload: false,
|
||
isAnchor: true,
|
||
percent: {
|
||
toc: true,
|
||
rightside: false,
|
||
},
|
||
autoDarkmode: true
|
||
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
|
||
title: 'MySql进阶教程',
|
||
isPost: true,
|
||
isHome: false,
|
||
isHighlightShrink: false,
|
||
isToc: true,
|
||
postUpdate: '2024-05-10 13:31:11'
|
||
}</script><noscript><style type="text/css">
|
||
#nav {
|
||
opacity: 1
|
||
}
|
||
.justified-gallery img {
|
||
opacity: 1
|
||
}
|
||
|
||
#recent-posts time,
|
||
#post-meta time {
|
||
display: inline !important
|
||
}
|
||
</style></noscript><script>(win=>{
|
||
win.saveToLocal = {
|
||
set: function setWithExpiry(key, value, ttl) {
|
||
if (ttl === 0) return
|
||
const now = new Date()
|
||
const expiryDay = ttl * 86400000
|
||
const item = {
|
||
value: value,
|
||
expiry: now.getTime() + expiryDay,
|
||
}
|
||
localStorage.setItem(key, JSON.stringify(item))
|
||
},
|
||
|
||
get: function getWithExpiry(key) {
|
||
const itemStr = localStorage.getItem(key)
|
||
|
||
if (!itemStr) {
|
||
return undefined
|
||
}
|
||
const item = JSON.parse(itemStr)
|
||
const now = new Date()
|
||
|
||
if (now.getTime() > item.expiry) {
|
||
localStorage.removeItem(key)
|
||
return undefined
|
||
}
|
||
return item.value
|
||
}
|
||
}
|
||
|
||
win.getScript = url => new Promise((resolve, reject) => {
|
||
const script = document.createElement('script')
|
||
script.src = url
|
||
script.async = true
|
||
script.onerror = reject
|
||
script.onload = script.onreadystatechange = function() {
|
||
const loadState = this.readyState
|
||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||
script.onload = script.onreadystatechange = null
|
||
resolve()
|
||
}
|
||
document.head.appendChild(script)
|
||
})
|
||
|
||
win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
|
||
const link = document.createElement('link')
|
||
link.rel = 'stylesheet'
|
||
link.href = url
|
||
if (id) link.id = id
|
||
link.onerror = reject
|
||
link.onload = link.onreadystatechange = function() {
|
||
const loadState = this.readyState
|
||
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
|
||
link.onload = link.onreadystatechange = null
|
||
resolve()
|
||
}
|
||
document.head.appendChild(link)
|
||
})
|
||
|
||
win.activateDarkMode = function () {
|
||
document.documentElement.setAttribute('data-theme', 'dark')
|
||
if (document.querySelector('meta[name="theme-color"]') !== null) {
|
||
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
|
||
}
|
||
}
|
||
win.activateLightMode = function () {
|
||
document.documentElement.setAttribute('data-theme', 'light')
|
||
if (document.querySelector('meta[name="theme-color"]') !== null) {
|
||
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
|
||
}
|
||
}
|
||
const t = saveToLocal.get('theme')
|
||
|
||
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
|
||
const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
|
||
const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
|
||
const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
|
||
|
||
if (t === undefined) {
|
||
if (isLightMode) activateLightMode()
|
||
else if (isDarkMode) activateDarkMode()
|
||
else if (isNotSpecified || hasNoSupport) {
|
||
const now = new Date()
|
||
const hour = now.getHours()
|
||
const isNight = hour <= 8 || hour >= 22
|
||
isNight ? activateDarkMode() : activateLightMode()
|
||
}
|
||
window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
|
||
if (saveToLocal.get('theme') === undefined) {
|
||
e.matches ? activateDarkMode() : activateLightMode()
|
||
}
|
||
})
|
||
} else if (t === 'light') activateLightMode()
|
||
else activateDarkMode()
|
||
|
||
const asideStatus = saveToLocal.get('aside-status')
|
||
if (asideStatus !== undefined) {
|
||
if (asideStatus === 'hide') {
|
||
document.documentElement.classList.add('hide-aside')
|
||
} else {
|
||
document.documentElement.classList.remove('hide-aside')
|
||
}
|
||
}
|
||
|
||
const detectApple = () => {
|
||
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
|
||
document.documentElement.classList.add('apple')
|
||
}
|
||
}
|
||
detectApple()
|
||
})(window)</script><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script><script type="text/javascript" src ="/js/welcome.js" ></script><script src="/js/sweetalert.js"></script><link rel="stylesheet" href="/css/sweetalert.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><br/><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog-info"><a href="/" title="The Blog"><img class="site-icon" src="/img/logo.png"/><span class="site-name">The Blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" target="_blank" rel="noopener" href="https://www.tutorialspoint.com/compile_java8_online.php"><i class="fa-fw fas fa-code"></i><span> 代码</span></a></div><div class="menus_item"><a class="site-page" href="/notice/"><i class="fa-fw fas fa-stream"></i><span> 公告</span></a></div><div class="menus_item"><a class="site-page" href="/website/"><i class="fa-fw fas fa-list"></i><span> 网址</span></a></div><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">MySql进阶教程</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-08-28T09:15:10.000Z" title="发表于 2023-08-28 17:15:10">2023-08-28</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-05-10T05:31:11.633Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">26.6k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>118分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="MySql进阶教程"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div><article class="post-content" id="article-container"><p><strong>全部的PDF笔记:</strong> <a href="https://qingling.icu/posts/50465.html">https://qingling.icu/posts/50465.html</a></p>
|
||
<p><strong>原视频地址:</strong> <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Kr4y1i7ru/?share_source=copy_web&vd_source=aee5e475191b69e6c781059ab6662584">黑马程序员 MySQL数据库入门到精通,从mysql安装到mysql高级、mysql优化全囊括</a></p>
|
||
<h1 id="一-Mysql入门"><a href="#一-Mysql入门" class="headerlink" title="一.Mysql入门"></a>一.Mysql入门</h1><h2 id="1-SQL"><a href="#1-SQL" class="headerlink" title="1.SQL"></a>1.SQL</h2><p>全称 Structured Query Language,结构化查询语言。操作关系型数据库的编程语言,定义了一套操作关系型数据库统一标准 。</p>
|
||
<h3 id="1-1-SQL通用语法"><a href="#1-1-SQL通用语法" class="headerlink" title="1.1 SQL通用语法"></a>1.1 SQL通用语法</h3><p><img src="/pictures/image-20230926220120609.png" alt="image-20230926220120609"></p>
|
||
<h3 id="1-2-SQL分类"><a href="#1-2-SQL分类" class="headerlink" title="1.2 SQL分类"></a>1.2 SQL分类</h3><p><img src="/pictures/image-20230926215941275.png" alt="image-20230926215941275"></p>
|
||
<h3 id="1-3-DDL-数据定义语言"><a href="#1-3-DDL-数据定义语言" class="headerlink" title="1.3 DDL-数据定义语言"></a>1.3 DDL-数据定义语言</h3><p><strong>连接本地mysql的命令</strong></p>
|
||
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mysql -u root -p</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>常用的DDL操作</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ------------------------------------------------------数据库的操作------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 展示所有的数据库</span></span><br><span class="line"><span class="keyword">show</span> databases ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询当前的数据库</span></span><br><span class="line"><span class="keyword">select</span> database();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建数据库</span></span><br><span class="line"><span class="keyword">create</span> database if <span class="keyword">not</span> <span class="keyword">exists</span> itcast;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据库</span></span><br><span class="line"><span class="keyword">drop</span> database if <span class="keyword">exists</span> itcast;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用数据库</span></span><br><span class="line">use itcast;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ----------------------------------------------------------表的操作--------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user(id <span class="type">int</span> comment <span class="string">'编号'</span>, name <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'姓名'</span>, age <span class="type">int</span> comment <span class="string">'年龄'</span>,gender <span class="type">varchar</span>(<span class="number">1</span>) comment <span class="string">'性别'</span> ) comment <span class="string">'用户表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显示创建表的结构</span></span><br><span class="line"><span class="keyword">desc</span> tb_user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显示创建表的语句</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> tb_user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表的操作</span></span><br><span class="line"><span class="comment">-- 举例:现有一张员工表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> workno <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'工号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> idcard <span class="type">char</span>(<span class="number">18</span>) comment <span class="string">'身份证号'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"><span class="comment">-- 查看表结构</span></span><br><span class="line"><span class="keyword">desc</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表结构(向emp表中添加昵称字段)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp <span class="keyword">add</span> nickname <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'昵称'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改相应字段的数据类型(把age的数据类型从int改回tinyint)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp modify age tinyint;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改字段名和字段的类型(把nicknem字段改为username,并修改数据类型)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp change nickname username <span class="type">varchar</span>(<span class="number">30</span>) comment <span class="string">'用户名'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除字段(删除username字段)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp <span class="keyword">drop</span> username;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表名(将表名字从emo修改为employee)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp rename <span class="keyword">to</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> employee;</span><br><span class="line"><span class="comment">-- 删除指定表,并重新创建该表(相当于删除表中的全部数据)</span></span><br><span class="line"><span class="keyword">truncate</span> <span class="keyword">table</span> employee;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>Mysql的数据类型</strong></p>
|
||
<p>数值数据类型</p>
|
||
<p><img src="/pictures/image-20230926224723586.png" alt="image-20230926224723586"></p>
|
||
<p> 字符串数据类型</p>
|
||
<p> <img src="/pictures/image-20230926224755542.png" alt="image-20230926224755542"></p>
|
||
<p>日期时间类型</p>
|
||
<p> <img src="/pictures/image-20230926224539052.png" alt="image-20230926224539052"></p>
|
||
<h3 id="1-4-DML-数据操作语言"><a href="#1-4-DML-数据操作语言" class="headerlink" title="1.4 DML-数据操作语言"></a>1.4 DML-数据操作语言</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ------------------------------------------------DML-增删改操作---------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 添加数据</span></span><br><span class="line"><span class="keyword">select</span> database();</span><br><span class="line"><span class="keyword">show</span> tables;</span><br><span class="line"><span class="comment">-- 该指定的字段添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee(id, workno, name, gender, age, idcard, entrydate) <span class="keyword">value</span> (<span class="number">1</span>, <span class="string">'1'</span>, <span class="string">'小华'</span>, <span class="string">'男'</span>, <span class="number">20</span>, <span class="string">'123456789123456789'</span>, <span class="string">'2001-01-01'</span>);</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> employee;</span><br><span class="line"><span class="comment">-- 给全部的字段添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee <span class="keyword">value</span> (<span class="number">2</span>, <span class="string">'2'</span>, <span class="string">'小刚'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>);</span><br><span class="line"><span class="comment">-- 批量添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee</span><br><span class="line"><span class="keyword">values</span> (<span class="number">3</span>, <span class="string">'3'</span>, <span class="string">'小李'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'4'</span>, <span class="string">'小黄'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改数据(不带条件修改所有)</span></span><br><span class="line"><span class="keyword">update</span> employee <span class="keyword">set</span> name <span class="operator">=</span> <span class="string">'张三'</span> <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">update</span> employee <span class="keyword">set</span> name <span class="operator">=</span> <span class="string">'李四'</span>, gender<span class="operator">=</span> <span class="string">'女'</span> <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据(删除id为4的数据,不带添加删除所有)</span></span><br><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> employee <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="1-5-DQL-数据查询语言"><a href="#1-5-DQL-数据查询语言" class="headerlink" title="1.5 DQL-数据查询语言"></a>1.5 DQL-数据查询语言</h3><p><img src="/pictures/image-20230930222149036.png" alt="image-20230930222149036"></p>
|
||
<ul>
|
||
<li>基本查询(不带任何条件)</li>
|
||
<li>条件查询(WHERE)</li>
|
||
<li>聚合函数(count、max、min、avg、sum)</li>
|
||
<li>分组查询(group by)</li>
|
||
<li>排序查询(order by)</li>
|
||
<li>分页查询(limit</li>
|
||
</ul>
|
||
<p><strong>测试数据准备</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> workno <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'工号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> idcard <span class="type">char</span>(<span class="number">18</span>) comment <span class="string">'身份证号'</span>,</span><br><span class="line"> workaddress <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'工作地址'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"><span class="comment">-- 插入数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> emp(id, workno, name, gender, age, idcard, workaddress,entrydate)</span><br><span class="line"><span class="keyword">values</span> (<span class="number">1</span>,<span class="string">'1'</span>,<span class="string">'柳岩'</span>,<span class="string">'女'</span>,<span class="number">20</span>,<span class="string">'12345678912345678'</span>,<span class="string">'北京'</span>,<span class="string">'2001-01-01'</span>),</span><br><span class="line"> (<span class="number">2</span>,<span class="string">'2'</span>,<span class="string">'张无忌'</span>,<span class="string">'男'</span>,<span class="number">18</span>,<span class="string">'123456789012345670'</span>,<span class="string">'北京'</span>,<span class="string">'2005-09-01'</span>),</span><br><span class="line"> (<span class="number">3</span>,<span class="string">'3'</span>,<span class="string">'韦一笑'</span>,<span class="string">'男'</span>,<span class="number">38</span>,<span class="string">'12345678972345670'</span>,<span class="string">'上海'</span>,<span class="string">'2005-08-01'</span>),</span><br><span class="line"> (<span class="number">4</span>,<span class="string">'4'</span>,<span class="string">'赵敏'</span>,<span class="string">'女'</span>,<span class="number">18</span>,<span class="string">'12345675712345670'</span>,<span class="string">'北京'</span>,<span class="string">'2009-12-01'</span>),</span><br><span class="line"> (<span class="number">5</span>,<span class="string">'5'</span>,<span class="string">'小昭'</span>,<span class="string">'女'</span>,<span class="number">16</span>,<span class="string">'123456769012345678'</span>,<span class="string">'上海'</span>,<span class="string">'2007-07-01'</span>),</span><br><span class="line"> (<span class="number">6</span>,<span class="string">'6'</span>,<span class="string">'杨逍'</span>,<span class="string">'男'</span>,<span class="number">28</span>,<span class="string">'1234567893123456X'</span>,<span class="string">'北京'</span>,<span class="string">'2006-01-01'</span>),</span><br><span class="line"> (<span class="number">7</span>,<span class="string">'7'</span>,<span class="string">'范瑶'</span>,<span class="string">'男'</span>,<span class="number">40</span>,<span class="string">'123456789212345670'</span>,<span class="string">'北京'</span>,<span class="string">'2005-05-01'</span>),</span><br><span class="line"> (<span class="number">8</span>,<span class="string">'8'</span>,<span class="string">'黛绮丝'</span>,<span class="string">'女'</span>,<span class="number">38</span>,<span class="string">'123456157123645670'</span>,<span class="string">'天津'</span>,<span class="string">'2015-05-01'</span>),</span><br><span class="line"> (<span class="number">9</span>,<span class="string">'9'</span>,<span class="string">'范凉凉'</span>,<span class="string">'女'</span>,<span class="number">45</span>,<span class="string">'123156789012345678'</span>,<span class="string">'北京'</span>,<span class="string">'2010-04-01'</span>),</span><br><span class="line"> (<span class="number">10</span>,<span class="string">'10'</span>,<span class="string">'陈友谅'</span>,<span class="string">'男'</span>,<span class="number">53</span>,<span class="string">'123456789012345670'</span>,<span class="string">'上海'</span>,<span class="string">'2011-01-01'</span>),</span><br><span class="line"> (<span class="number">11</span>,<span class="string">'11'</span>,<span class="string">'张士诚'</span>,<span class="string">'男'</span>,<span class="number">55</span>,<span class="string">'12356789712345670'</span>,<span class="string">'江苏'</span>,<span class="string">'2015-05-01'</span>),</span><br><span class="line"> (<span class="number">12</span>,<span class="string">'12'</span>,<span class="string">'常遇春'</span>,<span class="string">'男'</span>,<span class="number">32</span>,<span class="string">'123446757152345670'</span>,<span class="string">'北京'</span>,<span class="string">'2004-02-01'</span>),</span><br><span class="line"> (<span class="number">13</span>,<span class="string">'13'</span>,<span class="string">'张三丰'</span>,<span class="string">'男'</span>,<span class="number">88</span>,<span class="string">'123656789012345678'</span>,<span class="string">'江苏'</span>,<span class="string">'2020-11-01'</span>),</span><br><span class="line"> (<span class="number">14</span>,<span class="string">'14'</span>,<span class="string">'灭绝'</span>,<span class="string">'女'</span>,<span class="number">65</span>,<span class="string">'123456719012345670'</span>,<span class="string">'西安'</span>,<span class="string">'2019-05-01'</span>),</span><br><span class="line"> (<span class="number">15</span>,<span class="string">'15'</span>,<span class="string">'胡青年'</span>,<span class="string">'男'</span>,<span class="number">70</span>,<span class="string">'12345674971234567X'</span>,<span class="string">'西安'</span>,<span class="string">'2018-04-01'</span>),</span><br><span class="line"> (<span class="number">16</span>,<span class="string">'16'</span>,<span class="string">'周芷若'</span>,<span class="string">'女'</span>,<span class="number">18</span>,<span class="keyword">null</span>,<span class="string">'北京'</span>,<span class="string">'2012-06-01'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-1-基础查询"><a href="#1-5-1-基础查询" class="headerlink" title="1.5.1 基础查询"></a>1.5.1 基础查询</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查看插入的数据</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询指定字段 name workno age 返回</span></span><br><span class="line"><span class="keyword">select</span> name, workno, age</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有字段返回</span></span><br><span class="line"><span class="keyword">select</span> id,</span><br><span class="line"> workno,</span><br><span class="line"> name,</span><br><span class="line"> gender,</span><br><span class="line"> age,</span><br><span class="line"> idcard,</span><br><span class="line"> workaddress,</span><br><span class="line"> entrydate</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- distinct去重关键字</span></span><br><span class="line"><span class="comment">-- 查询所有员工的工作地址(不要重复的地址)</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> workaddress <span class="string">'工作地址'</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-2-条件查询"><a href="#1-5-2-条件查询" class="headerlink" title="1.5.2 条件查询"></a>1.5.2 条件查询</h4><p>语法: <code>SELECT 字段列表 FROM 表名 WHERE 条件列表 ;</code></p>
|
||
<p><img src="/pictures/image-20231001154822524.png" alt="image-20231001154822524"></p>
|
||
<p><img src="/pictures/image-20231001154903121.png" alt="image-20231001154903121"></p>
|
||
<p><strong>举例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 条件查询</span></span><br><span class="line"><span class="comment">-- 查询年龄等于 88 的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">=</span> <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于20的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于等于20的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询没有身份证号的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> idcard <span class="keyword">is</span> <span class="keyword">null</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄不等于88的员工信息(<>也表示不等于)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">!=</span> <span class="number">88</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><></span> <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄在15~20(包含)之间的员工信息(三种实现方式)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">>=</span> <span class="number">15</span> <span class="operator">&&</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">>=</span> <span class="number">15</span> <span class="keyword">and</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="keyword">between</span> <span class="number">15</span> <span class="keyword">and</span> <span class="number">20</span>; #包含了<span class="number">15</span>和<span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为女 且年龄小于25岁的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'女'</span> <span class="keyword">and</span> age <span class="operator"><</span> <span class="number">25</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄等于18 或 20 或 48 的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">=</span> <span class="number">18</span> <span class="keyword">or</span> age <span class="operator">=</span> <span class="number">20</span> <span class="keyword">or</span> age <span class="operator">=</span> <span class="number">48</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="keyword">in</span> (<span class="number">18</span>,<span class="number">20</span>,<span class="number">48</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询姓名为两个字的员工(模糊匹配(_匹配单个字符, %匹配任意个字符))</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="keyword">like</span> <span class="string">'__'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询身份证号最后一位是X的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> idcard <span class="keyword">like</span> <span class="string">'%X'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-3-聚合函数"><a href="#1-5-3-聚合函数" class="headerlink" title="1.5.3 聚合函数"></a>1.5.3 聚合函数</h4><p>语法: <code>SELECT 聚合函数(字段列表) FROM 表名 ;</code></p>
|
||
<blockquote>
|
||
<p>注意 : NULL值是不参与所有聚合函数运算的</p>
|
||
</blockquote>
|
||
<p><img src="/pictures/image-20231001160812611.png" alt="image-20231001160812611"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 聚合函数</span></span><br><span class="line"><span class="comment">-- 统计该企业员工数量(空值是不参与统计的)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp;</span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(idcard) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的平均年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(age) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最大年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">max</span>(age) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最小年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">min</span>(age) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计西安地区员工的年龄之和</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">sum</span>(age) <span class="keyword">from</span> emp <span class="keyword">where</span> workaddress <span class="operator">=</span> <span class="string">'西安'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-4-分组查询"><a href="#1-5-4-分组查询" class="headerlink" title="1.5.4 分组查询"></a>1.5.4 分组查询</h4><p>语法: <code>SELECT 字段列表 FROM 表名 [ WHERE 条件 ] GROUP BY 分组字段名 [ HAVING 分组 后过滤条件 ];</code></p>
|
||
<p><strong>where与having区别</strong></p>
|
||
<ul>
|
||
<li>执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤</li>
|
||
<li>判断条件不同:where不能对聚合函数进行判断,而having可以</li>
|
||
</ul>
|
||
<blockquote>
|
||
<p>注意事项:<br>• 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。<br>• 执行顺序: where > 聚合函数 > having 。<br>• 支持多字段分组, 具体语法为 : group by columnA,columnB</p>
|
||
</blockquote>
|
||
<p><img src="/pictures/image-20231001163315161.png" alt="image-20231001163315161"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 分组查询</span></span><br><span class="line"><span class="comment">-- 根据性别分组,统计男性员工和女性员工的数量</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据性别进行分组,统计男性员工和女性员工的平均年龄</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">avg</span>(age) <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于45岁的员工,并根据工作地址分组,获取员工数量大于等于3的工作地址(可以使用别名也可以不使用别名)</span></span><br><span class="line"><span class="keyword">select</span> workaddress,<span class="built_in">count</span>(<span class="operator">*</span>) address_count <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><</span> <span class="number">45</span> <span class="keyword">group</span> <span class="keyword">by</span> workaddress <span class="keyword">having</span> address_count <span class="operator">>=</span> <span class="number">3</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-5-排序查询"><a href="#1-5-5-排序查询" class="headerlink" title="1.5.5 排序查询"></a>1.5.5 排序查询</h4><p>语法: <code>SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式1 , 字段2 排序方式2 ;</code></p>
|
||
<blockquote>
|
||
<p>注意事项:<br>• 如果是升序, 可以不指定排序方式ASC,因为升序ASC是默认值 ;<br>• 如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序 ;</p>
|
||
</blockquote>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 排序查询</span></span><br><span class="line"><span class="comment">-- 根据年龄对公司的员工进行升序排序(asc可以省略)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> age ;</span><br><span class="line"><span class="comment">-- 降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据员工的入职时间降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> entrydate <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据员工的年龄升序排序,年龄相同,再按照入职时间进行降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">asc</span> ,entrydate <span class="keyword">desc</span> ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-6-分页查询"><a href="#1-5-6-分页查询" class="headerlink" title="1.5.6 分页查询"></a>1.5.6 分页查询</h4><p>语法: <code>SELECT 字段列表 FROM 表名 LIMIT 起始索引, 查询记录数 ;</code></p>
|
||
<blockquote>
|
||
<p>注意事项:<br>• 起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数。<br>• 分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT。<br>• 如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10。</p>
|
||
</blockquote>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 分页查询</span></span><br><span class="line"><span class="comment">-- 查询第一页的员工数据,每页显示10条记录</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp limit <span class="number">0</span>,<span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询第二页数据,每页展示十条数据(起始索引 = (查询页码 - 1)* 每页显示记录数)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp limit <span class="number">10</span>,<span class="number">10</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-7-DQL语句案例"><a href="#1-5-7-DQL语句案例" class="headerlink" title="1.5.7 DQL语句案例"></a>1.5.7 DQL语句案例</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- DQL语句案例</span></span><br><span class="line"><span class="comment">-- 查询年龄在20,21,22,23岁的女性员工的信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'女'</span> <span class="keyword">and</span> age <span class="keyword">in</span> (<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">23</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为男,并且年龄再20~40岁(含)以内的姓名为三个字的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'男'</span> <span class="keyword">and</span> (age <span class="keyword">between</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span>) <span class="keyword">and</span> name <span class="keyword">like</span> <span class="string">'___'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计员工表中,年龄小于60岁的,男性员工和女性员工的人数</span></span><br><span class="line"><span class="keyword">select</span> gender,<span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><</span> <span class="number">60</span> <span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按照入职时间降序排序</span></span><br><span class="line"><span class="keyword">select</span> name,age <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">35</span> <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">asc</span> ,entrydate <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为男,且年龄在20·40岁(含)以内的前五个员工信息,并对查询结果按年龄升序排序,如果年龄相同按照入职时间升序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'男'</span> <span class="keyword">and</span> age <span class="keyword">between</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span> <span class="keyword">order</span> <span class="keyword">by</span> age,entrydate limit <span class="number">5</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-5-8-执行顺序"><a href="#1-5-8-执行顺序" class="headerlink" title="1.5.8 执行顺序"></a>1.5.8 执行顺序</h4><p><img src="/pictures/image-20231001171425562.png" alt="image-20231001171425562"></p>
|
||
<p><img src="/pictures/image-20231001172032063.png" alt="image-20231001172032063"></p>
|
||
<p><strong>总结</strong></p>
|
||
<p> <img src="/pictures/image-20231001172211635.png" alt="image-20231001172211635"></p>
|
||
<h3 id="1-6-DCL-数据控制语言"><a href="#1-6-DCL-数据控制语言" class="headerlink" title="1.6 DCL-数据控制语言"></a>1.6 DCL-数据控制语言</h3><h4 id="1-6-1-管理用户"><a href="#1-6-1-管理用户" class="headerlink" title="1.6.1 管理用户"></a>1.6.1 管理用户</h4><p>查询用户: <code>select * from mysql.user;</code></p>
|
||
<p>创建用户:<code>CREATE USER '用户名'@'主机名' IDENTIFIED BY '密码';</code></p>
|
||
<p>修改用户密码: <code>ALTER USER '用户名'@'主机名' IDENTIFIED WITH mysql_native_password BY '新密码' ;</code></p>
|
||
<p>删除用户:<code>DROP USER '用户名'@'主机名' ;</code></p>
|
||
<p><img src="/pictures/image-20231001172731731.png" alt="image-20231001172731731"></p>
|
||
<blockquote>
|
||
<p>注意事项:<br>• 在MySQL中需要通过用户名@主机名的方式,来唯一标识一个用户</p>
|
||
<p>• 主机名可以使用 % 通配</p>
|
||
<p>• 这类SQL开发人员操作的比较少,主要是DBA( Database Administrator 数据库管理员)使用</p>
|
||
</blockquote>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 切换到系统中自带的mysql数据库</span></span><br><span class="line">use mysql;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有的用户</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> mysql.user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户itcast, 只能够在当前主机localhost访问, 密码123456;</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">'itcast'</span>@<span class="string">'localhost'</span> identified <span class="keyword">by</span> <span class="string">'123456'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户heima, 可以在任意主机访问该数据库, 密码123456;</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">'heima'</span>@<span class="string">'%'</span> identified <span class="keyword">by</span> <span class="string">'123456'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改用户heima的访问密码为1234;</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">user</span> <span class="string">'heima'</span>@<span class="string">'%'</span> identified <span class="keyword">with</span> mysql_native_password <span class="keyword">by</span> <span class="string">'1234'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除 itcast@localhost 用户</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> <span class="string">'itcast'</span>@<span class="string">'localhost'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="1-6-2-权限控制"><a href="#1-6-2-权限控制" class="headerlink" title="1.6.2 权限控制"></a>1.6.2 权限控制</h4><p>查询权限: <code>SHOW GRANTS FOR '用户名'@'主机名' ;</code></p>
|
||
<p>授予权限: <code>GRANT 权限列表 ON 数据库名.表名 TO '用户名'@'主机名';</code></p>
|
||
<p>撤销权限: <code>REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';</code></p>
|
||
<blockquote>
|
||
<p>注意事项:<br>• 多个权限之间,使用逗号分隔<br>• 授权时, 数据库名和表名可以使用 * 进行通配,代表所有</p>
|
||
</blockquote>
|
||
<p><img src="/pictures/image-20231001174406029.png" alt="image-20231001174406029"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 权限控制</span></span><br><span class="line"><span class="comment">-- 查询权限(查询heima用户的所有权限)</span></span><br><span class="line"><span class="keyword">show</span> grants <span class="keyword">for</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予权限(授予heima用户对数据库itcast的所有权限)</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> itcast.<span class="operator">*</span> <span class="keyword">to</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予用户所有数据库所有表的所有权限(相当于超级管理员)</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> <span class="operator">*</span>.<span class="operator">*</span> <span class="keyword">to</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 撤销权限(撤销heima用户在itcast数据库上的所有权限)</span></span><br><span class="line"><span class="keyword">revoke</span> <span class="keyword">all</span> <span class="keyword">on</span> itcast.<span class="operator">*</span> <span class="keyword">from</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>总结</strong></p>
|
||
<p> <img src="/pictures/image-20231001213059144.png" alt="image-20231001213059144"></p>
|
||
<p><strong>全部的SQL</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ------------------------------------------------------数据库的操作------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 展示所有的数据库</span></span><br><span class="line"><span class="keyword">show</span> databases;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询当前的数据库</span></span><br><span class="line"><span class="keyword">select</span> database();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建数据库</span></span><br><span class="line"><span class="keyword">create</span> database if <span class="keyword">not</span> <span class="keyword">exists</span> itcast;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据库</span></span><br><span class="line"><span class="keyword">drop</span> database if <span class="keyword">exists</span> itcast;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用数据库</span></span><br><span class="line">use itcast;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ----------------------------------------------------------表的操作--------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> gender <span class="type">varchar</span>(<span class="number">1</span>) comment <span class="string">'性别'</span></span><br><span class="line">) comment <span class="string">'用户表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显示创建表的结构</span></span><br><span class="line"><span class="keyword">desc</span> tb_user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显示创建表的语句</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> tb_user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表的操作</span></span><br><span class="line"><span class="comment">-- 举例:现有一张员工表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> workno <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'工号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> idcard <span class="type">char</span>(<span class="number">18</span>) comment <span class="string">'身份证号'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"><span class="comment">-- 查看表结构</span></span><br><span class="line"><span class="keyword">desc</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表结构(向emp表中添加昵称字段)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">add</span> nickname <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'昵称'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改相应字段的数据类型(把age的数据类型从int改回tinyint)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> modify age tinyint;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改字段名和字段的类型(把nickname字段改为username,并修改数据类型)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> change nickname username <span class="type">varchar</span>(<span class="number">30</span>) comment <span class="string">'用户名'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除字段(删除username字段)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">drop</span> username;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表名(将表名字从emo修改为employee)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp rename <span class="keyword">to</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> employee;</span><br><span class="line"><span class="comment">-- 删除指定表,并重新创建该表(相当于删除表中的全部数据)</span></span><br><span class="line"><span class="keyword">truncate</span> <span class="keyword">table</span> employee;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ------------------------------------------------DML-增删改操作---------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 添加数据</span></span><br><span class="line"><span class="keyword">select</span> database();</span><br><span class="line"><span class="keyword">show</span> tables;</span><br><span class="line"><span class="comment">-- 该指定的字段添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee(id, workno, name, gender, age, idcard, entrydate) <span class="keyword">value</span> (<span class="number">1</span>, <span class="string">'1'</span>, <span class="string">'小华'</span>, <span class="string">'男'</span>, <span class="number">20</span>, <span class="string">'123456789123456789'</span>, <span class="string">'2001-01-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> employee;</span><br><span class="line"><span class="comment">-- 给全部的字段添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee <span class="keyword">value</span> (<span class="number">2</span>, <span class="string">'2'</span>, <span class="string">'小刚'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>);</span><br><span class="line"><span class="comment">-- 批量添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee</span><br><span class="line"><span class="keyword">values</span> (<span class="number">3</span>, <span class="string">'3'</span>, <span class="string">'小李'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'4'</span>, <span class="string">'小黄'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改数据(不带条件修改所有)</span></span><br><span class="line"><span class="keyword">update</span> employee</span><br><span class="line"><span class="keyword">set</span> name <span class="operator">=</span> <span class="string">'张三'</span></span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">update</span> employee</span><br><span class="line"><span class="keyword">set</span> name <span class="operator">=</span> <span class="string">'李四'</span>,</span><br><span class="line"> gender<span class="operator">=</span> <span class="string">'女'</span></span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据(删除id为4的数据,不带添加删除所有)</span></span><br><span class="line"><span class="keyword">delete</span></span><br><span class="line"><span class="keyword">from</span> employee</span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- --------------------------------------------------------DQL数据查询语言--------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> workno <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'工号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> idcard <span class="type">char</span>(<span class="number">18</span>) comment <span class="string">'身份证号'</span>,</span><br><span class="line"> workaddress <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'工作地址'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 插入数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> emp(id, workno, name, gender, age, idcard, workaddress, entrydate)</span><br><span class="line"><span class="keyword">values</span> (<span class="number">1</span>, <span class="string">'1'</span>, <span class="string">'柳岩'</span>, <span class="string">'女'</span>, <span class="number">20</span>, <span class="string">'12345678912345678'</span>, <span class="string">'北京'</span>, <span class="string">'2001-01-01'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'2'</span>, <span class="string">'张无忌'</span>, <span class="string">'男'</span>, <span class="number">18</span>, <span class="string">'123456789012345670'</span>, <span class="string">'北京'</span>, <span class="string">'2005-09-01'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'3'</span>, <span class="string">'韦一笑'</span>, <span class="string">'男'</span>, <span class="number">38</span>, <span class="string">'12345678972345670'</span>, <span class="string">'上海'</span>, <span class="string">'2005-08-01'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'4'</span>, <span class="string">'赵敏'</span>, <span class="string">'女'</span>, <span class="number">18</span>, <span class="string">'12345675712345670'</span>, <span class="string">'北京'</span>, <span class="string">'2009-12-01'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'5'</span>, <span class="string">'小昭'</span>, <span class="string">'女'</span>, <span class="number">16</span>, <span class="string">'123456769012345678'</span>, <span class="string">'上海'</span>, <span class="string">'2007-07-01'</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'6'</span>, <span class="string">'杨逍'</span>, <span class="string">'男'</span>, <span class="number">28</span>, <span class="string">'1234567893123456X'</span>, <span class="string">'北京'</span>, <span class="string">'2006-01-01'</span>),</span><br><span class="line"> (<span class="number">7</span>, <span class="string">'7'</span>, <span class="string">'范瑶'</span>, <span class="string">'男'</span>, <span class="number">40</span>, <span class="string">'123456789212345670'</span>, <span class="string">'北京'</span>, <span class="string">'2005-05-01'</span>),</span><br><span class="line"> (<span class="number">8</span>, <span class="string">'8'</span>, <span class="string">'黛绮丝'</span>, <span class="string">'女'</span>, <span class="number">38</span>, <span class="string">'123456157123645670'</span>, <span class="string">'天津'</span>, <span class="string">'2015-05-01'</span>),</span><br><span class="line"> (<span class="number">9</span>, <span class="string">'9'</span>, <span class="string">'范凉凉'</span>, <span class="string">'女'</span>, <span class="number">45</span>, <span class="string">'123156789012345678'</span>, <span class="string">'北京'</span>, <span class="string">'2010-04-01'</span>),</span><br><span class="line"> (<span class="number">10</span>, <span class="string">'10'</span>, <span class="string">'陈友谅'</span>, <span class="string">'男'</span>, <span class="number">53</span>, <span class="string">'123456789012345670'</span>, <span class="string">'上海'</span>, <span class="string">'2011-01-01'</span>),</span><br><span class="line"> (<span class="number">11</span>, <span class="string">'11'</span>, <span class="string">'张士诚'</span>, <span class="string">'男'</span>, <span class="number">55</span>, <span class="string">'12356789712345670'</span>, <span class="string">'江苏'</span>, <span class="string">'2015-05-01'</span>),</span><br><span class="line"> (<span class="number">12</span>, <span class="string">'12'</span>, <span class="string">'常遇春'</span>, <span class="string">'男'</span>, <span class="number">32</span>, <span class="string">'123446757152345670'</span>, <span class="string">'北京'</span>, <span class="string">'2004-02-01'</span>),</span><br><span class="line"> (<span class="number">13</span>, <span class="string">'13'</span>, <span class="string">'张三丰'</span>, <span class="string">'男'</span>, <span class="number">88</span>, <span class="string">'123656789012345678'</span>, <span class="string">'江苏'</span>, <span class="string">'2020-11-01'</span>),</span><br><span class="line"> (<span class="number">14</span>, <span class="string">'14'</span>, <span class="string">'灭绝'</span>, <span class="string">'女'</span>, <span class="number">65</span>, <span class="string">'123456719012345670'</span>, <span class="string">'西安'</span>, <span class="string">'2019-05-01'</span>),</span><br><span class="line"> (<span class="number">15</span>, <span class="string">'15'</span>, <span class="string">'胡青年'</span>, <span class="string">'男'</span>, <span class="number">70</span>, <span class="string">'12345674971234567X'</span>, <span class="string">'西安'</span>, <span class="string">'2018-04-01'</span>),</span><br><span class="line"> (<span class="number">16</span>, <span class="string">'16'</span>, <span class="string">'周芷若'</span>, <span class="string">'女'</span>, <span class="number">18</span>, <span class="keyword">null</span>, <span class="string">'北京'</span>, <span class="string">'2012-06-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 基础查询</span></span><br><span class="line"><span class="comment">-- 查看插入的数据</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询指定字段 name workno age 返回</span></span><br><span class="line"><span class="keyword">select</span> name, workno, age</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有字段返回</span></span><br><span class="line"><span class="keyword">select</span> id,</span><br><span class="line"> workno,</span><br><span class="line"> name,</span><br><span class="line"> gender,</span><br><span class="line"> age,</span><br><span class="line"> idcard,</span><br><span class="line"> workaddress,</span><br><span class="line"> entrydate</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- distinct去重关键字</span></span><br><span class="line"><span class="comment">-- 查询所有员工的工作地址(不要重复的地址)</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> workaddress <span class="string">'工作地址'</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 条件查询</span></span><br><span class="line"><span class="comment">-- 查询年龄等于 88 的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">=</span> <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于20的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于等于20的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询没有身份证号的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> idcard <span class="keyword">is</span> <span class="keyword">null</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄不等于88的员工信息(<>也表示不等于)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">!=</span> <span class="number">88</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><></span> <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄在15~20(包含)之间的员工信息(三种实现方式)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">>=</span> <span class="number">15</span> <span class="operator">&&</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">>=</span> <span class="number">15</span> <span class="keyword">and</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="keyword">between</span> <span class="number">15</span> <span class="keyword">and</span> <span class="number">20</span>; #包含了<span class="number">15</span>和<span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为女 且年龄小于25岁的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'女'</span> <span class="keyword">and</span> age <span class="operator"><</span> <span class="number">25</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄等于18 或 20 或 48 的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator">=</span> <span class="number">18</span> <span class="keyword">or</span> age <span class="operator">=</span> <span class="number">20</span> <span class="keyword">or</span> age <span class="operator">=</span> <span class="number">48</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="keyword">in</span> (<span class="number">18</span>,<span class="number">20</span>,<span class="number">48</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询姓名为两个字的员工(模糊匹配(_匹配单个字符, %匹配任意个字符))</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="keyword">like</span> <span class="string">'__'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询身份证号最后一位是X的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> idcard <span class="keyword">like</span> <span class="string">'%X'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 聚合函数</span></span><br><span class="line"><span class="comment">-- 统计该企业员工数量(空值是不参与统计的)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp;</span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(idcard) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的平均年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(age) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最大年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">max</span>(age) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最小年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">min</span>(age) <span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计西安地区员工的年龄之和</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">sum</span>(age) <span class="keyword">from</span> emp <span class="keyword">where</span> workaddress <span class="operator">=</span> <span class="string">'西安'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 分组查询</span></span><br><span class="line"><span class="comment">-- 根据性别分组,统计男性员工和女性员工的数量</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据性别进行分组,统计男性员工和女性员工的平均年龄</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">avg</span>(age) <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于45岁的员工,并根据工作地址分组,获取员工数量大于等于3的工作地址(可以使用别名也可以不使用别名)</span></span><br><span class="line"><span class="keyword">select</span> workaddress,<span class="built_in">count</span>(<span class="operator">*</span>) address_count <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><</span> <span class="number">45</span> <span class="keyword">group</span> <span class="keyword">by</span> workaddress <span class="keyword">having</span> address_count <span class="operator">>=</span> <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 排序查询</span></span><br><span class="line"><span class="comment">-- 根据年龄对公司的员工进行升序排序(asc可以省略)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> age ;</span><br><span class="line"><span class="comment">-- 降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据员工的入职时间降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> entrydate <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据员工的年龄升序排序,年龄相同,再按照入职时间进行降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">asc</span> ,entrydate <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 分页查询</span></span><br><span class="line"><span class="comment">-- 查询第一页的员工数据,每页显示10条记录</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp limit <span class="number">0</span>,<span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询第二页数据,每页展示十条数据(起始索引 = (查询页码 - 1)* 每页显示记录数)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp limit <span class="number">10</span>,<span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- DQL语句案例</span></span><br><span class="line"><span class="comment">-- 查询年龄在20,21,22,23岁的女性员工的信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'女'</span> <span class="keyword">and</span> age <span class="keyword">in</span> (<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">23</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为男,并且年龄再20~40岁(含)以内的姓名为三个字的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'男'</span> <span class="keyword">and</span> (age <span class="keyword">between</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span>) <span class="keyword">and</span> name <span class="keyword">like</span> <span class="string">'___'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计员工表中,年龄小于60岁的,男性员工和女性员工的人数</span></span><br><span class="line"><span class="keyword">select</span> gender,<span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><</span> <span class="number">60</span> <span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按照入职时间降序排序</span></span><br><span class="line"><span class="keyword">select</span> name,age <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">35</span> <span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">asc</span> ,entrydate <span class="keyword">desc</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为男,且年龄在20·40岁(含)以内的前五个员工信息,并对查询结果按年龄升序排序,如果年龄相同按照入职时间升序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'男'</span> <span class="keyword">and</span> age <span class="keyword">between</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span> <span class="keyword">order</span> <span class="keyword">by</span> age,entrydate limit <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ---------------------------------------------------DCL--------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 用户管理</span></span><br><span class="line"><span class="comment">-- mysql数据库</span></span><br><span class="line">use mysql;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有的用户</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> mysql.user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户itcast, 只能够在当前主机localhost访问, 密码123456;</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">'itcast'</span>@<span class="string">'localhost'</span> identified <span class="keyword">by</span> <span class="string">'123456'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户heima, 可以在任意主机访问该数据库, 密码123456;</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">'heima'</span>@<span class="string">'%'</span> identified <span class="keyword">by</span> <span class="string">'123456'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改用户heima的访问密码为1234;</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">user</span> <span class="string">'heima'</span>@<span class="string">'%'</span> identified <span class="keyword">with</span> mysql_native_password <span class="keyword">by</span> <span class="string">'1234'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除 itcast@localhost 用户</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> <span class="string">'itcast'</span>@<span class="string">'localhost'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 权限控制</span></span><br><span class="line"><span class="comment">-- 查询权限(查询heima用户的所有权限)</span></span><br><span class="line"><span class="keyword">show</span> grants <span class="keyword">for</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予权限(授予heima用户对数据库itcast的所有权限)</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> itcast.<span class="operator">*</span> <span class="keyword">to</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予用户所有数据库所有表的所有权限</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> <span class="operator">*</span>.<span class="operator">*</span> <span class="keyword">to</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 撤销权限(撤销heima用户在itcast数据库上的所有权限)</span></span><br><span class="line"><span class="keyword">revoke</span> <span class="keyword">all</span> <span class="keyword">on</span> itcast.<span class="operator">*</span> <span class="keyword">from</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h2 id="2-函数"><a href="#2-函数" class="headerlink" title="2.函数"></a>2.函数</h2><h3 id="2-1-字符串函数"><a href="#2-1-字符串函数" class="headerlink" title="2.1 字符串函数"></a>2.1 字符串函数</h3><p><img src="/pictures/image-20231001213558221.png" alt="image-20231001213558221"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- concat函数(字符串拼接)</span></span><br><span class="line"><span class="keyword">select</span> concat(<span class="string">'hello '</span>,<span class="string">'word'</span>); # hello word</span><br><span class="line"></span><br><span class="line"><span class="comment">-- lower(全部转化成小写)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">lower</span>(<span class="string">'Hello'</span>); # hello</span><br><span class="line"></span><br><span class="line"><span class="comment">-- upper(全部转化成大写)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">upper</span>(<span class="string">'Hello'</span>); # HELLO</span><br><span class="line"></span><br><span class="line"><span class="comment">-- lpad(左侧填充)</span></span><br><span class="line"><span class="keyword">select</span> lpad(<span class="string">'01'</span>,<span class="number">5</span>,<span class="string">'-'</span>); # <span class="comment">---01</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- rpad(右侧填充)</span></span><br><span class="line"><span class="keyword">select</span> rpad(<span class="string">'01'</span>,<span class="number">5</span>,<span class="string">'-'</span>) ;# <span class="number">01</span><span class="comment">---</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- trim 去除头部和尾部的空格</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">trim</span>(<span class="string">' Hello Mysql '</span>);# Hello Mysql</span><br><span class="line"></span><br><span class="line"><span class="comment">-- substring 字符串截取</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">substring</span>(<span class="string">'Hello Mysql'</span>,<span class="number">1</span>,<span class="number">5</span>);# Hello</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>案例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0 比如:1号员工的工号应该为00001</span></span><br><span class="line"><span class="keyword">update</span> emp <span class="keyword">set</span> workno <span class="operator">=</span> lpad(workno,<span class="number">5</span>,<span class="string">'0'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="2-2-数值函数"><a href="#2-2-数值函数" class="headerlink" title="2.2 数值函数"></a>2.2 数值函数</h3><p><img src="/pictures/image-20231001215546554.png" alt="image-20231001215546554"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 数值函数</span></span><br><span class="line"><span class="comment">-- ceil 向上取整</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">ceil</span>(<span class="number">1.5</span>); # <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- floor 向下取整</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">floor</span>(<span class="number">1.1</span>); # <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- mod 求模运算</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">mod</span>(<span class="number">7</span>,<span class="number">4</span>); # <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- rand 随机数(0~1)</span></span><br><span class="line"><span class="keyword">select</span> rand();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- round 四舍五入</span></span><br><span class="line"><span class="keyword">select</span> round(<span class="number">2.345</span>,<span class="number">2</span>); # <span class="number">2.35</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>案例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 通过数据库函数生成一个的随机的六位验证码</span></span><br><span class="line"><span class="comment">-- 思路是通过rand()*100生成xxxxxx.xxx的小数,然后通过round四舍五入掉小数位,然后可能存在小于六位数的情况,我们</span></span><br><span class="line"><span class="comment">-- 通过lpd函数补齐六位</span></span><br><span class="line"><span class="keyword">select</span> lpad(round(rand()<span class="operator">*</span><span class="number">1000000</span>,<span class="number">0</span>),<span class="number">6</span>,<span class="string">'0'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="2-3-日期函数"><a href="#2-3-日期函数" class="headerlink" title="2.3 日期函数"></a>2.3 日期函数</h3><p><img src="/pictures/image-20231002114046980.png" alt="image-20231002114046980"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 日期函数</span></span><br><span class="line"><span class="comment">-- curdate:当前日期</span></span><br><span class="line"><span class="keyword">select</span> curdate();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- curtime:当前时间</span></span><br><span class="line"><span class="keyword">select</span> curtime();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- now:当前日期和时间</span></span><br><span class="line"><span class="keyword">select</span> now();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- YEAR , MONTH , DAY:当前年、月、日</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">year</span>(now());</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">month</span>(now());</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">day</span>(now());</span><br><span class="line"></span><br><span class="line"><span class="comment">-- date_add:增加指定的时间间隔</span></span><br><span class="line"><span class="keyword">select</span> date_add(now(),<span class="type">INTERVAL</span> <span class="number">70</span> <span class="keyword">DAY</span> ); # 当前的时间往后推<span class="number">70</span>天</span><br><span class="line"><span class="keyword">select</span> date_add(now(),<span class="type">INTERVAL</span> <span class="number">2</span> <span class="keyword">MONTH</span> ); #向后推<span class="number">2</span>个月</span><br><span class="line"></span><br><span class="line"><span class="comment">-- datediff:获取两个日期相差的天数</span></span><br><span class="line"><span class="keyword">select</span> datediff(<span class="string">'2021-12-01'</span>,<span class="string">'2021-10-01'</span>); # 查询两个时间之间的差值,第一个时间减去第二个时间</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>案例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询所有员工的入职天数,并根据入职天数倒叙排序</span></span><br><span class="line"><span class="keyword">select</span> name,datediff(curdate(),entrydate) <span class="string">'entrydays'</span> <span class="keyword">from</span> emp <span class="keyword">order</span> <span class="keyword">by</span> entrydays <span class="keyword">desc</span> ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="2-4-流程函数"><a href="#2-4-流程函数" class="headerlink" title="2.4 流程函数"></a>2.4 流程函数</h3><p><img src="/pictures/image-20231004124942068.png" alt="image-20231004124942068"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 流程控制函数</span></span><br><span class="line"><span class="comment">-- if 相当java中的三元运算符</span></span><br><span class="line"><span class="keyword">select</span> if(<span class="literal">true</span>, <span class="string">'OK'</span>, <span class="string">'ERROR'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- ifnull 第一个字符串不为空就返回第一个,第一个为空就返回第二个</span></span><br><span class="line"><span class="keyword">select</span> ifnull(<span class="string">'OK'</span>, <span class="string">'DEFAULT'</span>);</span><br><span class="line"><span class="keyword">select</span> ifnull(<span class="keyword">null</span>, <span class="string">'DEFAULT'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- case when then else end</span></span><br><span class="line"><span class="comment">-- 需求: 查询emp表的员工姓名和工作地址 (北京/上海 ----> 一线城市 , 其他 ----> 二线城市)</span></span><br><span class="line"><span class="keyword">select</span> name,</span><br><span class="line"> (<span class="keyword">case</span> workaddress</span><br><span class="line"> <span class="keyword">when</span> <span class="string">'北京'</span> <span class="keyword">then</span> <span class="string">'一线城市'</span></span><br><span class="line"> <span class="keyword">when</span> <span class="string">'上海'</span> <span class="keyword">then</span> <span class="string">'一线城市'</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="string">'二线城市'</span> <span class="keyword">end</span>) <span class="keyword">as</span> <span class="string">'工作地址'</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>案例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 案例:统计班级各个学员的成绩,展示规则如下:</span></span><br><span class="line"><span class="comment">-- >= 85 展示优秀</span></span><br><span class="line"><span class="comment">-- >= 60 展示及格</span></span><br><span class="line"><span class="comment">-- 否则展示不及格</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 建表数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> score</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> math <span class="type">int</span> comment <span class="string">'数学'</span>,</span><br><span class="line"> english <span class="type">int</span> comment <span class="string">'英语'</span>,</span><br><span class="line"> chinese <span class="type">int</span> comment <span class="string">'语文'</span></span><br><span class="line">) comment <span class="string">'学员成绩表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> score(id, name, math, english, chinese)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'Tom'</span>, <span class="number">67</span>, <span class="number">88</span>, <span class="number">95</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'Rose'</span>, <span class="number">23</span>, <span class="number">66</span>, <span class="number">90</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'Jack'</span>, <span class="number">56</span>, <span class="number">98</span>, <span class="number">76</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> score;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 案例sql实现</span></span><br><span class="line"><span class="keyword">select</span> id,</span><br><span class="line"> name,</span><br><span class="line"> (<span class="keyword">case</span> <span class="keyword">when</span> math <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span> <span class="keyword">when</span> math <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span> <span class="keyword">else</span> <span class="string">'不及格'</span> <span class="keyword">end</span>)</span><br><span class="line"> <span class="string">'数学'</span>,</span><br><span class="line"> (<span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> english <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span></span><br><span class="line"> <span class="keyword">when</span> english <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span></span><br><span class="line"> <span class="keyword">else</span> <span class="string">'不及格'</span></span><br><span class="line"> <span class="keyword">end</span>) <span class="string">'英语'</span>,</span><br><span class="line"> (<span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> chinese <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span></span><br><span class="line"> <span class="keyword">when</span> chinese <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span></span><br><span class="line"> <span class="keyword">else</span> <span class="string">'不及格'</span></span><br><span class="line"> <span class="keyword">end</span>) <span class="string">'语文'</span></span><br><span class="line"><span class="keyword">from</span> score;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>函数相关的全部sql</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ----------------------------------------------------------函数--------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 字符串函数</span></span><br><span class="line"><span class="comment">-- concat函数(字符串拼接)</span></span><br><span class="line"><span class="keyword">select</span> concat(<span class="string">'hello '</span>, <span class="string">'word'</span>);</span><br><span class="line"># hello word</span><br><span class="line"></span><br><span class="line"><span class="comment">-- lower(全部转化成小写)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">lower</span>(<span class="string">'Hello'</span>);</span><br><span class="line"># hello</span><br><span class="line"></span><br><span class="line"><span class="comment">-- upper(全部转化成大写)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">upper</span>(<span class="string">'Hello'</span>);</span><br><span class="line"># HELLO</span><br><span class="line"></span><br><span class="line"><span class="comment">-- lpad(左侧填充)</span></span><br><span class="line"><span class="keyword">select</span> lpad(<span class="string">'01'</span>, <span class="number">5</span>, <span class="string">'-'</span>);</span><br><span class="line"># <span class="comment">---01</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- rpad(右侧填充)</span></span><br><span class="line"><span class="keyword">select</span> rpad(<span class="string">'01'</span>, <span class="number">5</span>, <span class="string">'-'</span>);</span><br><span class="line"># <span class="number">01</span><span class="comment">---</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- trim 去除头部和尾部的空格</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">trim</span>(<span class="string">' Hello Mysql '</span>);</span><br><span class="line"># Hello Mysql</span><br><span class="line"></span><br><span class="line"><span class="comment">-- substring 字符串截取</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">substring</span>(<span class="string">'Hello Mysql'</span>, <span class="number">1</span>, <span class="number">5</span>);</span><br><span class="line"># Hello</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0 比如:1号员工的工号应该为00001</span></span><br><span class="line"><span class="keyword">update</span> emp</span><br><span class="line"><span class="keyword">set</span> workno <span class="operator">=</span> lpad(workno, <span class="number">5</span>, <span class="string">'0'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 数值函数</span></span><br><span class="line"><span class="comment">-- ceil 向上取整</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">ceil</span>(<span class="number">1.5</span>);</span><br><span class="line"># <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- floor 向下取整</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">floor</span>(<span class="number">1.1</span>);</span><br><span class="line"># <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- mod 求模运算</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">mod</span>(<span class="number">7</span>, <span class="number">4</span>);</span><br><span class="line"># <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- rand 随机数(0~1)</span></span><br><span class="line"><span class="keyword">select</span> rand();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- round 四舍五入</span></span><br><span class="line"><span class="keyword">select</span> round(<span class="number">2.345</span>, <span class="number">2</span>);</span><br><span class="line"># <span class="number">2.35</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 通过数据库函数生成一个的随机的六位验证码</span></span><br><span class="line"><span class="keyword">select</span> lpad(round(rand() <span class="operator">*</span> <span class="number">1000000</span>, <span class="number">0</span>), <span class="number">6</span>, <span class="string">'0'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 日期函数</span></span><br><span class="line"><span class="comment">-- curdate:当前日期</span></span><br><span class="line"><span class="keyword">select</span> curdate();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- curtime:当前时间</span></span><br><span class="line"><span class="keyword">select</span> curtime();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- now:当前日期和时间</span></span><br><span class="line"><span class="keyword">select</span> now();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- YEAR , MONTH , DAY:当前年、月、日</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">year</span>(now());</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">month</span>(now());</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">day</span>(now());</span><br><span class="line"></span><br><span class="line"><span class="comment">-- date_add:增加指定的时间间隔</span></span><br><span class="line"><span class="keyword">select</span> date_add(now(), <span class="type">INTERVAL</span> <span class="number">70</span> <span class="keyword">DAY</span>); # 当前的时间往后推<span class="number">70</span>天</span><br><span class="line"><span class="keyword">select</span> date_add(now(), <span class="type">INTERVAL</span> <span class="number">2</span> <span class="keyword">MONTH</span>);</span><br><span class="line">#向后推<span class="number">2</span>个月</span><br><span class="line"></span><br><span class="line"><span class="comment">-- datediff:获取两个日期相差的天数</span></span><br><span class="line"><span class="keyword">select</span> datediff(<span class="string">'2021-12-01'</span>, <span class="string">'2021-10-01'</span>);</span><br><span class="line"># 查询两个时间之间的差值,第一个时间减去第二个时间</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有员工的入职天数,并根据入职天数倒叙排序</span></span><br><span class="line"><span class="keyword">select</span> name, datediff(curdate(), entrydate) <span class="string">'entrydays'</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> entrydays <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 流程控制函数</span></span><br><span class="line"><span class="comment">-- if 相当java中的三元运算符</span></span><br><span class="line"><span class="keyword">select</span> if(<span class="literal">true</span>, <span class="string">'OK'</span>, <span class="string">'ERROR'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- ifnull 第一个字符串不为空就返回第一个,第一个为空就返回第二个</span></span><br><span class="line"><span class="keyword">select</span> ifnull(<span class="string">'OK'</span>, <span class="string">'DEFAULT'</span>);</span><br><span class="line"><span class="keyword">select</span> ifnull(<span class="keyword">null</span>, <span class="string">'DEFAULT'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- case when then else end</span></span><br><span class="line"><span class="comment">-- 需求: 查询emp表的员工姓名和工作地址 (北京/上海 ----> 一线城市 , 其他 ----> 二线城市)</span></span><br><span class="line"><span class="keyword">select</span> name,</span><br><span class="line"> (<span class="keyword">case</span> workaddress</span><br><span class="line"> <span class="keyword">when</span> <span class="string">'北京'</span> <span class="keyword">then</span> <span class="string">'一线城市'</span></span><br><span class="line"> <span class="keyword">when</span> <span class="string">'上海'</span> <span class="keyword">then</span> <span class="string">'一线城市'</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="string">'二线城市'</span> <span class="keyword">end</span>) <span class="keyword">as</span> <span class="string">'工作地址'</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 案例:统计班级各个学员的成绩,展示规则如下:</span></span><br><span class="line"><span class="comment">-- >= 85 展示优秀</span></span><br><span class="line"><span class="comment">-- >= 60 展示及格</span></span><br><span class="line"><span class="comment">-- 否则展示不及格</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 建表数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> score</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> math <span class="type">int</span> comment <span class="string">'数学'</span>,</span><br><span class="line"> english <span class="type">int</span> comment <span class="string">'英语'</span>,</span><br><span class="line"> chinese <span class="type">int</span> comment <span class="string">'语文'</span></span><br><span class="line">) comment <span class="string">'学员成绩表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> score(id, name, math, english, chinese)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'Tom'</span>, <span class="number">67</span>, <span class="number">88</span>, <span class="number">95</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'Rose'</span>, <span class="number">23</span>, <span class="number">66</span>, <span class="number">90</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'Jack'</span>, <span class="number">56</span>, <span class="number">98</span>, <span class="number">76</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> score;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 案例sql实现</span></span><br><span class="line"><span class="keyword">select</span> id,</span><br><span class="line"> name,</span><br><span class="line"> (<span class="keyword">case</span> <span class="keyword">when</span> math <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span> <span class="keyword">when</span> math <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span> <span class="keyword">else</span> <span class="string">'不及格'</span> <span class="keyword">end</span>)</span><br><span class="line"> <span class="string">'数学'</span>,</span><br><span class="line"> (<span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> english <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span></span><br><span class="line"> <span class="keyword">when</span> english <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span></span><br><span class="line"> <span class="keyword">else</span> <span class="string">'不及格'</span></span><br><span class="line"> <span class="keyword">end</span>) <span class="string">'英语'</span>,</span><br><span class="line"> (<span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> chinese <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span></span><br><span class="line"> <span class="keyword">when</span> chinese <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span></span><br><span class="line"> <span class="keyword">else</span> <span class="string">'不及格'</span></span><br><span class="line"> <span class="keyword">end</span>) <span class="string">'语文'</span></span><br><span class="line"><span class="keyword">from</span> score;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h2 id="3-约束"><a href="#3-约束" class="headerlink" title="3.约束"></a>3.约束</h2><h3 id="3-1-概述"><a href="#3-1-概述" class="headerlink" title="3.1 概述"></a>3.1 概述</h3><p><strong>概念</strong>:约束是作用于表中字段上的规则,用于限制存储在表中的数据。</p>
|
||
<p><strong>目的</strong>:保证数据库中数据的正确、有效性和完整性。</p>
|
||
<p><strong>分类</strong> :</p>
|
||
<p><img src="/pictures/image-20231004130826351.png" alt="image-20231004130826351"></p>
|
||
<h3 id="3-2-约束演示"><a href="#3-2-约束演示" class="headerlink" title="3.2 约束演示"></a>3.2 约束演示</h3><p><strong>案例需求: 根据需求,完成表结构的创建,需求如下:</strong></p>
|
||
<p><img src="/pictures/image-20231004160744943.png" alt="image-20231004160744943"></p>
|
||
<p>对应的建表语句为:</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user(</span><br><span class="line"> id <span class="type">int</span> AUTO_INCREMENT <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'ID唯一标识'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span> COMMENT <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> <span class="keyword">check</span> (age <span class="operator">></span> <span class="number">0</span> <span class="operator">&&</span> age <span class="operator"><=</span> <span class="number">120</span>) COMMENT <span class="string">'年龄'</span>,</span><br><span class="line"> status <span class="type">char</span>(<span class="number">1</span>) <span class="keyword">default</span> <span class="string">'1'</span> COMMENT <span class="string">'状态'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) COMMENT <span class="string">'性别'</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="3-3-外键约束"><a href="#3-3-外键约束" class="headerlink" title="3.3 外键约束"></a>3.3 外键约束</h3><h4 id="3-3-1-介绍"><a href="#3-3-1-介绍" class="headerlink" title="3.3.1 介绍"></a>3.3.1 介绍</h4><p><img src="/pictures/image-20231004162616775.png" alt="image-20231004162616775"></p>
|
||
<h4 id="3-3-2-语法"><a href="#3-3-2-语法" class="headerlink" title="3.3.2 语法"></a>3.3.2 语法</h4><p><strong>建立外键</strong>: <code>ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名) REFERENCES 主表 (主表列名) ;</code></p>
|
||
<p><strong>删除外键</strong>: <code>ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;</code></p>
|
||
<p><img src="/pictures/image-20231004163021062.png" alt="image-20231004163021062"></p>
|
||
<p>举例</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 外键约束</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> employee</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键 员工表employee的部门id字段关联到部门表的id字段以建立外键约束</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">drop</span> <span class="keyword">foreign</span> key fk_emp_dept;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="3-3-3-删除-更新行为"><a href="#3-3-3-删除-更新行为" class="headerlink" title="3.3.3 删除/更新行为"></a>3.3.3 删除/更新行为</h4><p>详细介绍见PDF文件 <a href="https://qingling.icu/posts/50465.html">https://qingling.icu/posts/50465.html</a></p>
|
||
<p><strong>添加了外键之后,再删除父表数据时产生的约束行为,我们就称为删除/更新行为。具体的删除/更新行为有以下几种:</strong></p>
|
||
<p><img src="/pictures/image-20231004164211535.png" alt="image-20231004164211535"></p>
|
||
<p>具体语法为:</p>
|
||
<p><code>ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段) REFERENCES 主表名 (主表字段名) ON UPDATE CASCADE ON DELETE CASCADE;</code></p>
|
||
<p><strong>约束相关的sql</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ---------------------------------------------------约束演示------------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- id 主键,并且自动增长</span></span><br><span class="line"><span class="comment">-- name 不为空,并且唯一</span></span><br><span class="line"><span class="comment">-- age 大于零,并且小于等于120(8.0以上的数据库才支持)age int check (age > 0 && age <= 120) COMMENT '年龄'</span></span><br><span class="line"><span class="comment">-- status 如果没有指定该值,默认为1</span></span><br><span class="line"><span class="comment">-- gender 无约束</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> <span class="keyword">user</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span></span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> AUTO_INCREMENT <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'ID唯一标识'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span> COMMENT <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> COMMENT <span class="string">'年龄'</span>,</span><br><span class="line"> status <span class="type">char</span>(<span class="number">1</span>) <span class="keyword">default</span> <span class="string">'1'</span> COMMENT <span class="string">'状态'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) COMMENT <span class="string">'性别'</span></span><br><span class="line">);</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> <span class="keyword">user</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 插入数据</span></span><br><span class="line"><span class="comment">-- name的值不能重复,status的值没有填写的话,将使用的是默认的值</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name, age, status, gender)</span><br><span class="line"><span class="keyword">values</span> (<span class="string">'Tom1'</span>, <span class="number">19</span>, <span class="string">'1'</span>, <span class="string">'男'</span>),</span><br><span class="line"> (<span class="string">'Tom2'</span>, <span class="number">29</span>, <span class="string">'1'</span>, <span class="string">'男'</span>),</span><br><span class="line"> (<span class="string">'Tom3'</span>, <span class="number">14</span>, <span class="string">'1'</span>, <span class="string">'男'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 外键约束</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> employee</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键 员工表employee的部门id字段关联到部门表的id字段以建立外键约束</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee <span class="keyword">drop</span> <span class="keyword">foreign</span> key fk_emp_dept;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h2 id="4-多表查询"><a href="#4-多表查询" class="headerlink" title="4.多表查询"></a>4.多表查询</h2><h3 id="4-1-多表关系"><a href="#4-1-多表关系" class="headerlink" title="4.1 多表关系"></a>4.1 多表关系</h3><p><strong>一对多</strong></p>
|
||
<p><img src="/pictures/image-20231004165257714.png" alt="image-20231004165257714"></p>
|
||
<p><strong>多对多</strong></p>
|
||
<p><img src="/pictures/image-20231004165425089.png" alt="image-20231004165425089"></p>
|
||
<p><strong>一对一</strong></p>
|
||
<p><img src="/pictures/image-20231004170148951.png" alt="image-20231004170148951"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 多表关系</span></span><br><span class="line"><span class="comment">-- 演示多对多的关系</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> <span class="keyword">no</span> <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'学号'</span></span><br><span class="line">) comment <span class="string">'学生表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'黛绮丝'</span>, <span class="string">'2000100101'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'谢逊'</span>,</span><br><span class="line"> <span class="string">'2000100102'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'殷天正'</span>, <span class="string">'2000100103'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'韦一笑'</span>, <span class="string">'2000100104'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> course</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'课程名称'</span></span><br><span class="line">) comment <span class="string">'课程表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'Java'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'PHP'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'MySQL'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'Hadoop'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student_course</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'主键'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> studentid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'学生ID'</span>,</span><br><span class="line"> courseid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'课程ID'</span>,</span><br><span class="line"> <span class="keyword">constraint</span> fk_courseid <span class="keyword">foreign</span> key (courseid) <span class="keyword">references</span> course (id),</span><br><span class="line"> <span class="keyword">constraint</span> fk_studentid <span class="keyword">foreign</span> key (studentid) <span class="keyword">references</span> student (id)</span><br><span class="line">) comment <span class="string">'学生课程中间表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student_course</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">2</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">2</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 演示一对一的关系</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'1: 男 , 2: 女'</span>,</span><br><span class="line"> phone <span class="type">char</span>(<span class="number">11</span>) comment <span class="string">'手机号'</span></span><br><span class="line">) comment <span class="string">'用户基本信息表'</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user_edu</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> degree <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'学历'</span>,</span><br><span class="line"> major <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'专业'</span>,</span><br><span class="line"> primaryschool <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'小学'</span>,</span><br><span class="line"> middleschool <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'中学'</span>,</span><br><span class="line"> university <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'大学'</span>,</span><br><span class="line"> userid <span class="type">int</span> <span class="keyword">unique</span> comment <span class="string">'用户ID'</span>,</span><br><span class="line"> <span class="keyword">constraint</span> fk_userid <span class="keyword">foreign</span> key (userid) <span class="keyword">references</span> tb_user (id)</span><br><span class="line">) comment <span class="string">'用户教育信息表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user(id, name, age, gender, phone)</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'黄渤'</span>, <span class="number">45</span>, <span class="string">'1'</span>, <span class="string">'18800001111'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'冰冰'</span>, <span class="number">35</span>, <span class="string">'2'</span>, <span class="string">'18800002222'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'码云'</span>, <span class="number">55</span>, <span class="string">'1'</span>, <span class="string">'18800008888'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'李彦宏'</span>, <span class="number">50</span>, <span class="string">'1'</span>, <span class="string">'18800009999'</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user_edu(id, degree, major, primaryschool, middleschool,</span><br><span class="line"> university, userid)</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'舞蹈'</span>, <span class="string">'静安区第一小学'</span>, <span class="string">'静安区第一中学'</span>, <span class="string">'北京舞蹈学院'</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'硕士'</span>, <span class="string">'表演'</span>, <span class="string">'朝阳区第一小学'</span>, <span class="string">'朝阳区第一中学'</span>, <span class="string">'北京电影学院'</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'英语'</span>, <span class="string">'杭州市第一小学'</span>, <span class="string">'杭州市第一中学'</span>, <span class="string">'杭州师范大学'</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'应用数学'</span>, <span class="string">'阳泉第一小学'</span>, <span class="string">'阳泉区第一中学'</span>, <span class="string">'清华大学'</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-2-多表查询概述"><a href="#4-2-多表查询概述" class="headerlink" title="4.2 多表查询概述"></a>4.2 多表查询概述</h3><h4 id="4-2-1-数据准备"><a href="#4-2-1-数据准备" class="headerlink" title="4.2.1 数据准备"></a>4.2.1 数据准备</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="comment">-- 创建dept表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>,</span><br><span class="line"> <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'人事部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建emp表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept_id <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span></span><br><span class="line"> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">7</span>, <span class="string">'灭绝'</span>, <span class="number">60</span>, <span class="string">'财务总监'</span>, <span class="number">8500</span>, <span class="string">'2002-09-12'</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">8</span>, <span class="string">'周芷若'</span>, <span class="number">19</span>, <span class="string">'会计'</span>, <span class="number">4800</span>, <span class="string">'2006-06-02'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">9</span>, <span class="string">'丁敏君'</span>, <span class="number">23</span>, <span class="string">'出纳'</span>, <span class="number">5250</span>, <span class="string">'2009-05-13'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">10</span>, <span class="string">'赵敏'</span>, <span class="number">20</span>, <span class="string">'市场部总监'</span>, <span class="number">12500</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">11</span>, <span class="string">'鹿杖客'</span>, <span class="number">56</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2006-10-03'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">12</span>, <span class="string">'鹤笔翁'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2007-05-09'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">13</span>, <span class="string">'方东白'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">5500</span>, <span class="string">'2009-02-12'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">14</span>, <span class="string">'张三丰'</span>, <span class="number">88</span>, <span class="string">'销售总监'</span>, <span class="number">14000</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">15</span>, <span class="string">'俞莲舟'</span>, <span class="number">38</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">16</span>, <span class="string">'宋远桥'</span>, <span class="number">40</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">17</span>, <span class="string">'陈友谅'</span>, <span class="number">42</span>, <span class="keyword">null</span>, <span class="number">2000</span>, <span class="string">'2011-10-12'</span>, <span class="number">1</span>, <span class="keyword">null</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-2-概述"><a href="#4-2-2-概述" class="headerlink" title="4.2.2 概述"></a>4.2.2 概述</h4><p><img src="/pictures/image-20231004200243641.png" alt="image-20231004200243641"></p>
|
||
<h4 id="4-2-3-分类"><a href="#4-2-3-分类" class="headerlink" title="4.2.3 分类"></a>4.2.3 分类</h4><p><img src="/pictures/image-20231004200624090.png" alt="image-20231004200624090"></p>
|
||
<h3 id="4-3-内连接"><a href="#4-3-内连接" class="headerlink" title="4.3 内连接"></a>4.3 内连接</h3><p><img src="/pictures/image-20231004200731770.png" alt="image-20231004200731770"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 内连接演示</span></span><br><span class="line"><span class="comment">-- 案例</span></span><br><span class="line"><span class="comment">-- 查询每一个员工的姓名 , 及关联的部门的名称 (隐式内连接实现)</span></span><br><span class="line"><span class="comment">-- 表结构: emp , dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name, d.name <span class="keyword">from</span> emp e, dept d <span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询每一个员工的姓名 , 及关联的部门的名称 (显式内连接实现) --- INNER JOIN ...ON ...</span></span><br><span class="line"><span class="comment">-- 表结构: emp , dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name ,d.name <span class="keyword">from</span> emp e <span class="keyword">inner</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure>
|
||
|
||
<blockquote>
|
||
<p>一旦为表起了别名,就不能再使用表名来指定对应的字段了,此时只能够使用别名来指定字段。</p>
|
||
</blockquote>
|
||
<h3 id="4-4-外连接"><a href="#4-4-外连接" class="headerlink" title="4.4 外连接"></a>4.4 外连接</h3><p><img src="/pictures/image-20231004201445881.png" alt="image-20231004201445881"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 外连接</span></span><br><span class="line"><span class="comment">-- 查询emp表的所有数据, 和对应的部门信息</span></span><br><span class="line"><span class="comment">-- 由于需求中提到,要查询emp的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询。</span></span><br><span class="line"><span class="comment">-- 表结构: emp, dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name,d.name <span class="keyword">from</span> emp e <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询dept表的所有数据, 和对应的员工信息(右外连接)</span></span><br><span class="line"><span class="comment">-- 由于需求中提到,要查询dept表的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询</span></span><br><span class="line"><span class="comment">-- 表结构: emp, dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name,d.name <span class="keyword">from</span> emp e <span class="keyword">right</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure>
|
||
|
||
<blockquote>
|
||
<p>注意事项:左外连接和右外连接是可以相互替换的,只需要调整在连接查询时SQL中,表结构的先后顺序就可以了。而我们在日常开发使用时,更偏向于左外连接。</p>
|
||
</blockquote>
|
||
<h3 id="4-5-自连接"><a href="#4-5-自连接" class="headerlink" title="4.5 自连接"></a>4.5 自连接</h3><p><img src="/pictures/image-20231004202219787.png" alt="image-20231004202219787"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 自连接</span></span><br><span class="line"><span class="comment">-- 查询员工 及其 所属领导的名字</span></span><br><span class="line"><span class="comment">-- 表结构: emp</span></span><br><span class="line"><span class="comment">-- 技巧:查询的时候看作是两张表</span></span><br><span class="line"><span class="keyword">select</span> e1.name <span class="string">'员工'</span> ,e2.name <span class="string">'领导'</span> <span class="keyword">from</span> emp e1,emp e2 <span class="keyword">where</span> e1.managerid <span class="operator">=</span> e2.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有员工 emp 及其领导的名字 emp , 如果员工没有领导, 也需要查询出来</span></span><br><span class="line"><span class="comment">-- 表结构: emp a , emp b</span></span><br><span class="line"><span class="keyword">select</span> e1.name <span class="string">'员工'</span> ,e2.name <span class="string">'领导'</span> <span class="keyword">from</span> emp e1 <span class="keyword">left</span> <span class="keyword">join</span> emp e2 <span class="keyword">on</span> e1.managerid <span class="operator">=</span> e2.id;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-6-联合查询"><a href="#4-6-联合查询" class="headerlink" title="4.6 联合查询"></a>4.6 联合查询</h3><p><img src="/pictures/image-20231004203207924.png" alt="image-20231004203207924"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 联合查询</span></span><br><span class="line"><span class="comment">-- 将薪资低于 5000 的员工 , 和 年龄大于 50 岁的员工全部查询出来.</span></span><br><span class="line"><span class="comment">-- 当前对于这个需求,我们可以直接使用多条件查询,使用逻辑运算符 or 连接即可,也可以通过union/union all来联合查询</span></span><br><span class="line"><span class="comment">-- 相当于把下面的两条sql的结果拼接在一起(去掉all就可以实现去重)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.salary <span class="operator"><</span> <span class="number">5000</span></span><br><span class="line"><span class="keyword">union</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.age <span class="operator">></span> <span class="number">50</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><img src="/pictures/image-20231004203724124.png" alt="image-20231004203724124"></p>
|
||
<blockquote>
|
||
<p>union all查询出来的结果,仅仅进行简单的合并,并未去重</p>
|
||
<p>union 联合查询,会对查询出来的结果进行去重处理</p>
|
||
</blockquote>
|
||
<h3 id="4-7-子查询"><a href="#4-7-子查询" class="headerlink" title="4.7 子查询"></a>4.7 子查询</h3><p><img src="/pictures/image-20231004204033345.png" alt="image-20231004204033345"></p>
|
||
<p><strong>标量子查询</strong></p>
|
||
<p>子查询返回的结果是单个值(数字、字符串、日期等),最简单的形式,这种子查询称为标量子查询。常用的操作符:= <> > >= < <=</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 标量子查询</span></span><br><span class="line"><span class="comment">-- 1.查询 "销售部" 的所有员工信息</span></span><br><span class="line"><span class="comment">-- 1.1 查询销售部的部门id</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept d <span class="keyword">where</span> d.name <span class="operator">=</span> <span class="string">'销售部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询在 "方东白" 入职之后的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e1 <span class="keyword">where</span> e1.entrydate <span class="operator">></span> (<span class="keyword">select</span> e2.entrydate <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.name <span class="operator">=</span> <span class="string">'方东白'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>列子查询</strong></p>
|
||
<p>子查询返回的结果是一列(可以是多行),这种子查询称为列子查询。常用的操作符:IN 、NOT IN 、 ANY 、SOME 、 ALL</p>
|
||
<p><img src="/pictures/image-20231004205225188.png" alt="image-20231004205225188"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 列子查询</span></span><br><span class="line"><span class="comment">-- 查询 "销售部" 和 "市场部" 的所有员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="keyword">in</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'销售部'</span> <span class="keyword">or</span> dept.name <span class="operator">=</span> <span class="string">'市场部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询比财务部所有人工资都高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.salary <span class="operator">></span> <span class="keyword">all</span> (<span class="keyword">select</span> e2.salary <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'财务部'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询比研发部其中任意一人工资高的员工信息(使用any或者some均可)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.salary <span class="operator">></span> <span class="keyword">any</span> (<span class="keyword">select</span> e2.salary <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'研发部'</span>));</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>行子查询</strong></p>
|
||
<p>子查询返回的结果是一行(可以是多列),这种子查询称为行子查询。常用的操作符:= 、<> 、IN 、NOT IN</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 行子查询</span></span><br><span class="line"><span class="comment">-- 查询与"张无忌"的薪资及与其直属领导相同的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e2 <span class="keyword">where</span> (e2.salary,e2.managerid) <span class="operator">=</span> (<span class="keyword">select</span> e.salary,e.managerid <span class="keyword">from</span> emp e <span class="keyword">where</span> e.name <span class="operator">=</span> <span class="string">'张无忌'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>表子查询</strong></p>
|
||
<p>子查询返回的结果是多行多列,这种子查询称为表子查询。常用的操作符:IN</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 表子查询</span></span><br><span class="line"><span class="comment">-- 查询与 "鹿杖客" , "宋远桥" 的职位和薪资相同的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e2 <span class="keyword">where</span> (e2.job,e2.salary) <span class="keyword">in</span> (<span class="keyword">select</span> e.job, e.salary <span class="keyword">from</span> emp e <span class="keyword">where</span> e.name <span class="operator">=</span> <span class="string">'鹿杖客'</span> <span class="keyword">or</span> name <span class="operator">=</span> <span class="string">'宋远桥'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询入职日期是 "2006-01-01" 之后的员工信息 , 及其部门信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> (<span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> entrydate <span class="operator">></span> <span class="string">'2006-01-01'</span>) e <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>多表查询相关的sql语句</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ---------------------------------------------多表查询--------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 多表关系</span></span><br><span class="line"><span class="comment">-- 演示多对多的关系</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> <span class="keyword">no</span> <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'学号'</span></span><br><span class="line">) comment <span class="string">'学生表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'黛绮丝'</span>, <span class="string">'2000100101'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'谢逊'</span>,</span><br><span class="line"> <span class="string">'2000100102'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'殷天正'</span>, <span class="string">'2000100103'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'韦一笑'</span>, <span class="string">'2000100104'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> course</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'课程名称'</span></span><br><span class="line">) comment <span class="string">'课程表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'Java'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'PHP'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'MySQL'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'Hadoop'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student_course</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'主键'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> studentid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'学生ID'</span>,</span><br><span class="line"> courseid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'课程ID'</span>,</span><br><span class="line"> <span class="keyword">constraint</span> fk_courseid <span class="keyword">foreign</span> key (courseid) <span class="keyword">references</span> course (id),</span><br><span class="line"> <span class="keyword">constraint</span> fk_studentid <span class="keyword">foreign</span> key (studentid) <span class="keyword">references</span> student (id)</span><br><span class="line">) comment <span class="string">'学生课程中间表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student_course</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">2</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">2</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 演示一对一的关系</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'1: 男 , 2: 女'</span>,</span><br><span class="line"> phone <span class="type">char</span>(<span class="number">11</span>) comment <span class="string">'手机号'</span></span><br><span class="line">) comment <span class="string">'用户基本信息表'</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user_edu</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> degree <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'学历'</span>,</span><br><span class="line"> major <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'专业'</span>,</span><br><span class="line"> primaryschool <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'小学'</span>,</span><br><span class="line"> middleschool <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'中学'</span>,</span><br><span class="line"> university <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'大学'</span>,</span><br><span class="line"> userid <span class="type">int</span> <span class="keyword">unique</span> comment <span class="string">'用户ID'</span>,</span><br><span class="line"> <span class="keyword">constraint</span> fk_userid <span class="keyword">foreign</span> key (userid) <span class="keyword">references</span> tb_user (id)</span><br><span class="line">) comment <span class="string">'用户教育信息表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user(id, name, age, gender, phone)</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'黄渤'</span>, <span class="number">45</span>, <span class="string">'1'</span>, <span class="string">'18800001111'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'冰冰'</span>, <span class="number">35</span>, <span class="string">'2'</span>, <span class="string">'18800002222'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'码云'</span>, <span class="number">55</span>, <span class="string">'1'</span>, <span class="string">'18800008888'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'李彦宏'</span>, <span class="number">50</span>, <span class="string">'1'</span>, <span class="string">'18800009999'</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user_edu(id, degree, major, primaryschool, middleschool,</span><br><span class="line"> university, userid)</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'舞蹈'</span>, <span class="string">'静安区第一小学'</span>, <span class="string">'静安区第一中学'</span>, <span class="string">'北京舞蹈学院'</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'硕士'</span>, <span class="string">'表演'</span>, <span class="string">'朝阳区第一小学'</span>, <span class="string">'朝阳区第一中学'</span>, <span class="string">'北京电影学院'</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'英语'</span>, <span class="string">'杭州市第一小学'</span>, <span class="string">'杭州市第一中学'</span>, <span class="string">'杭州师范大学'</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'应用数学'</span>, <span class="string">'阳泉第一小学'</span>, <span class="string">'阳泉区第一中学'</span>, <span class="string">'清华大学'</span>, <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 演示多表查询</span></span><br><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="comment">-- 创建dept表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>,</span><br><span class="line"> <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'人事部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建emp表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept_id <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span></span><br><span class="line"> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">7</span>, <span class="string">'灭绝'</span>, <span class="number">60</span>, <span class="string">'财务总监'</span>, <span class="number">8500</span>, <span class="string">'2002-09-12'</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">8</span>, <span class="string">'周芷若'</span>, <span class="number">19</span>, <span class="string">'会计'</span>, <span class="number">48000</span>, <span class="string">'2006-06-02'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">9</span>, <span class="string">'丁敏君'</span>, <span class="number">23</span>, <span class="string">'出纳'</span>, <span class="number">5250</span>, <span class="string">'2009-05-13'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">10</span>, <span class="string">'赵敏'</span>, <span class="number">20</span>, <span class="string">'市场部总监'</span>, <span class="number">12500</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">11</span>, <span class="string">'鹿杖客'</span>, <span class="number">56</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2006-10-03'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">12</span>, <span class="string">'鹤笔翁'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2007-05-09'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">13</span>, <span class="string">'方东白'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">5500</span>, <span class="string">'2009-02-12'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">14</span>, <span class="string">'张三丰'</span>, <span class="number">88</span>, <span class="string">'销售总监'</span>, <span class="number">14000</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">15</span>, <span class="string">'俞莲舟'</span>, <span class="number">38</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">16</span>, <span class="string">'宋远桥'</span>, <span class="number">40</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">17</span>, <span class="string">'陈友谅'</span>, <span class="number">42</span>, <span class="keyword">null</span>, <span class="number">2000</span>, <span class="string">'2011-10-12'</span>, <span class="number">1</span>, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 多表查询</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp,</span><br><span class="line"> dept</span><br><span class="line"><span class="keyword">where</span> emp.dept_id <span class="operator">=</span> dept.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 内连接演示</span></span><br><span class="line"><span class="comment">-- 案例</span></span><br><span class="line"><span class="comment">-- 查询每一个员工的姓名 , 及关联的部门的名称 (隐式内连接实现)</span></span><br><span class="line"><span class="comment">-- 表结构: emp , dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name, d.name <span class="keyword">from</span> emp e, dept d <span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询每一个员工的姓名 , 及关联的部门的名称 (显式内连接实现) --- INNER JOIN ...ON ...</span></span><br><span class="line"><span class="comment">-- 表结构: emp , dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name ,d.name <span class="keyword">from</span> emp e <span class="keyword">inner</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 外连接</span></span><br><span class="line"><span class="comment">-- 查询emp表的所有数据, 和对应的部门信息</span></span><br><span class="line"><span class="comment">-- 由于需求中提到,要查询emp的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询。</span></span><br><span class="line"><span class="comment">-- 表结构: emp, dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name,d.name <span class="keyword">from</span> emp e <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询dept表的所有数据, 和对应的员工信息(右外连接)</span></span><br><span class="line"><span class="comment">-- 由于需求中提到,要查询dept表的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询</span></span><br><span class="line"><span class="comment">-- 表结构: emp, dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name,d.name <span class="keyword">from</span> emp e <span class="keyword">right</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 自连接</span></span><br><span class="line"><span class="comment">-- 查询员工 及其 所属领导的名字</span></span><br><span class="line"><span class="comment">-- 表结构: emp</span></span><br><span class="line"><span class="comment">-- 技巧:查询的时候看作是两张表</span></span><br><span class="line"><span class="keyword">select</span> e1.name <span class="string">'员工'</span> ,e2.name <span class="string">'领导'</span> <span class="keyword">from</span> emp e1,emp e2 <span class="keyword">where</span> e1.managerid <span class="operator">=</span> e2.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有员工 emp 及其领导的名字 emp , 如果员工没有领导, 也需要查询出来</span></span><br><span class="line"><span class="comment">-- 表结构: emp a , emp b</span></span><br><span class="line"><span class="keyword">select</span> e1.name <span class="string">'员工'</span> ,e2.name <span class="string">'领导'</span> <span class="keyword">from</span> emp e1 <span class="keyword">left</span> <span class="keyword">join</span> emp e2 <span class="keyword">on</span> e1.managerid <span class="operator">=</span> e2.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 联合查询</span></span><br><span class="line"><span class="comment">-- 将薪资低于 5000 的员工 , 和 年龄大于 50 岁的员工全部查询出来.</span></span><br><span class="line"><span class="comment">-- 当前对于这个需求,我们可以直接使用多条件查询,使用逻辑运算符 or 连接即可,也可以通过union/union all来联合查询</span></span><br><span class="line"><span class="comment">-- 相当于把下面的两条sql的结果拼接在一起(去掉all就可以实现去重)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.salary <span class="operator"><</span> <span class="number">5000</span></span><br><span class="line"><span class="keyword">union</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.age <span class="operator">></span> <span class="number">50</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 子查询</span></span><br><span class="line"><span class="comment">-- 标量子查询</span></span><br><span class="line"><span class="comment">-- 1.查询 "销售部" 的所有员工信息</span></span><br><span class="line"><span class="comment">-- 1.1 查询销售部的部门id</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept d <span class="keyword">where</span> d.name <span class="operator">=</span> <span class="string">'销售部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询在 "方东白" 入职之后的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e1 <span class="keyword">where</span> e1.entrydate <span class="operator">></span> (<span class="keyword">select</span> e2.entrydate <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.name <span class="operator">=</span> <span class="string">'方东白'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 列子查询</span></span><br><span class="line"><span class="comment">-- 查询 "销售部" 和 "市场部" 的所有员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="keyword">in</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'销售部'</span> <span class="keyword">or</span> dept.name <span class="operator">=</span> <span class="string">'市场部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询比财务部所有人工资都高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.salary <span class="operator">></span> <span class="keyword">all</span> (<span class="keyword">select</span> e2.salary <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'财务部'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询比研发部其中任意一人工资高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e <span class="keyword">where</span> e.salary <span class="operator">></span> <span class="keyword">any</span> (<span class="keyword">select</span> e2.salary <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'研发部'</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 行子查询</span></span><br><span class="line"><span class="comment">-- 查询与"张无忌"的薪资及与其直属领导相同的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e2 <span class="keyword">where</span> (e2.salary,e2.managerid) <span class="operator">=</span> (<span class="keyword">select</span> e.salary,e.managerid <span class="keyword">from</span> emp e <span class="keyword">where</span> e.name <span class="operator">=</span> <span class="string">'张无忌'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 表子查询</span></span><br><span class="line"><span class="comment">-- 查询与 "鹿杖客" , "宋远桥" 的职位和薪资相同的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp e2 <span class="keyword">where</span> (e2.job,e2.salary) <span class="keyword">in</span> (<span class="keyword">select</span> e.job, e.salary <span class="keyword">from</span> emp e <span class="keyword">where</span> e.name <span class="operator">=</span> <span class="string">'鹿杖客'</span> <span class="keyword">or</span> name <span class="operator">=</span> <span class="string">'宋远桥'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询入职日期是 "2006-01-01" 之后的员工信息 , 及其部门信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> (<span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> entrydate <span class="operator">></span> <span class="string">'2006-01-01'</span>) e <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-8-多表查询案例"><a href="#4-8-多表查询案例" class="headerlink" title="4.8 多表查询案例"></a>4.8 多表查询案例</h3><p><strong>题目</strong></p>
|
||
<p><img src="/pictures/image-20231004212719134.png" alt="image-20231004212719134"></p>
|
||
<p><strong>数据准备</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="comment">-- 创建dept表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>,</span><br><span class="line"> <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'人事部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建emp表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept_id <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span></span><br><span class="line"> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">7</span>, <span class="string">'灭绝'</span>, <span class="number">60</span>, <span class="string">'财务总监'</span>, <span class="number">8500</span>, <span class="string">'2002-09-12'</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">8</span>, <span class="string">'周芷若'</span>, <span class="number">19</span>, <span class="string">'会计'</span>, <span class="number">4800</span>, <span class="string">'2006-06-02'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">9</span>, <span class="string">'丁敏君'</span>, <span class="number">23</span>, <span class="string">'出纳'</span>, <span class="number">5250</span>, <span class="string">'2009-05-13'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">10</span>, <span class="string">'赵敏'</span>, <span class="number">20</span>, <span class="string">'市场部总监'</span>, <span class="number">12500</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">11</span>, <span class="string">'鹿杖客'</span>, <span class="number">56</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2006-10-03'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">12</span>, <span class="string">'鹤笔翁'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2007-05-09'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">13</span>, <span class="string">'方东白'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">5500</span>, <span class="string">'2009-02-12'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">14</span>, <span class="string">'张三丰'</span>, <span class="number">88</span>, <span class="string">'销售总监'</span>, <span class="number">14000</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">15</span>, <span class="string">'俞莲舟'</span>, <span class="number">38</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">16</span>, <span class="string">'宋远桥'</span>, <span class="number">40</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">17</span>, <span class="string">'陈友谅'</span>, <span class="number">42</span>, <span class="keyword">null</span>, <span class="number">2000</span>, <span class="string">'2011-10-12'</span>, <span class="number">1</span>, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建salgrade并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> salgrade</span><br><span class="line">(</span><br><span class="line"> grade <span class="type">int</span>,</span><br><span class="line"> losal <span class="type">int</span>,</span><br><span class="line"> hisal <span class="type">int</span></span><br><span class="line">) comment <span class="string">'薪资等级表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">1</span>, <span class="number">0</span>, <span class="number">3000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">2</span>, <span class="number">3001</span>, <span class="number">5000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">3</span>, <span class="number">5001</span>, <span class="number">8000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">4</span>, <span class="number">8001</span>, <span class="number">10000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">5</span>, <span class="number">10001</span>, <span class="number">15000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">6</span>, <span class="number">15001</span>, <span class="number">20000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">7</span>, <span class="number">20001</span>, <span class="number">25000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">8</span>, <span class="number">25001</span>, <span class="number">30000</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>题解</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 题解</span></span><br><span class="line"><span class="comment">-- 1.查询员工的姓名、年龄、职位、部门信息(隐式内连接)</span></span><br><span class="line"><span class="keyword">select</span> e.name, e.age, e.job, d.name</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 2.查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显式内连接)</span></span><br><span class="line"><span class="keyword">select</span> e.name, e.age, e.job, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">inner</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"><span class="keyword">where</span> e.age <span class="operator"><</span> <span class="number">30</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 3.查询拥有员工的部门id、部门名称</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> d.id, d.name</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept</span><br><span class="line"><span class="keyword">where</span> dept.id <span class="keyword">in</span> (<span class="keyword">select</span> e.dept_id <span class="keyword">from</span> emp e);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 4.查询所有年龄大于40岁的员工,及其归属的部门名称;如果没有员工分配部门,也需要展示出来</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">></span> <span class="number">40</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 5.查询所有员工的工资等级</span></span><br><span class="line"><span class="keyword">select</span> e.name <span class="string">'姓名'</span>, s.grade <span class="string">'工资等级'</span></span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> salgrade s</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 6.查询研发部所有员工的信息已经工资等级</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, s.grade</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d,</span><br><span class="line"> salgrade s</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"> <span class="keyword">and</span> (e.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal)</span><br><span class="line"> <span class="keyword">and</span> d.name <span class="operator">=</span> <span class="string">'研发部'</span>;</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, s.grade</span><br><span class="line"><span class="keyword">from</span> (<span class="keyword">select</span> emp.<span class="operator">*</span></span><br><span class="line"> <span class="keyword">from</span> emp,</span><br><span class="line"> dept</span><br><span class="line"> <span class="keyword">where</span> emp.dept_id <span class="operator">=</span> dept.id</span><br><span class="line"> <span class="keyword">and</span> dept.name <span class="operator">=</span> <span class="string">'研发部'</span>) e,</span><br><span class="line"> salgrade s</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 7.查询研发部的平均工资</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(e.salary)</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"> <span class="keyword">and</span> d.name <span class="operator">=</span> <span class="string">'研发部'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 8.查询工资比灭绝高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> salary <span class="operator">></span> (<span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'灭绝'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 9.查询比平均工资高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> salary <span class="operator">></span> (<span class="keyword">select</span> <span class="built_in">avg</span>(salary) <span class="keyword">from</span> emp);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 10.查询低于本部门平均工资的员工</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e2</span><br><span class="line"><span class="keyword">where</span> e2.salary <span class="operator"><</span> (<span class="keyword">select</span> <span class="built_in">avg</span>(e1.salary) <span class="keyword">from</span> emp e1 <span class="keyword">where</span> e1.dept_id <span class="operator">=</span> e2.dept_id);</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> (<span class="keyword">select</span> dept_id, <span class="built_in">avg</span>(salary) <span class="string">'salavg'</span> <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> dept_id) eavg</span><br><span class="line"><span class="keyword">where</span> eavg.dept_id <span class="operator">=</span> e.dept_id</span><br><span class="line"> <span class="keyword">and</span> e.salary <span class="operator"><</span> eavg.salavg;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 11.查询所有的部门信息,并统计部门的员工人数</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> d.id, d.name, (<span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id) <span class="string">'人数'</span></span><br><span class="line"><span class="keyword">from</span> dept d;</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept d,</span><br><span class="line"> (<span class="keyword">select</span> e.dept_id, <span class="built_in">count</span>(<span class="operator">*</span>) <span class="string">'人数'</span> <span class="keyword">from</span> emp e <span class="keyword">group</span> <span class="keyword">by</span> e.dept_id) ecount</span><br><span class="line"><span class="keyword">where</span> d.id <span class="operator">=</span> ecount.dept_id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 12.查询所有学生的选课情况,展示出学生名称,学号,课程名称</span></span><br><span class="line"><span class="keyword">select</span> s.name, s.no, c.name</span><br><span class="line"><span class="keyword">from</span> course c,</span><br><span class="line"> student s,</span><br><span class="line"> student_course sc</span><br><span class="line"><span class="keyword">where</span> c.id <span class="operator">=</span> sc.courseid</span><br><span class="line"> <span class="keyword">and</span> s.id <span class="operator">=</span> sc.studentid;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>总结</strong></p>
|
||
<p> <img src="/pictures/image-20231005133456395.png" alt="image-20231005133456395"></p>
|
||
<h2 id="5-事务"><a href="#5-事务" class="headerlink" title="5.事务"></a>5.事务</h2><h3 id="5-1-事务介绍"><a href="#5-1-事务介绍" class="headerlink" title="5.1 事务介绍"></a>5.1 事务介绍</h3><p> 事务 是一组操作的集合,它是一个不可分割的工作单位,事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求,即这些操作要么同时成功,要么同时失败。就比如: 张三给李四转账1000块钱,张三银行账户的钱减少1000,而李四银行账户的钱要增加1000。 这一组操作就必须在一个事务的范围内,要么都成功,要么都失败。</p>
|
||
<blockquote>
|
||
<p>注意: 默认MySQL的事务是自动提交的,也就是说,当执行完一条DML语句时,MySQL会立即隐式的提交事务。</p>
|
||
</blockquote>
|
||
<h3 id="5-2-事务操作"><a href="#5-2-事务操作" class="headerlink" title="5.2 事务操作"></a>5.2 事务操作</h3><p><strong>数据准备</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> account;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> account</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> <span class="keyword">primary</span> key AUTO_INCREMENT comment <span class="string">'ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> money <span class="keyword">double</span>(<span class="number">10</span>, <span class="number">2</span>) comment <span class="string">'余额'</span></span><br><span class="line">) comment <span class="string">'账户表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> account(name, money)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="string">'张三'</span>, <span class="number">2000</span>),</span><br><span class="line"> (<span class="string">'李四'</span>, <span class="number">2000</span>);</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 转账操作(张三转1000给李四)</span></span><br><span class="line"><span class="comment">-- 1. 查询张三余额</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 2. 张三的余额减少1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">-</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 3. 李四的余额增加1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">+</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'李四'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>控制事务一</strong></p>
|
||
<p><img src="/pictures/image-20231005134601982.png" alt="image-20231005134601982"></p>
|
||
<p><strong>控制事务二</strong></p>
|
||
<p><img src="/pictures/image-20231005134750027.png" alt="image-20231005134750027"></p>
|
||
<p><img src="/pictures/image-20231005134835921.png" alt="image-20231005134835921"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 开启事务</span></span><br><span class="line"><span class="keyword">start</span> transaction;</span><br><span class="line"><span class="comment">-- 1. 查询张三余额</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 2. 张三的余额减少1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">-</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 3. 李四的余额增加1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">+</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'李四'</span>;</span><br><span class="line"><span class="comment">-- 如果正常执行完毕, 则提交事务</span></span><br><span class="line"><span class="keyword">commit</span>;</span><br><span class="line"><span class="comment">-- 如果执行过程中报错, 则回滚事务</span></span><br><span class="line"><span class="comment">-- rollback;</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="5-3-事务四大特性"><a href="#5-3-事务四大特性" class="headerlink" title="5.3 事务四大特性"></a>5.3 事务四大特性</h3><p><img src="/pictures/image-20231005135637347.png" alt="image-20231005135637347"></p>
|
||
<h3 id="5-4-并发事务问题"><a href="#5-4-并发事务问题" class="headerlink" title="5.4 并发事务问题"></a>5.4 并发事务问题</h3><p><img src="/pictures/image-20231005135741486.png" alt="image-20231005135741486"></p>
|
||
<p><img src="/pictures/image-20231005135811745.png" alt="image-20231005135811745"></p>
|
||
<h3 id="5-5-事务隔离级别"><a href="#5-5-事务隔离级别" class="headerlink" title="5.5 事务隔离级别"></a>5.5 事务隔离级别</h3><p><strong>为了解决并发事务所引发的问题,在数据库中引入了事务隔离级别,主要有以下几种:</strong></p>
|
||
<p> “X” 表示不会出现</p>
|
||
<p> <img src="/pictures/image-20231005140255060.png" alt="image-20231005140255060"></p>
|
||
<p><img src="/pictures/image-20231005140322039.png" alt="image-20231005140322039"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查看事务隔离级别</span></span><br><span class="line"><span class="keyword">SELECT</span> @<span class="variable">@TRANSACTION_ISOLATION</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 设置事务的隔离级别</span></span><br><span class="line"><span class="keyword">SET</span> [ SESSION <span class="operator">|</span> <span class="keyword">GLOBAL</span> ] TRANSACTION ISOLATION LEVEL { READ UNCOMMITTED <span class="operator">|</span> READ COMMITTED <span class="operator">|</span> REPEATABLE READ <span class="operator">|</span> SERIALIZABLE }</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>小结</strong></p>
|
||
<p> <img src="/pictures/image-20231005142056902.png" alt="image-20231005142056902"></p>
|
||
<p><strong>基础篇全部的Sql</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br><span class="line">452</span><br><span class="line">453</span><br><span class="line">454</span><br><span class="line">455</span><br><span class="line">456</span><br><span class="line">457</span><br><span class="line">458</span><br><span class="line">459</span><br><span class="line">460</span><br><span class="line">461</span><br><span class="line">462</span><br><span class="line">463</span><br><span class="line">464</span><br><span class="line">465</span><br><span class="line">466</span><br><span class="line">467</span><br><span class="line">468</span><br><span class="line">469</span><br><span class="line">470</span><br><span class="line">471</span><br><span class="line">472</span><br><span class="line">473</span><br><span class="line">474</span><br><span class="line">475</span><br><span class="line">476</span><br><span class="line">477</span><br><span class="line">478</span><br><span class="line">479</span><br><span class="line">480</span><br><span class="line">481</span><br><span class="line">482</span><br><span class="line">483</span><br><span class="line">484</span><br><span class="line">485</span><br><span class="line">486</span><br><span class="line">487</span><br><span class="line">488</span><br><span class="line">489</span><br><span class="line">490</span><br><span class="line">491</span><br><span class="line">492</span><br><span class="line">493</span><br><span class="line">494</span><br><span class="line">495</span><br><span class="line">496</span><br><span class="line">497</span><br><span class="line">498</span><br><span class="line">499</span><br><span class="line">500</span><br><span class="line">501</span><br><span class="line">502</span><br><span class="line">503</span><br><span class="line">504</span><br><span class="line">505</span><br><span class="line">506</span><br><span class="line">507</span><br><span class="line">508</span><br><span class="line">509</span><br><span class="line">510</span><br><span class="line">511</span><br><span class="line">512</span><br><span class="line">513</span><br><span class="line">514</span><br><span class="line">515</span><br><span class="line">516</span><br><span class="line">517</span><br><span class="line">518</span><br><span class="line">519</span><br><span class="line">520</span><br><span class="line">521</span><br><span class="line">522</span><br><span class="line">523</span><br><span class="line">524</span><br><span class="line">525</span><br><span class="line">526</span><br><span class="line">527</span><br><span class="line">528</span><br><span class="line">529</span><br><span class="line">530</span><br><span class="line">531</span><br><span class="line">532</span><br><span class="line">533</span><br><span class="line">534</span><br><span class="line">535</span><br><span class="line">536</span><br><span class="line">537</span><br><span class="line">538</span><br><span class="line">539</span><br><span class="line">540</span><br><span class="line">541</span><br><span class="line">542</span><br><span class="line">543</span><br><span class="line">544</span><br><span class="line">545</span><br><span class="line">546</span><br><span class="line">547</span><br><span class="line">548</span><br><span class="line">549</span><br><span class="line">550</span><br><span class="line">551</span><br><span class="line">552</span><br><span class="line">553</span><br><span class="line">554</span><br><span class="line">555</span><br><span class="line">556</span><br><span class="line">557</span><br><span class="line">558</span><br><span class="line">559</span><br><span class="line">560</span><br><span class="line">561</span><br><span class="line">562</span><br><span class="line">563</span><br><span class="line">564</span><br><span class="line">565</span><br><span class="line">566</span><br><span class="line">567</span><br><span class="line">568</span><br><span class="line">569</span><br><span class="line">570</span><br><span class="line">571</span><br><span class="line">572</span><br><span class="line">573</span><br><span class="line">574</span><br><span class="line">575</span><br><span class="line">576</span><br><span class="line">577</span><br><span class="line">578</span><br><span class="line">579</span><br><span class="line">580</span><br><span class="line">581</span><br><span class="line">582</span><br><span class="line">583</span><br><span class="line">584</span><br><span class="line">585</span><br><span class="line">586</span><br><span class="line">587</span><br><span class="line">588</span><br><span class="line">589</span><br><span class="line">590</span><br><span class="line">591</span><br><span class="line">592</span><br><span class="line">593</span><br><span class="line">594</span><br><span class="line">595</span><br><span class="line">596</span><br><span class="line">597</span><br><span class="line">598</span><br><span class="line">599</span><br><span class="line">600</span><br><span class="line">601</span><br><span class="line">602</span><br><span class="line">603</span><br><span class="line">604</span><br><span class="line">605</span><br><span class="line">606</span><br><span class="line">607</span><br><span class="line">608</span><br><span class="line">609</span><br><span class="line">610</span><br><span class="line">611</span><br><span class="line">612</span><br><span class="line">613</span><br><span class="line">614</span><br><span class="line">615</span><br><span class="line">616</span><br><span class="line">617</span><br><span class="line">618</span><br><span class="line">619</span><br><span class="line">620</span><br><span class="line">621</span><br><span class="line">622</span><br><span class="line">623</span><br><span class="line">624</span><br><span class="line">625</span><br><span class="line">626</span><br><span class="line">627</span><br><span class="line">628</span><br><span class="line">629</span><br><span class="line">630</span><br><span class="line">631</span><br><span class="line">632</span><br><span class="line">633</span><br><span class="line">634</span><br><span class="line">635</span><br><span class="line">636</span><br><span class="line">637</span><br><span class="line">638</span><br><span class="line">639</span><br><span class="line">640</span><br><span class="line">641</span><br><span class="line">642</span><br><span class="line">643</span><br><span class="line">644</span><br><span class="line">645</span><br><span class="line">646</span><br><span class="line">647</span><br><span class="line">648</span><br><span class="line">649</span><br><span class="line">650</span><br><span class="line">651</span><br><span class="line">652</span><br><span class="line">653</span><br><span class="line">654</span><br><span class="line">655</span><br><span class="line">656</span><br><span class="line">657</span><br><span class="line">658</span><br><span class="line">659</span><br><span class="line">660</span><br><span class="line">661</span><br><span class="line">662</span><br><span class="line">663</span><br><span class="line">664</span><br><span class="line">665</span><br><span class="line">666</span><br><span class="line">667</span><br><span class="line">668</span><br><span class="line">669</span><br><span class="line">670</span><br><span class="line">671</span><br><span class="line">672</span><br><span class="line">673</span><br><span class="line">674</span><br><span class="line">675</span><br><span class="line">676</span><br><span class="line">677</span><br><span class="line">678</span><br><span class="line">679</span><br><span class="line">680</span><br><span class="line">681</span><br><span class="line">682</span><br><span class="line">683</span><br><span class="line">684</span><br><span class="line">685</span><br><span class="line">686</span><br><span class="line">687</span><br><span class="line">688</span><br><span class="line">689</span><br><span class="line">690</span><br><span class="line">691</span><br><span class="line">692</span><br><span class="line">693</span><br><span class="line">694</span><br><span class="line">695</span><br><span class="line">696</span><br><span class="line">697</span><br><span class="line">698</span><br><span class="line">699</span><br><span class="line">700</span><br><span class="line">701</span><br><span class="line">702</span><br><span class="line">703</span><br><span class="line">704</span><br><span class="line">705</span><br><span class="line">706</span><br><span class="line">707</span><br><span class="line">708</span><br><span class="line">709</span><br><span class="line">710</span><br><span class="line">711</span><br><span class="line">712</span><br><span class="line">713</span><br><span class="line">714</span><br><span class="line">715</span><br><span class="line">716</span><br><span class="line">717</span><br><span class="line">718</span><br><span class="line">719</span><br><span class="line">720</span><br><span class="line">721</span><br><span class="line">722</span><br><span class="line">723</span><br><span class="line">724</span><br><span class="line">725</span><br><span class="line">726</span><br><span class="line">727</span><br><span class="line">728</span><br><span class="line">729</span><br><span class="line">730</span><br><span class="line">731</span><br><span class="line">732</span><br><span class="line">733</span><br><span class="line">734</span><br><span class="line">735</span><br><span class="line">736</span><br><span class="line">737</span><br><span class="line">738</span><br><span class="line">739</span><br><span class="line">740</span><br><span class="line">741</span><br><span class="line">742</span><br><span class="line">743</span><br><span class="line">744</span><br><span class="line">745</span><br><span class="line">746</span><br><span class="line">747</span><br><span class="line">748</span><br><span class="line">749</span><br><span class="line">750</span><br><span class="line">751</span><br><span class="line">752</span><br><span class="line">753</span><br><span class="line">754</span><br><span class="line">755</span><br><span class="line">756</span><br><span class="line">757</span><br><span class="line">758</span><br><span class="line">759</span><br><span class="line">760</span><br><span class="line">761</span><br><span class="line">762</span><br><span class="line">763</span><br><span class="line">764</span><br><span class="line">765</span><br><span class="line">766</span><br><span class="line">767</span><br><span class="line">768</span><br><span class="line">769</span><br><span class="line">770</span><br><span class="line">771</span><br><span class="line">772</span><br><span class="line">773</span><br><span class="line">774</span><br><span class="line">775</span><br><span class="line">776</span><br><span class="line">777</span><br><span class="line">778</span><br><span class="line">779</span><br><span class="line">780</span><br><span class="line">781</span><br><span class="line">782</span><br><span class="line">783</span><br><span class="line">784</span><br><span class="line">785</span><br><span class="line">786</span><br><span class="line">787</span><br><span class="line">788</span><br><span class="line">789</span><br><span class="line">790</span><br><span class="line">791</span><br><span class="line">792</span><br><span class="line">793</span><br><span class="line">794</span><br><span class="line">795</span><br><span class="line">796</span><br><span class="line">797</span><br><span class="line">798</span><br><span class="line">799</span><br><span class="line">800</span><br><span class="line">801</span><br><span class="line">802</span><br><span class="line">803</span><br><span class="line">804</span><br><span class="line">805</span><br><span class="line">806</span><br><span class="line">807</span><br><span class="line">808</span><br><span class="line">809</span><br><span class="line">810</span><br><span class="line">811</span><br><span class="line">812</span><br><span class="line">813</span><br><span class="line">814</span><br><span class="line">815</span><br><span class="line">816</span><br><span class="line">817</span><br><span class="line">818</span><br><span class="line">819</span><br><span class="line">820</span><br><span class="line">821</span><br><span class="line">822</span><br><span class="line">823</span><br><span class="line">824</span><br><span class="line">825</span><br><span class="line">826</span><br><span class="line">827</span><br><span class="line">828</span><br><span class="line">829</span><br><span class="line">830</span><br><span class="line">831</span><br><span class="line">832</span><br><span class="line">833</span><br><span class="line">834</span><br><span class="line">835</span><br><span class="line">836</span><br><span class="line">837</span><br><span class="line">838</span><br><span class="line">839</span><br><span class="line">840</span><br><span class="line">841</span><br><span class="line">842</span><br><span class="line">843</span><br><span class="line">844</span><br><span class="line">845</span><br><span class="line">846</span><br><span class="line">847</span><br><span class="line">848</span><br><span class="line">849</span><br><span class="line">850</span><br><span class="line">851</span><br><span class="line">852</span><br><span class="line">853</span><br><span class="line">854</span><br><span class="line">855</span><br><span class="line">856</span><br><span class="line">857</span><br><span class="line">858</span><br><span class="line">859</span><br><span class="line">860</span><br><span class="line">861</span><br><span class="line">862</span><br><span class="line">863</span><br><span class="line">864</span><br><span class="line">865</span><br><span class="line">866</span><br><span class="line">867</span><br><span class="line">868</span><br><span class="line">869</span><br><span class="line">870</span><br><span class="line">871</span><br><span class="line">872</span><br><span class="line">873</span><br><span class="line">874</span><br><span class="line">875</span><br><span class="line">876</span><br><span class="line">877</span><br><span class="line">878</span><br><span class="line">879</span><br><span class="line">880</span><br><span class="line">881</span><br><span class="line">882</span><br><span class="line">883</span><br><span class="line">884</span><br><span class="line">885</span><br><span class="line">886</span><br><span class="line">887</span><br><span class="line">888</span><br><span class="line">889</span><br><span class="line">890</span><br><span class="line">891</span><br><span class="line">892</span><br><span class="line">893</span><br><span class="line">894</span><br><span class="line">895</span><br><span class="line">896</span><br><span class="line">897</span><br><span class="line">898</span><br><span class="line">899</span><br><span class="line">900</span><br><span class="line">901</span><br><span class="line">902</span><br><span class="line">903</span><br><span class="line">904</span><br><span class="line">905</span><br><span class="line">906</span><br><span class="line">907</span><br><span class="line">908</span><br><span class="line">909</span><br><span class="line">910</span><br><span class="line">911</span><br><span class="line">912</span><br><span class="line">913</span><br><span class="line">914</span><br><span class="line">915</span><br><span class="line">916</span><br><span class="line">917</span><br><span class="line">918</span><br><span class="line">919</span><br><span class="line">920</span><br><span class="line">921</span><br><span class="line">922</span><br><span class="line">923</span><br><span class="line">924</span><br><span class="line">925</span><br><span class="line">926</span><br><span class="line">927</span><br><span class="line">928</span><br><span class="line">929</span><br><span class="line">930</span><br><span class="line">931</span><br><span class="line">932</span><br><span class="line">933</span><br><span class="line">934</span><br><span class="line">935</span><br><span class="line">936</span><br><span class="line">937</span><br><span class="line">938</span><br><span class="line">939</span><br><span class="line">940</span><br><span class="line">941</span><br><span class="line">942</span><br><span class="line">943</span><br><span class="line">944</span><br><span class="line">945</span><br><span class="line">946</span><br><span class="line">947</span><br><span class="line">948</span><br><span class="line">949</span><br><span class="line">950</span><br><span class="line">951</span><br><span class="line">952</span><br><span class="line">953</span><br><span class="line">954</span><br><span class="line">955</span><br><span class="line">956</span><br><span class="line">957</span><br><span class="line">958</span><br><span class="line">959</span><br><span class="line">960</span><br><span class="line">961</span><br><span class="line">962</span><br><span class="line">963</span><br><span class="line">964</span><br><span class="line">965</span><br><span class="line">966</span><br><span class="line">967</span><br><span class="line">968</span><br><span class="line">969</span><br><span class="line">970</span><br><span class="line">971</span><br><span class="line">972</span><br><span class="line">973</span><br><span class="line">974</span><br><span class="line">975</span><br><span class="line">976</span><br><span class="line">977</span><br><span class="line">978</span><br><span class="line">979</span><br><span class="line">980</span><br><span class="line">981</span><br><span class="line">982</span><br><span class="line">983</span><br><span class="line">984</span><br><span class="line">985</span><br><span class="line">986</span><br><span class="line">987</span><br><span class="line">988</span><br><span class="line">989</span><br><span class="line">990</span><br><span class="line">991</span><br><span class="line">992</span><br><span class="line">993</span><br><span class="line">994</span><br><span class="line">995</span><br><span class="line">996</span><br><span class="line">997</span><br><span class="line">998</span><br><span class="line">999</span><br><span class="line">1000</span><br><span class="line">1001</span><br><span class="line">1002</span><br><span class="line">1003</span><br><span class="line">1004</span><br><span class="line">1005</span><br><span class="line">1006</span><br><span class="line">1007</span><br><span class="line">1008</span><br><span class="line">1009</span><br><span class="line">1010</span><br><span class="line">1011</span><br><span class="line">1012</span><br><span class="line">1013</span><br><span class="line">1014</span><br><span class="line">1015</span><br><span class="line">1016</span><br><span class="line">1017</span><br><span class="line">1018</span><br><span class="line">1019</span><br><span class="line">1020</span><br><span class="line">1021</span><br><span class="line">1022</span><br><span class="line">1023</span><br><span class="line">1024</span><br><span class="line">1025</span><br><span class="line">1026</span><br><span class="line">1027</span><br><span class="line">1028</span><br><span class="line">1029</span><br><span class="line">1030</span><br><span class="line">1031</span><br><span class="line">1032</span><br><span class="line">1033</span><br><span class="line">1034</span><br><span class="line">1035</span><br><span class="line">1036</span><br><span class="line">1037</span><br><span class="line">1038</span><br><span class="line">1039</span><br><span class="line">1040</span><br><span class="line">1041</span><br><span class="line">1042</span><br><span class="line">1043</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- ------------------------------------------------------数据库的操作------------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 展示所有的数据库</span></span><br><span class="line"><span class="keyword">show</span> databases;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询当前的数据库</span></span><br><span class="line"><span class="keyword">select</span> database();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建数据库</span></span><br><span class="line"><span class="keyword">create</span> database if <span class="keyword">not</span> <span class="keyword">exists</span> itcast;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据库</span></span><br><span class="line"><span class="keyword">drop</span> database if <span class="keyword">exists</span> itcast;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用数据库</span></span><br><span class="line">use itcast;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ----------------------------------------------------------表的操作--------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> gender <span class="type">varchar</span>(<span class="number">1</span>) comment <span class="string">'性别'</span></span><br><span class="line">) comment <span class="string">'用户表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显示创建表的结构</span></span><br><span class="line"><span class="keyword">desc</span> tb_user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显示创建表的语句</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> tb_user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表的操作</span></span><br><span class="line"><span class="comment">-- 举例:现有一张员工表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> workno <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'工号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> idcard <span class="type">char</span>(<span class="number">18</span>) comment <span class="string">'身份证号'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"><span class="comment">-- 查看表结构</span></span><br><span class="line"><span class="keyword">desc</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表结构(向emp表中添加昵称字段)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">add</span> nickname <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'昵称'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改相应字段的数据类型(把age的数据类型从int改回tinyint)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> modify age tinyint;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改字段名和字段的类型(把nickname字段改为username,并修改数据类型)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> change nickname username <span class="type">varchar</span>(<span class="number">30</span>) comment <span class="string">'用户名'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除字段(删除username字段)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">drop</span> username;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表名(将表名字从emo修改为employee)</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp rename <span class="keyword">to</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> employee;</span><br><span class="line"><span class="comment">-- 删除指定表,并重新创建该表(相当于删除表中的全部数据)</span></span><br><span class="line"><span class="keyword">truncate</span> <span class="keyword">table</span> employee;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ------------------------------------------------DML-增删改操作---------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 添加数据</span></span><br><span class="line"><span class="keyword">select</span> database();</span><br><span class="line"><span class="keyword">show</span> tables;</span><br><span class="line"><span class="comment">-- 该指定的字段添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee(id, workno, name, gender, age, idcard, entrydate) <span class="keyword">value</span> (<span class="number">1</span>, <span class="string">'1'</span>, <span class="string">'小华'</span>, <span class="string">'男'</span>, <span class="number">20</span>, <span class="string">'123456789123456789'</span>, <span class="string">'2001-01-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> employee;</span><br><span class="line"><span class="comment">-- 给全部的字段添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee <span class="keyword">value</span> (<span class="number">2</span>, <span class="string">'2'</span>, <span class="string">'小刚'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>);</span><br><span class="line"><span class="comment">-- 批量添加数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> employee</span><br><span class="line"><span class="keyword">values</span> (<span class="number">3</span>, <span class="string">'3'</span>, <span class="string">'小李'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'4'</span>, <span class="string">'小黄'</span>, <span class="string">'男'</span>, <span class="number">23</span>, <span class="string">'123456784124456789'</span>, <span class="string">'2008-01-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改数据(不带条件修改所有)</span></span><br><span class="line"><span class="keyword">update</span> employee</span><br><span class="line"><span class="keyword">set</span> name <span class="operator">=</span> <span class="string">'张三'</span></span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">update</span> employee</span><br><span class="line"><span class="keyword">set</span> name <span class="operator">=</span> <span class="string">'李四'</span>,</span><br><span class="line"> gender<span class="operator">=</span> <span class="string">'女'</span></span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除数据(删除id为4的数据,不带添加删除所有)</span></span><br><span class="line"><span class="keyword">delete</span></span><br><span class="line"><span class="keyword">from</span> employee</span><br><span class="line"><span class="keyword">where</span> id <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- --------------------------------------------------------DQL数据查询语言--------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'编号'</span>,</span><br><span class="line"> workno <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'工号'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> idcard <span class="type">char</span>(<span class="number">18</span>) comment <span class="string">'身份证号'</span>,</span><br><span class="line"> workaddress <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'工作地址'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 插入数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> emp(id, workno, name, gender, age, idcard, workaddress, entrydate)</span><br><span class="line"><span class="keyword">values</span> (<span class="number">1</span>, <span class="string">'1'</span>, <span class="string">'柳岩'</span>, <span class="string">'女'</span>, <span class="number">20</span>, <span class="string">'12345678912345678'</span>, <span class="string">'北京'</span>, <span class="string">'2001-01-01'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'2'</span>, <span class="string">'张无忌'</span>, <span class="string">'男'</span>, <span class="number">18</span>, <span class="string">'123456789012345670'</span>, <span class="string">'北京'</span>, <span class="string">'2005-09-01'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'3'</span>, <span class="string">'韦一笑'</span>, <span class="string">'男'</span>, <span class="number">38</span>, <span class="string">'12345678972345670'</span>, <span class="string">'上海'</span>, <span class="string">'2005-08-01'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'4'</span>, <span class="string">'赵敏'</span>, <span class="string">'女'</span>, <span class="number">18</span>, <span class="string">'12345675712345670'</span>, <span class="string">'北京'</span>, <span class="string">'2009-12-01'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'5'</span>, <span class="string">'小昭'</span>, <span class="string">'女'</span>, <span class="number">16</span>, <span class="string">'123456769012345678'</span>, <span class="string">'上海'</span>, <span class="string">'2007-07-01'</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'6'</span>, <span class="string">'杨逍'</span>, <span class="string">'男'</span>, <span class="number">28</span>, <span class="string">'1234567893123456X'</span>, <span class="string">'北京'</span>, <span class="string">'2006-01-01'</span>),</span><br><span class="line"> (<span class="number">7</span>, <span class="string">'7'</span>, <span class="string">'范瑶'</span>, <span class="string">'男'</span>, <span class="number">40</span>, <span class="string">'123456789212345670'</span>, <span class="string">'北京'</span>, <span class="string">'2005-05-01'</span>),</span><br><span class="line"> (<span class="number">8</span>, <span class="string">'8'</span>, <span class="string">'黛绮丝'</span>, <span class="string">'女'</span>, <span class="number">38</span>, <span class="string">'123456157123645670'</span>, <span class="string">'天津'</span>, <span class="string">'2015-05-01'</span>),</span><br><span class="line"> (<span class="number">9</span>, <span class="string">'9'</span>, <span class="string">'范凉凉'</span>, <span class="string">'女'</span>, <span class="number">45</span>, <span class="string">'123156789012345678'</span>, <span class="string">'北京'</span>, <span class="string">'2010-04-01'</span>),</span><br><span class="line"> (<span class="number">10</span>, <span class="string">'10'</span>, <span class="string">'陈友谅'</span>, <span class="string">'男'</span>, <span class="number">53</span>, <span class="string">'123456789012345670'</span>, <span class="string">'上海'</span>, <span class="string">'2011-01-01'</span>),</span><br><span class="line"> (<span class="number">11</span>, <span class="string">'11'</span>, <span class="string">'张士诚'</span>, <span class="string">'男'</span>, <span class="number">55</span>, <span class="string">'12356789712345670'</span>, <span class="string">'江苏'</span>, <span class="string">'2015-05-01'</span>),</span><br><span class="line"> (<span class="number">12</span>, <span class="string">'12'</span>, <span class="string">'常遇春'</span>, <span class="string">'男'</span>, <span class="number">32</span>, <span class="string">'123446757152345670'</span>, <span class="string">'北京'</span>, <span class="string">'2004-02-01'</span>),</span><br><span class="line"> (<span class="number">13</span>, <span class="string">'13'</span>, <span class="string">'张三丰'</span>, <span class="string">'男'</span>, <span class="number">88</span>, <span class="string">'123656789012345678'</span>, <span class="string">'江苏'</span>, <span class="string">'2020-11-01'</span>),</span><br><span class="line"> (<span class="number">14</span>, <span class="string">'14'</span>, <span class="string">'灭绝'</span>, <span class="string">'女'</span>, <span class="number">65</span>, <span class="string">'123456719012345670'</span>, <span class="string">'西安'</span>, <span class="string">'2019-05-01'</span>),</span><br><span class="line"> (<span class="number">15</span>, <span class="string">'15'</span>, <span class="string">'胡青年'</span>, <span class="string">'男'</span>, <span class="number">70</span>, <span class="string">'12345674971234567X'</span>, <span class="string">'西安'</span>, <span class="string">'2018-04-01'</span>),</span><br><span class="line"> (<span class="number">16</span>, <span class="string">'16'</span>, <span class="string">'周芷若'</span>, <span class="string">'女'</span>, <span class="number">18</span>, <span class="keyword">null</span>, <span class="string">'北京'</span>, <span class="string">'2012-06-01'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 基础查询</span></span><br><span class="line"><span class="comment">-- 查看插入的数据</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询指定字段 name workno age 返回</span></span><br><span class="line"><span class="keyword">select</span> name, workno, age</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有字段返回</span></span><br><span class="line"><span class="keyword">select</span> id,</span><br><span class="line"> workno,</span><br><span class="line"> name,</span><br><span class="line"> gender,</span><br><span class="line"> age,</span><br><span class="line"> idcard,</span><br><span class="line"> workaddress,</span><br><span class="line"> entrydate</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- distinct去重关键字</span></span><br><span class="line"><span class="comment">-- 查询所有员工的工作地址(不要重复的地址)</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> workaddress <span class="string">'工作地址'</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 条件查询</span></span><br><span class="line"><span class="comment">-- 查询年龄等于 88 的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">=</span> <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于20的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator"><</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于等于20的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询没有身份证号的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> idcard <span class="keyword">is</span> <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄不等于88的员工信息(<>也表示不等于)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">!=</span> <span class="number">88</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator"><></span> <span class="number">88</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄在15~20(包含)之间的员工信息(三种实现方式)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">>=</span> <span class="number">15</span> <span class="operator">&&</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">>=</span> <span class="number">15</span></span><br><span class="line"> <span class="keyword">and</span> age <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="keyword">between</span> <span class="number">15</span> <span class="keyword">and</span> <span class="number">20</span>;</span><br><span class="line">#包含了<span class="number">15</span>和<span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为女 且年龄小于25岁的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'女'</span></span><br><span class="line"> <span class="keyword">and</span> age <span class="operator"><</span> <span class="number">25</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄等于18 或 20 或 48 的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">=</span> <span class="number">18</span></span><br><span class="line"> <span class="keyword">or</span> age <span class="operator">=</span> <span class="number">20</span></span><br><span class="line"> <span class="keyword">or</span> age <span class="operator">=</span> <span class="number">48</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="keyword">in</span> (<span class="number">18</span>, <span class="number">20</span>, <span class="number">48</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询姓名为两个字的员工(模糊匹配(_匹配单个字符, %匹配任意个字符))</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> name <span class="keyword">like</span> <span class="string">'__'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询身份证号最后一位是X的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> idcard <span class="keyword">like</span> <span class="string">'%X'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 聚合函数</span></span><br><span class="line"><span class="comment">-- 统计该企业员工数量(空值是不参与统计的)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>)</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(idcard)</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的平均年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(age)</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最大年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">max</span>(age)</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计该企业员工的最小年龄</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">min</span>(age)</span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计西安地区员工的年龄之和</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">sum</span>(age)</span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> workaddress <span class="operator">=</span> <span class="string">'西安'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 分组查询</span></span><br><span class="line"><span class="comment">-- 根据性别分组,统计男性员工和女性员工的数量</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">count</span>(<span class="operator">*</span>)</span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据性别进行分组,统计男性员工和女性员工的平均年龄</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">avg</span>(age)</span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于45岁的员工,并根据工作地址分组,获取员工数量大于等于3的工作地址(可以使用别名也可以不使用别名)</span></span><br><span class="line"><span class="keyword">select</span> workaddress, <span class="built_in">count</span>(<span class="operator">*</span>) address_count</span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator"><</span> <span class="number">45</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> workaddress</span><br><span class="line"><span class="keyword">having</span> address_count <span class="operator">>=</span> <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 排序查询</span></span><br><span class="line"><span class="comment">-- 根据年龄对公司的员工进行升序排序(asc可以省略)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> age;</span><br><span class="line"><span class="comment">-- 降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据员工的入职时间降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> entrydate <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 根据员工的年龄升序排序,年龄相同,再按照入职时间进行降序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">asc</span>, entrydate <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 分页查询</span></span><br><span class="line"><span class="comment">-- 查询第一页的员工数据,每页显示10条记录</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line">limit <span class="number">0</span>,<span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询第二页数据,每页展示十条数据(起始索引 = (查询页码 - 1)* 每页显示记录数)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line">limit <span class="number">10</span>,<span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- DQL语句案例</span></span><br><span class="line"><span class="comment">-- 查询年龄在20,21,22,23岁的女性员工的信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'女'</span></span><br><span class="line"> <span class="keyword">and</span> age <span class="keyword">in</span> (<span class="number">20</span>, <span class="number">21</span>, <span class="number">22</span>, <span class="number">23</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为男,并且年龄再20~40岁(含)以内的姓名为三个字的员工</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'男'</span></span><br><span class="line"> <span class="keyword">and</span> (age <span class="keyword">between</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span>)</span><br><span class="line"> <span class="keyword">and</span> name <span class="keyword">like</span> <span class="string">'___'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计员工表中,年龄小于60岁的,男性员工和女性员工的人数</span></span><br><span class="line"><span class="keyword">select</span> gender, <span class="built_in">count</span>(<span class="operator">*</span>)</span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator"><</span> <span class="number">60</span></span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按照入职时间降序排序</span></span><br><span class="line"><span class="keyword">select</span> name, age</span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">35</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> age <span class="keyword">asc</span>, entrydate <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别为男,且年龄在20·40岁(含)以内的前五个员工信息,并对查询结果按年龄升序排序,如果年龄相同按照入职时间升序排序</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> gender <span class="operator">=</span> <span class="string">'男'</span></span><br><span class="line"> <span class="keyword">and</span> age <span class="keyword">between</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> age, entrydate</span><br><span class="line">limit <span class="number">5</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ---------------------------------------------------DCL--------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 用户管理</span></span><br><span class="line"><span class="comment">-- mysql数据库</span></span><br><span class="line">use mysql;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有的用户</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> mysql.user;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户itcast, 只能够在当前主机localhost访问, 密码123456;</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">'itcast'</span>@<span class="string">'localhost'</span> identified <span class="keyword">by</span> <span class="string">'123456'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户heima, 可以在任意主机访问该数据库, 密码123456;</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">user</span> <span class="string">'heima'</span>@<span class="string">'%'</span> identified <span class="keyword">by</span> <span class="string">'123456'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改用户heima的访问密码为1234;</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">user</span> <span class="string">'heima'</span>@<span class="string">'%'</span> identified <span class="keyword">with</span> mysql_native_password <span class="keyword">by</span> <span class="string">'1234'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除 itcast@localhost 用户</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">user</span> <span class="string">'itcast'</span>@<span class="string">'localhost'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 权限控制</span></span><br><span class="line"><span class="comment">-- 查询权限(查询heima用户的所有权限)</span></span><br><span class="line"><span class="keyword">show</span> grants <span class="keyword">for</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予权限(授予heima用户对数据库itcast的所有权限)</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> itcast.<span class="operator">*</span> <span class="keyword">to</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予用户所有数据库所有表的所有权限</span></span><br><span class="line"><span class="keyword">grant</span> <span class="keyword">all</span> <span class="keyword">on</span> <span class="operator">*</span>.<span class="operator">*</span> <span class="keyword">to</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 撤销权限(撤销heima用户在itcast数据库上的所有权限)</span></span><br><span class="line"><span class="keyword">revoke</span> <span class="keyword">all</span> <span class="keyword">on</span> itcast.<span class="operator">*</span> <span class="keyword">from</span> <span class="string">'heima'</span>@<span class="string">'%'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ----------------------------------------------------------函数--------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 字符串函数</span></span><br><span class="line"><span class="comment">-- concat函数(字符串拼接)</span></span><br><span class="line"><span class="keyword">select</span> concat(<span class="string">'hello '</span>, <span class="string">'word'</span>);</span><br><span class="line"># hello word</span><br><span class="line"></span><br><span class="line"><span class="comment">-- lower(全部转化成小写)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">lower</span>(<span class="string">'Hello'</span>);</span><br><span class="line"># hello</span><br><span class="line"></span><br><span class="line"><span class="comment">-- upper(全部转化成大写)</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">upper</span>(<span class="string">'Hello'</span>);</span><br><span class="line"># HELLO</span><br><span class="line"></span><br><span class="line"><span class="comment">-- lpad(左侧填充)</span></span><br><span class="line"><span class="keyword">select</span> lpad(<span class="string">'01'</span>, <span class="number">5</span>, <span class="string">'-'</span>);</span><br><span class="line"># <span class="comment">---01</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- rpad(右侧填充)</span></span><br><span class="line"><span class="keyword">select</span> rpad(<span class="string">'01'</span>, <span class="number">5</span>, <span class="string">'-'</span>);</span><br><span class="line"># <span class="number">01</span><span class="comment">---</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- trim 去除头部和尾部的空格</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">trim</span>(<span class="string">' Hello Mysql '</span>);</span><br><span class="line"># Hello Mysql</span><br><span class="line"></span><br><span class="line"><span class="comment">-- substring 字符串截取</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">substring</span>(<span class="string">'Hello Mysql'</span>, <span class="number">1</span>, <span class="number">5</span>);</span><br><span class="line"># Hello</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0 比如:1号员工的工号应该为00001</span></span><br><span class="line"><span class="keyword">update</span> emp</span><br><span class="line"><span class="keyword">set</span> workno <span class="operator">=</span> lpad(workno, <span class="number">5</span>, <span class="string">'0'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 数值函数</span></span><br><span class="line"><span class="comment">-- ceil 向上取整</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">ceil</span>(<span class="number">1.5</span>);</span><br><span class="line"># <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- floor 向下取整</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">floor</span>(<span class="number">1.1</span>);</span><br><span class="line"># <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- mod 求模运算</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">mod</span>(<span class="number">7</span>, <span class="number">4</span>);</span><br><span class="line"># <span class="number">3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- rand 随机数(0~1)</span></span><br><span class="line"><span class="keyword">select</span> rand();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- round 四舍五入</span></span><br><span class="line"><span class="keyword">select</span> round(<span class="number">2.345</span>, <span class="number">2</span>);</span><br><span class="line"># <span class="number">2.35</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 通过数据库函数生成一个的随机的六位验证码</span></span><br><span class="line"><span class="keyword">select</span> lpad(round(rand() <span class="operator">*</span> <span class="number">1000000</span>, <span class="number">0</span>), <span class="number">6</span>, <span class="string">'0'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 日期函数</span></span><br><span class="line"><span class="comment">-- curdate:当前日期</span></span><br><span class="line"><span class="keyword">select</span> curdate();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- curtime:当前时间</span></span><br><span class="line"><span class="keyword">select</span> curtime();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- now:当前日期和时间</span></span><br><span class="line"><span class="keyword">select</span> now();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- YEAR , MONTH , DAY:当前年、月、日</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">year</span>(now());</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">month</span>(now());</span><br><span class="line"><span class="keyword">select</span> <span class="keyword">day</span>(now());</span><br><span class="line"></span><br><span class="line"><span class="comment">-- date_add:增加指定的时间间隔</span></span><br><span class="line"><span class="keyword">select</span> date_add(now(), <span class="type">INTERVAL</span> <span class="number">70</span> <span class="keyword">DAY</span>); # 当前的时间往后推<span class="number">70</span>天</span><br><span class="line"><span class="keyword">select</span> date_add(now(), <span class="type">INTERVAL</span> <span class="number">2</span> <span class="keyword">MONTH</span>);</span><br><span class="line">#向后推<span class="number">2</span>个月</span><br><span class="line"></span><br><span class="line"><span class="comment">-- datediff:获取两个日期相差的天数</span></span><br><span class="line"><span class="keyword">select</span> datediff(<span class="string">'2021-12-01'</span>, <span class="string">'2021-10-01'</span>);</span><br><span class="line"># 查询两个时间之间的差值,第一个时间减去第二个时间</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有员工的入职天数,并根据入职天数倒叙排序</span></span><br><span class="line"><span class="keyword">select</span> name, datediff(curdate(), entrydate) <span class="string">'entrydays'</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> entrydays <span class="keyword">desc</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 流程控制函数</span></span><br><span class="line"><span class="comment">-- if 相当java中的三元运算符</span></span><br><span class="line"><span class="keyword">select</span> if(<span class="literal">true</span>, <span class="string">'OK'</span>, <span class="string">'ERROR'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- ifnull 第一个字符串不为空就返回第一个,第一个为空就返回第二个</span></span><br><span class="line"><span class="keyword">select</span> ifnull(<span class="string">'OK'</span>, <span class="string">'DEFAULT'</span>);</span><br><span class="line"><span class="keyword">select</span> ifnull(<span class="keyword">null</span>, <span class="string">'DEFAULT'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- case when then else end</span></span><br><span class="line"><span class="comment">-- 需求: 查询emp表的员工姓名和工作地址 (北京/上海 ----> 一线城市 , 其他 ----> 二线城市)</span></span><br><span class="line"><span class="keyword">select</span> name,</span><br><span class="line"> (<span class="keyword">case</span> workaddress</span><br><span class="line"> <span class="keyword">when</span> <span class="string">'北京'</span> <span class="keyword">then</span> <span class="string">'一线城市'</span></span><br><span class="line"> <span class="keyword">when</span> <span class="string">'上海'</span> <span class="keyword">then</span> <span class="string">'一线城市'</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="string">'二线城市'</span> <span class="keyword">end</span>) <span class="keyword">as</span> <span class="string">'工作地址'</span></span><br><span class="line"><span class="keyword">from</span> emp;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 案例:统计班级各个学员的成绩,展示规则如下:</span></span><br><span class="line"><span class="comment">-- >= 85 展示优秀</span></span><br><span class="line"><span class="comment">-- >= 60 展示及格</span></span><br><span class="line"><span class="comment">-- 否则展示不及格</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 建表数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> score</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> comment <span class="string">'ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> math <span class="type">int</span> comment <span class="string">'数学'</span>,</span><br><span class="line"> english <span class="type">int</span> comment <span class="string">'英语'</span>,</span><br><span class="line"> chinese <span class="type">int</span> comment <span class="string">'语文'</span></span><br><span class="line">) comment <span class="string">'学员成绩表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> score(id, name, math, english, chinese)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'Tom'</span>, <span class="number">67</span>, <span class="number">88</span>, <span class="number">95</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'Rose'</span>, <span class="number">23</span>, <span class="number">66</span>, <span class="number">90</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'Jack'</span>, <span class="number">56</span>, <span class="number">98</span>, <span class="number">76</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> score;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 案例sql实现</span></span><br><span class="line"><span class="keyword">select</span> id,</span><br><span class="line"> name,</span><br><span class="line"> (<span class="keyword">case</span> <span class="keyword">when</span> math <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span> <span class="keyword">when</span> math <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span> <span class="keyword">else</span> <span class="string">'不及格'</span> <span class="keyword">end</span>)</span><br><span class="line"> <span class="string">'数学'</span>,</span><br><span class="line"> (<span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> english <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span></span><br><span class="line"> <span class="keyword">when</span> english <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span></span><br><span class="line"> <span class="keyword">else</span> <span class="string">'不及格'</span></span><br><span class="line"> <span class="keyword">end</span>) <span class="string">'英语'</span>,</span><br><span class="line"> (<span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> chinese <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span> <span class="string">'优秀'</span></span><br><span class="line"> <span class="keyword">when</span> chinese <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span> <span class="string">'及格'</span></span><br><span class="line"> <span class="keyword">else</span> <span class="string">'不及格'</span></span><br><span class="line"> <span class="keyword">end</span>) <span class="string">'语文'</span></span><br><span class="line"><span class="keyword">from</span> score;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ---------------------------------------------------约束演示------------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- id 主键,并且自动增长</span></span><br><span class="line"><span class="comment">-- name 不为空,并且唯一</span></span><br><span class="line"><span class="comment">-- age 大于零,并且小于等于120(8.0以上的数据库才支持)age int check (age > 0 && age <= 120) COMMENT '年龄'</span></span><br><span class="line"><span class="comment">-- status 如果没有指定该值,默认为1</span></span><br><span class="line"><span class="comment">-- gender 无约束</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> <span class="keyword">user</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> <span class="keyword">user</span></span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> AUTO_INCREMENT <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'ID唯一标识'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span> COMMENT <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> COMMENT <span class="string">'年龄'</span>,</span><br><span class="line"> status <span class="type">char</span>(<span class="number">1</span>) <span class="keyword">default</span> <span class="string">'1'</span> COMMENT <span class="string">'状态'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) COMMENT <span class="string">'性别'</span></span><br><span class="line">);</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> <span class="keyword">user</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 插入数据</span></span><br><span class="line"><span class="comment">-- name的值不能重复,status的值没有填写的话,将使用的是默认的值</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name, age, status, gender)</span><br><span class="line"><span class="keyword">values</span> (<span class="string">'Tom1'</span>, <span class="number">19</span>, <span class="string">'1'</span>, <span class="string">'男'</span>),</span><br><span class="line"> (<span class="string">'Tom2'</span>, <span class="number">29</span>, <span class="string">'1'</span>, <span class="string">'男'</span>),</span><br><span class="line"> (<span class="string">'Tom3'</span>, <span class="number">14</span>, <span class="string">'1'</span>, <span class="string">'男'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 外键约束</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> employee</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> employee (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> employee;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键 员工表employee的部门id字段关联到部门表的id字段以建立外键约束</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> employee</span><br><span class="line"> <span class="keyword">drop</span> <span class="keyword">foreign</span> key fk_emp_dept;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ---------------------------------------------多表查询--------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 多表关系</span></span><br><span class="line"><span class="comment">-- 演示多对多的关系</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> <span class="keyword">no</span> <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'学号'</span></span><br><span class="line">) comment <span class="string">'学生表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'黛绮丝'</span>, <span class="string">'2000100101'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'谢逊'</span>,</span><br><span class="line"> <span class="string">'2000100102'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'殷天正'</span>, <span class="string">'2000100103'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'韦一笑'</span>, <span class="string">'2000100104'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> course</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'课程名称'</span></span><br><span class="line">) comment <span class="string">'课程表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'Java'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'PHP'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'MySQL'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'Hadoop'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student_course</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'主键'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> studentid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'学生ID'</span>,</span><br><span class="line"> courseid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'课程ID'</span>,</span><br><span class="line"> <span class="keyword">constraint</span> fk_courseid <span class="keyword">foreign</span> key (courseid) <span class="keyword">references</span> course (id),</span><br><span class="line"> <span class="keyword">constraint</span> fk_studentid <span class="keyword">foreign</span> key (studentid) <span class="keyword">references</span> student (id)</span><br><span class="line">) comment <span class="string">'学生课程中间表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student_course</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">2</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">2</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 演示一对一的关系</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'1: 男 , 2: 女'</span>,</span><br><span class="line"> phone <span class="type">char</span>(<span class="number">11</span>) comment <span class="string">'手机号'</span></span><br><span class="line">) comment <span class="string">'用户基本信息表'</span>;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user_edu</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line"> degree <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'学历'</span>,</span><br><span class="line"> major <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'专业'</span>,</span><br><span class="line"> primaryschool <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'小学'</span>,</span><br><span class="line"> middleschool <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'中学'</span>,</span><br><span class="line"> university <span class="type">varchar</span>(<span class="number">50</span>) comment <span class="string">'大学'</span>,</span><br><span class="line"> userid <span class="type">int</span> <span class="keyword">unique</span> comment <span class="string">'用户ID'</span>,</span><br><span class="line"> <span class="keyword">constraint</span> fk_userid <span class="keyword">foreign</span> key (userid) <span class="keyword">references</span> tb_user (id)</span><br><span class="line">) comment <span class="string">'用户教育信息表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user(id, name, age, gender, phone)</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'黄渤'</span>, <span class="number">45</span>, <span class="string">'1'</span>, <span class="string">'18800001111'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'冰冰'</span>, <span class="number">35</span>, <span class="string">'2'</span>, <span class="string">'18800002222'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'码云'</span>, <span class="number">55</span>, <span class="string">'1'</span>, <span class="string">'18800008888'</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'李彦宏'</span>, <span class="number">50</span>, <span class="string">'1'</span>, <span class="string">'18800009999'</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user_edu(id, degree, major, primaryschool, middleschool,</span><br><span class="line"> university, userid)</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'舞蹈'</span>, <span class="string">'静安区第一小学'</span>, <span class="string">'静安区第一中学'</span>, <span class="string">'北京舞蹈学院'</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'硕士'</span>, <span class="string">'表演'</span>, <span class="string">'朝阳区第一小学'</span>, <span class="string">'朝阳区第一中学'</span>, <span class="string">'北京电影学院'</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'英语'</span>, <span class="string">'杭州市第一小学'</span>, <span class="string">'杭州市第一中学'</span>, <span class="string">'杭州师范大学'</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'应用数学'</span>, <span class="string">'阳泉第一小学'</span>, <span class="string">'阳泉区第一中学'</span>, <span class="string">'清华大学'</span>, <span class="number">4</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 演示多表查询</span></span><br><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="comment">-- 创建dept表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> dept</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'部门名称'</span></span><br><span class="line">) comment <span class="string">'部门表'</span>;</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (id, name)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'研发部'</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'市场部'</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'财务部'</span>),</span><br><span class="line"> (<span class="number">4</span>,</span><br><span class="line"> <span class="string">'销售部'</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'总经办'</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'人事部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建emp表,并插入数据</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> emp</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> auto_increment comment <span class="string">'ID'</span> <span class="keyword">primary</span> key,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'姓名'</span>,</span><br><span class="line"> age <span class="type">int</span> comment <span class="string">'年龄'</span>,</span><br><span class="line"> job <span class="type">varchar</span>(<span class="number">20</span>) comment <span class="string">'职位'</span>,</span><br><span class="line"> salary <span class="type">int</span> comment <span class="string">'薪资'</span>,</span><br><span class="line"> entrydate <span class="type">date</span> comment <span class="string">'入职时间'</span>,</span><br><span class="line"> managerid <span class="type">int</span> comment <span class="string">'直属领导ID'</span>,</span><br><span class="line"> dept_id <span class="type">int</span> comment <span class="string">'部门ID'</span></span><br><span class="line">) comment <span class="string">'员工表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加外键</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> emp</span><br><span class="line"> <span class="keyword">add</span> <span class="keyword">constraint</span> fk_emp_dept_id <span class="keyword">foreign</span> key (dept_id) <span class="keyword">references</span></span><br><span class="line"> dept (id);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp (id, name, age, job, salary, entrydate, managerid, dept_id)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">null</span>, <span class="number">5</span>),</span><br><span class="line"> (<span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">3</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">5</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">6</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<span class="number">7</span>, <span class="string">'灭绝'</span>, <span class="number">60</span>, <span class="string">'财务总监'</span>, <span class="number">8500</span>, <span class="string">'2002-09-12'</span>, <span class="number">1</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">8</span>, <span class="string">'周芷若'</span>, <span class="number">19</span>, <span class="string">'会计'</span>, <span class="number">48000</span>, <span class="string">'2006-06-02'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">9</span>, <span class="string">'丁敏君'</span>, <span class="number">23</span>, <span class="string">'出纳'</span>, <span class="number">5250</span>, <span class="string">'2009-05-13'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<span class="number">10</span>, <span class="string">'赵敏'</span>, <span class="number">20</span>, <span class="string">'市场部总监'</span>, <span class="number">12500</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">11</span>, <span class="string">'鹿杖客'</span>, <span class="number">56</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2006-10-03'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">12</span>, <span class="string">'鹤笔翁'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2007-05-09'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">13</span>, <span class="string">'方东白'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">5500</span>, <span class="string">'2009-02-12'</span>, <span class="number">10</span>, <span class="number">2</span>),</span><br><span class="line"> (<span class="number">14</span>, <span class="string">'张三丰'</span>, <span class="number">88</span>, <span class="string">'销售总监'</span>, <span class="number">14000</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">15</span>, <span class="string">'俞莲舟'</span>, <span class="number">38</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">16</span>, <span class="string">'宋远桥'</span>, <span class="number">40</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span>),</span><br><span class="line"> (<span class="number">17</span>, <span class="string">'陈友谅'</span>, <span class="number">42</span>, <span class="keyword">null</span>, <span class="number">2000</span>, <span class="string">'2011-10-12'</span>, <span class="number">1</span>, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 多表查询</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp,</span><br><span class="line"> dept</span><br><span class="line"><span class="keyword">where</span> emp.dept_id <span class="operator">=</span> dept.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 内连接演示</span></span><br><span class="line"><span class="comment">-- 案例</span></span><br><span class="line"><span class="comment">-- 查询每一个员工的姓名 , 及关联的部门的名称 (隐式内连接实现)</span></span><br><span class="line"><span class="comment">-- 表结构: emp , dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name, d.name</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询每一个员工的姓名 , 及关联的部门的名称 (显式内连接实现) --- INNER JOIN ...ON ...</span></span><br><span class="line"><span class="comment">-- 表结构: emp , dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">inner</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 外连接</span></span><br><span class="line"><span class="comment">-- 查询emp表的所有数据, 和对应的部门信息</span></span><br><span class="line"><span class="comment">-- 由于需求中提到,要查询emp的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询。</span></span><br><span class="line"><span class="comment">-- 表结构: emp, dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询dept表的所有数据, 和对应的员工信息(右外连接)</span></span><br><span class="line"><span class="comment">-- 由于需求中提到,要查询dept表的所有数据,所以是不能内连接查询的,需要考虑使用外连接查询</span></span><br><span class="line"><span class="comment">-- 表结构: emp, dept</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.dept_id = dept.id</span></span><br><span class="line"><span class="keyword">select</span> e.name, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">right</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 自连接</span></span><br><span class="line"><span class="comment">-- 查询员工 及其 所属领导的名字</span></span><br><span class="line"><span class="comment">-- 表结构: emp</span></span><br><span class="line"><span class="comment">-- 技巧:查询的时候看作是两张表</span></span><br><span class="line"><span class="keyword">select</span> e1.name <span class="string">'员工'</span>, e2.name <span class="string">'领导'</span></span><br><span class="line"><span class="keyword">from</span> emp e1,</span><br><span class="line"> emp e2</span><br><span class="line"><span class="keyword">where</span> e1.managerid <span class="operator">=</span> e2.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有员工 emp 及其领导的名字 emp , 如果员工没有领导, 也需要查询出来</span></span><br><span class="line"><span class="comment">-- 表结构: emp a , emp b</span></span><br><span class="line"><span class="keyword">select</span> e1.name <span class="string">'员工'</span>, e2.name <span class="string">'领导'</span></span><br><span class="line"><span class="keyword">from</span> emp e1</span><br><span class="line"> <span class="keyword">left</span> <span class="keyword">join</span> emp e2 <span class="keyword">on</span> e1.managerid <span class="operator">=</span> e2.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 联合查询</span></span><br><span class="line"><span class="comment">-- 将薪资低于 5000 的员工 , 和 年龄大于 50 岁的员工全部查询出来.</span></span><br><span class="line"><span class="comment">-- 当前对于这个需求,我们可以直接使用多条件查询,使用逻辑运算符 or 连接即可,也可以通过union/union all来联合查询</span></span><br><span class="line"><span class="comment">-- 相当于把下面的两条sql的结果拼接在一起(去掉all就可以实现去重)</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="operator"><</span> <span class="number">5000</span></span><br><span class="line"><span class="keyword">union</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">where</span> e.age <span class="operator">></span> <span class="number">50</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 子查询</span></span><br><span class="line"><span class="comment">-- 标量子查询</span></span><br><span class="line"><span class="comment">-- 1.查询 "销售部" 的所有员工信息</span></span><br><span class="line"><span class="comment">-- 1.1 查询销售部的部门id</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept d <span class="keyword">where</span> d.name <span class="operator">=</span> <span class="string">'销售部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询在 "方东白" 入职之后的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e1</span><br><span class="line"><span class="keyword">where</span> e1.entrydate <span class="operator">></span> (<span class="keyword">select</span> e2.entrydate <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.name <span class="operator">=</span> <span class="string">'方东白'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 列子查询</span></span><br><span class="line"><span class="comment">-- 查询 "销售部" 和 "市场部" 的所有员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="keyword">in</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'销售部'</span> <span class="keyword">or</span> dept.name <span class="operator">=</span> <span class="string">'市场部'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询比财务部所有人工资都高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="operator">></span> <span class="keyword">all</span> (<span class="keyword">select</span> e2.salary <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'财务部'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询比研发部其中任意一人工资高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="operator">></span> <span class="keyword">any</span> (<span class="keyword">select</span> e2.salary <span class="keyword">from</span> emp e2 <span class="keyword">where</span> e2.dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> dept.name <span class="operator">=</span> <span class="string">'研发部'</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 行子查询</span></span><br><span class="line"><span class="comment">-- 查询与"张无忌"的薪资及与其直属领导相同的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e2</span><br><span class="line"><span class="keyword">where</span> (e2.salary, e2.managerid) <span class="operator">=</span> (<span class="keyword">select</span> e.salary, e.managerid <span class="keyword">from</span> emp e <span class="keyword">where</span> e.name <span class="operator">=</span> <span class="string">'张无忌'</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 表子查询</span></span><br><span class="line"><span class="comment">-- 查询与 "鹿杖客" , "宋远桥" 的职位和薪资相同的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e2</span><br><span class="line"><span class="keyword">where</span> (e2.job, e2.salary) <span class="keyword">in</span> (<span class="keyword">select</span> e.job, e.salary <span class="keyword">from</span> emp e <span class="keyword">where</span> e.name <span class="operator">=</span> <span class="string">'鹿杖客'</span> <span class="keyword">or</span> name <span class="operator">=</span> <span class="string">'宋远桥'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询入职日期是 "2006-01-01" 之后的员工信息 , 及其部门信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> (<span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> entrydate <span class="operator">></span> <span class="string">'2006-01-01'</span>) e</span><br><span class="line"> <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 多表查询案例</span></span><br><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> salgrade</span><br><span class="line">(</span><br><span class="line"> grade <span class="type">int</span>,</span><br><span class="line"> losal <span class="type">int</span>,</span><br><span class="line"> hisal <span class="type">int</span></span><br><span class="line">) comment <span class="string">'薪资等级表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">1</span>, <span class="number">0</span>, <span class="number">3000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">2</span>, <span class="number">3001</span>, <span class="number">5000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">3</span>, <span class="number">5001</span>, <span class="number">8000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">4</span>, <span class="number">8001</span>, <span class="number">10000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">5</span>, <span class="number">10001</span>, <span class="number">15000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">6</span>, <span class="number">15001</span>, <span class="number">20000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">7</span>, <span class="number">20001</span>, <span class="number">25000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> salgrade</span><br><span class="line"><span class="keyword">values</span> (<span class="number">8</span>, <span class="number">25001</span>, <span class="number">30000</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 题解</span></span><br><span class="line"><span class="comment">-- 1.查询员工的姓名、年龄、职位、部门信息(隐式内连接)</span></span><br><span class="line"><span class="keyword">select</span> e.name, e.age, e.job, d.name</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 2.查询年龄小于30岁的员工的姓名、年龄、职位、部门信息(显式内连接)</span></span><br><span class="line"><span class="keyword">select</span> e.name, e.age, e.job, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">inner</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"><span class="keyword">where</span> e.age <span class="operator"><</span> <span class="number">30</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 3.查询拥有员工的部门id、部门名称</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> d.id, d.name</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept</span><br><span class="line"><span class="keyword">where</span> dept.id <span class="keyword">in</span> (<span class="keyword">select</span> e.dept_id <span class="keyword">from</span> emp e);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 4.查询所有年龄大于40岁的员工,及其归属的部门名称;如果没有员工分配部门,也需要展示出来</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, d.name</span><br><span class="line"><span class="keyword">from</span> emp e</span><br><span class="line"> <span class="keyword">left</span> <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"><span class="keyword">where</span> age <span class="operator">></span> <span class="number">40</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 5.查询所有员工的工资等级</span></span><br><span class="line"><span class="keyword">select</span> e.name <span class="string">'姓名'</span>, s.grade <span class="string">'工资等级'</span></span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> salgrade s</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 6.查询研发部所有员工的信息已经工资等级</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, s.grade</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d,</span><br><span class="line"> salgrade s</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"> <span class="keyword">and</span> (e.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal)</span><br><span class="line"> <span class="keyword">and</span> d.name <span class="operator">=</span> <span class="string">'研发部'</span>;</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, s.grade</span><br><span class="line"><span class="keyword">from</span> (<span class="keyword">select</span> emp.<span class="operator">*</span></span><br><span class="line"> <span class="keyword">from</span> emp,</span><br><span class="line"> dept</span><br><span class="line"> <span class="keyword">where</span> emp.dept_id <span class="operator">=</span> dept.id</span><br><span class="line"> <span class="keyword">and</span> dept.name <span class="operator">=</span> <span class="string">'研发部'</span>) e,</span><br><span class="line"> salgrade s</span><br><span class="line"><span class="keyword">where</span> e.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 7.查询研发部的平均工资</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(e.salary)</span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> dept d</span><br><span class="line"><span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"> <span class="keyword">and</span> d.name <span class="operator">=</span> <span class="string">'研发部'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 8.查询工资比灭绝高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> salary <span class="operator">></span> (<span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'灭绝'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 9.查询比平均工资高的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp</span><br><span class="line"><span class="keyword">where</span> salary <span class="operator">></span> (<span class="keyword">select</span> <span class="built_in">avg</span>(salary) <span class="keyword">from</span> emp);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 10.查询低于本部门平均工资的员工</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e2</span><br><span class="line"><span class="keyword">where</span> e2.salary <span class="operator"><</span> (<span class="keyword">select</span> <span class="built_in">avg</span>(e1.salary) <span class="keyword">from</span> emp e1 <span class="keyword">where</span> e1.dept_id <span class="operator">=</span> e2.dept_id);</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> emp e,</span><br><span class="line"> (<span class="keyword">select</span> dept_id, <span class="built_in">avg</span>(salary) <span class="string">'salavg'</span> <span class="keyword">from</span> emp <span class="keyword">group</span> <span class="keyword">by</span> dept_id) eavg</span><br><span class="line"><span class="keyword">where</span> eavg.dept_id <span class="operator">=</span> e.dept_id</span><br><span class="line"> <span class="keyword">and</span> e.salary <span class="operator"><</span> eavg.salavg;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 11.查询所有的部门信息,并统计部门的员工人数</span></span><br><span class="line"><span class="comment">-- 方法一</span></span><br><span class="line"><span class="keyword">select</span> d.id, d.name, (<span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id) <span class="string">'人数'</span></span><br><span class="line"><span class="keyword">from</span> dept d;</span><br><span class="line"><span class="comment">-- 方法二</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> dept d,</span><br><span class="line"> (<span class="keyword">select</span> e.dept_id, <span class="built_in">count</span>(<span class="operator">*</span>) <span class="string">'人数'</span> <span class="keyword">from</span> emp e <span class="keyword">group</span> <span class="keyword">by</span> e.dept_id) ecount</span><br><span class="line"><span class="keyword">where</span> d.id <span class="operator">=</span> ecount.dept_id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 12.查询所有学生的选课情况,展示出学生名称,学号,课程名称</span></span><br><span class="line"><span class="keyword">select</span> s.name, s.no, c.name</span><br><span class="line"><span class="keyword">from</span> course c,</span><br><span class="line"> student s,</span><br><span class="line"> student_course sc</span><br><span class="line"><span class="keyword">where</span> c.id <span class="operator">=</span> sc.courseid</span><br><span class="line"> <span class="keyword">and</span> s.id <span class="operator">=</span> sc.studentid;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- ----------------------------------------------------------事务--------------------------------------------------------------------</span></span><br><span class="line"><span class="comment">-- 数据准备</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> account;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> account</span><br><span class="line">(</span><br><span class="line"> id <span class="type">int</span> <span class="keyword">primary</span> key AUTO_INCREMENT comment <span class="string">'ID'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> money <span class="keyword">double</span>(<span class="number">10</span>, <span class="number">2</span>) comment <span class="string">'余额'</span></span><br><span class="line">) comment <span class="string">'账户表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> account(name, money)</span><br><span class="line"><span class="keyword">VALUES</span> (<span class="string">'张三'</span>, <span class="number">2000</span>),</span><br><span class="line"> (<span class="string">'李四'</span>, <span class="number">2000</span>);</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 转账操作(张三转1000给李四)</span></span><br><span class="line"><span class="comment">-- 1. 查询张三余额</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 2. 张三的余额减少1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">-</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 3. 李四的余额增加1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">+</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'李四'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 开启事务</span></span><br><span class="line"><span class="keyword">start</span> transaction;</span><br><span class="line"><span class="comment">-- 1. 查询张三余额</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 2. 张三的余额减少1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">-</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张三'</span>;</span><br><span class="line"><span class="comment">-- 3. 李四的余额增加1000</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money <span class="operator">=</span> money <span class="operator">+</span> <span class="number">1000</span> <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'李四'</span>;</span><br><span class="line"><span class="comment">-- 如果正常执行完毕, 则提交事务</span></span><br><span class="line"><span class="keyword">commit</span>;</span><br><span class="line"><span class="comment">-- 如果执行过程中报错, 则回滚事务</span></span><br><span class="line"><span class="comment">-- rollback;</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h1 id="二-MySql进阶"><a href="#二-MySql进阶" class="headerlink" title="二.MySql进阶"></a>二.MySql进阶</h1><h2 id="1-存储引擎"><a href="#1-存储引擎" class="headerlink" title="1.存储引擎"></a>1.存储引擎</h2><h3 id="1-1-MySql的体系结构"><a href="#1-1-MySql的体系结构" class="headerlink" title="1.1 MySql的体系结构"></a>1.1 MySql的体系结构</h3><p><img src="/pictures/image-20230916152706301.png" alt="image-20230916152706301"></p>
|
||
<p><img src="/pictures/image-20230916152828704.png" alt="image-20230916152828704"></p>
|
||
<h3 id="1-2-存储引擎简介"><a href="#1-2-存储引擎简介" class="headerlink" title="1.2 存储引擎简介"></a>1.2 存储引擎简介</h3><p><strong>简介</strong></p>
|
||
<p> 存储引擎就是存储数据、建立索引、更新/查询数据等技术的实现方式 。存储引擎是基于表的,而不是 基于库的,所以存储引擎也可被称为表类型。我们可以在创建表的时候,来指定选择的存储引擎,如果 没有指定将自动选择默认的存储引擎。</p>
|
||
<p><strong>查询一张数据库表的建表语句</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询建表语句</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">table</span> book_info;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><img src="/pictures/image-20230916153448893.png" alt="image-20230916153448893"></p>
|
||
<p><strong>建表的时候指定存储引擎的语句</strong></p>
|
||
<p><img src="/pictures/image-20230916153931010.png" alt="image-20230916153931010"></p>
|
||
<h3 id="1-3-存储引擎特点"><a href="#1-3-存储引擎特点" class="headerlink" title="1.3 存储引擎特点"></a>1.3 存储引擎特点</h3><p><img src="/pictures/image-20230916154915533.png" alt="image-20230916154915533"></p>
|
||
<p><img src="/pictures/image-20230916155113863.png" alt="image-20230916155113863"></p>
|
||
<p><img src="/pictures/image-20230916155356755.png" alt="image-20230916155356755"></p>
|
||
<p><img src="/pictures/image-20230916155525548.png" alt="image-20230916155525548"></p>
|
||
<p> <strong>存储引擎的区别</strong></p>
|
||
<p><img src="/pictures/image-20230916155637133.png" alt="image-20230916155637133"></p>
|
||
<h3 id="1-4-存储引擎的选择"><a href="#1-4-存储引擎的选择" class="headerlink" title="1.4 存储引擎的选择"></a>1.4 存储引擎的选择</h3><p> 在选择存储引擎时,应该根据应用系统的特点选择合适的存储引擎。对于复杂的应用系统,还可以根据 实际情况选择多种存储引擎进行组合。 </p>
|
||
<ul>
|
||
<li>InnoDB: 是Mysql的默认存储引擎,支持事务、外键。如果应用对事务的完整性有比较高的要 求,在并发条件下要求数据的一致性,数据操作除了插入和查询之外,还包含很多的更新、删除操 作,那么InnoDB存储引擎是比较合适的选择。 </li>
|
||
<li>MyISAM : 如果应用是以读操作和插入操作为主,只有很少的更新和删除操作,并且对事务的完 整性、并发性要求不是很高,那么选择这个存储引擎是非常合适的。(例如评论的数据、日志相关的数据、电商中足迹相关的数据)</li>
|
||
<li>MEMORY:将所有数据保存在内存中,访问速度快,通常用于临时表及缓存。MEMORY的缺陷就是 对表的大小有限制,太大的表无法缓存在内存中,而且无法保障数据的安全性。</li>
|
||
</ul>
|
||
<p>一句话:项目中绝大多数的时候使用的都是InnoDB</p>
|
||
<h2 id="2-索引"><a href="#2-索引" class="headerlink" title="2.索引"></a>2.索引</h2><h3 id="2-1-索引概述"><a href="#2-1-索引概述" class="headerlink" title="2.1 索引概述"></a>2.1 索引概述</h3><h4 id="2-1-1-介绍"><a href="#2-1-1-介绍" class="headerlink" title="2.1.1 介绍"></a>2.1.1 介绍</h4><p> 索引(index)是帮助MySQL高效获取数据的数据结构(有序)。在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据, 这样就可以在这些数据结构上实现高级查找算法,这种数据结构就是索引。</p>
|
||
<h4 id="2-1-2-演示"><a href="#2-1-2-演示" class="headerlink" title="2.1.2 演示"></a>2.1.2 演示</h4><p>注:下面的二叉树并不是一个真实的索引结构,而是一个举例</p>
|
||
<p><img src="/pictures/image-20230916150211012.png" alt="image-20230916150211012"></p>
|
||
<h4 id="2-1-3-优缺点"><a href="#2-1-3-优缺点" class="headerlink" title="2.1.3 优缺点"></a>2.1.3 优缺点</h4><p><img src="/pictures/image-20230916150430708.png" alt="image-20230916150430708"></p>
|
||
<h3 id="2-2-索引结构"><a href="#2-2-索引结构" class="headerlink" title="2.2 索引结构"></a>2.2 索引结构</h3><h4 id="2-2-1-索引的数据结构有哪些"><a href="#2-2-1-索引的数据结构有哪些" class="headerlink" title="2.2.1 索引的数据结构有哪些"></a>2.2.1 索引的数据结构有哪些</h4><p>默认的索引结构是B+树索引结构</p>
|
||
<p><img src="/pictures/image-20230916161524219.png" alt="image-20230916161524219"></p>
|
||
<p><strong>不同类型的存储引擎对不同的索引结构的支持情况</strong></p>
|
||
<p><img src="/pictures/image-20230916161623546.png" alt="image-20230916161623546"></p>
|
||
<h4 id="2-2-2-B-树的索引数据结构"><a href="#2-2-2-B-树的索引数据结构" class="headerlink" title="2.2.2 B+树的索引数据结构"></a>2.2.2 B+树的索引数据结构</h4><p><strong>二叉树和红黑树的不足</strong></p>
|
||
<p><img src="/pictures/image-20230916162305576.png" alt="image-20230916162305576"></p>
|
||
<p><strong>B树</strong></p>
|
||
<p><img src="/pictures/image-20230916163234103.png" alt="image-20230916163234103"></p>
|
||
<p><strong>B+树</strong></p>
|
||
<p><img src="/pictures/image-20230916164042303.png" alt="image-20230916164042303"></p>
|
||
<p><img src="/pictures/image-20230916164314700.png" alt="image-20230916164314700"></p>
|
||
<p><strong>MySql中的B+树结构</strong></p>
|
||
<p><img src="/pictures/image-20230916164500444.png" alt="image-20230916164500444"></p>
|
||
<h4 id="2-2-3-hash的索引数据结构"><a href="#2-2-3-hash的索引数据结构" class="headerlink" title="2.2.3 hash的索引数据结构"></a>2.2.3 hash的索引数据结构</h4><p><img src="/pictures/image-20230916164903702.png" alt="image-20230916164903702"></p>
|
||
<p><img src="/pictures/image-20230916165007465.png" alt="image-20230916165007465"></p>
|
||
<h4 id="2-3-4-思考题"><a href="#2-3-4-思考题" class="headerlink" title="2.3.4 思考题"></a>2.3.4 思考题</h4><h4 id=""><a href="#" class="headerlink" title=""></a><img src="/pictures/image-20230916165329434.png" alt="image-20230916165329434"></h4><h3 id="2-3-索引分类"><a href="#2-3-索引分类" class="headerlink" title="2.3 索引分类"></a>2.3 索引分类</h3><p><img src="/pictures/image-20230916165516779.png" alt="image-20230916165516779"></p>
|
||
<p><img src="/pictures/image-20230916165647255.png" alt="image-20230916165647255"></p>
|
||
<p><img src="/pictures/image-20230916170012664.png" alt="image-20230916170012664"></p>
|
||
<p>当我们要查询name值是Arm的数据的时候,会先通过二级索引查询,然后通过聚集索引查询这一行的信息(回表查询)</p>
|
||
<p><img src="/pictures/image-20230916170421435.png" alt="image-20230916170421435"></p>
|
||
<p><strong>思考题</strong></p>
|
||
<p><img src="/pictures/image-20230916170537693.png" alt="image-20230916170537693"></p>
|
||
<p>第一条的执行效率高,第二条要回表查询(先查询name所在行对应的主键id,然后通过主键id查询这一行的数据)</p>
|
||
<h3 id="2-4-索引语法"><a href="#2-4-索引语法" class="headerlink" title="2.4 索引语法"></a>2.4 索引语法</h3><p><strong>创建|查看|删除语法</strong></p>
|
||
<p><img src="/pictures/image-20230917091746406.png" alt="image-20230917091746406"></p>
|
||
<p><strong>举例</strong></p>
|
||
<p><img src="/pictures/image-20230917093020287.png" alt="image-20230917093020287"></p>
|
||
<h3 id="2-5-SQL性能分析"><a href="#2-5-SQL性能分析" class="headerlink" title="2.5 SQL性能分析"></a>2.5 SQL性能分析</h3><h4 id="2-5-1-SQL执行频率"><a href="#2-5-1-SQL执行频率" class="headerlink" title="2.5.1 SQL执行频率"></a>2.5.1 SQL执行频率</h4><p><img src="/pictures/image-20230917093634100.png" alt="image-20230917093634100"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查看系统的状态信息</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">global</span> status <span class="keyword">like</span> <span class="string">'Com_______'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="2-5-2-慢查询日志"><a href="#2-5-2-慢查询日志" class="headerlink" title="2.5.2 慢查询日志"></a>2.5.2 慢查询日志</h4><p><img src="/pictures/image-20230917094314803.png" alt="image-20230917094314803"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询慢查询日志是否开启</span></span><br><span class="line"><span class="keyword">show</span> variables <span class="keyword">like</span> <span class="string">'slow_query_log'</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="2-5-3-profile详情"><a href="#2-5-3-profile详情" class="headerlink" title="2.5.3 profile详情"></a>2.5.3 profile详情</h4><p><img src="/pictures/image-20230917095232011.png" alt="image-20230917095232011"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 是否支持profile详情功能</span></span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@have_profiling</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看是否开启该功能</span></span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@profiling</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 开启profile</span></span><br><span class="line"><span class="keyword">set</span> profiling <span class="operator">=</span> <span class="number">1</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><img src="/pictures/image-20230917095320953.png" alt="image-20230917095320953"></p>
|
||
<h4 id="2-5-4-explain执行计划"><a href="#2-5-4-explain执行计划" class="headerlink" title="2.5.4 explain执行计划"></a>2.5.4 explain执行计划</h4><p><img src="/pictures/image-20230917095911304.png" alt="image-20230917095911304"></p>
|
||
<p><img src="/pictures/image-20230917101205097.png" alt="image-20230917101205097"></p>
|
||
<p><img src="/pictures/image-20230917101337147.png" alt="image-20230917101337147"></p>
|
||
<h3 id="2-6-索引使用"><a href="#2-6-索引使用" class="headerlink" title="2.6 索引使用"></a>2.6 索引使用</h3><h4 id="2-6-1-验证索引效率"><a href="#2-6-1-验证索引效率" class="headerlink" title="2.6.1 验证索引效率"></a>2.6.1 验证索引效率</h4><p><img src="/pictures/image-20230917141334774.png" alt="image-20230917141334774"></p>
|
||
<p>现在有一个有着10000000数据的表(tb_sku),我们根据sn这一字段查询某一条具体的数据,没有建立索引之前的查询耗费的时间是20多秒,建立索引耗费大约90秒,为sn字段建立索引之后,查询耗费时间大概是零点几秒。</p>
|
||
<h4 id="2-6-2-最左前缀法则"><a href="#2-6-2-最左前缀法则" class="headerlink" title="2.6.2 最左前缀法则"></a>2.6.2 最左前缀法则</h4><p><img src="/pictures/image-20230917142410276.png" alt="image-20230917142410276"></p>
|
||
<p><img src="/pictures/image-20230917142707646.png" alt="image-20230917142707646"></p>
|
||
<h4 id="2-6-3-索引失效的情况"><a href="#2-6-3-索引失效的情况" class="headerlink" title="2.6.3 索引失效的情况"></a>2.6.3 索引失效的情况</h4><p><img src="/pictures/image-20230917142948227.png" alt="image-20230917142948227"></p>
|
||
<p><img src="/pictures/image-20230917143143047.png" alt="image-20230917143143047"></p>
|
||
<p><img src="/pictures/image-20230917143421975.png" alt="image-20230917143421975"></p>
|
||
<p><img src="/pictures/image-20230917143842497.png" alt="image-20230917143842497"></p>
|
||
<p><img src="/pictures/image-20230917144459567.png" alt="image-20230917144459567"></p>
|
||
<h4 id="2-6-4-SQL提示"><a href="#2-6-4-SQL提示" class="headerlink" title="2.6.4 SQL提示"></a>2.6.4 SQL提示</h4><p><img src="/pictures/image-20230917145046280.png" alt="image-20230917145046280"></p>
|
||
<h4 id="2-6-5-覆盖索引"><a href="#2-6-5-覆盖索引" class="headerlink" title="2.6.5 覆盖索引"></a>2.6.5 覆盖索引</h4><p><img src="/pictures/image-20230917145643194.png" alt="image-20230917145643194"></p>
|
||
<h4 id="2-6-6-前缀索引"><a href="#2-6-6-前缀索引" class="headerlink" title="2.6.6 前缀索引"></a>2.6.6 前缀索引</h4><p><img src="/pictures/image-20230917153757773.png" alt="image-20230917153757773"></p>
|
||
<p><img src="/pictures/image-20230917154300147.png" alt="image-20230917154300147"></p>
|
||
<h4 id="2-6-7-单例索引与联合索引的选择问题"><a href="#2-6-7-单例索引与联合索引的选择问题" class="headerlink" title="2.6.7 单例索引与联合索引的选择问题"></a>2.6.7 单例索引与联合索引的选择问题</h4><p><img src="/pictures/image-20230917155017641.png" alt="image-20230917155017641"></p>
|
||
<p><img src="/pictures/image-20230917155223679.png" alt="image-20230917155223679"></p>
|
||
<h3 id="2-7-索引设计原则"><a href="#2-7-索引设计原则" class="headerlink" title="2.7 索引设计原则"></a>2.7 索引设计原则</h3><p><img src="/pictures/image-20230917155748055.png" alt="image-20230917155748055"></p>
|
||
<h2 id="3-SQL优化"><a href="#3-SQL优化" class="headerlink" title="3.SQL优化"></a>3.SQL优化</h2><h3 id="3-1-插入数据的优化"><a href="#3-1-插入数据的优化" class="headerlink" title="3.1 插入数据的优化"></a>3.1 插入数据的优化</h3><p><img src="/pictures/image-20230917181618942.png" alt="image-20230917181618942"></p>
|
||
<p><img src="/pictures/image-20230917181842030.png" alt="image-20230917181842030"></p>
|
||
<p>100万的数据如果使用insert插入的话需要10分钟左右,如果使用文件的方式导入,只需要10多秒。</p>
|
||
<h3 id="3-2-主键优化"><a href="#3-2-主键优化" class="headerlink" title="3.2 主键优化"></a>3.2 主键优化</h3><p><img src="/pictures/image-20230917205500523.png" alt="image-20230917205500523"></p>
|
||
<p><img src="/pictures/image-20230917205636137.png" alt="image-20230917205636137"></p>
|
||
<p><img src="/pictures/image-20230917205833596.png" alt="image-20230917205833596"></p>
|
||
<p><img src="/pictures/image-20230917210112389.png" alt="image-20230917210112389"></p>
|
||
<p><img src="/pictures/image-20230917210303469.png" alt="image-20230917210303469"></p>
|
||
<h3 id="3-3-order-by优化"><a href="#3-3-order-by优化" class="headerlink" title="3.3 order by优化"></a>3.3 order by优化</h3><p><img src="/pictures/image-20230917210730392.png" alt="image-20230917210730392"></p>
|
||
<p><img src="/pictures/image-20230917211254167.png" alt="image-20230917211254167"></p>
|
||
<p><img src="/pictures/image-20230917210616858.png" alt="image-20230917210616858"></p>
|
||
<p><img src="/pictures/image-20230917211543109.png" alt="image-20230917211543109"></p>
|
||
<h3 id="3-4-group-by优化"><a href="#3-4-group-by优化" class="headerlink" title="3.4 group by优化"></a>3.4 group by优化</h3><p><img src="/pictures/image-20230917212026101.png" alt="image-20230917212026101"></p>
|
||
<h3 id="3-5-limit优化"><a href="#3-5-limit优化" class="headerlink" title="3.5 limit优化"></a>3.5 limit优化</h3><p><img src="/pictures/image-20230917212530539.png" alt="image-20230917212530539"></p>
|
||
<h3 id="3-6-count优化"><a href="#3-6-count优化" class="headerlink" title="3.6 count优化"></a>3.6 count优化</h3><p><img src="/pictures/image-20230917212709489.png" alt="image-20230917212709489"></p>
|
||
<p><img src="/pictures/image-20230917213029734.png" alt="image-20230917213029734"></p>
|
||
<p><img src="/pictures/image-20230917213316193.png" alt="image-20230917213316193"></p>
|
||
<h3 id="3-7-update优化"><a href="#3-7-update优化" class="headerlink" title="3.7 update优化"></a>3.7 update优化</h3><p><img src="/pictures/image-20230917213859581.png" alt="image-20230917213859581"></p>
|
||
<h3 id="3-8-总结"><a href="#3-8-总结" class="headerlink" title="3.8 总结"></a>3.8 总结</h3><p><img src="/pictures/image-20230917214424722.png" alt="image-20230917214424722"></p>
|
||
<h2 id="4-视图-存储过程-触发器"><a href="#4-视图-存储过程-触发器" class="headerlink" title="4.视图/存储过程/触发器"></a>4.视图/存储过程/触发器</h2><h3 id="4-1-视图"><a href="#4-1-视图" class="headerlink" title="4.1 视图"></a>4.1 视图</h3><h4 id="4-1-1-视图介绍"><a href="#4-1-1-视图介绍" class="headerlink" title="4.1.1 视图介绍"></a>4.1.1 视图介绍</h4><p> 视图(View)是一种虚拟存在的表。视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的。通俗的讲,视图只保存了查询的SQL逻辑,不保存查询结果。所以我们在创建视图的时候,主要的工作就落在创建这条SQL查询语句上。</p>
|
||
<h4 id="4-1-2-语法"><a href="#4-1-2-语法" class="headerlink" title="4.1.2 语法"></a>4.1.2 语法</h4><p><strong>创建</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> [<span class="keyword">OR</span> REPLACE] <span class="keyword">VIEW</span> 视图名称[(列名列表)] <span class="keyword">AS</span> <span class="keyword">SELECT</span>语句 [ <span class="keyword">WITH</span> [<span class="keyword">CASCADED</span> <span class="operator">|</span> <span class="keyword">LOCAL</span> ] <span class="keyword">CHECK</span> OPTION ]</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>查询</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">查看创建视图语句:<span class="keyword">SHOW</span> <span class="keyword">CREATE</span> <span class="keyword">VIEW</span> 视图名称;</span><br><span class="line">查看视图数据:<span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> 视图名称 ...... ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>修改</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">方式一:<span class="keyword">CREATE</span> [<span class="keyword">OR</span> REPLACE] <span class="keyword">VIEW</span> 视图名称[(列名列表)] <span class="keyword">AS</span> <span class="keyword">SELECT</span>语句 [ <span class="keyword">WITH</span> [ <span class="keyword">CASCADED</span> <span class="operator">|</span> <span class="keyword">LOCAL</span> ] <span class="keyword">CHECK</span> OPTION ]</span><br><span class="line">方式二:<span class="keyword">ALTER</span> <span class="keyword">VIEW</span> 视图名称[(列名列表)] <span class="keyword">AS</span> <span class="keyword">SELECT</span>语句 [ <span class="keyword">WITH</span> [ <span class="keyword">CASCADED</span> <span class="operator">|</span> <span class="keyword">LOCAL</span> ] <span class="keyword">CHECK</span> OPTION ]</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>删除</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">VIEW</span> [IF <span class="keyword">EXISTS</span>] 视图名称 [,视图名称] ...</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>演示示例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 视图</span></span><br><span class="line"><span class="comment">-- 创建视图</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_1 <span class="keyword">as</span> <span class="keyword">select</span> id, name <span class="keyword">from</span> student <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看创建视图的语句</span></span><br><span class="line"><span class="keyword">show</span> <span class="keyword">create</span> <span class="keyword">view</span> stu_v_1;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看视图数据</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> stu_v_1;</span><br><span class="line"><span class="comment">-- 也可以加上条件</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> stu_v_1 <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改视图的方式(or replace可以实现修改效果)</span></span><br><span class="line"><span class="comment">-- 方式一</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_1 <span class="keyword">as</span> <span class="keyword">select</span> id, name, <span class="keyword">no</span> <span class="keyword">from</span> student <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">10</span>;</span><br><span class="line"><span class="comment">-- 方式二</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">view</span> stu_v_1 <span class="keyword">as</span> <span class="keyword">select</span> id, name <span class="keyword">from</span> student <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除视图</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">view</span> if <span class="keyword">exists</span> stu_v_1;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 通过视图实现插入操作</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_1 <span class="keyword">as</span> <span class="keyword">select</span> id, name <span class="keyword">from</span> student <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 向视图中添加数据(实际上插入的数据插入到原table表中)</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_1 <span class="keyword">values</span>(<span class="number">6</span>,<span class="string">'Tom'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-1-3-检查选项"><a href="#4-1-3-检查选项" class="headerlink" title="4.1.3 检查选项"></a>4.1.3 检查选项</h4><p> 当使用WITH CHECK OPTION子句创建视图时,MySQL会通过视图检查正在更改的每个行,例如 插入,更新,删除,以使其符合视图的定义。MySQL允许基于另一个视图创建视图,它还会检查依赖视图中的规则以保持一致性。为了确定检查的范围,mysql提供了两个选项: CASCADED 和 LOCAL,默认值为 CASCADED 。</p>
|
||
<p><strong>1.CASCADED</strong> 级联</p>
|
||
<p> 比如,v2视图是基于v1视图的,如果在v2视图创建的时候指定了检查选项为 cascaded,但是v1视图创建时未指定检查选项。 则在执行检查时,不仅会检查v2,还会级联检查v2的关联视图v1。</p>
|
||
<p> <img src="/pictures/image-20231006182938266.png" alt="image-20231006182938266"></p>
|
||
<p><strong>2.LOCAL</strong> 本地</p>
|
||
<p> 比如,v2视图是基于v1视图的,如果在v2视图创建的时候指定了检查选项为 local ,但是v1视图创建时未指定检查选项。 则在执行检查时,知会检查v2,不会检查v2的关联视图v1。</p>
|
||
<p> <img src="/pictures/image-20231006183051143.png" alt="image-20231006183051143"></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 检查选项</span></span><br><span class="line"><span class="comment">-- 添加了检查选项,在插入数据的时候会检查插入的数据是否满足视图创建的条件(创建语句为create or replace view stu_v_1 as select id, name from student where id <= 20;此时插入的数据id要<=20,否则报错)</span></span><br><span class="line"><span class="comment">-- cascaded:插入时会检查是否满足当前视图和依赖的视图的条件</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_1 <span class="keyword">as</span> <span class="keyword">select</span> id, name <span class="keyword">from</span> student <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">20</span> <span class="keyword">with</span> <span class="keyword">cascaded</span> <span class="keyword">check</span> option ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- local:插入时会检查是否满足当前视图和依赖的视图(定义了检查选项的视图)的条件</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_1 <span class="keyword">as</span> <span class="keyword">select</span> id, name <span class="keyword">from</span> student <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">20</span> <span class="keyword">with</span> <span class="keyword">local</span> <span class="keyword">check</span> option ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- local和cascaded的区别:</span></span><br><span class="line"><span class="comment">-- local:v2依赖了v1,只有v1也加上了with local check option检查选项,在v2插入数据的时候才会检查v1的条件</span></span><br><span class="line"><span class="comment">-- cascaded:v2依赖了v1,不管v1有没有加上检查选项,在v2插入数据时都会检查v1的条件</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 重点</span></span><br><span class="line"><span class="comment">-- 1.加不加检查选项的区别</span></span><br><span class="line"><span class="comment">-- 2.with cascaded check option和with local check option的区别</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-1-4-视图更新"><a href="#4-1-4-视图更新" class="headerlink" title="4.1.4 视图更新"></a>4.1.4 视图更新</h4><p>要使视图可更新,视图中的行与基础表中的行之间必须存在一对一的关系。如果视图包含以下任何一项,则该视图不可更新:</p>
|
||
<p>A. 聚合函数或窗口函数(SUM()、 MIN()、 MAX()、 COUNT()等)</p>
|
||
<p>B. DISTINCT</p>
|
||
<p>C. GROUP BY</p>
|
||
<p>D. HAVING</p>
|
||
<p>E. UNION 或者 UNION ALL</p>
|
||
<p> <img src="/pictures/image-20231006184725321.png" alt="image-20231006184725321"></p>
|
||
<h4 id="4-1-5-视图作用"><a href="#4-1-5-视图作用" class="headerlink" title="4.1.5 视图作用"></a>4.1.5 视图作用</h4><p><img src="/pictures/image-20231006185121986.png" alt="image-20231006185121986"></p>
|
||
<h4 id="4-1-6-视图案例"><a href="#4-1-6-视图案例" class="headerlink" title="4.1.6 视图案例"></a>4.1.6 视图案例</h4><p>tb_user表结构以及sql语句</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> tb_user(</span><br><span class="line"> id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment comment <span class="string">'主键'</span>,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'用户名'</span>,</span><br><span class="line"> phone <span class="type">varchar</span>(<span class="number">11</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'手机号'</span>,</span><br><span class="line"> email <span class="type">varchar</span>(<span class="number">100</span>) comment <span class="string">'邮箱'</span>,</span><br><span class="line"> profession <span class="type">varchar</span>(<span class="number">11</span>) comment <span class="string">'专业'</span>,</span><br><span class="line"> age tinyint unsigned comment <span class="string">'年龄'</span>,</span><br><span class="line"> gender <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'性别 , 1: 男, 2: 女'</span>,</span><br><span class="line"> status <span class="type">char</span>(<span class="number">1</span>) comment <span class="string">'状态'</span>,</span><br><span class="line"> createtime datetime comment <span class="string">'创建时间'</span></span><br><span class="line">) comment <span class="string">'系统用户表'</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'吕布'</span>, <span class="string">'17799990000'</span>, <span class="string">'lvbu666@163.com'</span>, <span class="string">'软件工程'</span>, <span class="number">23</span>, <span class="string">'1'</span>, <span class="string">'6'</span>, <span class="string">'2001-02-02 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'曹操'</span>, <span class="string">'17799990001'</span>, <span class="string">'caocao666@qq.com'</span>, <span class="string">'通讯工程'</span>, <span class="number">33</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2001-03-05 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'赵云'</span>, <span class="string">'17799990002'</span>, <span class="string">'17799990@139.com'</span>, <span class="string">'英语'</span>, <span class="number">34</span>, <span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'2002-03-02 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'孙悟空'</span>, <span class="string">'17799990003'</span>, <span class="string">'17799990@sina.com'</span>, <span class="string">'工程造价'</span>, <span class="number">54</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2001-07-02 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'花木兰'</span>, <span class="string">'17799990004'</span>, <span class="string">'19980729@sina.com'</span>, <span class="string">'软件工程'</span>, <span class="number">23</span>, <span class="string">'2'</span>, <span class="string">'1'</span>, <span class="string">'2001-04-22 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'大乔'</span>, <span class="string">'17799990005'</span>, <span class="string">'daqiao666@sina.com'</span>, <span class="string">'舞蹈'</span>, <span class="number">22</span>, <span class="string">'2'</span>, <span class="string">'0'</span>, <span class="string">'2001-02-07 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'露娜'</span>, <span class="string">'17799990006'</span>, <span class="string">'luna_love@sina.com'</span>, <span class="string">'应用数学'</span>, <span class="number">24</span>, <span class="string">'2'</span>, <span class="string">'0'</span>, <span class="string">'2001-02-08 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'程咬金'</span>, <span class="string">'17799990007'</span>, <span class="string">'chengyaojin@163.com'</span>, <span class="string">'化工'</span>, <span class="number">38</span>, <span class="string">'1'</span>, <span class="string">'5'</span>, <span class="string">'2001-05-23 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'项羽'</span>, <span class="string">'17799990008'</span>, <span class="string">'xiaoyu666@qq.com'</span>, <span class="string">'金属材料'</span>, <span class="number">43</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2001-09-18 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'白起'</span>, <span class="string">'17799990009'</span>, <span class="string">'baiqi666@sina.com'</span>, <span class="string">'机械工程及其自动化'</span>, <span class="number">27</span>, <span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'2001-08-16 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'韩信'</span>, <span class="string">'17799990010'</span>, <span class="string">'hanxin520@163.com'</span>, <span class="string">'无机非金属材料工程'</span>, <span class="number">27</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2001-06-12 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'荆轲'</span>, <span class="string">'17799990011'</span>, <span class="string">'jingke123@163.com'</span>, <span class="string">'会计'</span>, <span class="number">29</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2001-05-11 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'兰陵王'</span>, <span class="string">'17799990012'</span>, <span class="string">'lanlinwang666@126.com'</span>, <span class="string">'工程造价'</span>, <span class="number">44</span>, <span class="string">'1'</span>, <span class="string">'1'</span>, <span class="string">'2001-04-09 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'狂铁'</span>, <span class="string">'17799990013'</span>, <span class="string">'kuangtie@sina.com'</span>, <span class="string">'应用数学'</span>, <span class="number">43</span>, <span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'2001-04-10 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'貂蝉'</span>, <span class="string">'17799990014'</span>, <span class="string">'84958948374@qq.com'</span>, <span class="string">'软件工程'</span>, <span class="number">40</span>, <span class="string">'2'</span>, <span class="string">'3'</span>, <span class="string">'2001-02-12 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'妲己'</span>, <span class="string">'17799990015'</span>, <span class="string">'2783238293@qq.com'</span>, <span class="string">'软件工程'</span>, <span class="number">31</span>, <span class="string">'2'</span>, <span class="string">'0'</span>, <span class="string">'2001-01-30 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'芈月'</span>, <span class="string">'17799990016'</span>, <span class="string">'xiaomin2001@sina.com'</span>, <span class="string">'工业经济'</span>, <span class="number">35</span>, <span class="string">'2'</span>, <span class="string">'0'</span>, <span class="string">'2000-05-03 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'嬴政'</span>, <span class="string">'17799990017'</span>, <span class="string">'8839434342@qq.com'</span>, <span class="string">'化工'</span>, <span class="number">38</span>, <span class="string">'1'</span>, <span class="string">'1'</span>, <span class="string">'2001-08-08 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'狄仁杰'</span>, <span class="string">'17799990018'</span>, <span class="string">'jujiamlm8166@163.com'</span>, <span class="string">'国际贸易'</span>, <span class="number">30</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2007-03-12 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'安琪拉'</span>, <span class="string">'17799990019'</span>, <span class="string">'jdodm1h@126.com'</span>, <span class="string">'城市规划'</span>, <span class="number">51</span>, <span class="string">'2'</span>, <span class="string">'0'</span>, <span class="string">'2001-08-15 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'典韦'</span>, <span class="string">'17799990020'</span>, <span class="string">'ycaunanjian@163.com'</span>, <span class="string">'城市规划'</span>, <span class="number">52</span>, <span class="string">'1'</span>, <span class="string">'2'</span>, <span class="string">'2000-04-12 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'廉颇'</span>, <span class="string">'17799990021'</span>, <span class="string">'lianpo321@126.com'</span>, <span class="string">'土木工程'</span>, <span class="number">19</span>, <span class="string">'1'</span>, <span class="string">'3'</span>, <span class="string">'2002-07-18 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'后羿'</span>, <span class="string">'17799990022'</span>, <span class="string">'altycj2000@139.com'</span>, <span class="string">'城市园林'</span>, <span class="number">20</span>, <span class="string">'1'</span>, <span class="string">'0'</span>, <span class="string">'2002-03-10 00:00:00'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> itcast.tb_user (name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span> (<span class="string">'姜子牙'</span>, <span class="string">'17799990023'</span>, <span class="string">'37483844@qq.com'</span>, <span class="string">'工程造价'</span>, <span class="number">29</span>, <span class="string">'1'</span>, <span class="string">'4'</span>, <span class="string">'2003-05-26 00:00:00'</span>);</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>案例</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 视图案例</span></span><br><span class="line"><span class="comment">-- 为了保证数据库表的安全性,开发人员在操作tb_user表时,只能看到的用户的基本字段,屏蔽手机号和邮箱两个字段</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> tb_user_view <span class="keyword">as</span> <span class="keyword">select</span> id, name, profession, age, gender, status, createtime <span class="keyword">from</span> tb_user;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> tb_user_view;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询每个学生所选修的课程(三张表联查),这个功能在很多的业务中都有使用到,为了简化操作,定义一个视图</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">view</span> tb_stu_course_view <span class="keyword">as</span> <span class="keyword">select</span> s.name student_name,s.no,c.name course_name <span class="keyword">from</span> student s, course c, student_course sc <span class="keyword">where</span> s.id <span class="operator">=</span> sc.studentid <span class="keyword">and</span> c.id <span class="operator">=</span> sc.courseid;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h3 id="4-2-存储过程"><a href="#4-2-存储过程" class="headerlink" title="4.2 存储过程"></a>4.2 存储过程</h3><h4 id="4-2-1-介绍"><a href="#4-2-1-介绍" class="headerlink" title="4.2.1 介绍"></a>4.2.1 介绍</h4><p> <img src="/pictures/image-20231006190705216.png" alt="image-20231006190705216"></p>
|
||
<p> <img src="/pictures/image-20231006190743209.png" alt="image-20231006190743209"></p>
|
||
<h4 id="4-2-2-基本语法"><a href="#4-2-2-基本语法" class="headerlink" title="4.2.2 基本语法"></a>4.2.2 基本语法</h4><p><strong>创建</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">PROCEDURE</span> 存储过程名称 ([ 参数列表 ])</span><br><span class="line"><span class="keyword">BEGIN</span></span><br><span class="line"> <span class="comment">-- SQL语句</span></span><br><span class="line"><span class="keyword">END</span> ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>调用</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CALL</span> 名称 ([ 参数 ]);</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>查看</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> INFORMATION_SCHEMA.ROUTINES <span class="keyword">WHERE</span> ROUTINE_SCHEMA <span class="operator">=</span> <span class="string">'xxx'</span>; <span class="comment">-- 查询指定数据库的存储过程及状态信息</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">SHOW</span> <span class="keyword">CREATE</span> <span class="keyword">PROCEDURE</span> 存储过程名称 ; <span class="comment">-- 查询某个存储过程的定义</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>删除</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">PROCEDURE</span> [ IF <span class="keyword">EXISTS</span> ] 存储过程名称 ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<blockquote>
|
||
<p>注意:<br>在命令行中,执行创建存储过程的SQL时,需要通过关键字 delimiter 指定SQL语句的结束符</p>
|
||
</blockquote>
|
||
<p><strong>演示示例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 存储过程</span></span><br><span class="line"><span class="comment">-- 创建存储过程</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p1()</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">from</span> student;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 调用</span></span><br><span class="line"><span class="keyword">call</span> p1();</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看</span></span><br><span class="line"><span class="comment">-- 查询指定数据库的存储过程及状态信息</span></span><br><span class="line"><span class="comment">-- 查询itcast数据库中定义的存储过程</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> information_schema.ROUTINES <span class="keyword">where</span> ROUTINE_SCHEMA <span class="operator">=</span> <span class="string">'itcast'</span>;</span><br><span class="line"><span class="comment">-- 查询某个存储过程的定义</span></span><br><span class="line"><span class="keyword">SHOW</span> <span class="keyword">CREATE</span> <span class="keyword">PROCEDURE</span> p1 ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">procedure</span> if <span class="keyword">exists</span> p1;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-3-变量"><a href="#4-2-3-变量" class="headerlink" title="4.2.3 变量"></a>4.2.3 变量</h4><p>在MySQL中变量分为三种类型: 系统变量、用户定义变量、局部变量</p>
|
||
<h5 id="4-2-3-1-系统变量"><a href="#4-2-3-1-系统变量" class="headerlink" title="4.2.3.1 系统变量"></a>4.2.3.1 系统变量</h5><p>系统变量 是MySQL服务器提供,不是用户定义的,属于服务器层面。分为全局变量(GLOBAL)、会话 变量(SESSION)</p>
|
||
<p>1.查看系统变量</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SHOW</span> [ SESSION <span class="operator">|</span> <span class="keyword">GLOBAL</span> ] VARIABLES ; <span class="comment">-- 查看所有系统变量</span></span><br><span class="line"><span class="keyword">SHOW</span> [ SESSION <span class="operator">|</span> <span class="keyword">GLOBAL</span> ] VARIABLES <span class="keyword">LIKE</span> <span class="string">'......'</span>; <span class="comment">-- 可以通过LIKE模糊匹配方式查找变量</span></span><br><span class="line"><span class="keyword">SELECT</span> @@[SESSION <span class="operator">|</span> <span class="keyword">GLOBAL</span>] 系统变量名; <span class="comment">-- 查看指定变量的值</span></span><br></pre></td></tr></table></figure>
|
||
|
||
<p>2.设置系统变量</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SET</span> [ SESSION <span class="operator">|</span> <span class="keyword">GLOBAL</span> ] 系统变量名 <span class="operator">=</span> 值 ;</span><br><span class="line"><span class="keyword">SET</span> @@[SESSION <span class="operator">|</span> <span class="keyword">GLOBAL</span>]系统变量名 <span class="operator">=</span> 值 ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<blockquote>
|
||
<p><strong>注意</strong>:</p>
|
||
<pre><code>如果没有指定SESSION/GLOBAL,默认是SESSION,会话变量
|
||
</code></pre>
|
||
<blockquote>
|
||
<p>1 mysql服务重新启动之后,所设置的全局参数会失效,要想不失效,可以在 /etc/my.cnf 中配置</p>
|
||
</blockquote>
|
||
<p> A. 全局变量(GLOBAL): 全局变量针对于所有的会话</p>
|
||
<p> B. 会话变量(SESSION): 会话变量针对于单个会话,在另外一个会话窗口就不生效了</p>
|
||
</blockquote>
|
||
<p><strong>演示示例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 系统变量</span></span><br><span class="line"><span class="comment">-- 查看所有系统变量</span></span><br><span class="line"><span class="keyword">show</span> session variables ; #查看会话的系统变量</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">global</span> variables ; #查看全局的系统变量</span><br><span class="line"><span class="comment">-- 查看以auto开头的系统变量</span></span><br><span class="line"><span class="keyword">show</span> variables <span class="keyword">like</span> <span class="string">'auto%'</span>;</span><br><span class="line"><span class="comment">-- 查看某一个具体的环境变量值(以autocommit为例)</span></span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@autocommit</span>; #会话的</span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@global</span>.autocommit; #全局的</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 设置系统变量(以autocommit为例)</span></span><br><span class="line"><span class="keyword">set</span> session autocommit <span class="operator">=</span> <span class="number">1</span>; #会话级别的</span><br><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> autocommit <span class="operator">=</span> <span class="number">1</span>; #全局级别的</span><br><span class="line"><span class="comment">-- 或者</span></span><br><span class="line"><span class="keyword">set</span> @<span class="variable">@autocommit</span> <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"><span class="keyword">set</span> @<span class="variable">@global</span>.autocommit <span class="operator">=</span> <span class="number">1</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="4-2-3-2-用户定义变量"><a href="#4-2-3-2-用户定义变量" class="headerlink" title="4.2.3.2 用户定义变量"></a>4.2.3.2 用户定义变量</h5><p>用户定义变量 是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用 “@变量 名” 使用就可以。其作用域为当前连接</p>
|
||
<p><strong>1.赋值</strong></p>
|
||
<p>方式一:</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SET</span> <span class="variable">@var_name</span> <span class="operator">=</span> expr [, <span class="variable">@var_name</span> <span class="operator">=</span> expr] ... ;</span><br><span class="line"><span class="keyword">SET</span> <span class="variable">@var_name</span> :<span class="operator">=</span> expr [, <span class="variable">@var_name</span> :<span class="operator">=</span> expr] ... ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>赋值时,可以使用 = ,也可以使用 := </p>
|
||
<p>方式二:</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="variable">@var_name</span> :<span class="operator">=</span> expr [, <span class="variable">@var_name</span> :<span class="operator">=</span> expr] ... ;</span><br><span class="line"><span class="keyword">SELECT</span> 字段名 <span class="keyword">INTO</span> <span class="variable">@var_name</span> <span class="keyword">FROM</span> 表名;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>2.使用</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="variable">@var_name</span> ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<blockquote>
|
||
<p>注意: 用户定义的变量无需对其进行声明或初始化,只不过获取到的值为NULL</p>
|
||
</blockquote>
|
||
<p><strong>演示示例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 用户定义变量</span></span><br><span class="line"><span class="comment">-- 赋值</span></span><br><span class="line"><span class="comment">-- 方式一</span></span><br><span class="line"><span class="keyword">set</span> <span class="variable">@myname</span> <span class="operator">=</span> <span class="string">'itcast'</span>;</span><br><span class="line"><span class="keyword">set</span> <span class="variable">@myage</span> :<span class="operator">=</span> <span class="number">10</span>;</span><br><span class="line"><span class="keyword">set</span> <span class="variable">@mygender</span> :<span class="operator">=</span> <span class="string">'男'</span>,<span class="variable">@myhobby</span> <span class="operator">=</span> <span class="string">'java'</span>;</span><br><span class="line"><span class="comment">-- 方式二</span></span><br><span class="line"><span class="keyword">select</span> <span class="variable">@mycolor</span> :<span class="operator">=</span> <span class="string">'red'</span>;</span><br><span class="line"><span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">into</span> <span class="variable">@mycount</span> <span class="keyword">from</span> tb_user; #将tb_user表中的总记录数赋值给<span class="variable">@mycount</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 使用</span></span><br><span class="line"><span class="keyword">select</span> <span class="variable">@myname</span>,<span class="variable">@myage</span>,<span class="variable">@mygender</span>,<span class="variable">@myhobby</span>,<span class="variable">@mycolor</span>,<span class="variable">@mycount</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h5 id="4-2-3-3-局部变量"><a href="#4-2-3-3-局部变量" class="headerlink" title="4.2.3.3 局部变量"></a>4.2.3.3 局部变量</h5><p>局部变量 是根据需要定义的在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的 局部变量和输入参数,局部变量的范围是在其内声明的BEGIN … END块</p>
|
||
<p>1**.声明**</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DECLARE</span> 变量名 变量类型 [<span class="keyword">DEFAULT</span> ... ] ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>变量类型就是数据库字段类型:INT、BIGINT、CHAR、VARCHAR、DATE、TIME等</p>
|
||
<p><strong>2.赋值</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SET</span> 变量名 <span class="operator">=</span> 值 ;</span><br><span class="line"><span class="keyword">SET</span> 变量名 :<span class="operator">=</span> 值 ;</span><br><span class="line"><span class="keyword">SELECT</span> 字段名 <span class="keyword">INTO</span> 变量名 <span class="keyword">FROM</span> 表名 ... ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>演示示例</strong></p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 局部变量</span></span><br><span class="line"><span class="comment">-- 创建存储过程</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p2()</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> # 定义一个<span class="type">int</span>类型的局部变量,默认值是<span class="number">0</span></span><br><span class="line"> <span class="keyword">declare</span> stu_count <span class="type">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line"> # 给stu_count赋值</span><br><span class="line"> <span class="comment">-- set stu_count = 100;</span></span><br><span class="line"> <span class="keyword">select</span> <span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">into</span> stu_count <span class="keyword">from</span> tb_user;</span><br><span class="line"> #输出</span><br><span class="line"> <span class="keyword">select</span> stu_count;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"><span class="comment">-- 调用函数</span></span><br><span class="line"><span class="keyword">call</span> p2();</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-4-if判断"><a href="#4-2-4-if判断" class="headerlink" title="4.2.4 if判断"></a>4.2.4 if判断</h4><p><strong>介绍</strong></p>
|
||
<p>if 用于做条件判断,具体的语法结构为:</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">IF 条件<span class="number">1</span> <span class="keyword">THEN</span></span><br><span class="line"> .....</span><br><span class="line">ELSEIF 条件<span class="number">2</span> <span class="keyword">THEN</span> <span class="comment">-- 可选</span></span><br><span class="line"> .....</span><br><span class="line"><span class="keyword">ELSE</span> <span class="comment">-- 可选</span></span><br><span class="line"> .....</span><br><span class="line"><span class="keyword">END</span> IF;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>在if条件判断的结构中,ELSE IF 结构可以有多个,也可以没有. ELSE结构可以有,也可以没有.</p>
|
||
<p><strong>案例</strong></p>
|
||
<p>根据定义的分数score变量,判定当前分数对应的分数等级</p>
|
||
<ul>
|
||
<li><p>score >= 85分,等级为优秀</p>
|
||
</li>
|
||
<li><p>score >= 60分 且 score < 85分,等级为及格</p>
|
||
</li>
|
||
<li><p>score < 60分,等级为不及格</p>
|
||
</li>
|
||
</ul>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- if判断</span></span><br><span class="line"><span class="comment">-- 根据定义的分数score变量,判定当前分数对应的分数等级</span></span><br><span class="line"><span class="comment">-- score >= 85分,等级为优秀</span></span><br><span class="line"><span class="comment">-- score >= 60分 且 score < 85分,等级为及格</span></span><br><span class="line"><span class="comment">-- score < 60分,等级为不及格</span></span><br><span class="line"><span class="keyword">drop</span> <span class="keyword">procedure</span> p3;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p3()</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> # 先给一个默认值 <span class="number">58</span></span><br><span class="line"> <span class="keyword">declare</span> score <span class="type">int</span> <span class="keyword">default</span> <span class="number">58</span>;</span><br><span class="line"> <span class="keyword">declare</span> <span class="keyword">result</span> <span class="type">varchar</span>(<span class="number">10</span>);</span><br><span class="line"> if score <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">set</span> <span class="keyword">result</span> :<span class="operator">=</span> <span class="string">'优秀'</span>;</span><br><span class="line"> elseif score <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">set</span> <span class="keyword">result</span> :<span class="operator">=</span> <span class="string">'及格'</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">set</span> <span class="keyword">result</span> :<span class="operator">=</span> <span class="string">'不及格'</span>;</span><br><span class="line"> <span class="keyword">end</span> if;</span><br><span class="line"> #输出</span><br><span class="line"> <span class="keyword">select</span> <span class="keyword">result</span>;</span><br><span class="line"><span class="keyword">end</span> ;</span><br><span class="line"><span class="comment">-- 调用</span></span><br><span class="line"><span class="keyword">call</span> p3();</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-5-参数"><a href="#4-2-5-参数" class="headerlink" title="4.2.5 参数"></a>4.2.5 参数</h4><p><strong>介绍</strong></p>
|
||
<p>参数的类型,主要分为以下三种:IN、OUT、INOUT. 具体的含义如下:</p>
|
||
<p><img src="/pictures/image-20231006213944694.png" alt="image-20231006213944694"></p>
|
||
<p>语法</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">PROCEDURE</span> 存储过程名称 ([ <span class="keyword">IN</span><span class="operator">/</span><span class="keyword">OUT</span><span class="operator">/</span><span class="keyword">INOUT</span> 参数名 参数类型 ])</span><br><span class="line"><span class="keyword">BEGIN</span></span><br><span class="line"> <span class="comment">-- SQL语句</span></span><br><span class="line"><span class="keyword">END</span> ;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p><strong>案例</strong></p>
|
||
<p>1.根据传入的分数score变量,判定当前分数对应的分数等级,并返回</p>
|
||
<ul>
|
||
<li><p>score >= 85分,等级为优秀</p>
|
||
</li>
|
||
<li><p>score >= 60分 且 score < 85分,等级为及格</p>
|
||
</li>
|
||
<li><p>score < 60分,等级为不及格</p>
|
||
</li>
|
||
</ul>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 参数</span></span><br><span class="line"><span class="comment">-- 1.根据传入的分数score变量,判定当前分数对应的分数等级,并返回</span></span><br><span class="line"><span class="comment">-- score >= 85分,等级为优秀</span></span><br><span class="line"><span class="comment">-- score >= 60分 且 score < 85分,等级为及格</span></span><br><span class="line"><span class="comment">-- score < 60分,等级为不及格</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p4(<span class="keyword">in</span> score <span class="type">int</span>, <span class="keyword">out</span> <span class="keyword">result</span> <span class="type">varchar</span>(<span class="number">10</span>))</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> if score <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">set</span> <span class="keyword">result</span> :<span class="operator">=</span> <span class="string">'优秀'</span>;</span><br><span class="line"> elseif score <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">set</span> <span class="keyword">result</span> :<span class="operator">=</span> <span class="string">'及格'</span>;</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> <span class="keyword">set</span> <span class="keyword">result</span> :<span class="operator">=</span> <span class="string">'不及格'</span>;</span><br><span class="line"> <span class="keyword">end</span> if;</span><br><span class="line"><span class="keyword">end</span> ;</span><br><span class="line"><span class="comment">-- 调用</span></span><br><span class="line"><span class="keyword">call</span> p4(<span class="number">88</span>,<span class="variable">@result</span>);</span><br><span class="line"><span class="comment">-- 查看结果</span></span><br><span class="line"><span class="keyword">select</span> <span class="variable">@result</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<p>2.将传入的200分制的分数,进行换算,换成百分制,然后返回</p>
|
||
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 2.将传入的200分制的分数,进行换算,换成百分制,然后返回</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p5(<span class="keyword">inout</span> score <span class="keyword">double</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">set</span> score :<span class="operator">=</span> score <span class="operator">*</span> <span class="number">0.5</span>;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"><span class="comment">-- 调用(score既是输入又是输出)</span></span><br><span class="line"><span class="keyword">set</span> <span class="variable">@score</span> <span class="operator">=</span> <span class="number">78</span>;</span><br><span class="line"><span class="keyword">call</span> p5(<span class="variable">@score</span>);</span><br><span class="line"><span class="keyword">select</span> <span class="variable">@score</span>;</span><br></pre></td></tr></table></figure>
|
||
|
||
<h4 id="4-2-6-case"><a href="#4-2-6-case" class="headerlink" title="4.2.6 case"></a>4.2.6 case</h4></article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Mysql/">Mysql</a></div><div class="post_share"><div class="social-share" data-image="/img/9.png" data-sites="wechat,weibo,qq"></div><link rel="stylesheet" href="/cdn/css/share.min.css" media="print" onload="this.media='all'"><script src="/cdn/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><br/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="far fa-comment-alt fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info__name">Jason</div><div class="author-info__description">Debug the World!</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">60</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">39</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div><a id="card-info-btn"><i class="fab fa-microsoft"></i><span>Ctrl + D 收藏</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/JasonsGong" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=2602183349&website=www.oicqzone.com" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:2602183349@qq.com" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a><a class="social-icon" href="https://github.com/JasonsGong?tab=repositories" target="_blank" title="代码仓库"><i class="fas fa-database"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">本网站是静态网站,更新页面资源请使用Ctrl+F5;若网站内文章对你有帮助,请使用Ctrl+D收藏该网站!</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80-Mysql%E5%85%A5%E9%97%A8"><span class="toc-text">一.Mysql入门</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-SQL"><span class="toc-text">1.SQL</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-SQL%E9%80%9A%E7%94%A8%E8%AF%AD%E6%B3%95"><span class="toc-text">1.1 SQL通用语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-SQL%E5%88%86%E7%B1%BB"><span class="toc-text">1.2 SQL分类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-DDL-%E6%95%B0%E6%8D%AE%E5%AE%9A%E4%B9%89%E8%AF%AD%E8%A8%80"><span class="toc-text">1.3 DDL-数据定义语言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-DML-%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C%E8%AF%AD%E8%A8%80"><span class="toc-text">1.4 DML-数据操作语言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-5-DQL-%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80"><span class="toc-text">1.5 DQL-数据查询语言</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-1-%E5%9F%BA%E7%A1%80%E6%9F%A5%E8%AF%A2"><span class="toc-text">1.5.1 基础查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-2-%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2"><span class="toc-text">1.5.2 条件查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-3-%E8%81%9A%E5%90%88%E5%87%BD%E6%95%B0"><span class="toc-text">1.5.3 聚合函数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-4-%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2"><span class="toc-text">1.5.4 分组查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-5-%E6%8E%92%E5%BA%8F%E6%9F%A5%E8%AF%A2"><span class="toc-text">1.5.5 排序查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-6-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-text">1.5.6 分页查询</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-7-DQL%E8%AF%AD%E5%8F%A5%E6%A1%88%E4%BE%8B"><span class="toc-text">1.5.7 DQL语句案例</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-5-8-%E6%89%A7%E8%A1%8C%E9%A1%BA%E5%BA%8F"><span class="toc-text">1.5.8 执行顺序</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-6-DCL-%E6%95%B0%E6%8D%AE%E6%8E%A7%E5%88%B6%E8%AF%AD%E8%A8%80"><span class="toc-text">1.6 DCL-数据控制语言</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-6-1-%E7%AE%A1%E7%90%86%E7%94%A8%E6%88%B7"><span class="toc-text">1.6.1 管理用户</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-6-2-%E6%9D%83%E9%99%90%E6%8E%A7%E5%88%B6"><span class="toc-text">1.6.2 权限控制</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E5%87%BD%E6%95%B0"><span class="toc-text">2.函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%87%BD%E6%95%B0"><span class="toc-text">2.1 字符串函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E6%95%B0%E5%80%BC%E5%87%BD%E6%95%B0"><span class="toc-text">2.2 数值函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E6%97%A5%E6%9C%9F%E5%87%BD%E6%95%B0"><span class="toc-text">2.3 日期函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-%E6%B5%81%E7%A8%8B%E5%87%BD%E6%95%B0"><span class="toc-text">2.4 流程函数</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E7%BA%A6%E6%9D%9F"><span class="toc-text">3.约束</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-%E6%A6%82%E8%BF%B0"><span class="toc-text">3.1 概述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E7%BA%A6%E6%9D%9F%E6%BC%94%E7%A4%BA"><span class="toc-text">3.2 约束演示</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-%E5%A4%96%E9%94%AE%E7%BA%A6%E6%9D%9F"><span class="toc-text">3.3 外键约束</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-1-%E4%BB%8B%E7%BB%8D"><span class="toc-text">3.3.1 介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-2-%E8%AF%AD%E6%B3%95"><span class="toc-text">3.3.2 语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-3-3-%E5%88%A0%E9%99%A4-%E6%9B%B4%E6%96%B0%E8%A1%8C%E4%B8%BA"><span class="toc-text">3.3.3 删除/更新行为</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2"><span class="toc-text">4.多表查询</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E5%A4%9A%E8%A1%A8%E5%85%B3%E7%B3%BB"><span class="toc-text">4.1 多表关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E6%A6%82%E8%BF%B0"><span class="toc-text">4.2 多表查询概述</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-1-%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87"><span class="toc-text">4.2.1 数据准备</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-2-%E6%A6%82%E8%BF%B0"><span class="toc-text">4.2.2 概述</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-3-%E5%88%86%E7%B1%BB"><span class="toc-text">4.2.3 分类</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E5%86%85%E8%BF%9E%E6%8E%A5"><span class="toc-text">4.3 内连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-4-%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="toc-text">4.4 外连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-5-%E8%87%AA%E8%BF%9E%E6%8E%A5"><span class="toc-text">4.5 自连接</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-6-%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-text">4.6 联合查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-7-%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-text">4.7 子查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-8-%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E6%A1%88%E4%BE%8B"><span class="toc-text">4.8 多表查询案例</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E4%BA%8B%E5%8A%A1"><span class="toc-text">5.事务</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E4%BA%8B%E5%8A%A1%E4%BB%8B%E7%BB%8D"><span class="toc-text">5.1 事务介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-%E4%BA%8B%E5%8A%A1%E6%93%8D%E4%BD%9C"><span class="toc-text">5.2 事务操作</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-%E4%BA%8B%E5%8A%A1%E5%9B%9B%E5%A4%A7%E7%89%B9%E6%80%A7"><span class="toc-text">5.3 事务四大特性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-4-%E5%B9%B6%E5%8F%91%E4%BA%8B%E5%8A%A1%E9%97%AE%E9%A2%98"><span class="toc-text">5.4 并发事务问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-5-%E4%BA%8B%E5%8A%A1%E9%9A%94%E7%A6%BB%E7%BA%A7%E5%88%AB"><span class="toc-text">5.5 事务隔离级别</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C-MySql%E8%BF%9B%E9%98%B6"><span class="toc-text">二.MySql进阶</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%AD%98%E5%82%A8%E5%BC%95%E6%93%8E"><span class="toc-text">1.存储引擎</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-MySql%E7%9A%84%E4%BD%93%E7%B3%BB%E7%BB%93%E6%9E%84"><span class="toc-text">1.1 MySql的体系结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E5%AD%98%E5%82%A8%E5%BC%95%E6%93%8E%E7%AE%80%E4%BB%8B"><span class="toc-text">1.2 存储引擎简介</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-%E5%AD%98%E5%82%A8%E5%BC%95%E6%93%8E%E7%89%B9%E7%82%B9"><span class="toc-text">1.3 存储引擎特点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-%E5%AD%98%E5%82%A8%E5%BC%95%E6%93%8E%E7%9A%84%E9%80%89%E6%8B%A9"><span class="toc-text">1.4 存储引擎的选择</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E7%B4%A2%E5%BC%95"><span class="toc-text">2.索引</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E7%B4%A2%E5%BC%95%E6%A6%82%E8%BF%B0"><span class="toc-text">2.1 索引概述</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-1-%E4%BB%8B%E7%BB%8D"><span class="toc-text">2.1.1 介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-2-%E6%BC%94%E7%A4%BA"><span class="toc-text">2.1.2 演示</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-1-3-%E4%BC%98%E7%BC%BA%E7%82%B9"><span class="toc-text">2.1.3 优缺点</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E7%B4%A2%E5%BC%95%E7%BB%93%E6%9E%84"><span class="toc-text">2.2 索引结构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-1-%E7%B4%A2%E5%BC%95%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E6%9C%89%E5%93%AA%E4%BA%9B"><span class="toc-text">2.2.1 索引的数据结构有哪些</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-2-B-%E6%A0%91%E7%9A%84%E7%B4%A2%E5%BC%95%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-text">2.2.2 B+树的索引数据结构</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-2-3-hash%E7%9A%84%E7%B4%A2%E5%BC%95%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-text">2.2.3 hash的索引数据结构</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-3-4-%E6%80%9D%E8%80%83%E9%A2%98"><span class="toc-text">2.3.4 思考题</span></a></li><li class="toc-item toc-level-4"><a class="toc-link"><span class="toc-text"></span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-%E7%B4%A2%E5%BC%95%E5%88%86%E7%B1%BB"><span class="toc-text">2.3 索引分类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-%E7%B4%A2%E5%BC%95%E8%AF%AD%E6%B3%95"><span class="toc-text">2.4 索引语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-SQL%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90"><span class="toc-text">2.5 SQL性能分析</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-1-SQL%E6%89%A7%E8%A1%8C%E9%A2%91%E7%8E%87"><span class="toc-text">2.5.1 SQL执行频率</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-2-%E6%85%A2%E6%9F%A5%E8%AF%A2%E6%97%A5%E5%BF%97"><span class="toc-text">2.5.2 慢查询日志</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-3-profile%E8%AF%A6%E6%83%85"><span class="toc-text">2.5.3 profile详情</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-5-4-explain%E6%89%A7%E8%A1%8C%E8%AE%A1%E5%88%92"><span class="toc-text">2.5.4 explain执行计划</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-6-%E7%B4%A2%E5%BC%95%E4%BD%BF%E7%94%A8"><span class="toc-text">2.6 索引使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-1-%E9%AA%8C%E8%AF%81%E7%B4%A2%E5%BC%95%E6%95%88%E7%8E%87"><span class="toc-text">2.6.1 验证索引效率</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-2-%E6%9C%80%E5%B7%A6%E5%89%8D%E7%BC%80%E6%B3%95%E5%88%99"><span class="toc-text">2.6.2 最左前缀法则</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-3-%E7%B4%A2%E5%BC%95%E5%A4%B1%E6%95%88%E7%9A%84%E6%83%85%E5%86%B5"><span class="toc-text">2.6.3 索引失效的情况</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-4-SQL%E6%8F%90%E7%A4%BA"><span class="toc-text">2.6.4 SQL提示</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-5-%E8%A6%86%E7%9B%96%E7%B4%A2%E5%BC%95"><span class="toc-text">2.6.5 覆盖索引</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-6-%E5%89%8D%E7%BC%80%E7%B4%A2%E5%BC%95"><span class="toc-text">2.6.6 前缀索引</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-6-7-%E5%8D%95%E4%BE%8B%E7%B4%A2%E5%BC%95%E4%B8%8E%E8%81%94%E5%90%88%E7%B4%A2%E5%BC%95%E7%9A%84%E9%80%89%E6%8B%A9%E9%97%AE%E9%A2%98"><span class="toc-text">2.6.7 单例索引与联合索引的选择问题</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-%E7%B4%A2%E5%BC%95%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99"><span class="toc-text">2.7 索引设计原则</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-SQL%E4%BC%98%E5%8C%96"><span class="toc-text">3.SQL优化</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-%E6%8F%92%E5%85%A5%E6%95%B0%E6%8D%AE%E7%9A%84%E4%BC%98%E5%8C%96"><span class="toc-text">3.1 插入数据的优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E4%B8%BB%E9%94%AE%E4%BC%98%E5%8C%96"><span class="toc-text">3.2 主键优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-order-by%E4%BC%98%E5%8C%96"><span class="toc-text">3.3 order by优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-group-by%E4%BC%98%E5%8C%96"><span class="toc-text">3.4 group by优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-5-limit%E4%BC%98%E5%8C%96"><span class="toc-text">3.5 limit优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-6-count%E4%BC%98%E5%8C%96"><span class="toc-text">3.6 count优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-7-update%E4%BC%98%E5%8C%96"><span class="toc-text">3.7 update优化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-8-%E6%80%BB%E7%BB%93"><span class="toc-text">3.8 总结</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E8%A7%86%E5%9B%BE-%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B-%E8%A7%A6%E5%8F%91%E5%99%A8"><span class="toc-text">4.视图/存储过程/触发器</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E8%A7%86%E5%9B%BE"><span class="toc-text">4.1 视图</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-1-%E8%A7%86%E5%9B%BE%E4%BB%8B%E7%BB%8D"><span class="toc-text">4.1.1 视图介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-2-%E8%AF%AD%E6%B3%95"><span class="toc-text">4.1.2 语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-3-%E6%A3%80%E6%9F%A5%E9%80%89%E9%A1%B9"><span class="toc-text">4.1.3 检查选项</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-4-%E8%A7%86%E5%9B%BE%E6%9B%B4%E6%96%B0"><span class="toc-text">4.1.4 视图更新</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-5-%E8%A7%86%E5%9B%BE%E4%BD%9C%E7%94%A8"><span class="toc-text">4.1.5 视图作用</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-1-6-%E8%A7%86%E5%9B%BE%E6%A1%88%E4%BE%8B"><span class="toc-text">4.1.6 视图案例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B"><span class="toc-text">4.2 存储过程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-1-%E4%BB%8B%E7%BB%8D"><span class="toc-text">4.2.1 介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-2-%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95"><span class="toc-text">4.2.2 基本语法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-3-%E5%8F%98%E9%87%8F"><span class="toc-text">4.2.3 变量</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#4-2-3-1-%E7%B3%BB%E7%BB%9F%E5%8F%98%E9%87%8F"><span class="toc-text">4.2.3.1 系统变量</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-2-3-2-%E7%94%A8%E6%88%B7%E5%AE%9A%E4%B9%89%E5%8F%98%E9%87%8F"><span class="toc-text">4.2.3.2 用户定义变量</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-2-3-3-%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F"><span class="toc-text">4.2.3.3 局部变量</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-4-if%E5%88%A4%E6%96%AD"><span class="toc-text">4.2.4 if判断</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-5-%E5%8F%82%E6%95%B0"><span class="toc-text">4.2.5 参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-6-case"><span class="toc-text">4.2.6 case</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最近更新</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/posts/8957.html" title="Linux从入门到进阶"><img src="/img/8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux从入门到进阶"/></a><div class="content"><a class="title" href="/posts/8957.html" title="Linux从入门到进阶">Linux从入门到进阶</a><time datetime="2024-05-10T05:31:11.691Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/35630.html" title="接口测试工具"><img src="/img/2.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="接口测试工具"/></a><div class="content"><a class="title" href="/posts/35630.html" title="接口测试工具">接口测试工具</a><time datetime="2024-05-10T05:31:11.686Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/32246.html" title="SpringBoot中整合Swagger2"><img src="/img/3.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="SpringBoot中整合Swagger2"/></a><div class="content"><a class="title" href="/posts/32246.html" title="SpringBoot中整合Swagger2">SpringBoot中整合Swagger2</a><time datetime="2024-05-10T05:31:11.681Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/19306.html" title="Docker容器化技术"><img src="/img/1.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Docker容器化技术"/></a><div class="content"><a class="title" href="/posts/19306.html" title="Docker容器化技术">Docker容器化技术</a><time datetime="2024-05-10T05:31:11.675Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/posts/20683.html" title="Linux中开发环境的搭建"><img src="/img/8.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux中开发环境的搭建"/></a><div class="content"><a class="title" href="/posts/20683.html" title="Linux中开发环境的搭建">Linux中开发环境的搭建</a><time datetime="2024-05-10T05:31:11.669Z" title="更新于 2024-05-10 13:31:11">2024-05-10</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comment-alt"></i></a><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/cdn/js/medium-zoom.min.js"></script><script src="/cdn/js/instantpage.min.js" type="module"></script><script src="/cdn/js/snackbar.min.js"></script><div class="js-pjax"><script>function loadGitalk () {
|
||
function initGitalk () {
|
||
var gitalk = new Gitalk(Object.assign({
|
||
clientID: '00fb27b1e484536359c2',
|
||
clientSecret: 'be41a12281c68b6e228d1a27e8d08aeb91541145',
|
||
repo: 'BlogComment',
|
||
owner: 'JasonsGong',
|
||
admin: ['JasonsGong'],
|
||
id: '88a8a56431f67dee0fe3c4bc56fc793b',
|
||
updateCountCallback: commentCount
|
||
},null))
|
||
|
||
gitalk.render('gitalk-container')
|
||
}
|
||
|
||
if (typeof Gitalk === 'function') initGitalk()
|
||
else {
|
||
getCSS('/cdn/css/gitalk.min.css')
|
||
getScript('/cdn/js/gitalk.min.js').then(initGitalk)
|
||
}
|
||
}
|
||
|
||
function commentCount(n){
|
||
let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
|
||
if (isCommentCount) {
|
||
isCommentCount.textContent= n
|
||
}
|
||
}
|
||
|
||
if ('Gitalk' === 'Gitalk' || !true) {
|
||
if (true) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
|
||
else loadGitalk()
|
||
} else {
|
||
function loadOtherComment () {
|
||
loadGitalk()
|
||
}
|
||
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><br/><div class="no-result" id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div><!-- hexo injector body_end start --><script data-pjax>
|
||
function butterfly_swiper_injector_config(){
|
||
var parent_div_git = document.getElementById('recent-posts');
|
||
var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/19306.html" alt=""><img width="48" height="48" src="/img/1.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-04-21</span><a class="blog-slider__title" href="posts/19306.html" alt="">Docker容器化技术</a><div class="blog-slider__text">Docker</div><a class="blog-slider__button" href="posts/19306.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/47003.html" alt=""><img width="48" height="48" src="/img/5.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-03-10</span><a class="blog-slider__title" href="posts/47003.html" alt="">常用正则表达式大全</a><div class="blog-slider__text">正则表达式</div><a class="blog-slider__button" href="posts/47003.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/20683.html" alt=""><img width="48" height="48" src="/img/8.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-05</span><a class="blog-slider__title" href="posts/20683.html" alt="">Linux中开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/20683.html" alt="">详情 </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" href="posts/63333.html" alt=""><img width="48" height="48" src="/img/10.png" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2023-06-03</span><a class="blog-slider__title" href="posts/63333.html" alt="">开发环境的搭建</a><div class="blog-slider__text">环境搭建</div><a class="blog-slider__button" href="posts/63333.html" alt="">详情 </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
|
||
if (parent_div_git !== null && typeof parent_div_git !== 'undefined') {
|
||
parent_div_git.insertAdjacentHTML("afterbegin",item_html)
|
||
}
|
||
}
|
||
var elist = 'undefined'.split(',');
|
||
var cpage = location.pathname;
|
||
var epage = 'all';
|
||
var flag = 0;
|
||
|
||
for (var i=0;i<elist.length;i++){
|
||
if (cpage.includes(elist[i])){
|
||
flag++;
|
||
}
|
||
}
|
||
|
||
if ((epage ==='all')&&(flag == 0)){
|
||
butterfly_swiper_injector_config();
|
||
}
|
||
else if (epage === cpage){
|
||
butterfly_swiper_injector_config();
|
||
}
|
||
</script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><!-- hexo injector body_end end --></body></html> |