-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
474 lines (228 loc) · 470 KB
/
atom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>松果会</title>
<subtitle>勇敢追寻着,依然热爱着</subtitle>
<link href="http://example.com/atom.xml" rel="self"/>
<link href="http://example.com/"/>
<updated>2023-10-16T11:33:25.684Z</updated>
<id>http://example.com/</id>
<author>
<name>松花鼠</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>窗口函数</title>
<link href="http://example.com/posts/6648.html"/>
<id>http://example.com/posts/6648.html</id>
<published>2023-10-16T11:33:00.000Z</published>
<updated>2023-10-16T11:33:25.684Z</updated>
<content type="html"><![CDATA[<h2 id="课程资源"><a href="#课程资源" class="headerlink" title="课程资源"></a>课程资源</h2><p>窗口函数篇:<a href="https://www.bilibili.com/video/BV1iF411z7Pu?p=91">开始</a> ——- <a href="https://www.bilibili.com/video/BV1iF411z7Pu?p=98">结束</a></p><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>窗口函数是从MySQL8.0版本开始引入的,别名开窗函数,也被称为分析函数。</p><p>窗口函数和聚集函数是两种不同的函数,<font color=#9932CD>聚集函数用于计算并返回多行数据的单个值</font>,通常与GROUP BY一起使用,而<font color=#008c8c>窗口函数操作不会改变查询结果的行数,它会为每一行计算一个单独的值,然后将该值添加为新的一列</font>,窗口函数常常与OVER字句一起使用,用于定义数据分区和排序方式</p><p>常用的MySQL窗口函数如下:</p><ol><li>ROW_NUMBER:为查询结果集中的每一行分配一个唯一的行号</li><li>RANK:为查询结果集中的每一行分配一个排名,相同数值的行将获得相同的排名,下一个排名将根据出现的下降数量而增加</li><li>DENSE_RANK:与RANK类似,为查询结果集中的每一行分配一个密集排名,不会跳过排名</li><li>NTILE(n):将查询结果集均匀地划分为n组,每组的行数尽可能接近</li><li>LAG(column, offset, default):返回查询结果集中当前行之前偏移量为offset的行的指定列的值。如果偏移量超出了结果集的范围,可以指定一个默认值</li><li>LEAN(column, offset, default):返回查询结果集中当前行之后偏移量为offset的行的指定列的值。如果偏移量超出了结果集的范围,可以指定一个默认值</li><li>FIRST_VALUE(column) 和 LAST_VALUE(column):分别返回查询结果集中第一行和最后一行的指定列的值</li></ol><p><strong>语法结构</strong></p><figure class="highlight plaintext"><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"><窗口函数> OVER(</span><br><span class="line">[PARTITION BY <列名1>,<列名2>,...]</span><br><span class="line"> [ORDER BY <排序列1>[ASC|DESC],<排序列2>[ASC|DESC],...]</span><br><span class="line"> [<窗口帧>]</span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"># PARTITION BY(可选):用于定义窗口的分区方式,指定列名将结果集划分为多个分区,每个分区内的数据会单独计算窗口函数的值,如果省略,则整个结果集被视为一个分区</span><br><span class="line"># ORDER BY(可选):用于定义窗口内的排序方式,指定列名和排序方向。如果省略,则窗口函数将使用默认排序顺序</span><br><span class="line"># 窗口帧:用于确定窗口函数在分区内的计算范围。窗口帧分为行窗口帧和范围窗口帧。行窗口帧使用ROWS关键字,范围窗口帧使用RANGE关键字。通过指定起始行和结束行,可以定义窗口函数的计算范围。如果省略,则默认使用整个分区作为计算范围</span><br></pre></td></tr></table></figure><h2 id="排名函数"><a href="#排名函数" class="headerlink" title="排名函数"></a>排名函数</h2><p>ROW_NUMBER()、RANK()、DENSE_RANK()又被称为排名函数,用于为查询结果集中每行数据分配一个排名值</p><p><strong>实践</strong></p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903213147375.png" alt="image-20230903213147375"></p><figure class="highlight plaintext"><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><br><span class="line">select </span><br><span class="line">dname,</span><br><span class="line">ename,</span><br><span class="line">salary,</span><br><span class="line">row_number() over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by salary desc</span><br><span class="line"> ) t</span><br><span class="line">from </span><br><span class="line">employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903194446236.png" alt="image-20230903194446236"></p><figure class="highlight plaintext"><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><br><span class="line">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> rank() over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by salary desc</span><br><span class="line"> ) t</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903194606526.png" alt="image-20230903194606526"></p><figure class="highlight plaintext"><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><br><span class="line">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> dense_rank() over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by salary desc</span><br><span class="line"> ) t</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903194909339.png" alt="image-20230903194909339"></p><figure class="highlight plaintext"><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></pre></td><td class="code"><pre><span class="line"># 求出每个部门薪资排在前三名的员工 - 分组求TOPN</span><br><span class="line">select </span><br><span class="line">*</span><br><span class="line">from</span><br><span class="line">(</span><br><span class="line">select </span><br><span class="line">dname,</span><br><span class="line">ename,</span><br><span class="line">salary,</span><br><span class="line">dense_rank() over(</span><br><span class="line">partition by dname</span><br><span class="line">order by salary desc</span><br><span class="line">) nr</span><br><span class="line">from </span><br><span class="line">employee</span><br><span class="line">) t</span><br><span class="line">where t.nr <= 3</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903195925558.png" alt="image-20230903195925558"></p><h2 id="开窗聚合函数"><a href="#开窗聚合函数" class="headerlink" title="开窗聚合函数"></a>开窗聚合函数</h2><p>开窗聚合函数是一类特殊的窗口函数,它们结合了聚合操作和窗口计算的功能</p><p>常见的开窗聚合函数包括:</p><ul><li>SUM() OVER():计算指定列或表达式在窗口中的总和</li><li>AVG() OVER(): 计算指定列或表达式在窗口中的平均值</li><li>COUNT() OVER(): 计算指定列或表达式在窗口中的行数</li><li>MIN() OVER(): 计算指定列或表达式在窗口中的最小值</li><li>MAX() OVER(): 计算指定列或表达式在窗口中的最大值</li></ul><figure class="highlight plaintext"><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">select </span><br><span class="line">dname,</span><br><span class="line">ename,</span><br><span class="line">salary,</span><br><span class="line">sum(salary) over(</span><br><span class="line">partition by dname</span><br><span class="line">order by hiredate</span><br><span class="line">) nr</span><br><span class="line">from </span><br><span class="line">employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903201801787.png" alt="image-20230903201801787"></p><figure class="highlight plaintext"><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></pre></td><td class="code"><pre><span class="line">select </span><br><span class="line">dname,</span><br><span class="line">ename,</span><br><span class="line">salary,</span><br><span class="line">sum(salary) over(</span><br><span class="line">partition by dname</span><br><span class="line">) nr</span><br><span class="line">from </span><br><span class="line">employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903201916968.png" alt="image-20230903201916968"></p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> sum(salary) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line">rows between 3 preceding and current row</span><br><span class="line"> ) nr</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903202500653.png" alt="image-20230903202500653"></p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> sum(salary) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line">rows between 3 preceding and 1 following</span><br><span class="line"> ) nr</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903202852173.png" alt="image-20230903202852173"></p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> sum(salary) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line">rows between current row and unbounded following</span><br><span class="line"> ) nr</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903203041749.png" alt="image-20230903203041749"></p><h2 id="累积分布函数"><a href="#累积分布函数" class="headerlink" title="累积分布函数"></a>累积分布函数</h2><p>CUME_DIST、PERCENT_RANK、NTILE(n)也被称为累积分布函数</p><ul><li>CUME_DIST函数用于计算给定值在排序结果中的累积分布,返回值为在排序结果中小于或等于给定值的行数占总行数的比例</li><li>CUME_DIST函数返回的结果介于0和1之间</li><li>PERCENT_RANK函数用于计算给定值在排序结果中的百分数,返回值为在排序结果中小于给定值的行数占总行数的比例</li><li>NTILE(n)函数将<strong>排序后</strong>的数据<strong>等分</strong>为n个桶,然后返回给定值所属的桶编号,返回值为1到n之间的整数</li></ul><figure class="highlight plaintext"><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><br><span class="line">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line">cume_dist() over(</span><br><span class="line"> order by salary</span><br><span class="line"> ) nr1,</span><br><span class="line"> cume_dist() over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by salary</span><br><span class="line"> ) nr2</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903205554016.png" alt="image-20230903205554016"></p><figure class="highlight plaintext"><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><br><span class="line">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line">rank() over(</span><br><span class="line">partition by dname</span><br><span class="line"> order by salary desc</span><br><span class="line"> ) nr1,</span><br><span class="line"> percent_rank() over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by salary desc</span><br><span class="line"> ) nr2</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903210056740.png" alt="image-20230903210056740"></p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> hiredate,</span><br><span class="line"> ntile(3) over(</span><br><span class="line">partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) nt</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903214259985.png" alt="image-20230903214259985"></p><figure class="highlight plaintext"><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><br><span class="line">select </span><br><span class="line">*</span><br><span class="line">from </span><br><span class="line">(</span><br><span class="line">select </span><br><span class="line">dname,</span><br><span class="line">ename,</span><br><span class="line">salary,</span><br><span class="line">hiredate,</span><br><span class="line">ntile(3) over(</span><br><span class="line">partition by dname</span><br><span class="line">order by hiredate</span><br><span class="line">) nt</span><br><span class="line">from </span><br><span class="line">employee</span><br><span class="line">) t</span><br><span class="line">where t.nt = 1</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903214510383.png" alt="image-20230903214510383"></p><h2 id="LEAD-和-LAG"><a href="#LEAD-和-LAG" class="headerlink" title="LEAD 和 LAG"></a>LEAD 和 LAG</h2><p>LEAD函数用于获取当前行之后的指定行的值,而LAG函数用于获取当前行之前指定行的值</p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line">hiredate,</span><br><span class="line">lag(hiredate, 1, '2000-01-01') over(</span><br><span class="line">partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) nr1,</span><br><span class="line"> lag(hiredate, 2) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) nr2</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903213238922.png" alt="image-20230903213238922"></p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line">hiredate,</span><br><span class="line">lead(hiredate, 1, '2000-01-01') over(</span><br><span class="line">partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) nr1,</span><br><span class="line"> lead(hiredate, 2) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) nr2</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903213102948.png" alt="image-20230903213102948"></p><h2 id="FIRST-VALUE、LAST-VALUE、NTH-VALUE"><a href="#FIRST-VALUE、LAST-VALUE、NTH-VALUE" class="headerlink" title="FIRST_VALUE、LAST_VALUE、NTH_VALUE"></a>FIRST_VALUE、LAST_VALUE、NTH_VALUE</h2><p>FIRST_VALUE函数用于获取窗口排序后的第一个非空值,LAST_VALUE函数用于获取窗口排序后的最后一个非空值,NTH_VALUE函数用于获取窗口排序后指定位置的值</p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line">hiredate,</span><br><span class="line">first_value(salary) over(</span><br><span class="line">partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) first,</span><br><span class="line"> last_value(salary) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) last</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903213319417.png" alt="image-20230903213319417"></p><figure class="highlight plaintext"><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">select </span><br><span class="line"> dname,</span><br><span class="line"> ename,</span><br><span class="line"> salary,</span><br><span class="line"> hiredate,</span><br><span class="line"> nth_value(salary, 2) over(</span><br><span class="line">partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) rank_s,</span><br><span class="line"> nth_value(salary, 3) over(</span><br><span class="line"> partition by dname</span><br><span class="line"> order by hiredate</span><br><span class="line"> ) rank_t</span><br><span class="line">from </span><br><span class="line"> employee</span><br><span class="line">;</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20230903213749068.png" alt="image-20230903213749068"></p>]]></content>
<summary type="html"><h2 id="课程资源"><a href="#课程资源" class="headerlink" title="课程资源"></a>课程资源</h2><p>窗口函数篇:<a href="https://www.bilibili.com/video/BV1iF411z7Pu?p=9</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>LeetCode刷题</title>
<link href="http://example.com/posts/6647.html"/>
<id>http://example.com/posts/6647.html</id>
<published>2023-10-16T09:42:00.000Z</published>
<updated>2023-10-16T11:32:35.353Z</updated>
<content type="html"><![CDATA[<h2 id="顺序"><a href="#顺序" class="headerlink" title="顺序"></a>顺序</h2><p>先易后难,题号如下:</p><ol><li>175、577(第一天)</li><li>596、595、627、586、610、607、1050、181、197、196、183、182、1068、620、619、584、511、1075(第二天)</li><li>1084、1141、1148、1211、1251、1280、1327、1378、1407、1484、1517、1527、1581、1587、1633、1661、1667、1683、1693、1729、1731(第三天)</li><li>1741、1757、1789、1795、1873、1890、1965、1978、2356、178(第四天)</li><li>180、176、184、550、570、585(第五天)</li><li>602、608、626、1045、177、1070、1158、1164、1174、1179(第六天)</li><li>1193、1204、1321、1341、1393、1907、1934、185、262、601(第七天)</li></ol>]]></content>
<summary type="html"><h2 id="顺序"><a href="#顺序" class="headerlink" title="顺序"></a>顺序</h2><p>先易后难,题号如下:</p>
<ol>
<li>175、577(第一天)</li>
<li>596、595、627、586、610、607、</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>MySQL运维篇</title>
<link href="http://example.com/posts/6646.html"/>
<id>http://example.com/posts/6646.html</id>
<published>2023-10-16T06:26:00.000Z</published>
<updated>2023-10-16T11:31:25.982Z</updated>
<content type="html"><![CDATA[<h2 id="日志"><a href="#日志" class="headerlink" title="日志"></a>日志</h2><h3 id="错误日志"><a href="#错误日志" class="headerlink" title="错误日志"></a>错误日志</h3><p>错误日志是 MySQL 中最重要的日志之一,它记录了当mysqld 启动、停止以及服务器在运行过程中发生任何严重错误时的相关信息</p><p>当数据库出现故障导致无法正常使用时,建议首先查看此日志</p><p>该日志是默认开启的,默认存放目录 /var/log/,默认的日志文件名:mysqld.log。查看日志位置:<code>show variables like '%log_error%;'</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802204010374.png" alt="image-20230802204010374"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802204547856.png" alt="image-20230802204547856"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802204523041.png" alt="image-20230802204523041"></p><h3 id="二进制日志"><a href="#二进制日志" class="headerlink" title="二进制日志"></a>二进制日志</h3><p>二进制日志(BINLOG)记录了所有的DDL(数据定义语言)语句和DML(数据操纵语言)语句,但不包括数据查询(SELECT、SHOW)语句</p><p><strong>作用:</strong> </p><ol><li>灾难时的数据恢复</li><li>MySQL的主从复制。在MySQL8版本中,默认二进制日志是开启的,涉及到的参数:<code>show variables like '%log_bin%';</code></li></ol><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802205326428.png" alt="image-20230802205326428"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802205538047.png" alt="image-20230802205538047"></p><p><strong>日志格式</strong></p><p>MySQL 服务器中提供了多种格式来记录二进制日志,具体格式及特点如下</p><div class="table-container"><table><thead><tr><th>日志格式</th><th>含义</th></tr></thead><tbody><tr><td>STATEMENT</td><td>基于SQL语句的日志记录,记录的是SQL语句,对数据进行修改的SQL都会记录在日志文件中</td></tr><tr><td>ROW</td><td>基于行的日志记录,记录的是每一行的数据变更(默认)</td></tr><tr><td>MIXED</td><td>混合了STATEMENT和ROW两种格式,默认采用STATEMENT,在某些特殊情况下会自动切换为ROW进行记录</td></tr></tbody></table></div><p><code>show variables like '%binlog_format%';</code></p><p><strong>日志查看</strong></p><p>由于日志是以二进制方式存储的,不能直接读取,需要通过二进制日志查询工具 mysqlbinlog 来查看,具体语法</p><figure class="highlight sh"><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">mysqlbinlog [ 参数选项 ] logfilename</span><br><span class="line"></span><br><span class="line">参数选项:</span><br><span class="line">-d 指定数据库名称,只列出指定的数据库相关操作。</span><br><span class="line">-o 忽略掉日志中的前n行命令。</span><br><span class="line">-v 将行事件(数据变更)重构为SQL语句</span><br><span class="line">-vv 将行事件(数据变更)重构为SQL语句,并输出注释信息</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802210828528.png" alt="image-20230802210828528"></p><p><code>mysqlbinlog -v binlog.000012</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802210900577.png" alt="image-20230802210900577"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802211356685.png" alt="image-20230802211356685"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802211457848.png" alt="image-20230802211457848"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802211647134.png" alt="image-20230802211647134"></p><p><code>mysqlbinlog binlog.000013</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802211738859.png" alt="image-20230802211738859"></p><h4 id="日志删除"><a href="#日志删除" class="headerlink" title="日志删除"></a>日志删除</h4><p>对于比较繁忙的业务系统,每天生成的binlog数据巨大,如果长时间不清除,将会占用大量磁盘空间。可通过以下几种方式清理日志</p><div class="table-container"><table><thead><tr><th>指令</th><th>含义</th></tr></thead><tbody><tr><td>reset master</td><td>删除全部binlog日志,删除之后,日志编号,将从binlog.000001重新开始</td></tr><tr><td>purge master logs to ‘binlog.******‘</td><td>删除******编号之前的所有日志</td></tr><tr><td>purge master logs before ‘yyyy-mm-dd hh24:mi:ss’</td><td>删除日志为”yyyy-mm-dd hh24:mi:ss”之前产生的所有日志</td></tr></tbody></table></div><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802212641025.png" alt="image-20230802212641025"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802212641025.png" alt=""></p><p>也可以在mysql配置文件中配置二进制日志的过期时间,设置了之后,二进制日志过期会自动删除</p><p><code>show variables like '%binlog_expire_logs_seconds%';</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802212942953.png" alt="image-20230802212942953"></p><h3 id="查询日志"><a href="#查询日志" class="headerlink" title="查询日志"></a>查询日志</h3><p>查询日志中记录了客户端的所有操作语句,而二进制日志不包含查询数据的SQL语句。默认情况下,查询日志是==未开启==的。如果需要开启查询日志,可以设置一下配置</p><p><code>show variables like '%general%';</code></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802213157079.png" alt="image-20230802213157079"></p><p>修改MySQL的配置文件 /etc/my.cnf文件,添加如下内容</p><figure class="highlight properties"><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">#该选项用来开启查询日志 , 可选值 : 0 或者 1 ; 0 代表关闭, 1 代表开启</span></span><br><span class="line"></span><br><span class="line"><span class="attr">general_log</span>=<span class="string">1</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">#设置日志的文件名 , 如果没有指定, 默认的文件名为 host_name.log</span></span><br><span class="line"></span><br><span class="line"><span class="attr">general_log_file</span>=<span class="string">mysql_query.log</span></span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802213550545.png" alt="image-20230802213550545"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802213631584.png" alt="image-20230802213631584"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802213950108.png" alt="image-20230802213950108"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802214004044.png" alt="image-20230802214004044"></p><h3 id="慢查询日志"><a href="#慢查询日志" class="headerlink" title="慢查询日志"></a>慢查询日志</h3><p>慢查询日志记录了所有执行时间超过参数 long_query_time 设置值并且扫描记录数不小于 min_examined_row_limit 的所有的SQL语句的日志,默认未开启。long_query_time 默认为10 秒,最小为 0, 精度可以到微秒</p><p>如果需要开启慢查询日志,需要在MySQL的配置文件 /etc/my.cnf 中配置如下参数</p><figure class="highlight properties"><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><br><span class="line"><span class="attr">slow_query_log</span>=<span class="string">1</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><br><span class="line"><span class="attr">long_query_time</span>=<span class="string">2</span></span><br></pre></td></tr></table></figure><p>默认情况下,不会记录管理语句,也不会记录不使用索引进行查找的查询。可以使用 log_slow_admin_statements和 更改此行为 log_queries_not_using_indexes,如下所述</p><figure class="highlight properties"><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><br><span class="line"><span class="attr">log_slow_admin_statements</span> =<span class="string">1</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><br><span class="line"><span class="attr">log_queries_not_using_indexes</span> = <span class="string">1</span></span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><h2 id="日志"><a href="#日志" class="headerlink" title="日志"></a>日志</h2><h3 id="错误日志"><a href="#错误日志" class="headerlink" title="错误日志"></a>错误日志</h</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>MySQL管理</title>
<link href="http://example.com/posts/6645.html"/>
<id>http://example.com/posts/6645.html</id>
<published>2023-10-15T03:30:00.000Z</published>
<updated>2023-10-15T03:28:19.421Z</updated>
<content type="html"><![CDATA[<h2 id="系统数据库"><a href="#系统数据库" class="headerlink" title="系统数据库"></a>系统数据库</h2><p>MySQL数据库安装完成后,自带了四个数据库,具体作用如下</p><div class="table-container"><table><thead><tr><th>数据库</th><th>含义</th></tr></thead><tbody><tr><td>mysql</td><td>存储MySQL服务器正常运行所需要的各种信息(时区、主从、用户、权限等)</td></tr><tr><td>information_schema</td><td>提供了访问数据元数据的各种表和视图,包含数据库、表、字段类型及访问权限等</td></tr><tr><td>performance_schema</td><td>为MySQL服务器运行时状态提供了一个底层监控功能,主要用于收集数据库服务器性能参数</td></tr><tr><td>sys</td><td>包含了一系列方便DBA和开发人员利用performance_schema性能数据库进行性能调优和诊断的视图</td></tr></tbody></table></div><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802171211562.png" alt="image-20230802171211562"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802171742100.png" alt="image-20230802171742100"></p><h2 id="常用工具"><a href="#常用工具" class="headerlink" title="常用工具"></a>常用工具</h2><p><strong>mysql</strong></p><p>这里指的是mysql客户端工具,而不是mysql服务</p><p><strong>语法:</strong> <code>mysql [options] [database]</code></p><p><strong>选项:</strong> </p><ul><li>-u, —user=name # 指定用户名</li><li>-p, —password[=name] # 指定密码</li><li>-h, —host=name # 指定服务器IP或域名</li><li>-p, —port=port # 指定连接端口</li><li>-e, —execute=name # 执行SQL语句并退出</li></ul><p>-e选项可以在MySQL客户端执行SQL语句,而不用连接到MySQL数据库再执行,对于一些批处理脚本,这种方式尤为方便</p><p>例如:<code>mysql -uroot -p123456 itcast -e "select * from stu";</code></p><p><strong>mysqladmin</strong></p><p>mysqladmin是一个执行管理操作的客户端程序。可以用它来检查服务器的配置和当前状态、创建并删除数据库等</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802173217494.png" alt="image-20230802173217494"></p><p><strong>mysqlbinlog</strong></p><p>由于服务器生成的二进制日志文件以二进制格式保存,所以如果想要检查这些文本的文本格式,就会使用到mysqlbinlog日志管理工具</p><figure class="highlight sh"><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><br><span class="line">mysqlbinlog [options] log-files1 log-files2 ...</span><br><span class="line">选项 :</span><br><span class="line">-d, --database=name 指定数据库名称,只列出指定的数据库相关操作。</span><br><span class="line">-o, --offset=<span class="comment"># 忽略掉日志中的前n行命令。</span></span><br><span class="line">-r,--result-file=name 将输出的文本格式日志输出到指定文件。</span><br><span class="line">-s, --short-form 显示简单格式, 省略掉一些信息。</span><br><span class="line">--start-datatime=date1 --stop-datetime=date2 指定日期间隔内的所有日志。</span><br><span class="line">--start-position=pos1 --stop-position=pos2 指定位置间隔内的所有日志。</span><br></pre></td></tr></table></figure><p><strong>mysqlshow</strong></p><p>客户端对象查找工具,用来很快地查找存在哪些数据库、数据库中的表、表中的列或者索引</p><figure class="highlight sh"><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></pre></td><td class="code"><pre><span class="line">语法 :</span><br><span class="line">mysqlshow [options] [db_name [table_name [col_name]]]</span><br><span class="line">选项 :</span><br><span class="line">--count 显示数据库及表的统计信息(数据库,表 均可以不指定)</span><br><span class="line">-i 显示指定数据库或者指定表的状态信息</span><br><span class="line">示例:</span><br><span class="line"><span class="comment">#查询test库中每个表中的字段书,及行数</span></span><br><span class="line">mysqlshow -uroot -p2143 <span class="built_in">test</span> --count</span><br><span class="line"><span class="comment">#查询test库中book表的详细情况</span></span><br><span class="line">mysqlshow -uroot -p2143 <span class="built_in">test</span> book --count</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802174631913.png" alt="image-20230802174631913"></p><p><strong>mysqldump</strong></p><p>用来备份数据库或在不同数据库之间进行数据迁移。备份内容包含创建表,及插入表的SQL语句</p><figure class="highlight sh"><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><br><span class="line">mysqldump [options] db_name [tables]</span><br><span class="line">mysqldump [options] --database/-B db1 [db2 db3...]</span><br><span class="line">mysqldump [options] --all-databases/-A</span><br><span class="line">连接选项 :</span><br><span class="line">-u, --user=name 指定用户名</span><br><span class="line">-p, --password[=name] 指定密码</span><br><span class="line">-h, --host=name 指定服务器ip或域名</span><br><span class="line">-P, --port=<span class="comment"># 指定连接端口</span></span><br><span class="line">输出选项:</span><br><span class="line">--add-drop-database 在每个数据库创建语句前加上 drop database 语句</span><br><span class="line">--add-drop-table 在每个表创建语句前加上 drop table 语句 , 默认开启 ; 不</span><br><span class="line">开启 (--skip-add-drop-table)</span><br><span class="line">-n, --no-create-db 不包含数据库的创建语句</span><br><span class="line">-t, --no-create-info 不包含数据表的创建语句</span><br><span class="line">-d --no-data 不包含数据</span><br><span class="line">-T, --tab=name 自动生成两个文件:一个.sql文件,创建表结构的语句;一个.txt文件,数据文件</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802175511444.png" alt="image-20230802175511444"></p><p><strong>mysqlimport</strong></p><p>用来导入mysqldump 加 -T 参数后导出的文本文件</p><figure class="highlight sh"><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><br><span class="line">mysqlimport [options] db_name textfile1 [textfile2...]</span><br><span class="line">示例 :</span><br><span class="line">mysqlimport -uroot -p2143 <span class="built_in">test</span> /tmp/city.txt</span><br></pre></td></tr></table></figure><p><strong>source</strong></p><p>如果需要导入sql文件,可以使用 source指令,这是针对Linux系统所使用的指令</p><p>对于Windows,可以使用navicat或datagrip导入sql文件</p><p><strong>过程如下</strong></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802182400124.png" alt="image-20230802182400124"></p><p>发现报错:<code>No databse selected</code>,解决是新建一个itcast数据库,然后运行sql文件</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802182452057.png" alt="image-20230802182452057"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802182531243.png" alt="image-20230802182531243"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802182559284.png" alt="image-20230802182559284"></p>]]></content>
<summary type="html"><h2 id="系统数据库"><a href="#系统数据库" class="headerlink" title="系统数据库"></a>系统数据库</h2><p>MySQL数据库安装完成后,自带了四个数据库,具体作用如下</p>
<div class="table-contai</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>InnoDB引擎</title>
<link href="http://example.com/posts/6644.html"/>
<id>http://example.com/posts/6644.html</id>
<published>2023-10-15T03:25:00.000Z</published>
<updated>2023-10-15T03:27:17.090Z</updated>
<content type="html"><![CDATA[<h2 id="逻辑存储结构"><a href="#逻辑存储结构" class="headerlink" title="逻辑存储结构"></a>逻辑存储结构</h2><p>表空间(idb文件),一个mysql实例可以对应多个表空间,用于存储记录、索引等数据</p><p>段分为数据段(Leaf node segment)、索引段(Non-leaf node segment)、回滚段(Rollback segment),InnoDB是索引组织表,数据段就是B+树的叶子节点,索引段即为B+树的非叶子节点。段用来管理多个Exten(区)</p><p>区是表空间的单元结构,每个区的大小为1M,默认情况下,InnoDB存储引擎页大小为16k,即一个区中一共有64个连续的页</p><p>页是InnoDB存储引擎磁盘管理的最小单元,每个页的大小默认为16KB。为了保证页的连续性,InnoDB存储引擎每次从磁盘申请4-5个区</p><p>InnoDB存储引擎数据是按行进行存放的</p><p>Trx_id:每次对某条记录进行改动时,都会把对应的事务id赋值给try_id隐藏列</p><p>Roll_pointer:每次对某条引记录进行改动时,都会把旧的版本写入到undo日志,然后这个隐藏列就相当于一个指针,可以通过它来找到该记录修改前的信息</p><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720225610794.png" alt="image-20230720225610794"></p><h2 id="架构"><a href="#架构" class="headerlink" title="架构"></a>架构</h2><p>MySQL 5.5版本开始,默认使用InnoDB存储引擎,它擅长事务处理,具有崩溃恢复特性,在日常开发中使用非常广泛</p><p>下面是MySQL 8.0下的InnoDB架构图,左侧为内存结构,右侧为磁盘结构</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/mysql8.0_jg.png" alt="mysql8.0_jg"></p><p>MySQL 5.7及之前InnoDB架构图</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/mysql5.7_jg.png" alt="mysql5.7_jg"></p><p><strong>Buffer Pool</strong></p><p>缓冲池是主内存中的一个区域,里面可以缓存磁盘上经常操作的真实数据,在执行增删改查操作时,先操作缓冲池中的数据(若缓冲池没有数据,则从磁盘加载并缓存),然后再以一定频率刷新到磁盘,从而减少磁盘IO,加快处理速度</p><p>缓冲池以Page为单位,底层采用链表结构管理Page。根据状态,将Page分为三种类型:</p><ul><li>free page:空闲page,未被使用</li><li>clean page:被使用page,数据没有被修改过</li><li>dirty page:脏页,被使用page,数据被修改过,其中的数据与磁盘的数据产生了不一致</li></ul><p><strong>Chage Buffer</strong></p><p>更改缓冲区(针对于非唯一二级索引页),在执行DML语句时,如果这些数据Page没有在Buffer Pool中,不会直接操作磁盘,而会将数据变更存在更改缓冲区 Change Buffer 中,在未来数据被读取时,再将数据合并恢复到Buffer Pool中,再将合并后的数据刷新到磁盘中</p><p>与聚集索引不同,二级索引通常是非唯一的,并且以相对随机的顺序插入二级索引。同样,删除和更新可能会影响索引树中不相邻的二级索引页,如果每一次都操作磁盘,会造成大量的磁盘IO。有了Change Buffer后,可以在缓冲池中进行合并出来,减少磁盘IO</p><p><strong>Adaptive Hash Index</strong></p><p>自适应hash索引,用于优化对Buffer Pool数据的查询。InnoDB存储引擎会监控对表上各索引页的查询,如果观察到hash索引可以提升速度,则建立hash索引,称为自适应hash索引</p><font color=#9932CD>自适应哈希索引,无需人工干预,是系统根据情况自动完成</font><p>参数:adaptive_hash_inde</p><p><strong>Log Buffer</strong></p><p>日志缓冲区,用来保存要写入到磁盘中的log日志数据(redo log、undo log),默认大小为16MB,日志缓冲区的日志会定期刷新到磁盘中。如果需要更新、插入或删除许多行事务,增加日志缓冲区的大小可以节省磁盘IO</p><p>参数:innodb_log_buffer_size:缓冲区大小;innodb_flush_log_at_trx_commit:日志刷新到磁盘时机</p><p>innodb_flush_log_at_trx_commit取值含义:</p><ul><li>1:日志在每次事务提交时写入并刷新到磁盘</li><li>0:每秒将日志写入并刷新到磁盘一次</li><li>2:日志在每次事务提交后写入,并每秒刷新到磁盘一次</li></ul><h3 id="磁盘结构"><a href="#磁盘结构" class="headerlink" title="磁盘结构"></a>磁盘结构</h3><p><strong>System Tablespace</strong></p><p>系统表空间是更改缓冲区的存储区域。如果表是在系统表空间而不是每个表文件或通用表空间中创建的,它也可能包含表和索引数据</p><p>参数:innodb_data_file_path</p><p><strong>File-Per-Table Tablespaces</strong></p><p>每个表的文件表空间包含单个InnoDB表的数据和索引,并存储在文件系统上的单个数据文件中</p><p>参数:innodb_file_per_table</p><p><strong>General Tablespaces</strong></p><p>通用表空间,需要通过 CREATE TABLESPACE 语法创建通用表空间,在创建表时,可以指定该表空间</p><p><code>CREATE TABLESPACE xxx ADD DATAFILE 'file_name' ENGINE = engine_name;</code></p><p><code>CREATE TABLE xxx ... TABLESPACE tablespaceName;</code></p><p><strong>Undo Tablespaces</strong></p><p>撤销表空间,MySQL实例在初始化时会自动创建两个默认的undo表空间(初始大小16M),用于存储undo log日志</p><p><strong>Temporary Tablespaces</strong></p><p>InnoDB使用会话临时表空间和全局临时表空间。存储用户创建的临时表等数据</p><p><strong>Doublewrite Buffer Files</strong></p><p>双写缓冲区,InnoDB引擎将数据页从Buffer Pool刷新到磁盘前,先将数据页写入双写缓冲区中,便于系统异常时恢复数据</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801224011035.png" alt="image-20230801224011035"></p><p><strong>Redo Log</strong></p><p>重做日志,是用来实现事务的持久性。该日志文件由两部分组成:重做日志缓冲(redo log buffer)以及重做日志文件(redo log),前者是在内存中,后者在磁盘中。当事务提交之后会把所有修改信息都存到该日志中,用于在刷新脏页到磁盘时,发生错误后,进行数据恢复</p><p>以循环方式写入重做日志文件,涉及两个文件</p><h4 id="后台线程"><a href="#后台线程" class="headerlink" title="后台线程"></a>后台线程</h4><p><strong>Master Thread</strong></p><p>核心后台线程,负责调度其他线程,还负责将缓冲池中的数据异步刷新到磁盘中,保持数据的一致性,还包括脏页的刷新、合并插入缓存、undo页的回收</p><p><strong>IO Thread</strong></p><p>在InnoDB存储引擎中大量使用了AIO来处理IO请求,这样可以极大提高数据库的性能,而IO Thread主要负责这些IO请求的回调</p><div class="table-container"><table><thead><tr><th>线程类型</th><th>默认个数</th><th>职责</th></tr></thead><tbody><tr><td>Read thread</td><td>4</td><td>负责读操作</td></tr><tr><td>Write thread</td><td>4</td><td>负责写操作</td></tr><tr><td>Log thread</td><td>1</td><td>负责将日志缓冲区刷新到磁盘</td></tr><tr><td>Insert buffer thread</td><td>1</td><td>负责将写缓冲区内容刷新到磁盘</td></tr></tbody></table></div><p><strong>Purge Thread</strong></p><p>主要用于回收事务已经提交了undo log,在事务提交之后,undo log可能不用了,就用它来回收</p><p><strong>Page Cleaner Thread</strong></p><p>协助 Master Thread 刷新脏页到磁盘的线程,它可以减轻 Master Thread 的工作压力,减少阻塞</p><h2 id="事务原理"><a href="#事务原理" class="headerlink" title="事务原理"></a>事务原理</h2><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/transaction_theory.png" alt="transaction_theory"></p><p><strong>redo log</strong></p><p>重做日志,记录的是事务提交时数据页的物理修改,是用来实现事务的持久性</p><p>该日志文件由两部分:重做日志缓冲(redo log buffer)以及重做日志文件(redo log file),前者是在内存中,后者在磁盘中。当事务提交之后会把所有修改信息都保存到该日志文件中,用于刷新脏页到磁盘,发生错误后,进行数据恢复使用</p><blockquote><p>在一个事务中,执行多个增删改的操作时,InnoDB引擎会先操作缓冲池中的数据,如果缓冲区没有对应的数据,会通过后台线程将磁盘中的数据加载出来,存放在缓冲区中,然后将缓冲池中的数据修改,修改后的数据页我们称为脏页。 而脏页则会在一定的时机,通过后台线程刷新到磁盘中,从而保证缓冲区与磁盘的数据一致。 而缓冲区的脏页数据并不是实时刷新的,而是一段时间之后将缓冲区的数据刷新到磁盘中,假如刷新到磁盘的过程出错了,而提示给用户事务提交成功,而数据却没有持久化下来,这就出现问题了,没有保证事务的持久性</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/zy_redo.png" alt="zy_redo"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/redo%2520buffer.png" alt=""></p><blockquote><p>有了redo log之后,当对缓冲区的数据进行增删改之后,会首先将操作的数据页的变化,记录在redo log buffer中。在事务提交时,会将redo log buffer中的数据刷新到redo log磁盘文件中。过一段时间之后,如果刷新缓冲区的脏页到磁盘时,<strong>发生错误,此时就可以借助于redo log进行数据恢复,这样就保证了事务的持久性</strong></p><p><strong>操作数据一般都是随机读写磁盘的,而不是顺序读写磁盘。 而redo log在往磁盘文件中写入数据,由于是日志文件,所以都是顺序写的。顺序写的效率,要远大于随机写。</strong> 这种先写日志的方式,称之为 WAL(Write-Ahead Logging)</p><p>如果脏页成功刷新到磁盘 或 或者涉及到的数据已经落盘,此时redolog就没有作用了,就可以删除了,所以存在的两个redo log文件是循环写的</p></blockquote><p><strong>undo log</strong></p><p>回滚日志,用于记录数据被修改前的信息 , 作用包含两个 : 提供回滚(保证事务的原子性) 和MVCC(多版本并发控制)</p><p>undo log和redo log记录物理日志不一样,它是逻辑日志。可以认为当delete一条记录时,undo log中会记录一条对应的insert记录,反之亦然,当update一条记录时,它记录一条对应相反的update记录。当执行rollback时,就可以从undo log中的逻辑记录读取到相应的内容并进行回滚</p><p>undo log销毁:undo log在事务执行时产生,事务提交时,并不会立即删除undo log,因为这些日志可能还用于MVCC</p><p>undo log存储:undo log采用段的方式进行管理和记录,存放在rollback segment(回滚段)中,内部包含1024个undo log segment</p><h2 id="MVCC"><a href="#MVCC" class="headerlink" title="MVCC"></a>MVCC</h2><p><strong>当前读</strong></p><p>读取的是记录的最新版本,读取时还要保证其他并发事务不能修改当前记录,会对读取的记录进行加锁。对于日常操作,如:select … lock in share mode(共享锁),select … for update、update、insert、delete(排他锁)都是一种当前锁</p><p><strong>快照读</strong></p><p>简单的select(不加锁)就是快照读,快照读 读取的是记录数据的可见版本,有可能是历史数据,不加锁,是非阻塞读</p><ul><li>Read Coomitted:每次select都生成一个快照读</li><li>Repeatable Read:开启事务后第一个select语句才是快照读的地方</li><li>Serializable:快照读会退化为当前读</li></ul><p><strong>MVCC</strong></p><p>全称 Multi-Version Concurrency Control,多版本并发控制。指维护一个数据的多个版本,使得读写操作没有冲突,快照读为MySQL实现MVCC提供了一个非阻塞读功能。MVCC的具体实现,还需要依赖于数据库记录中的三个隐式字段、undo log日志、readView</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802144847203.png" alt="image-20230802144847203"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802145034389.png" alt="image-20230802145034389"></p><p><strong>记录中的隐藏字段</strong></p><div class="table-container"><table><thead><tr><th>隐藏字段</th><th>含义</th></tr></thead><tbody><tr><td>DB_TRX_ID</td><td>最近修改事务ID,记录插入这条记录或最后一次修改该记录的事务ID</td></tr><tr><td>DB_ROLL_PTR</td><td>回滚指针,指向这条记录的上一个版本,用于配合undo log,指向上一个版本</td></tr><tr><td>DB_ROW_ID</td><td>隐藏主键,如果表结构没有指定主键,将会生成该隐藏字段</td></tr></tbody></table></div><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802150608977.png" alt="image-20230802150608977"></p><p><strong>undo log</strong></p><p>回滚日志,在insert、update、delete时产生的便于数据回滚的日志</p><p>当insert时,产生的undo log日志只在回滚时需要,在事务提交后,可被立即删除;而update、delete时,产生的undo log日志不仅在回滚时需要,在快照读时也需要,不会立即被删除</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802152208428.png" alt="image-20230802152208428"></p><p>不同事务或相同事务对同一记录进行修改,会导致该记录的undo log生成一条记录版本链表,链表头部是最新的旧记录,链表尾部是最早的旧记录</p><h3 id="readview"><a href="#readview" class="headerlink" title="readview"></a>readview</h3><p>ReadView(读视图)是 快照读 SQL执行时MVCC提取数据的依据,记录并维护系统当前活跃的事务(未提交的)id</p><p>ReadView中包含了四个核心字段:</p><div class="table-container"><table><thead><tr><th>字段</th><th>含义</th></tr></thead><tbody><tr><td>m_ids</td><td>当前活跃的事务ID集合</td></tr><tr><td>min_trx_id</td><td>最小活跃事务ID</td></tr><tr><td>max_trx_id</td><td>预分配事务ID,当前最大事务ID+1(因为事务ID是自增的)</td></tr><tr><td>creator_trx_id</td><td>ReadView创建者的事务ID</td></tr></tbody></table></div><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802153737449.png" alt="image-20230802153737449"></p><p>不同隔离级别,生成ReadView的时机不同:</p><ul><li>READ COMMITTED:在事务中每一次执行快照读时生成ReadView</li><li>REPEATBALE READ:仅在事务中第一次执行快照读时生成ReadView,后续复用该ReadView</li></ul><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802155358081.png" alt="image-20230802155358081"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802155829354.png" alt="image-20230802155829354"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802160702137.png" alt="image-20230802160702137"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802160931181.png" alt="image-20230802160931181"></p><h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>原子性由undo log保证;持久性由redo log保证;一致性由undo log + redo log保证;隔离性由锁 + MVCC保证</p><p>MVCC的作用:在快照读的时候要通过MVCC来查找对应的历史版本</p>]]></content>
<summary type="html"><h2 id="逻辑存储结构"><a href="#逻辑存储结构" class="headerlink" title="逻辑存储结构"></a>逻辑存储结构</h2><p>表空间(idb文件),一个mysql实例可以对应多个表空间,用于存储记录、索引等数据</p>
<p>段分为数</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>锁</title>
<link href="http://example.com/posts/6643.html"/>
<id>http://example.com/posts/6643.html</id>
<published>2023-10-15T02:21:00.000Z</published>
<updated>2023-10-15T03:26:20.256Z</updated>
<content type="html"><![CDATA[<h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>锁是计算机协调多个进程或线程并访问某一资源的机制。在数据库中,除传统的计算机资源(CPU、RAM、I/O)的争用以外,数据也是一种供许多用户共享的资源。如何保证数据并发访问的一致性、有效性是所有数据库必须解决的一个问题,锁冲突也是影响数据库并发访问性能的一个重要因素。从这个角度来说,锁对数据库而言显得尤为重要,也更加复杂</p><p><strong>分类</strong></p><p>MySQL中的锁,按照锁的粒度分为以下三类:</p><ol><li>全局锁:锁定数据库中的所有表</li><li>表级锁:每次操作锁住整张表</li><li>行级锁:每次操作锁住对应的行数据</li></ol><h2 id="全局锁"><a href="#全局锁" class="headerlink" title="全局锁"></a>全局锁</h2><p>全局锁就是对整个数据库实例加锁,加锁后整个实例就处于只读状态,后续的DML的写语句,DDL语句,已经更新操作的事务提交语句都将被阻塞</p><p>其典型的使用场景是做全库的逻辑备份,对所有的表进行锁定,从而获取一致性视图,保证数据的完整性</p><p><code>flush tables with read lock;</code>:创建全局锁</p><p><code>unlock tables;</code>:解锁</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230730154213964.png" alt="image-20230730154213964"></p><p>数据库加全局锁是一个比较重的操作,存在以下问题:</p><ol><li>如果在主库上备份,那么在备份期间都不能执行更新,业务基本上就得停摆</li><li>如果在从库上备份,那么在备份期间从库不能执行主库同步过来的二进制日志(binlog),会导致主从延迟</li></ol><p>在InnoDB引擎中,可以在备份时加上参数 —single-transaction 参数来<font color=#9932CD>完成不加锁的一致性数据备份</font></p><p><code>mysqldump --single-transaction -uroot -p123456 itcast > D:/itcast.sql</code></p><h2 id="表级锁"><a href="#表级锁" class="headerlink" title="表级锁"></a>表级锁</h2><p>表级锁,每次操作锁住整张表。锁定粒度大,发生锁冲突的概率最高,并发度最低。应用在MyISAM、InnoDB、BDB等存储引擎中</p><p>对于表级锁,主要分为以下三类:</p><ol><li>表锁</li><li>元数据锁(meta data lock, MDL)</li><li>意向锁</li></ol><h3 id="表锁"><a href="#表锁" class="headerlink" title="表锁"></a>表锁</h3><p>对于表锁,分为两类:</p><ol><li>表<strong>共享读</strong>锁(read lock)</li><li>表<strong>独占写</strong>锁(write lock)</li></ol><p><strong>语法</strong></p><p><code>lock tables tableName read|write;</code>:加锁</p><p><code>unlock tables;</code>:释放锁</p><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230730161943884.png" alt="image-20230730161943884"></p><h3 id="元数据锁"><a href="#元数据锁" class="headerlink" title="元数据锁"></a>元数据锁</h3><p>MDL加锁过程是系统自动控制,无需显示使用,在访问一张表的时候会自动加上。MDL锁主要作用是维护表元数据的数据一致性,在表上有活动事务时,不可以对元数据进行写入操作。<strong>为了避免DML与DDL冲突,保证读写的正确性</strong></p><p>在MySQL5.5中引入了MDL,当对一张表进行增删改查时,加MDL读锁(共享);当对表结构进行变更操作时,加MDL写锁(排他)</p><div class="table-container"><table><thead><tr><th>对应SQL</th><th>锁类型</th><th>说明</th></tr></thead><tbody><tr><td>lock tables tableName read \</td><td>write</td><td>SHARED_READ_ONLY/SHARED_NO_READ_WRITE</td><td></td></tr><tr><td>select、select … lock in share mode</td><td>SHARED_READ</td><td>与SHARED_READ、SHARED_WRITE兼容,与EXCLUSIVE互斥</td></tr><tr><td>insert、update、delete、select … for update</td><td>SHARED_WRITE</td><td>与SHARED_READ、SHARED_WRITE兼容,与EXCLUSIVE互斥</td></tr><tr><td>after table …</td><td>EXCLUSIVE</td><td>与其他DML都互斥</td></tr></tbody></table></div><p><strong>查看元数据锁</strong></p><p><code>select object_type,object_schema,object_name,lock_type,lock_duration from performance_schema.metadata_locks;</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230730164602012.png" alt="image-20230730164602012"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230730165600709.png" alt="image-20230730165600709"></p><h3 id="意向锁"><a href="#意向锁" class="headerlink" title="意向锁"></a>意向锁</h3><p>为了避免DML在执行时,加的行锁与表锁的冲突,在InnoDB中引入了意向锁,使得表锁不用检查每行数据是否加锁,使用意向锁来减少表锁的检查</p><p>意向共享锁(IS):由语句 select … lock in share mode 添加</p><p>意向排他锁(IX):由insert 、update、delete、select … for update 添加</p><p>意向共享锁(IS): 与表锁共享锁(read)兼容,与表锁排他锁(write)互斥</p><p>意向排他锁(IX):与表锁共享锁(read)及 排他锁(write)都互斥,意向锁之间不会互斥</p><p>可以通过以下SQL查看意向锁及行锁的加锁情况:<code>select object_schema,object_name,index_name,lock_type,lock_mode,lock_data from performance_schema.data_locks;</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230730200623625.png" alt="image-20230730200623625"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230730200944754.png" alt="image-20230730200944754"></p><h2 id="行级锁"><a href="#行级锁" class="headerlink" title="行级锁"></a>行级锁</h2><p>行级锁指的是每次操作锁住对应行的数据。锁定粒度最小,发生锁冲突的概率最低,并发度最高。应用在InnoDB存储引擎中</p><p>InnoDB的数据是基于索引组织的,行锁是通过对索引上索引项加锁来实现的,而不是对记录加的锁。对于行级锁,主要分为以下三类:</p><ol><li>行锁(Record Lock):锁定单个行记录的锁,防止其他事务对此进行update和delete。在RC、RR隔离级别下都支持</li><li>同隙锁(Gap Lock):锁定索引记录间隙(不含该记录),确保索引记录间隙不变,防止其他事务在这个间隙进行insert,产生幻读。在RR隔离级别下都支持</li><li>临建锁(Next-Key Lock):行锁和间隙锁组合,同时锁住数据,并锁住数据前面的间隙Gap。在隔离级别下支持</li></ol><h3 id="行锁"><a href="#行锁" class="headerlink" title="行锁"></a>行锁</h3><p>InnoDB实现了以下两种类型的行锁:</p><ol><li>共享锁(S):允许一个事务去读一行,阻止其他事务获得相同数据集的排他锁</li><li>排他锁(X):允许获取排他锁的事务更新,阻止其他事务获得相同数据集的共享锁和排他锁</li></ol><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801201254995.png" alt=""></p><div class="table-container"><table><thead><tr><th>SQL</th><th>行锁类型</th><th>说明</th></tr></thead><tbody><tr><td>INSERT …</td><td>排他锁</td><td>自动加锁</td></tr><tr><td>UPDATE …</td><td>排他锁</td><td>自动加锁</td></tr><tr><td>DELETE …</td><td>排他锁</td><td>自动加锁</td></tr><tr><td>SELECT(正常)</td><td>不加任何锁</td><td></td></tr><tr><td>SELECT … LOCK IN SHARE MODE</td><td>共享锁</td><td>需要手动在SELECT之后加LOCK IN SHARE MODE</td></tr><tr><td>SELECT … FOR UPDATE</td><td>排他锁</td><td>需要手动在SELECT之后加FOR UPDATE</td></tr></tbody></table></div><p>默认情况下,InnoDB在REPEATABLE READ事务隔离级别运行,InnoDB使用 next-key锁进行搜索和索引扫描,以防止幻读</p><p><strong>注意:</strong></p><ol><li><p>针对唯一索引进行检索时,对已存在的记录进行等值匹配时,将会自动优化为行锁</p></li><li><p>InnoDB的行锁是针对于索引加的锁,不通过索引条件检索数据,那么InnoDB将对表中的所有记录加锁,此时 <strong>就会升级为表锁</strong></p></li></ol><p>可以通过以下SQL查看意向锁及行锁的加锁情况:<code>select object_schema,object_name,index_name,lock_type,lock_mode,lock_data from performance_schema.data_locks;</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801162011722.png" alt="image-20230801162011722"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801162636468.png" alt="image-20230801162636468"></p><h3 id="间隙锁与临键锁"><a href="#间隙锁与临键锁" class="headerlink" title="间隙锁与临键锁"></a>间隙锁与临键锁</h3><p>默认情况下,InnoDB在REPEATABLE READ事务隔离级别运行,InnoDB使用 next-key锁进行搜索和索引扫描,以防止幻读</p><p><strong>注意:</strong></p><ol><li>索引上的等值查询(唯一索引),给不存在的记录加锁时,优化为间隙锁</li><li>索引上的等值查询(普通索引),向右遍历时最后一个值不满足查询需求时,next-key lock退化为间隙锁</li><li>索引上的范围查询(唯一索引)会访问到不满足条件的第一个值为止</li></ol><font color=#9932CD>间隙锁唯一目的是防止其他事务插入间隙。间隙锁可以共存,一个事务采用的间隙锁不会阻止另一个事务在同一间隙上采用间隙锁</font><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801205701592.png" alt="image-20230801205701592"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801211057935.png" alt="image-20230801211057935"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230801211418493.png" alt="image-20230801211418493"></p><h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><ul><li><p>元数据锁的 object_name 取值为 metadata_locks</p></li><li><p>意向锁的 lock_mode 取值为 IS或IX</p></li><li>行锁的 lock_mode 取值为 S,REC_NOT_GAP或X,REC_NOT_GAP</li><li>间隙锁的 lock_mode 取值为 S,GAP</li><li>临建锁的 lock_mode 取值为 S</li></ul>]]></content>
<summary type="html"><h2 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h2><p>锁是计算机协调多个进程或线程并访问某一资源的机制。在数据库中,除传统的计算机资源(CPU、RAM、I/O)的争用以外,数据也是一种供许多用户</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>Redis客户端</title>
<link href="http://example.com/posts/6723.html"/>
<id>http://example.com/posts/6723.html</id>
<published>2023-10-14T14:30:00.000Z</published>
<updated>2023-10-14T14:31:49.989Z</updated>
<content type="html"><![CDATA[<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>安装完Redis目录,就可以操作Redis,实现数据的CRUD,这需要用到Redis客户端,包括:</p><ul><li>命令行客户端</li><li>图形化桌面客户端</li><li>编程客户端</li></ul><h2 id="命令行客户端"><a href="#命令行客户端" class="headerlink" title="命令行客户端"></a>命令行客户端</h2><p>Redis安装完成后就自带了命令行客户端:<code>redis-cli</code>,使用方式如下</p><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">redis-cli [options][commands]</span><br></pre></td></tr></table></figure><p>其中场景的<code>options</code>有:</p><ul><li><code>-h 127.0.0.1</code>:指定要连接的redis节点的IP地址,默认是127.0.0.1</li><li><code>-p 6379</code>:指定要连接的redis节点的端口,默认是6379</li><li><code>-a 132</code>:指定redis的访问密码</li></ul><p>其中的<code>commands</code>就是Redis操作命令,如:</p><ul><li><code>ping</code>:与redis服务端做心跳测试,服务端正常会返回<code>pong</code></li><li>不指定commond时,会进入<code>redis-cli</code>的交互控制台</li></ul><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013110700950.png" alt="image-20231013110700950"></p><h2 id="图形化界面客户端"><a href="#图形化界面客户端" class="headerlink" title="图形化界面客户端"></a>图形化界面客户端</h2><p>GitHub上的大神编写了Redis图形化界面客户端,地址:<a href="https://github.com/RedisInsight/RedisDesktopManager">https://github.com/RedisInsight/RedisDesktopManager</a>,不过该仓库只提供了RedisDesktopManager的源码,并未提供Windows安装包</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013111544271.png" alt="image-20231013111544271"></p><p>有热心市民提供了其编译后的安装包,地址:<a href="https://github.com/lework/RedisDesktopManager-Windows/releases">https://github.com/lework/RedisDesktopManager-Windows/releases</a></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013111843119.png" alt="image-20231013111843119"></p><p>安装后打开,其界面如图</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013112035206.png" alt="image-20231013112035206"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013113439496.png" alt="image-20231013113439496"></p><p>首先使用<code>firewall-cmd --zone=public --list-ports</code>查看开放了哪些防火墙端口,如果没有开放防火墙的6379端口就需要开放,否则监听不到</p><figure class="highlight sh"><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">firewall-cmd --add-prot=6379/tcp --permanent</span><br><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013113802677.png" alt="image-20231013113802677"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013113844783.png" alt="image-20231013113844783"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013113927251.png" alt="image-20231013113927251"></p><h2 id="Redis的Java客户端"><a href="#Redis的Java客户端" class="headerlink" title="Redis的Java客户端"></a>Redis的Java客户端</h2><p>在Redis官网中提供了各种语言的客户端,网址:<a href="https://redis.io/resources/clients">https://redis.io/resources/clients</a></p><p>选择Jedis作为Redis的Java客户端,开源网址:<a href="https://github.com/redis/jedis">https://github.com/redis/jedis</a></p><p>要开始使用Jedis,首先将其添加为Java项目中的依赖项</p><p>1.如果使用Maven,则引入依赖如下</p><figure class="highlight xml"><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"><!--引入Jedis依赖--></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>redis.clients<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jedis<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>4.2.0<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br><span class="line"></span><br><span class="line"><span class="comment"><!--引入单元测试依赖--></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.junit.jupiter<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>junit-jupiter<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">version</span>></span>5.8.2<span class="tag"></<span class="name">version</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">scope</span>></span>test<span class="tag"></<span class="name">scope</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013200309712.png" alt="image-20231013200309712"></p><p>2.编写测试类与Redis建立连接</p><figure class="highlight java"><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="keyword">private</span> Jedis jedis;</span><br><span class="line"></span><br><span class="line"><span class="meta">@BeforeEach</span> <span class="comment">//被该注解修饰的方法每次执行其他方法前自动执行</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">setUp</span><span class="params">()</span>{</span><br><span class="line"> <span class="comment">// 1. 获取连接</span></span><br><span class="line"> jedis = <span class="keyword">new</span> <span class="title class_">Jedis</span>(<span class="string">"192.168.42.135"</span>,<span class="number">6379</span>);</span><br><span class="line"> <span class="comment">// 2. 设置密码</span></span><br><span class="line"> jedis.auth(<span class="string">"132"</span>);</span><br><span class="line"> <span class="comment">// 3. 选择库(默认是下标为0的库)</span></span><br><span class="line"> jedis.select(<span class="number">0</span>);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>3.测试string</p><figure class="highlight java"><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></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">testString</span><span class="params">()</span>{</span><br><span class="line"> <span class="comment">// 插入数据,方法名称就是redis命令名称</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">result</span> <span class="operator">=</span> jedis.set(<span class="string">"url"</span>, <span class="string">"https://zjnanqu.top/"</span>);</span><br><span class="line"> System.out.println(<span class="string">"result = "</span> + result);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 获取数据</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> jedis.get(<span class="string">"url"</span>);</span><br><span class="line"> System.out.println(<span class="string">"url = "</span> + url);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>4.释放资源</p><figure class="highlight java"><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="meta">@AfterEach</span> <span class="comment">//被该注解修饰的方法会在每次执行其他方法后执行</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">tearDown</span><span class="params">()</span>{</span><br><span class="line"> <span class="comment">// 1.释放资源</span></span><br><span class="line"> <span class="keyword">if</span> (jedis != <span class="literal">null</span>){</span><br><span class="line"> jedis.close();</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>如果报错<code>Error running 'JedisTest.testString': Failed to resolve org.junit.platform:junit-platform-launcher:1.8.2</code></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/error_redis.png" alt=""></p><p>加上下面这个依赖</p><figure class="highlight java"><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"><dependency></span><br><span class="line"> <groupId>org.junit.platform</groupId></span><br><span class="line"> <artifactId>junit-platform-launcher</artifactId></span><br><span class="line"> <scope>test</scope></span><br><span class="line"></dependency></span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013205936372.png" alt="image-20231013205936372"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013210029133.png" alt="image-20231013210029133"></p><figure class="highlight java"><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="keyword">void</span> <span class="title function_">testHash</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 插入hash数据</span></span><br><span class="line"> jedis.hset(<span class="string">"user:1"</span>, <span class="string">"name"</span>, <span class="string">"Jack"</span>);</span><br><span class="line"> jedis.hset(<span class="string">"user:1"</span>, <span class="string">"age"</span>, <span class="string">"21"</span>);</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 获取</span></span><br><span class="line"> Map<String, String> map = jedis.hgetAll(<span class="string">"user:1"</span>);</span><br><span class="line"> System.out.println(map);</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013213351530.png" alt="image-20231013213351530"></p><h2 id="Jedis连接池"><a href="#Jedis连接池" class="headerlink" title="Jedis连接池"></a>Jedis连接池</h2><p>Jedis本身被设计为线程不安全的,这意味着它在多线程环境下使用时可能会出现问题,并且频繁的创建和销毁连接会有性能损耗,因此推荐使用Jedis连接池代替Jedis直连方式</p><p>配置连接池</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014172838623.png" alt="image-20231014172838623"></p><p>使用连接池</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014173131333.png" alt="image-20231014173131333"></p><p><strong>Java知识点回顾:</strong> </p><ol><li>静态成员和静态代码块会在类加载时自动执行一次,而静态方法只有在被调用时执行,可通过类名调用;</li><li>导包是为了在当前文件中引入其他类或类库的功能,而在同一包下不同类之间可以相互引用,无需使用导包语句</li></ol><h2 id="SpringDataRedis"><a href="#SpringDataRedis" class="headerlink" title="SpringDataRedis"></a>SpringDataRedis</h2><p>SpringData是Spring中数据操作的模块,包含了对各种数据库的集成,其中对Redis的集成模块叫做SpringDataRedis,官网:<a href="https://spring.io/projects/spring-data-redis">https://spring.io/projects/spring-data-redis</a></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014170517500.png" alt="image-20231014170517500"></p><p>SpringDataRedis具备如下特征:</p><ul><li>提供了对不同Redis客户端的整合(Lettuce和Jedis)</li><li>提供了RedisTemplate统一API来操作Redis</li><li>支持Redis的发布订阅模型</li><li>支持Redis哨兵和Redis集群</li><li>支持基于Lettuce的响应式编程</li><li>支持基于JDK、JSON、字符串、Spring对象的数据序列化及反序列化</li><li>支持基于Redis的JDKCollection实现</li></ul><p>SpringDataRedis中提供了RedisTemplate工具类,其中封装了各种对Redis的操作(将不同数据类型的操作即API封装到了对应的类型中)</p><div class="table-container"><table><thead><tr><th>API</th><th>返回值类型</th><th>说明</th></tr></thead><tbody><tr><td>redisTemplate.opsForValue()</td><td>ValueOperations</td><td>操作String类型数据</td></tr><tr><td>redisTemplate.opsForHash()</td><td>HashOperations</td><td>操作Hash类型数据</td></tr><tr><td>redisTemplate.opsForList()</td><td>ListOperations</td><td>操作List类型数据</td></tr><tr><td>redisTemplate.opsForSet()</td><td>SetOperations</td><td>操作Set类型数据</td></tr><tr><td>redisTemplate.opsForZSet()</td><td>ZSetOperations</td><td>操作SortedSet类型数据</td></tr><tr><td>redisTemplate</td><td></td><td>通用命令</td></tr></tbody></table></div><h2 id="SpringDataRedis快速入门"><a href="#SpringDataRedis快速入门" class="headerlink" title="SpringDataRedis快速入门"></a>SpringDataRedis快速入门</h2><p>SpringBoot已经提供了对SpringDataRedis的支持,使用非常简单</p><p>首先新建一个SpringBoot项目</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014194253461.png" alt="image-20231014194253461"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014195824530.png" alt="image-20231014195824530"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014203851456.png" alt="image-20231014203851456"></p><ol><li>引入依赖</li></ol><figure class="highlight xml"><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="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>org.apache.commons<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>commons-pool2<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014200306705.png" alt="image-20231014200306705"></p><ol><li>编写配置文件application.yaml</li></ol><figure class="highlight yaml"><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="attr">spring:</span></span><br><span class="line"> <span class="attr">redis:</span></span><br><span class="line"> <span class="attr">host:</span> <span class="number">192.168</span><span class="number">.42</span><span class="number">.135</span></span><br><span class="line"> <span class="attr">port:</span> <span class="number">6379</span></span><br><span class="line"> <span class="attr">password:</span> <span class="number">132</span></span><br><span class="line"> <span class="attr">lettuce:</span></span><br><span class="line"> <span class="attr">pool:</span></span><br><span class="line"> <span class="attr">max-active:</span> <span class="number">8</span></span><br><span class="line"> <span class="attr">max-idle:</span> <span class="number">8</span></span><br><span class="line"> <span class="attr">min-idle:</span> <span class="number">0</span></span><br><span class="line"> <span class="attr">max-wait:</span> <span class="string">100ms</span></span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014205106999.png" alt="image-20231014205106999"></p><ol><li>编写测试类执行测试方法</li></ol><figure class="highlight java"><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="keyword">package</span> com.heima.redisdemo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.core.RedisTemplate;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">RedisDemoApplicationTests</span> {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> RedisTemplate redisTemplate;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="keyword">void</span> <span class="title function_">contextLoads</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 写入一条String数据</span></span><br><span class="line"> redisTemplate.opsForValue().set(<span class="string">"name"</span>, <span class="string">"虎哥"</span>);</span><br><span class="line"> <span class="comment">// 获取String数据</span></span><br><span class="line"> <span class="type">Object</span> <span class="variable">name</span> <span class="operator">=</span> redisTemplate.opsForValue().get(<span class="string">"name"</span>);</span><br><span class="line"> System.out.println(<span class="string">"name = "</span> + name);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014205356540.png" alt="image-20231014205356540"></p><h2 id="RedisSerializer配置"><a href="#RedisSerializer配置" class="headerlink" title="RedisSerializer配置"></a>RedisSerializer配置</h2><p>RedisTemplate可以接收任意Object作为键值写入Redis,只不过写入前会把Object序列化为字节形式,默认采用的是JDK序列化,可能出现如下结果</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014205906633.png" alt="image-20231014205906633"></p><p>这样不仅可读性差且内存占用较大</p><p>为了避免出现这样的问题,可以通过自定义RedisTemplate序列化方式来解决</p><p>编写配置类<code>RedisConfig</code></p><figure class="highlight java"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.heima.redis.config;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Bean;</span><br><span class="line"><span class="keyword">import</span> org.springframework.context.annotation.Configuration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.connection.RedisConnectionFactory;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.core.RedisTemplate;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.serializer.RedisSerializer;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RedisConfig</span> {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Bean</span></span><br><span class="line"> <span class="keyword">public</span> RedisTemplate<String, Object> <span class="title function_">redisTemplate</span><span class="params">(RedisConnectionFactory connectionFactory)</span> {</span><br><span class="line"> <span class="comment">// 创建RedisTemplate对象</span></span><br><span class="line"> RedisTemplate<String, Object> template = <span class="keyword">new</span> <span class="title class_">RedisTemplate</span><>();</span><br><span class="line"> <span class="comment">// 设置连接工厂</span></span><br><span class="line"> template.setConnectionFactory(connectionFactory);</span><br><span class="line"> <span class="comment">// 创建JSON序列化工具</span></span><br><span class="line"> <span class="type">GenericJackson2JsonRedisSerializer</span> <span class="variable">jsonRedisSerializer</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">GenericJackson2JsonRedisSerializer</span>();</span><br><span class="line"> <span class="comment">// 设置key的序列化</span></span><br><span class="line"> template.setKeySerializer(RedisSerializer.string());</span><br><span class="line"> template.setHashKeySerializer(RedisSerializer.string());</span><br><span class="line"> <span class="comment">// 设置value的序列化</span></span><br><span class="line"> template.setValueSerializer(jsonRedisSerializer);</span><br><span class="line"> template.setHashValueSerializer(jsonRedisSerializer);</span><br><span class="line"> <span class="comment">// 返回</span></span><br><span class="line"> <span class="keyword">return</span> template;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>引入依赖</p><figure class="highlight xml"><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"><!-- Jackson依赖 --></span></span><br><span class="line"><span class="tag"><<span class="name">dependency</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">groupId</span>></span>com.fasterxml.jackson.core<span class="tag"></<span class="name">groupId</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">artifactId</span>></span>jackson-databind<span class="tag"></<span class="name">artifactId</span>></span></span><br><span class="line"><span class="tag"></<span class="name">dependency</span>></span></span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014213940313.png" alt="image-20231014213940313"></p><p>也可以存入对象,因为会把对象自动序列化为JSON字符串</p><p>创建实体类</p><figure class="highlight java"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.heima.pojo;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> lombok.AllArgsConstructor;</span><br><span class="line"><span class="keyword">import</span> lombok.Data;</span><br><span class="line"><span class="keyword">import</span> lombok.NoArgsConstructor;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@NoArgsConstructor</span></span><br><span class="line"><span class="meta">@AllArgsConstructor</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">User</span> {</span><br><span class="line"> <span class="keyword">private</span> String name;</span><br><span class="line"> <span class="keyword">private</span> Integer age;</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014214546040.png" alt="image-20231014214546040"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014214637172.png" alt="image-20231014214637172"></p><p>尽管JSON的序列化方式可以满足我们的需求,但依然存在一些问题,如图</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014215014962.png" alt="image-20231014215014962"></p><p>为了在反序列化时知道对象的类型,JSON序列化器会将类的class类型写入json结果中,存入Redis,这样会带来额外的内存开销</p><h2 id="StringRedisTemplate"><a href="#StringRedisTemplate" class="headerlink" title="StringRedisTemplate"></a>StringRedisTemplate</h2><p>为了节省内存空间,我们并不会使用JSON序列化器来处理value,而是统一使用String序列化器,要求只能存储String类型的key和value。当需要存储Java对象时,<u>手动完成对象的序列化和反序列化</u></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014215640357.png" alt="image-20231014215640357"></p><p>Spring默认提示了一个StringRedisTemplate类,它的key和value的序列化方式默认就是String方式。不用在自定义RedisTemplate</p><figure class="highlight java"><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.heima.redis;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.core.JsonProcessingException;</span><br><span class="line"><span class="keyword">import</span> com.fasterxml.jackson.databind.ObjectMapper;</span><br><span class="line"><span class="keyword">import</span> com.heima.pojo.User;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.boot.test.context.SpringBootTest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.redis.core.StringRedisTemplate;</span><br><span class="line"></span><br><span class="line"><span class="meta">@SpringBootTest</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">RedisDemoApplicationTests</span> {</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Autowired</span></span><br><span class="line"> <span class="keyword">private</span> StringRedisTemplate stringRedisTemplate;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="keyword">void</span> <span class="title function_">testString</span><span class="params">()</span> {</span><br><span class="line"> <span class="comment">// 写入一条String数据</span></span><br><span class="line"> stringRedisTemplate.opsForValue().set(<span class="string">"name"</span>, <span class="string">"虎哥"</span>);</span><br><span class="line"> <span class="comment">// 获取String数据</span></span><br><span class="line"> <span class="type">Object</span> <span class="variable">name</span> <span class="operator">=</span> stringRedisTemplate.opsForValue().get(<span class="string">"name"</span>);</span><br><span class="line"> System.out.println(<span class="string">"name = "</span> + name);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">ObjectMapper</span> <span class="variable">mapper</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">ObjectMapper</span>();</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Test</span></span><br><span class="line"> <span class="keyword">void</span> <span class="title function_">testSaveUser</span><span class="params">()</span> <span class="keyword">throws</span> JsonProcessingException {</span><br><span class="line"> <span class="comment">// 创建对象</span></span><br><span class="line"> <span class="type">User</span> <span class="variable">user</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">User</span>(<span class="string">"虎哥"</span>, <span class="number">21</span>);</span><br><span class="line"> <span class="comment">// 手动序列化</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">json</span> <span class="operator">=</span> mapper.writeValueAsString(user);</span><br><span class="line"> <span class="comment">// 写入数据</span></span><br><span class="line"> stringRedisTemplate.opsForValue().set(<span class="string">"user:200"</span>, json);</span><br><span class="line"> <span class="comment">// 获取数据</span></span><br><span class="line"> <span class="type">String</span> <span class="variable">jsonUser</span> <span class="operator">=</span> stringRedisTemplate.opsForValue().get(<span class="string">"user:200"</span>);</span><br><span class="line"> <span class="comment">// 手动反序列化</span></span><br><span class="line"> <span class="type">User</span> <span class="variable">user1</span> <span class="operator">=</span> mapper.readValue(jsonUser, User.class);</span><br><span class="line"> System.out.println(<span class="string">"user1 = "</span> + user1);</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014221027841.png" alt="image-20231014221027841"></p><p>对Hash存储类型进行操作</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014221655096.png" alt="image-20231014221655096"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231014221745231.png" alt="image-20231014221745231"></p><h2 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h2><p>RedisTemplate有两种序列化方案,各有优缺点</p><p>方案一:</p><ol><li>自定义RedisTemplate</li><li>修改RedisTemplate序列化器为GenericJackson2JsonRedisSerializer</li></ol><p>方案二:</p><ol><li>使用StringRedisTemplate</li><li>写入Redis时,手动把对象序列化为JSON</li><li>读取Redis时,手动把读取到的JSON反序列化为对象</li></ol>]]></content>
<summary type="html"><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>安装完Redis目录,就可以操作Redis,实现数据的CRUD,这需要用到Redis客户端,包括:</p>
<ul>
<li>命令行客户端<</summary>
<category term="Redis" scheme="http://example.com/categories/Redis/"/>
</entry>
<entry>
<title>Redis常见命令</title>
<link href="http://example.com/posts/6722.html"/>
<id>http://example.com/posts/6722.html</id>
<published>2023-10-14T13:28:00.000Z</published>
<updated>2023-10-14T14:31:12.704Z</updated>
<content type="html"><![CDATA[<h2 id="文档查询"><a href="#文档查询" class="headerlink" title="文档查询"></a>文档查询</h2><p>可以通过Redis中文文档来学习:<a href="http://www.redis.cn/commands.html">http://www.redis.cn/commands.html</a></p><p>也可以通过菜鸟教程官网来学习:<a href="https://www.runoob.com/redis/redis-keys.html">https://www.runoob.com/redis/redis-keys.html</a></p><h2 id="Redis数据结构介绍"><a href="#Redis数据结构介绍" class="headerlink" title="Redis数据结构介绍"></a>Redis数据结构介绍</h2><p>Redis是一个key-value的数据库,key一般是String类型,不过value的类型多种多样</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/redis_structure.png" alt="redis_structure"></p><h2 id="通用命令"><a href="#通用命令" class="headerlink" title="通用命令"></a>通用命令</h2><div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>KEYS</td><td>查看符合模版的所有key,<font color=#9932CD>不建议在生成环境设备上使用</font></td></tr><tr><td>DEL</td><td>删除一个指定的key</td></tr><tr><td>EXISTS</td><td>判断key是否存在</td></tr><tr><td>EXPIRE</td><td>给一个设置有效期,有效期到期时该key会被自动删除</td></tr><tr><td>TTL</td><td>查看一个key的剩余有效期</td></tr></tbody></table></div><p><strong>需知:</strong> 可以通过<code>help [command]</code>查看一个命令的具体用法</p><p>keys和del命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013151836607.png" alt="image-20231013151836607"></p><p>exists命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013152250975.png" alt="image-20231013152250975"></p><p>expire和ttl命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013152734572.png" alt="image-20231013152734572"></p><h2 id="String类型"><a href="#String类型" class="headerlink" title="String类型"></a>String类型</h2><p>String类型即字符串类型,是Redis中最简单的存储类型</p><p>其value是字符串,不过根据字符串的格式不同,又可分为3类:</p><ul><li>string:普通字符串</li><li>int:整数类型,可以自增、自减操作</li><li>float:浮点类型,可以自增、自减操作</li></ul><p>不管是哪种格式,底层都是字节数组形式存储,只不过编码方式不同。字符串类型的最大空间不能超过512M</p><p>String类型的常见命令</p><div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>SET</td><td>添加或修改已存在的一个String类型的键值对</td></tr><tr><td>GET</td><td>根据key获取String类型的value</td></tr><tr><td>MSET</td><td>批量添加多个String类型的键值对</td></tr><tr><td>MGET</td><td>根据多个key获取多个String类型的value</td></tr><tr><td>INCR</td><td>让一个整型的key自增1</td></tr><tr><td>INCRBY</td><td>让一个整型的key自增并指定步长,如:incrby um 2让num值自增2</td></tr><tr><td>INCRBYFLOAT</td><td>让一个浮点类型的数字自增并指定步长</td></tr><tr><td>SETNX</td><td>添加一个String类型的键值对,前提是这个key不存在,否则不执行</td></tr><tr><td>SETEX</td><td>添加一个String类型的键值对,并且指定有效期</td></tr></tbody></table></div><p>setex命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013154939112.png" alt="image-20231013154939112"></p><h2 id="Key的层级格式"><a href="#Key的层级格式" class="headerlink" title="Key的层级格式"></a>Key的层级格式</h2><p>Redis的key允许多个单词形成层级结构,多个单词之间用’:’隔开,格式可以这么设计:<code>项目名:业务名:类型:id</code>,这个格式并非固定,根据自己需求来删除或添加词条</p><p>例如项目名称叫heima,有user和product两种不同类型的数据,可以这样定义key:</p><ul><li>user相关的key:<code>heima:user:1</code></li><li>product相关的key:<code>heima:product:1</code></li></ul><p>如果value是一个Java对象,如一个User对象,可以将对象序列化转为JSON字符串后存储:</p><div class="table-container"><table><thead><tr><th>KEY</th><th>VALUE</th></tr></thead><tbody><tr><td>heima:user:1</td><td>{“id”: 1, “name”: “Jack”, “age”: 21}</td></tr><tr><td>heima:product:1</td><td>{“id”: 1, “name”: “小米11”, “price”: 4999}</td></tr></tbody></table></div><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013160358438.png" alt="image-20231013160358438"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013160341387.png" alt="image-20231013160341387"></p><h2 id="Hash类型"><a href="#Hash类型" class="headerlink" title="Hash类型"></a>Hash类型</h2><p>Hash类型,也叫散列,其value是一个无序字典,类似于Java中的HashMap结构</p><p>String结构是将对象序列化转为JSON字符串后存储,当需要修改对象某个字段时很不方便</p><div class="table-container"><table><thead><tr><th>KEY</th><th>VALUE</th></tr></thead><tbody><tr><td>heima:user:1</td><td>{“id”: 1, “name”: “Jack”, “age”: 21}</td></tr><tr><td>heima:product:1</td><td>{“id”: 1, “name”: “小米11”, “price”: 4999}</td></tr></tbody></table></div><p>Hash结构可以将对象中的每个字段独立存储,可以针对每个字段做CRUD</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013162855624.png" alt="image-20231013162855624"></p><p>Hash类型的常见命令</p><div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>HSET key field value</td><td>添加或修改hash类型key的field值</td></tr><tr><td>HGET key field</td><td>获取一个hash类型key的field值</td></tr><tr><td>HMSET</td><td>hmset和hset效果相同,4.0之后hmset可以弃用了</td></tr><tr><td>HMGET</td><td>批量获取多个hash类型key的field值</td></tr><tr><td>HGETALL</td><td>获取一个hash类型的key中的所有的field和value</td></tr><tr><td>HKEYS</td><td>获取一个hash类型的key中的所有field</td></tr><tr><td>HAVLS</td><td>获取一个hash类型的key中的所有value</td></tr><tr><td>HINCRBY</td><td>让一个hash类型key的字段值自增并指定步长</td></tr><tr><td>HSETNX</td><td>添加一个hash类型的key的field值,前提是这个field不存在,否则不执行</td></tr></tbody></table></div><p>hset、hget、hmset和hmget命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013164318313.png" alt="image-20231013164318313"></p><p>hgetall、hkeys、hvals和hincrby命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013164734530.png" alt="image-20231013164734530"></p><p> hsetnx命令的具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013165019061.png" alt="image-20231013165019061"></p><h2 id="List类型"><a href="#List类型" class="headerlink" title="List类型"></a>List类型</h2><p>Redis中的List类型与Java中的LinkedList类似,可以看做是一个双向链表结构。既可以支持正向检索,也可以支持反向检索</p><p>特征与LinkedList类似:</p><ul><li>有序</li><li>元素可以重复</li><li>插入和删除快</li><li>查询速度一般</li></ul><p>List类型的常见命令有:</p><div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>LPUSH key element …</td><td>向列表左侧插入一个或多个元素</td></tr><tr><td>LPOP key</td><td>移除并返回列表左侧的第一个元素,没有则返回null</td></tr><tr><td>RPUSH key element …</td><td>向列表右侧插入一个或多个元素</td></tr><tr><td>RPOP key</td><td>移除并返回列表右侧的第一个元素</td></tr><tr><td>LRANGE key star end</td><td>返回一段角标范围内的所有元素</td></tr><tr><td>BLPOP 和 BRPOP</td><td>与LPOP和RPOP类似,只不过在没有元素时等待指定时间,而不是直接返回nil</td></tr></tbody></table></div><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/List_redis.gif" alt="List_redis"></p><p>具体用法</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013170725951.png" alt="image-20231013170725951"></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013170738690.png" alt="image-20231013170738690"></p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013171158081.png" alt="image-20231013171158081"></p><blockquote><h3 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h3><ol><li><p>如何利用List结构模拟一个栈?</p><p>先进后出,入口和出口在同一边</p></li><li><p>如何利用List结构模拟一个对垒?</p><p>先进先出,入口和出口在不同边</p></li><li><p>如何利用List结构模拟一个阻塞队列?</p><p>入口和出口在不同边</p><p>出队时采用BLPOP或BRPOP</p></li></ol></blockquote><h2 id="Set类型"><a href="#Set类型" class="headerlink" title="Set类型"></a>Set类型</h2><p>Redis的Set结构与Java中的HashSet类似,可以看做是一个value为null的HashMap</p><p>因为是一个hash表,因此具备与HashSet类似的特征</p><ul><li>无序</li><li>元素不可重复</li><li>查找快</li><li>支持交集、并集、差集等功能</li></ul><p>Set类型的常见命令</p><div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>SADD key member …</td><td>向set中添加一个或多个元素</td></tr><tr><td>SREM key member …</td><td>移除set中的指定元素</td></tr><tr><td>SCARD key</td><td>返回set中元素的个数</td></tr><tr><td>SISMEMBER key member</td><td>判断一个元素是否存在于set中</td></tr><tr><td>SMEMBERS</td><td>获取set中的所有元素</td></tr><tr><td>SINTER key1 key2 …</td><td>求key1与key2的交集</td></tr><tr><td>SDIFF key1 key2 …</td><td>求key1与key2的差集</td></tr><tr><td>SUNION key1 key2 …</td><td>求key1和key2的并集</td></tr></tbody></table></div><blockquote><h3 id="Set命令的联系"><a href="#Set命令的联系" class="headerlink" title="Set命令的联系"></a>Set命令的联系</h3><p>将下列数据用Redis的Set集合来存储:</p><ul><li>张三的好友有:李四、王五、赵六</li><li>李四的好友有:王五、麻子、二狗</li></ul><p>利用Set的命令实现下列功能:</p><ul><li>计算张三的好友有几人</li><li>计算张三和李四有哪些共同好友</li><li>查询哪些人是张三的好友却不是李四的好友</li><li>查询张三和李四的好友总共有哪些人</li><li>判断李四是否是张三的好友</li><li>判断张三是否是李四的好友</li><li>将李四从张三的好友列表中移除</li></ul></blockquote><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013175213129.png" alt="image-20231013175213129"></p><h2 id="SortedSet类型"><a href="#SortedSet类型" class="headerlink" title="SortedSet类型"></a>SortedSet类型</h2><p>Redis的SortedSet是一个可排序的set集合,与Java中的TreeSet有些类似,但底层数据结构却差别很大。SortedSet中每一个元素都带有score属性,可以基于score属性对元素排序,底层实现是一个跳表(SkipList)加hash表</p><p>SortedSet类型具备以下特性:</p><ul><li>可排序</li><li>元素不重复</li><li>查询速度快</li></ul><p>因为SortedSet的可排序特性,经常被用来实现排行榜这样的功能</p><p>SortedSet类型常见命令</p><div class="table-container"><table><thead><tr><th>命令</th><th>描述</th></tr></thead><tbody><tr><td>ZADD key score member</td><td>添加一个或多个元素到sorted set,如果已经存在则更新其score值</td></tr><tr><td>ZREM key member</td><td>删除sorted set中的一个指定元素</td></tr><tr><td>ZSCORE key member</td><td>获取sorted set中的指定元素的score值</td></tr><tr><td>ZRANK key member</td><td>获取sorted set中的指定元素的排名</td></tr><tr><td>ZCARD key</td><td>获取sorted set中的元素个数</td></tr><tr><td>ZCOUNT key min max</td><td>统计score值在给定范围内的所有的元素的个数</td></tr><tr><td>ZINCRBY key increment member</td><td>让sorted set中的指定元素自增,步长为指定的increment值</td></tr><tr><td>ZRANGE key min max</td><td>按照score排序后,获取指定排名范围内的元素</td></tr><tr><td>ZRANGEBYSCORE key min max</td><td>按照score排序后,获取指定score范围内的元素</td></tr><tr><td>ZDIFF、ZINTER、ZUNION</td><td>求差集、交集、并集</td></tr></tbody></table></div><p><strong>注意:</strong> 所有的排名默认是升序,如果要降序则在命令Z后面添加REV即可</p><blockquote><h3 id="SortedSet命令练习"><a href="#SortedSet命令练习" class="headerlink" title="SortedSet命令练习"></a>SortedSet命令练习</h3><p>将 Jack 85、Lucy 89、Rose 82、Tom 95、Jerry 78、Amy 92、Miles 76 这些学生得分存入Redis的SortedSet中,并实现下列功能:</p><ul><li>删除Tom同学</li><li>获取Amy同学的分数</li><li>获取Rose同学的排名</li><li>查询80分以下有几个学生</li><li>给Amy同学加2分</li><li>查出成绩前3名的同学</li><li>查出成绩80分以下的所有同学</li></ul></blockquote><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013182410494.png" alt="image-20231013182410494"></p>]]></content>
<summary type="html"><h2 id="文档查询"><a href="#文档查询" class="headerlink" title="文档查询"></a>文档查询</h2><p>可以通过Redis中文文档来学习:<a href="http://www.redis.cn/commands.html">h</summary>
<category term="Redis" scheme="http://example.com/categories/Redis/"/>
</entry>
<entry>
<title>Redis快速入门</title>
<link href="http://example.com/posts/6721.html"/>
<id>http://example.com/posts/6721.html</id>
<published>2023-10-14T08:28:00.000Z</published>
<updated>2023-10-14T14:30:21.090Z</updated>
<content type="html"><![CDATA[<h2 id="课程资源"><a href="#课程资源" class="headerlink" title="课程资源"></a>课程资源</h2><p>黑马程序员Redis入门到实战教程:<a href="https://www.bilibili.com/video/BV1cr4y1671t">https://www.bilibili.com/video/BV1cr4y1671t</a></p><h2 id="NoSQL介绍"><a href="#NoSQL介绍" class="headerlink" title="NoSQL介绍"></a>NoSQL介绍</h2><p>NoSQL(Not Only SQL)是一种<font color=#9932CD>非关系型数据库管理系统</font>,用于存储和检索大量<strong>非结构化和半结构化数据</strong></p><p>与传统关系型数据库不同,NoSQL数据库<strong>不依赖</strong>于<font color=#9932CD>固定的表结构和SQL查询语言</font></p><p>NoSQL数据库的<strong>设计目标</strong>是解决关系型数据库在处理大规模数据和高并发访问时遇到的一些限制和挑战。这些数据库通常采用键值存储、文档存储、列存储或图形数据库等<font color=#9932CD>不同的数据模型</font></p><p>以下是一些常见的NoSQL数据库类型:</p><ol><li>键值存储(Key-Value Stores):这种类型的数据库使用键值对的方式存储数据。它们提供了简单且高效的数据访问,适用于需要快速存储和检索数据的场景。流行的键值存储数据库如Redis</li><li>文档数据库(Document Databases):文档数据库以类似于JSON或XML的文档格式存储数据。每个文档可以包含不同字段和值,没有固定模式。这种数据模型适用于存储和查询半结构化数据。MongoDB是一种常见的文档数据库</li><li>列存储数据库(Columnar Databases):列存储数据库以列的方式存储数据,而不是行。这种存储方式使得它们在需要查询特定列或进行大规模数据分析时非常高效。HBase和Apache Cassandra是常见的列存储数据库</li><li>图形数据库(Graph Databases):图形数据库用于存储和查询图形结构数据,如节点和边。它们提供了高效的图形遍历和复杂的关系查询能力,适用于网络分析、社交网络和推荐系统等领域。Neo4j和Amazon Neptune是一些著名的图形数据库</li></ol><p>NoSQL最常见的解释是<code>"non-relational"</code>,也有很多说是”Not Only SQL”,这仅仅是一个概念,泛指非关系型数据库</p><h2 id="NoSQL-与-SQL-的差异"><a href="#NoSQL-与-SQL-的差异" class="headerlink" title="NoSQL 与 SQL 的差异"></a>NoSQL 与 SQL 的差异</h2><div class="table-container"><table><thead><tr><th>数据库类型</th><th>SQL</th><th>NoSQL</th></tr></thead><tbody><tr><td>数据结构</td><td>结构化</td><td>非结构化</td></tr><tr><td>数据关联</td><td>关联的</td><td>无关联的</td></tr><tr><td>查询方式</td><td>SQL查询</td><td>非SQL</td></tr><tr><td>事务特性</td><td>ACID</td><td>BASE</td></tr><tr><td>存储方式</td><td>磁盘</td><td>内存</td></tr><tr><td>扩展性</td><td>垂直</td><td>水平</td></tr><tr><td>使用场景</td><td>1. 数据结构固定 2. 相关业务对数据安全性、一致性要求较高</td><td>1. 数据结构不固定 2. 对一致性、安全性要求不高 3. 对性能要求</td></tr></tbody></table></div><h2 id="认识Redis"><a href="#认识Redis" class="headerlink" title="认识Redis"></a>认识Redis</h2><p>Redis诞生于2009年,全称是Remote Dictionary Server,远程词典服务器,是一个基于内存的键值型NoSQL数据库</p><p><strong>特征</strong></p><ul><li>键值型(<code>key-value</code>)型,value支持多种不同数据结构,功能丰富</li><li>单线程,每个命令具备原子性</li><li>低延迟,速度快(基于内存、IO多路复用、良好的编码)</li><li>支持数据持久化</li><li>支持主从集群、分片集群</li><li>支持多语言客户端</li></ul><p>Redis官网有一整套的命令详解,遇到需要或不会的地方可以查一下:<a href="http://doc.redisfans.com">http://doc.redisfans.com</a></p><h2 id="安装Redis"><a href="#安装Redis" class="headerlink" title="安装Redis"></a>安装Redis</h2><p><strong>第一步</strong> 使用虚拟机VMWare安装CentOS7,然后使用finalshell作为SSH连接工具,远程连接Linux服务器</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013093640680.png" alt="image-20231013093640680"></p><p><strong>第二步</strong> 将Redis安装包上传到虚拟机的<code>/usr/local/scr</code>目录下</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013094024803.png" alt="image-20231013094024803"></p><p><strong>第三步</strong> 解压并编译安装</p><p>进入安装包所在目录,然后运行<code>tar -zxvf redis-6.2.6.tar.gz</code>命令</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013094405030.png" alt="image-20231013094405030"></p><p>然后在进入解压后的redis目录<code>cd redis-6.2.6</code>,接着运行编译命令<code>make && make install</code></p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013095302932.png" alt="image-20231013095302932"></p><p>默认的安装路径是在<code>/usr/local/bin</code>目录下</p><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013095455768.png" alt="image-20231013095455768"></p><p>该目录已经默认配置到环境变量,所以可以在任意目录下运行这些命令。其中:</p><ul><li>redis-cli:是redis提供的命令行客户端</li><li>redis-server:是redis的服务器启动脚本</li><li>redis-sentinel:是redis的哨兵启动脚本</li></ul><h2 id="启动Redis"><a href="#启动Redis" class="headerlink" title="启动Redis"></a>启动Redis</h2><p>Redis启动方式有很多种,如:前台启动、后台启动、开机启动</p><h3 id="前台启动(不推荐)"><a href="#前台启动(不推荐)" class="headerlink" title="前台启动(不推荐)"></a>前台启动(不推荐)</h3><blockquote><p>前台启动会阻塞整个会话窗口,窗口关闭或按下<code>CTRL+C</code>就会停止Redis,不推荐使用</p></blockquote><p>安装完成后,在任意目录输入<code>redis-server</code>命令即可启动Redis,启动界面如下</p><p><img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013100259636.png" alt="image-20231013100259636"></p><h3 id="后台启动(不推荐)"><a href="#后台启动(不推荐)" class="headerlink" title="后台启动(不推荐)"></a>后台启动(不推荐)</h3><blockquote><p>要让Redis以后台方式启动,则必须修改Redis配置文件,配置文件所在目录就是之前解压的安装包目录</p></blockquote><p>要修改配置文件,那么首先对其备份一份</p><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cp redis.conf redis.conf.bck</span><br></pre></td></tr></table></figure><p>接着修改<code>redis.conf</code>文件中的一些配置</p><figure class="highlight properties"><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"># 监听的地址,默认是127.0.0.1,会导致只能在本地访问。修改为0.0.0.0则可以在任意IP访问,生产环境不要设置为0.0.0.0</span></span><br><span class="line"><span class="attr">bind</span> <span class="string">0.0.0.0</span></span><br><span class="line"><span class="comment"># 守护进程,修改为yes后即可后台运行</span></span><br><span class="line"><span class="attr">daemonize</span> <span class="string">yes </span></span><br><span class="line"><span class="comment"># 密码,设置后访问Redis必须输入密码</span></span><br><span class="line"><span class="attr">requirepass</span> <span class="string">132</span></span><br></pre></td></tr></table></figure><p>在Linux中的vi编辑器分为三种模式,分别是命令行模式、插入模式和底行模式,这三种模式可以互相切换,使用vim时一定要注意当前所处模式</p><p><strong>命令模式</strong></p><ul><li>通过vim命令打开文件后默认进入命令行模式,<font color=#9932CD>另外两种模式需要先进入命令模式</font></li><li>命令模式下可以查看文件内容、移动光标</li></ul><p><strong>插入模式</strong></p><ul><li>插入模式下可以对文件内容进行编辑</li><li>在命令模式下按下[i,a,o]任意一个,就可以进入插入模式,进入插入模式后,下方会出现【insert】字样</li><li>在插入模式下按下<code>ESC</code>键即可回到命令行模式</li></ul><p><strong>底行模式</strong></p><ul><li>底行模式下可以通过命令对文件内容进行<font color=#9932CD>查找、显示行号、退出等操作</font></li><li>在命令行模式下按下[:,/]任意一个就可进入底行模式</li><li>通过/方式进入底行模式后,可以对文件内容进行查找</li><li>通过:方式进入底行模式后,可以输入wq(保存并退出)、q!(不保存退出)、set nu(显示行号)</li></ul><p>Redis的其他场景配置</p><figure class="highlight properties"><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></pre></td><td class="code"><pre><span class="line"><span class="comment"># 监听的端口</span></span><br><span class="line"><span class="attr">port</span> <span class="string">6379</span></span><br><span class="line"><span class="comment"># 工作目录,默认是当前目录,也就是运行redis-server时的命令,日志、持久化等文件会保存在这个目录</span></span><br><span class="line"><span class="attr">dir</span> <span class="string">.</span></span><br><span class="line"><span class="comment"># 数据库数量,设置为1,代表只使用1个库,默认有16个库,编号0~15</span></span><br><span class="line"><span class="attr">databases</span> <span class="string">1</span></span><br><span class="line"><span class="comment"># 设置redis能够使用的最大内存</span></span><br><span class="line"><span class="attr">maxmemory</span> <span class="string">512mb</span></span><br><span class="line"><span class="comment"># 日志文件,默认为空,不记录日志,可以指定日志文件名</span></span><br><span class="line"><span class="attr">logfile</span> <span class="string">"redis.log"</span></span><br></pre></td></tr></table></figure><p>启动Redis</p><figure class="highlight sh"><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"># 进入redis安装目录</span></span><br><span class="line"><span class="built_in">cd</span> /usr/local/src/redis-6.2.6</span><br><span class="line"></span><br><span class="line"><span class="comment"># 启动</span></span><br><span class="line">redis-server redis.conf</span><br></pre></td></tr></table></figure><p> <img src="https://mk-gzh.oss-cn-guangzhou.aliyuncs.com/images/image-20231013104209829.png" alt="image-20231013104209829"></p><h3 id="开机自启"><a href="#开机自启" class="headerlink" title="开机自启"></a>开机自启</h3><p>可以通过配置来实现开机自启</p><p>首先新建一个系统服务文件</p><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi /etc/systemd/system/redis.service</span><br></pre></td></tr></table></figure><p>内容如下</p><figure class="highlight plaintext"><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">[Unit]</span><br><span class="line">Description=redis-server</span><br><span class="line">After=network.target</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">Type=forking</span><br><span class="line">ExecStart=/usr/local/bin/redis-server /usr/local/src/redis-6.2.6/redis.conf</span><br><span class="line">PrivateTmp=true</span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br></pre></td></tr></table></figure><p>然后重载系统服务</p><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl daemon-reload</span><br></pre></td></tr></table></figure><p>接着就可以用下面这组命令来操作redis</p><figure class="highlight sh"><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">systemctl start redis</span><br><span class="line"></span><br><span class="line"><span class="comment"># 停止</span></span><br><span class="line">systemctl stop redis</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重启</span></span><br><span class="line">systemctl restart redis</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看状态</span></span><br><span class="line">systemctl status redis</span><br></pre></td></tr></table></figure><p>执行下面命令就可以让redis开机自启了</p><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl <span class="built_in">enable</span> redis</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html"><h2 id="课程资源"><a href="#课程资源" class="headerlink" title="课程资源"></a>课程资源</h2><p>黑马程序员Redis入门到实战教程:<a href="https://www.bilibili.com/video/BV1c</summary>
<category term="Redis" scheme="http://example.com/categories/Redis/"/>
</entry>
<entry>
<title>视图/存储过程/触发器</title>
<link href="http://example.com/posts/6642.html"/>
<id>http://example.com/posts/6642.html</id>
<published>2023-10-14T08:22:00.000Z</published>
<updated>2023-10-14T08:23:55.863Z</updated>
<content type="html"><![CDATA[<h2 id="一、视图"><a href="#一、视图" class="headerlink" title="一、视图"></a>一、视图</h2><p>视图(View)是一种虚拟存在的表。视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,并且是在使用视图时动态生成的</p><p>通俗地讲,视图只保存了查询的SQL逻辑,不保存查询结果。所以在创建视图时,主要工作就落到创建这条SQL查询语句上</p><p><strong>视图创建</strong></p><p><code>CREATE [OR REPLACE] VIEW 视图名称[(列名列表)] AS SELECT语句 [WITH[CASCADED|LOCAL] CHECK OPTION]</code></p><p><strong>视图查询</strong></p><p><code>SHOW CREATE VIEW 视图名称;</code>:查看插件视图语句</p><p><code>SELECT * FROM 视图名称 ......;</code>:查看视图数据</p><p><strong>修改视图</strong></p><p>方式一:<code>CREATE [OR REPLACE] VIEW 视图名称[(列名列表)] AS SELECT语句 [WITH[CASCADED|LOCAL] CHECK OPTION]</code></p><p>方式二:<code>ALTER VIEW 视图名称[(列名列表)] AS SELECT语句 [WITH[CACADED|LOCAL] CHECK OPTION]</code></p><p><strong>删除视图</strong></p><p><code>DROP VIEW [IF EXISTS] 视图名称 [,视图名称] ...</code></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="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v <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;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> stu_v;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> stu_v <span class="keyword">where</span> id <span class="operator"><</span> <span class="number">3</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">or</span> replace <span class="keyword">view</span> stu_v <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><br><span class="line"><span class="keyword">alter</span> <span class="keyword">view</span> stu_v <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;</span><br></pre></td></tr></table></figure><h3 id="检查选项"><a href="#检查选项" class="headerlink" title="检查选项"></a>检查选项</h3><p>当使用WITH CHECK OPTION子句创建视图时,MySQL会通过视图检查正在更改的每个行,例如插入、更新、删除,以使其符合视图的定义。MySQL允许基于另一个视图创建视图,它还会检查依赖视图中的规则以保持一致性。为了确定检查的范围,mysql提供了两个选项:CASCADED 和 LOCAL,默认值为CASCADED</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">or</span> replace <span class="keyword">view</span> stu_v <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="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> stu_v;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v <span class="keyword">values</span> (<span class="number">6</span>, <span class="string">'Tom'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v <span class="keyword">values</span> (<span class="number">30</span>, <span class="string">'Alice'</span>);</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729103010506.png" alt="image-20230729103010506"></p><h4 id="cascaded"><a href="#cascaded" class="headerlink" title="cascaded"></a>cascaded</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></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> stu_v <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="keyword">insert</span> <span class="keyword">into</span> stu_v <span class="keyword">values</span>(<span class="number">5</span>, <span class="string">'Bob'</span>);</span><br><span class="line"></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> stu_v <span class="keyword">where</span> id <span class="operator">>=</span> <span class="number">10</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="keyword">insert</span> <span class="keyword">into</span> stu_v_1 <span class="keyword">values</span> (<span class="number">7</span>, <span class="string">'Curiosity'</span>);</span><br><span class="line"></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">26</span>, <span class="string">'Curiosity'</span>);</span><br><span class="line"></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">15</span>, <span class="string">'Curiosity'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_2 <span class="keyword">as</span> <span class="keyword">select</span> id,name <span class="keyword">from</span> stu_v_1 <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">15</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_2 <span class="keyword">values</span> (<span class="number">11</span>, <span class="string">'Saly'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_2 <span class="keyword">values</span> (<span class="number">17</span>, <span class="string">'Lily'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_2 <span class="keyword">values</span> (<span class="number">28</span>, <span class="string">'Jack'</span>);</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729104839526.png" alt="image-20230729104839526"></p><h4 id="local"><a href="#local" class="headerlink" title="local"></a>local</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="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_3 <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">15</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_3 <span class="keyword">values</span>(<span class="number">5</span>, <span class="string">'Bob'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_3 <span class="keyword">values</span>(<span class="number">16</span>, <span class="string">'Tom'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_4 <span class="keyword">as</span> <span class="keyword">select</span> id,name <span class="keyword">from</span> stu_v_3 <span class="keyword">where</span> id <span class="operator">>=</span> <span class="number">10</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="keyword">insert</span> <span class="keyword">into</span> stu_v_4 <span class="keyword">values</span> (<span class="number">13</span>, <span class="string">'Curiosity'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> stu_v_4 <span class="keyword">values</span> (<span class="number">17</span>, <span class="string">'Curious'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> stu_v_5 <span class="keyword">as</span> <span class="keyword">select</span> id,name <span class="keyword">from</span> stu_v_4 <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="keyword">insert</span> <span class="keyword">into</span> stu_v_5 <span class="keyword">values</span> (<span class="number">14</span>, <span class="string">'Saly'</span>);</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729110122581.png" alt="image-20230729110122581"></p><h4 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h4><p>有检查选项就会校验条件,而cascaded检查选项会级联所依赖的基表或视图对条件进行校验,local检查选项只管本视图对条件的校验</p><h3 id="更新及作用"><a href="#更新及作用" class="headerlink" title="更新及作用"></a>更新及作用</h3><p>要使视图可更新,视图中的行与基础表中的行之间必须存在一对一的关系。如果视图包含以下任何一项,则该视图不可更新:</p><ol><li>聚合函数或窗口函数(SUM()、MIN()、MAX()、COUNT()等)</li><li>DISTINCT</li><li>GOURP BY</li><li>HAVING</li><li><p>UNION 或 UNION ALL</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729111048516.png" alt="image-20230729111048516"></p></li></ol><p><strong>简化</strong></p><p>视图不仅可以简化用户对数据的理解,也可以简化对数据的操作。那些经常被使用的查询可以被定义为视图,从而使得用户不必为以后的操作每次指定全部的条件</p><p><strong>敏感信息不可见</strong></p><p>数据库可以授权,但不能授权到数据库特定行和特定列上。通过视图,用户只能查询和修改所能见到的数据</p><p><strong>数据独立</strong></p><p>视图可帮助用户屏蔽真实表结构变化带来的影响</p><h3 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h3><blockquote><h3 id="根据如下需求,定义视图"><a href="#根据如下需求,定义视图" class="headerlink" title="根据如下需求,定义视图"></a>根据如下需求,定义视图</h3><ol><li>为了保证数据库表的安全性,开发人员在操作tb_user表时,只能看到用户的基本字段,屏蔽手机号和邮箱两个字段</li><li>查询每个学生所选修的课程(三张表联查),这个功能在很多的业务中都有使用到,为了简化操作,定义一个视图</li></ol></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">-- 为了保证数据库表的安全性,开发人员在操作tb_user表时,只能看到用户的基本字段,屏蔽手机号和邮箱两个字段</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> tb_user_v <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><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> tb_user_v;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询每个学生所选修的课程(三张表联查),这个功能在很多的业务中都有使用到,为了简化操作,定义一个视图</span></span><br><span class="line"><span class="keyword">select</span> s.name, s.no, c.name <span class="keyword">from</span> student s, student_course sc, course c <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><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">or</span> replace <span class="keyword">view</span> tb_stu_course_v <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, student_course sc, course c <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><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> tb_stu_course_v;</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729112613694.png" alt="image-20230729112613694"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729113154975.png" alt="image-20230729113154975"></p><h2 id="二、存储过程"><a href="#二、存储过程" class="headerlink" title="二、存储过程"></a>二、存储过程</h2><p>存储过程是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程可以简化应用开发人员的很多工作,减少数据在数据库和应用服务器之间的传输,对于提高数据处理的效率是有好处的</p><p>存储过程就是将数据库SQL语言层面的代码封装与重用</p><p><strong>特点</strong></p><ul><li>封装,复用</li><li>可以接收参数,也可以返回数据</li><li>减少网络交互,提升效率</li></ul><h3 id="基本语法"><a href="#基本语法" class="headerlink" title="基本语法"></a>基本语法</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></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><br><code>CALL 名称([参数]);</code></p><p><strong>查看</strong></p><p><code>SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_SCHEMA = 'xxx';</code>:查询指定数据库的存储过程及状态信息</p><p><code>SHOW CREATE PROCEDURE 存储过程名称;</code>:查询某个存储过程的定义</p><p><strong>删除</strong></p><p><code>DROP PROCEDURE [IF EXISTS] 存储过程名称;</code></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">-- 创建</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="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">'itheima'</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> 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><p><strong>注意:</strong> 在命令行中,执行创建存储过程的SQL时,需要<font color=#9932CD>通过关键字 delimiter 指定SQL语句的结束符</font></p><h3 id="变量"><a href="#变量" class="headerlink" title="变量"></a>变量</h3><h4 id="系统变量"><a href="#系统变量" class="headerlink" title="系统变量"></a>系统变量</h4><p>==系统变量== 是MySQL服务提供的,不是用户定义的,属于服务器层面。分为全局变量(GLOBAL)、会话变量(SESSION)</p><p><strong>查看系统变量</strong><br><code>SHOW [SESSION|GLOBAL] VARIABLES;</code>:查看所有系统变量</p><p><code>SHOW [SESSION|GLOBAL] VARIABLES LIKE '......';</code>:可以通过LIKE模糊查询方式查找变量</p><p><code>SELECT @@[SESSION|GLOABL]系统变量名;</code>:查看指定变量的值</p><p><strong>设置系统变量</strong><br><code>SET [SESSION|GLOBAL] 系统变量名 = 值;</code></p><p><code>SET @@[SESSION|GLOBAL] 系统变量名 = 值;</code></p><p><strong>注意:</strong> 如果没有指定SESSION/GLOBAL,默认是SESSION即会话变量;MySQL服务重新启动后,所设置的全局参数会失效,要想不失效,可以在Linux下的 /etc/my.cnf中配置</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="keyword">show</span> session variables ;</span><br><span class="line"></span><br><span class="line"><span class="keyword">show</span> session variables <span class="keyword">like</span> <span class="string">'auto%'</span>;</span><br><span class="line"><span class="keyword">show</span> <span class="keyword">global</span> variables <span class="keyword">like</span> <span class="string">'auto%'</span>;</span><br><span class="line"></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">@session</span>.autocommit;</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 autocommit <span class="operator">=</span> <span class="number">0</span>;</span><br></pre></td></tr></table></figure><h4 id="用户定义变量"><a href="#用户定义变量" class="headerlink" title="用户定义变量"></a>用户定义变量</h4><p>==用户定义变量== 是用户根据需要自己定义的变量,用户变量不用提前声明,在用的时候直接用“@变量名”使用即可,其作用域为当前连接</p><p><strong>赋值</strong></p><p><code>SET @var_name = expr [,@var_name=expr]...;</code></p><p><code>SET @var_name := expr [,@var_name:=expr]...;</code></p><p><code>SELECT @var_name :=expr [,@var_name:=expr]...;</code></p><p><code>SELECT 字段名 INTO @var_name FROM 表名;</code></p><p><strong>使用</strong></p><p><code>SELECT @var_name;</code></p><p><strong>注意:</strong> 用户定义的变量无需对其进行声明或初始化,只不过获取到的值为NULL</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="keyword">set</span> <span class="variable">@myname</span> <span class="operator">=</span> <span class="string">'itheima'</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><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;</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><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="variable">@mycolor</span>,<span class="variable">@mycount</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> <span class="variable">@abc</span>;</span><br></pre></td></tr></table></figure><h4 id="局部变量"><a href="#局部变量" class="headerlink" title="局部变量"></a>局部变量</h4><p>==局部变量== 是根据需要定义在局部生效的变量,访问之前,需要DECLARE声明。可用作存储过程内的局部变量和输入参数,局部变量的范围是在其内声明的BEGIN … END块</p><p><strong>声明</strong></p><p><code>DECLARE 变量名 变量类型[DEFAULT ...];</code></p><p>变量类型就是数据库字段类型:INT、BIGINT、CHAR、VARCHAR、DATE、TIME等</p><p><strong>赋值</strong></p><p><code>SET 变量名 = 值;</code></p><p><code>SET 变量名 := 值;</code></p><p><code>SELECT 字段名 INTO 变量名 FROM 表名...;</code></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></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="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"> <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> student;</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><br><span class="line"><span class="keyword">call</span> p2();</span><br></pre></td></tr></table></figure><h3 id="if判断"><a href="#if判断" class="headerlink" title="if判断"></a>if判断</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></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><br><span class="line">......</span><br><span class="line"><span class="keyword">END</span> IF;</span><br></pre></td></tr></table></figure><blockquote><h3 id="定义存储过程,完成如下需求"><a href="#定义存储过程,完成如下需求" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><p>根据定义的分数score变量,判定当前分数对应的分数等级</p><ol><li>score >= 85分,等级为优秀</li><li>score >= 60分 且 score < 85分,等级为及格</li><li>score < 60分,等级为不及格</li></ol></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></pre></td><td class="code"><pre><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="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"></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">select</span> <span class="keyword">result</span>;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p3();</span><br></pre></td></tr></table></figure><h3 id="参数"><a href="#参数" class="headerlink" title="参数"></a>参数</h3><div class="table-container"><table><thead><tr><th>类型</th><th>含义</th><th>备注</th></tr></thead><tbody><tr><td>IN</td><td>该类参数作为输入,也就是需要调用时传入值</td><td>默认</td></tr><tr><td>OUT</td><td>该类参数作为输出,也就是该参数可以作为返回值</td><td></td></tr><tr><td>INOUT</td><td>既可以作为输入参数,也可以作为输出参数</td></tr></tbody></table></div><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 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><blockquote><h3 id="定义存储过程,完成如下需求-1"><a href="#定义存储过程,完成如下需求-1" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><p>根据传入参数score判定当前分数对应的分数等级并返回</p><ol><li>score >= 85分,等级为优秀</li><li>score >= 65分 且 score < 85 分,等级为及格</li><li>score < 60分,等级为不及格</li></ol><p>将传入的200分制的分数换算成百分制,然后返回</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><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 根据传入参数score判定当前分数对应的分数等级并返回</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><br><span class="line"><span class="keyword">call</span> p4(<span class="number">68</span>, <span class="variable">@result</span>);</span><br><span class="line"><span class="keyword">select</span> <span class="variable">@result</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 将传入的200分制的分数换算成百分制,然后返回</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p5(<span class="keyword">inout</span> scroe <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> scroe :<span class="operator">=</span> scroe <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><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><h3 id="case"><a href="#case" class="headerlink" title="case"></a>case</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></pre></td><td class="code"><pre><span class="line"><span class="keyword">CASE</span> case_value</span><br><span class="line"><span class="keyword">WHEN</span> when_value1 <span class="keyword">THEN</span> statement_list1</span><br><span class="line">[<span class="keyword">WHEN</span> when_value2 <span class="keyword">THEN</span> statement_list2]...</span><br><span class="line">[<span class="keyword">ELSE</span> statement_list]</span><br><span class="line"><span class="keyword">END</span> <span class="keyword">CASE</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></pre></td><td class="code"><pre><span class="line"><span class="keyword">CASE</span></span><br><span class="line"><span class="keyword">WHEN</span> search_condition1 <span class="keyword">THEN</span> statement_list1</span><br><span class="line">[<span class="keyword">WHEN</span> search_condition2 <span class="keyword">THEN</span> statement_list2]...</span><br><span class="line">[<span class="keyword">ELSE</span> statement_list]</span><br><span class="line"><span class="keyword">END</span> <span class="keyword">CASE</span>;</span><br></pre></td></tr></table></figure><blockquote><h3 id="定义存储过程,完成如下需求-2"><a href="#定义存储过程,完成如下需求-2" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><p>根据传入的月份,判定月份所属的季节(要求采用case结构)</p><ol><li>1-3月份为第一季度</li><li>4-6月份为第二季度</li><li>7-9月份为第三季度</li><li>10-12月份为第四季度</li></ol></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><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="keyword">create</span> <span class="keyword">procedure</span> p6(<span class="keyword">in</span> <span class="keyword">month</span> <span class="type">int</span>)</span><br><span class="line"><span class="keyword">begin</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"> <span class="keyword">case</span></span><br><span class="line"> <span class="keyword">when</span> <span class="keyword">month</span> <span class="operator">>=</span> <span class="number">1</span> <span class="keyword">and</span> <span class="keyword">month</span> <span class="operator"><=</span> <span class="number">3</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">when</span> <span class="keyword">month</span> <span class="operator">>=</span> <span class="number">4</span> <span class="keyword">and</span> <span class="keyword">month</span> <span class="operator"><=</span> <span class="number">6</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">when</span> <span class="keyword">month</span> <span class="operator">>=</span> <span class="number">7</span> <span class="keyword">and</span> <span class="keyword">month</span> <span class="operator"><=</span> <span class="number">9</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">when</span> <span class="keyword">month</span> <span class="operator">>=</span> <span class="number">10</span> <span class="keyword">and</span> <span class="keyword">month</span> <span class="operator"><=</span> <span class="number">12</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> <span class="keyword">case</span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">select</span> concat(<span class="string">'您输入的月份为: '</span>, <span class="keyword">month</span>, <span class="string">',所属的季度为: '</span>,<span class="keyword">result</span>);</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p6(<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p6(<span class="number">16</span>);</span><br></pre></td></tr></table></figure><h3 id="循环"><a href="#循环" class="headerlink" title="循环"></a>循环</h3><h4 id="while"><a href="#while" class="headerlink" title="while"></a>while</h4><p>while循环是有条件的循环控制语句。满足条件后,再执行循环体中的SQL语句</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></pre></td><td class="code"><pre><span class="line">WHILE 条件 DO</span><br><span class="line"><span class="keyword">SQL</span>逻辑...</span><br><span class="line"><span class="keyword">END</span> WHILE;</span><br></pre></td></tr></table></figure><blockquote><h3 id="定义存储过程,完成如下需求-3"><a href="#定义存储过程,完成如下需求-3" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><p>计算从1累加到n的值,n为传入的参数值</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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- while</span></span><br><span class="line"><span class="comment">-- A. 定义局部变量,记录累加之后的值</span></span><br><span class="line"><span class="comment">-- B。 每循环一次,就会对n进行减1,如果n减到0,则退出循环</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p7(<span class="keyword">in</span> n <span class="type">int</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">declare</span> total <span class="type">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line"> while n <span class="operator">></span> <span class="number">0</span> do</span><br><span class="line"> <span class="keyword">set</span> total :<span class="operator">=</span> total<span class="operator">+</span>n;</span><br><span class="line"> <span class="keyword">set</span> n :<span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">end</span> while;</span><br><span class="line"> <span class="keyword">select</span> total;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p7(<span class="number">12</span>);</span><br></pre></td></tr></table></figure><h4 id="repeat"><a href="#repeat" class="headerlink" title="repeat"></a>repeat</h4><p>repeat循环是有条件的循环控制语句,当满足条件的时候就退出循环</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></pre></td><td class="code"><pre><span class="line">REPEAT</span><br><span class="line"><span class="keyword">SQL</span>逻辑...</span><br><span class="line">UNTIL 条件</span><br><span class="line"><span class="keyword">END</span> REPEAT;</span><br></pre></td></tr></table></figure><blockquote><h3 id="定义存储过程,完成如下需求-4"><a href="#定义存储过程,完成如下需求-4" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><p>计算从1累加到n的值,n为传入的参数值</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><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- repeat</span></span><br><span class="line"><span class="comment">-- a. 定义局部变量,记录累加之后的值</span></span><br><span class="line"><span class="comment">-- b. 每循环一次,就会对n进行减1,如果n减到0,则退出循环</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p8(<span class="keyword">in</span> n <span class="type">int</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">declare</span> total <span class="type">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> repeat</span><br><span class="line"> <span class="keyword">set</span> total :<span class="operator">=</span> total <span class="operator">+</span> n;</span><br><span class="line"> <span class="keyword">set</span> n :<span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"> until n <span class="operator"><=</span> <span class="number">0</span></span><br><span class="line"> <span class="keyword">end</span> repeat;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">select</span> total;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p8(<span class="number">24</span>);</span><br><span class="line"><span class="keyword">call</span> p8(<span class="number">100</span>);</span><br></pre></td></tr></table></figure><p>先执行循环内语句在判定是否满足条件,满足则退出</p><h4 id="loop"><a href="#loop" class="headerlink" title="loop"></a>loop</h4><p>loop实现简单的循环,如果不在SQL逻辑中增加退出循环的条件,可以用其来实现简单的死循环。loop可以配合两个语句使用</p><ul><li>leave:简单循环使用,退出循环</li><li>iterate:必须用在循环中,作用是跳过当前循环剩下的语句,直接进入下一次循环</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></pre></td><td class="code"><pre><span class="line">[begin_label:] LOOP</span><br><span class="line"><span class="keyword">SQL</span>逻辑...</span><br><span class="line"><span class="keyword">END</span> LOOP [end_lable];</span><br></pre></td></tr></table></figure><p><code>LEAVE label;</code>:退出指定标记的循环体</p><p><code>ITERATE lable;</code>:直接进入下一次循环</p><blockquote><h3 id="定义存储过程,完成如下需求-5"><a href="#定义存储过程,完成如下需求-5" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><ol><li>计算从1累加到n的值,n为传入的参数值</li><li>计算从1到n之间的偶数累加的值,n为传入的参数值</li></ol></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><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 计算从1累加到n的值,n为传入的参数值</span></span><br><span class="line"><span class="comment">-- loop 计算从1累加到n的值,n为传入的参数值</span></span><br><span class="line"><span class="comment">-- a. 定义局部变量,记录累加之后的值</span></span><br><span class="line"><span class="comment">-- b. 每循环一次,就会对n进行减1,如果n减到0,则退出循环 ---> leave xx</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p9(<span class="keyword">in</span> n <span class="type">int</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">declare</span> total <span class="type">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> sum:loop</span><br><span class="line"> if n <span class="operator"><=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> leave sum;</span><br><span class="line"> <span class="keyword">end</span> if;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">set</span> total :<span class="operator">=</span> total <span class="operator">+</span> n;</span><br><span class="line"> <span class="keyword">set</span> n :<span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">end</span> loop sum;</span><br><span class="line"> <span class="keyword">select</span> total;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p9(<span class="number">18</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 计算从1到n之间的偶数累加的值,n为传入的参数值</span></span><br><span class="line"><span class="comment">-- a. 定义局部变量,记录累加之后的值</span></span><br><span class="line"><span class="comment">-- b. 每循环一次,就会对n进行-1,如果减到0,则退出循环 ---> leave xx</span></span><br><span class="line"><span class="comment">-- c. 如果当次累加的数据是奇数,则直接进行下一次循环 ---> iterate xx</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p10(<span class="keyword">in</span> n <span class="type">int</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">declare</span> total <span class="type">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line"> sum:loop</span><br><span class="line"> if n <span class="operator"><=</span> <span class="number">0</span> <span class="keyword">then</span></span><br><span class="line"> leave sum;</span><br><span class="line"> <span class="keyword">end</span> if;</span><br><span class="line"></span><br><span class="line"> if n <span class="operator">%</span> <span class="number">2</span> <span class="operator">=</span> <span class="number">1</span> <span class="keyword">then</span></span><br><span class="line"> <span class="keyword">set</span> n :<span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"> iterate sum;</span><br><span class="line"> <span class="keyword">end</span> if;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">set</span> total :<span class="operator">=</span> total <span class="operator">+</span> n;</span><br><span class="line"> <span class="keyword">set</span> n :<span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">end</span> loop sum;</span><br><span class="line"> <span class="keyword">select</span> total;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">call</span> p10(<span class="number">10</span>);</span><br></pre></td></tr></table></figure><h3 id="游标"><a href="#游标" class="headerlink" title="游标"></a>游标</h3><p>游标(CURSOR)是用来存储查询结果集的数据类型,在存储过程和函数中可以使用游标对结果集进行循环的处理。游标的使用包括游标的声明、OPEN、FETCH和CLOSE,其语法分别如下:</p><p><strong>声明游标</strong></p><p><code>DECLARE 游标名称 CURSOR FOR 查询语句;</code></p><p><strong>打开游标</strong><br><code>OPEN 游标名称;</code></p><p><strong>获取游标记录</strong></p><p><code>FETCH 游标名称 INTO 变量[,变量];</code></p><p><strong>关闭游标</strong></p><p><code>CLOSE 游标名称;</code></p><blockquote><h3 id="定义存储过程,完成如下需求-6"><a href="#定义存储过程,完成如下需求-6" class="headerlink" title="定义存储过程,完成如下需求"></a>定义存储过程,完成如下需求</h3><p>根据传入的参数uage,来查询用户表tb_user中,所有的用户年龄小于等于uage的用户姓名(name)和专业(profession),并将用户的姓名和专业插入到所创建的一张新表(id,name,profession)中</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><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="comment">-- a. 声明游标,存储查询结果集</span></span><br><span class="line"><span class="comment">-- b. 准备:创建表结构</span></span><br><span class="line"><span class="comment">-- c. 开启游标</span></span><br><span class="line"><span class="comment">-- d. 获取游标中的记录</span></span><br><span class="line"><span class="comment">-- e. 插入数据到新表中</span></span><br><span class="line"><span class="comment">-- f. 关闭游标</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">procedure</span> p11(<span class="keyword">in</span> uage <span class="type">int</span>)</span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">declare</span> uname <span class="type">varchar</span>(<span class="number">100</span>) <span class="type">character</span> <span class="keyword">set</span> utf8;</span><br><span class="line"> <span class="keyword">declare</span> upro <span class="type">varchar</span>(<span class="number">100</span>) <span class="type">character</span> <span class="keyword">set</span> utf8;</span><br><span class="line"> <span class="keyword">declare</span> u_cursor <span class="keyword">cursor</span> <span class="keyword">for</span> <span class="keyword">select</span> name, profession <span class="keyword">from</span> tb_user <span class="keyword">where</span> age <span class="operator"><=</span> uage;</span><br><span class="line"> <span class="keyword">declare</span> exit handler <span class="keyword">for</span> <span class="keyword">SQLSTATE</span> <span class="string">'02000'</span> <span class="keyword">close</span> u_cursor;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">drop</span> <span class="keyword">table</span> if <span class="keyword">exists</span> tb_user_pro;</span><br><span class="line"> <span class="keyword">create</span> <span class="keyword">table</span> if <span class="keyword">not</span> <span class="keyword">exists</span> tb_user_pro(</span><br><span class="line"> id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line"> name <span class="type">varchar</span>(<span class="number">100</span>),</span><br><span class="line"> profession <span class="type">varchar</span>(<span class="number">100</span>)</span><br><span class="line"> ) <span class="type">character</span> <span class="keyword">set</span> utf8;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">open</span> u_cursor;</span><br><span class="line"> while <span class="literal">true</span> do</span><br><span class="line"> <span class="keyword">fetch</span> u_cursor <span class="keyword">into</span> uname,upro;</span><br><span class="line"> <span class="keyword">insert</span> <span class="keyword">into</span> tb_user_pro <span class="keyword">values</span> (<span class="keyword">null</span>, uname, upro);</span><br><span class="line"> <span class="keyword">end</span> while;</span><br><span class="line"> <span class="keyword">close</span> u_cursor;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"><span class="keyword">call</span> p11(<span class="number">30</span>);</span><br></pre></td></tr></table></figure><h4 id="条件处理程序"><a href="#条件处理程序" class="headerlink" title="条件处理程序"></a>条件处理程序</h4><p>条件处理程序(Handler)可用来定义在流程控制结构执行过程中遇到问题时相应的处理步骤。具体语法为</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></pre></td><td class="code"><pre><span class="line"><span class="keyword">DECLARE</span> handler_action HANDLER <span class="keyword">FOR</span> condition_value [,condition_value] ... statement;</span><br><span class="line"></span><br><span class="line">handler_action</span><br><span class="line">CONTINUE:继续执行当前程序</span><br><span class="line">EXIT:终止执行当前程序</span><br><span class="line">condition_value</span><br><span class="line"><span class="keyword">SQLSTATE</span> sqlstate_value:状态码,如<span class="number">02000</span></span><br><span class="line"><span class="keyword">SQLWARNING</span>: 所有以<span class="number">01</span>开头的<span class="keyword">SQLSTATE</span>代码的简写</span><br><span class="line"><span class="keyword">NOT</span> FOUND: 所有以<span class="number">02</span>开头的<span class="keyword">SQLSTATE</span>代码的简写</span><br><span class="line"><span class="keyword">SQLEXCEPTION</span>: 所有没有被<span class="keyword">SQLWARNING</span> 或 <span class="keyword">NOT</span> FOUND 捕获的<span class="keyword">SQLSTATE</span>代码的简写</span><br></pre></td></tr></table></figure><p>官方文档:<a href="https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html">https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html</a></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729180651105.png" alt="image-20230729180651105"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729180955139.png" alt="image-20230729180955139"></p><h3 id="存储函数"><a href="#存储函数" class="headerlink" title="存储函数"></a>存储函数</h3><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><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="keyword">CREATE</span> FUCTION 存储函数名称([参数列表])</span><br><span class="line"><span class="keyword">RETURNS</span> type [characteristic ...]</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">RETURN</span> ...;</span><br><span class="line"><span class="keyword">END</span>;</span><br><span class="line"></span><br><span class="line">characteristic 说明:</span><br><span class="line"><span class="keyword">DETERMINISTIC</span>:相同的输入参数总是产生相同的结果</span><br><span class="line"><span class="keyword">NO</span> <span class="keyword">SQL</span>:不包含<span class="keyword">SQL</span>语句</span><br><span class="line"><span class="keyword">READS</span> <span class="keyword">SQL</span> DATA:包含读取数据的语句,但不包含写入数据的语句</span><br></pre></td></tr></table></figure><blockquote><h3 id="定义存储函数,完成如下需求"><a href="#定义存储函数,完成如下需求" class="headerlink" title="定义存储函数,完成如下需求"></a>定义存储函数,完成如下需求</h3><p>计算从1累加到n的值,n作为传入的参数值</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></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">function</span> fun1(n <span class="type">int</span>)</span><br><span class="line"><span class="keyword">returns</span> <span class="type">int</span> <span class="keyword">deterministic</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">declare</span> total <span class="type">int</span> <span class="keyword">default</span> <span class="number">0</span>;</span><br><span class="line"> while n <span class="operator">></span> <span class="number">0</span> do</span><br><span class="line"> <span class="keyword">set</span> total :<span class="operator">=</span> total <span class="operator">+</span> n;</span><br><span class="line"> <span class="keyword">set</span> n :<span class="operator">=</span> n <span class="operator">-</span> <span class="number">1</span>;</span><br><span class="line"> <span class="keyword">end</span> while;</span><br><span class="line"> <span class="keyword">return</span> total;</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> fun1(<span class="number">100</span>);</span><br></pre></td></tr></table></figure><h2 id="三、触发器"><a href="#三、触发器" class="headerlink" title="三、触发器"></a>三、触发器</h2><p>触发器是与表有关的数据库对象,指在insert/update/delete之前或之后,触发并执行触发器中定义的SQL语句集合。触发器的这种特性可以协助应用在数据库端确保数据的完整性,日志记录,数据校验等操作</p><p>使用别名 OLD 和 NEW 来引用触发器中发生变化的记录内容,这与其他数据库是相似的。现在触发器还只支持行级触发,不支持语句级触发</p><div class="table-container"><table><thead><tr><th>触发器类型</th><th>NEW 和 OLD</th></tr></thead><tbody><tr><td>INSERT 型触发器</td><td>NEW 表示将要或者已经新增的数据</td></tr><tr><td>UPDATE 型触发器</td><td>OLD 表示修改之前的数据,NEW 表示将要或已经修改后的数据</td></tr><tr><td>DELETE 型触发器</td><td>OLD 表示将要或者已经删除的数据</td></tr></tbody></table></div><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TRIGGER</span> trigger_name</span><br><span class="line">BEFORE<span class="operator">|</span>AFTER <span class="keyword">INSERT</span><span class="operator">|</span><span class="keyword">UPDATE</span><span class="operator">|</span><span class="keyword">DELETE</span></span><br><span class="line"><span class="keyword">ON</span> tbl_name <span class="keyword">FOR</span> <span class="keyword">EACH</span> <span class="type">ROW</span></span><br><span class="line"><span class="keyword">BEGIN</span> </span><br><span class="line">trigger_stmt;</span><br><span class="line"><span class="keyword">END</span>;</span><br></pre></td></tr></table></figure><p><strong>查看</strong></p><p><code>SHOW TRIGGERS;</code></p><p><strong>删除</strong></p><p><code>DROP TRIGGER [schema_name.]trigger_name;</code>:如果没有指定schema_name,默认为当前数据库</p><blockquote><h3 id="定义触发器,完成如下需求"><a href="#定义触发器,完成如下需求" class="headerlink" title="定义触发器,完成如下需求"></a>定义触发器,完成如下需求</h3><p>通过触发器记录tb_user表的数据变更日志,将变更日志插入到日志表user_logs中,包含增加、修改、删除</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><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></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> user_logs(</span><br><span class="line">id <span class="type">int</span>(<span class="number">11</span>) <span class="keyword">not</span> <span class="keyword">null</span> auto_increment,</span><br><span class="line"> operation <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'操作类型: insert|update|delete'</span>,</span><br><span class="line"> operate_time datetime <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'操作时间'</span>,</span><br><span class="line"> operate_id <span class="type">int</span>(<span class="number">11</span>) <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'操作的ID'</span>,</span><br><span class="line"> operate_params <span class="type">varchar</span>(<span class="number">500</span>) comment <span class="string">'操作参数'</span>,</span><br><span class="line"> <span class="keyword">primary</span> key(`id`)</span><br><span class="line">)engine <span class="operator">=</span> innodb <span class="keyword">default</span> charset<span class="operator">=</span>utf8;</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">trigger</span> tb_user_insert_trigger</span><br><span class="line"> after <span class="keyword">insert</span> <span class="keyword">on</span> tb_user <span class="keyword">for</span> <span class="keyword">each</span> <span class="type">row</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">insert</span> <span class="keyword">into</span> user_logs(id, operation, operate_time, operate_id, operate_params) <span class="keyword">VALUES</span></span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'insert'</span>, now(), new.id, concat(<span class="string">'插入的数据内容为: id='</span>, new.id,<span class="string">',name='</span>,new.name,<span class="string">',phone='</span>,new.phone,<span class="string">',email='</span>,new.email,<span class="string">',profession='</span>,new.profession));</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">show</span> triggers ;</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">trigger</span> tb_user_insert_trigger;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 插入数据到tb_user表</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_user(id, name, phone, email, profession, age, gender, status, createtime) <span class="keyword">VALUES</span></span><br><span class="line"> (<span class="number">25</span>, <span class="string">'二皇子'</span>, <span class="string">'18809091212'</span>, <span class="string">'erhuangzi@163.com'</span>, <span class="string">'软件工程'</span>, <span class="number">23</span>, <span class="string">'1'</span>, <span class="string">'1'</span>, now());</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">trigger</span> tb_user_update_trigger</span><br><span class="line"> after <span class="keyword">update</span> <span class="keyword">on</span> tb_user <span class="keyword">for</span> <span class="keyword">each</span> <span class="type">row</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">insert</span> <span class="keyword">into</span> user_logs(id, operation, operate_time, operate_id, operate_params) <span class="keyword">VALUES</span></span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'update'</span>, now(), new.id,concat(<span class="string">'更新之前的数据: id='</span>,old.id,<span class="string">',name='</span>,old.name,<span class="string">',phone='</span>,old.phone,<span class="string">',email='</span>,old.email,<span class="string">',profession='</span>,old.profession,</span><br><span class="line"> <span class="string">'|更新之后的数据: id='</span>,new.id,<span class="string">',name='</span>,new.name,<span class="string">'.phone='</span>,new.phone,<span class="string">',email='</span>,new.email,<span class="string">',profession='</span>,new.profession));</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">update</span> tb_user <span class="keyword">set</span> age <span class="operator">=</span> <span class="number">20</span> <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">23</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">update</span> tb_user <span class="keyword">set</span> profession <span class="operator">=</span> <span class="string">'会计'</span> <span class="keyword">where</span> id <span class="operator"><=</span> <span class="number">3</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">trigger</span> tb_user_delete_trigger</span><br><span class="line"> after <span class="keyword">delete</span> <span class="keyword">on</span> tb_user <span class="keyword">for</span> <span class="keyword">each</span> <span class="type">row</span></span><br><span class="line"><span class="keyword">begin</span></span><br><span class="line"> <span class="keyword">insert</span> <span class="keyword">into</span> user_logs(id, operation, operate_time, operate_id, operate_params) <span class="keyword">VALUES</span></span><br><span class="line"> (<span class="keyword">null</span>, <span class="string">'delete'</span>, now(), old.id, concat(<span class="string">'删除的数据内容为: id='</span>, old.id,<span class="string">',name='</span>,old.name,<span class="string">',phone='</span>,old.phone,<span class="string">',email='</span>,old.email,<span class="string">',profession='</span>,old.profession));</span><br><span class="line"><span class="keyword">end</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> tb_user <span class="keyword">where</span> id <span class="operator">=</span> <span class="number">25</span>;</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230729211521053.png" alt="image-20230729211521053"></p>]]></content>
<summary type="html"><h2 id="一、视图"><a href="#一、视图" class="headerlink" title="一、视图"></a>一、视图</h2><p>视图(View)是一种虚拟存在的表。视图中的数据并不在数据库中实际存在,行和列数据来自定义视图的查询中使用的表,并且是在使用</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>SQL优化</title>
<link href="http://example.com/posts/6641.html"/>
<id>http://example.com/posts/6641.html</id>
<published>2023-10-14T06:45:00.000Z</published>
<updated>2023-10-14T08:23:00.444Z</updated>
<content type="html"><![CDATA[<h2 id="一、插入数据"><a href="#一、插入数据" class="headerlink" title="一、插入数据"></a>一、插入数据</h2><p><strong>批量插入</strong></p><p><code>insert into tb_text values(1, 'Tom'),(2, 'Cat');</code></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></pre></td><td class="code"><pre><span class="line"><span class="keyword">start</span> transaction;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_test <span class="keyword">values</span>(<span class="number">1</span>, <span class="string">'Tom'</span>),(<span class="number">2</span>, <span class="string">'Cat'</span>),(<span class="number">3</span>, <span class="string">'Jerry'</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_test <span class="keyword">values</span>(<span class="number">4</span>, <span class="string">'Tom'</span>),(<span class="number">5</span>, <span class="string">'Cat'</span>),(<span class="number">6</span>, <span class="string">'Jerry'</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> tb_test <span class="keyword">values</span>(<span class="number">7</span>, <span class="string">'Tom'</span>),(<span class="number">8</span>, <span class="string">'Cat'</span>),(<span class="number">9</span>, <span class="string">'Jerry'</span>);</span><br></pre></td></tr></table></figure><p><strong>主键顺序插入</strong></p><p>乱序插入:8 1 9 21 88 2 4</p><p>顺序插入:1 2 4 8 9 21 88</p><font color=#9932CD>主键顺序插入性能高于乱序插入</font><p><strong>大批量插入数据</strong></p><p>如果一次性需要插入大批量数据,使用insert语句插入性能较低,此时可以使用MySQL数据库提供的load指令进行插入</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">--local-infile</span></span><br><span class="line">mysql <span class="comment">--local-infile -uroot -p123456</span></span><br><span class="line"># 设置全局参数local_infile为<span class="number">1</span>,开启从本地加载文件导入数据的开关</span><br><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> local_infile <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"># 执行load指令将准备好的数据加载倒表结构中</span><br><span class="line">load data <span class="keyword">local</span> infile <span class="string">'/root/load_user_100w_sort.sql'</span> <span class="keyword">into</span> <span class="keyword">table</span> tb_user fields terminated <span class="keyword">by</span> <span class="string">','</span> lines terminated <span class="keyword">by</span> <span class="string">'\n'</span>;</span><br></pre></td></tr></table></figure><p><strong>准备工作</strong></p><p>扩展虚拟机内存,防止内存不够用</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726215235981.png" alt="image-20230726215235981"></p><p>上传sql文件到linux上</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726215536883.png" alt="image-20230726215536883"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726220311245.png" alt="image-20230726220311245"></p><h2 id="二、主键优化"><a href="#二、主键优化" class="headerlink" title="二、主键优化"></a>二、主键优化</h2><p>在InnoDB存储引擎中,表数据都是根据主键顺序组织存放的,这种存储方式的表称为<font color=#9932CD>索引组织表</font>(index organized table IOT)</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/B%255EMTree%25E4%25BC%2598%25E5%258C%2596.png" alt="B^MTree优化"></p><p><strong>页分裂</strong></p><p>页可以为空,也可以填充一半,也可以填充满。每个页包含了2-N行数据(如果一行数据过大,会行溢出),根据主键排列</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/pkey_yh.jpg" alt="pkey_yh"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/pkey_lx.png" alt="pkey_lx"></p><p><strong>页合并</strong></p><p>当删除一行记录时,实际上记录并没有被物理删除,只是记录被标记(flaged)为删除并且它的空间变得允许被其他记录声明使用。当页中删除的记录达到MERGE_THRESHOLD(默认为页的50%),InnoDB会开始寻找最靠近的页(前或后)看看是否可以将两个页合并以优化空间使用</p><p><code>MERGE_THRESHOLD</code>:合并页的阈值,可以自己设置,在创建表或者创建索引时指定</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/pkey_hb.png" alt="pkey_hb"></p><p><strong>主键设计原则</strong></p><ul><li>满足业务需求的情况下,尽量降低主键的长度</li><li>插入数据时,尽量选择顺序插入,选择使用AUTO_INCREMENT自增主键</li><li>尽量不要使用UUID做主键或其他自然主键,如身份证号</li><li>业务操作时,避免对主键的修改</li></ul><h2 id="三、order-by优化"><a href="#三、order-by优化" class="headerlink" title="三、order by优化"></a>三、order by优化</h2><p><strong>Using filesort:</strong> 通过表的索引或全表扫描,读取满足条件的数据行,然后在排序缓冲区sort buffer中完成排序操作,所有不是通过索引直接返回排序结果的排序都叫FileSort排序</p><p><strong>Using index:</strong> 通过有序索引顺序扫描直接返回有序数据,这种情况即为using index,不需要额外排序,操作效率高</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727154719067.png" alt="image-20230727154719067"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727155524277.png" alt="image-20230727155524277"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727155702790.png" alt="image-20230727155702790"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/asc_yh.jpg" alt="asc_yh"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/desc_yh.jpg" alt="desc_yh"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727160335913.png" alt="image-20230727160335913"></p><p>🔥根据排序字段建立合适的索引,多字段排序时,也遵循最左前缀法则</p><p>🔥尽量使用覆盖索引</p><p>🔥多字段排序,一个升序一个降序,此时需要注意联合索引在创建时的规则(ASC/DESC)</p><p>🔥如果不可避免的出现filesort,大数据量排序时,可以适当增大排序缓冲区大小sort_buffer_size(默认256k)</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727160716078.png" alt="image-20230727160716078"></p><h2 id="四、group-by优化"><a href="#四、group-by优化" class="headerlink" title="四、group by优化"></a>四、group by优化</h2><p>在分组操作时,可以通过索引来提高效率</p><p>分组操作时,索引的使用也是满足最左前缀法则的</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727161433033.png" alt="image-20230727161433033"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727161654124.png" alt="image-20230727161654124"></p><h2 id="五、limit优化"><a href="#五、limit优化" class="headerlink" title="五、limit优化"></a>五、limit优化</h2><p>在数据量比较大时,如果进行limit分页查询,在查询时,越往后,分页查询效率越低</p><p>当在进行分页查询时,如果执行 limit 2000000,10 ,此时需要MySQL排序前2000010 记录,仅仅返回 2000000 - 2000010 的记录,其他记录丢弃,查询排序的代价非常大</p><p>优化思路:一般分页查询时,通过创建覆盖索引能够比较好地提供性能,可以通过<font color=#9932CD>覆盖索引加子查询</font>形式进行优化</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727163806653.png" alt="image-20230727163806653"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727163833346.png" alt="image-20230727163833346"></p><h2 id="六、count优化"><a href="#六、count优化" class="headerlink" title="六、count优化"></a>六、count优化</h2><p>MyISAM引擎把一个表的总行数存在了磁盘上,因此执行count(*)的时候会直接返回这个数,效率很高</p><p>InnoDB引擎就麻烦了,它执行count(*)的时候,需要把数据一行一行地从引擎里面读出来,然后累积计数</p><p>优化思路:自己计数</p><p>count()是一个聚合函数,对于返回的结果集,一行行地判断,如果count函数的参数不是NULL,累计值加1,否则不加,最后返回累计值</p><p>用法:count(*)、count(主键)、count(字段)、count(1)</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230727165000553.png" alt="image-20230727165000553"></p><p><strong>count(主键):</strong> InnoDB引擎会遍历整张表,把每一行的主键id值都取出来,返回给服务层。服务层拿到主键后,直接按行进行累加(主键不可能为null)</p><p><strong>count(字段):</strong> 没有not null 约束:InnoDB引擎会遍历整张表把每一行的字段值都取出来,返回给服务层,服务层判断是否为null,不为null,计数累加;有not null 约束:InnoDB引擎会遍历整张表把每一行的字段值提取出来,返回给服务层,直接按行进行累加</p><p><strong>count(1):</strong> InnoDB引擎遍历整张表,但不取值。服务层对于返回的每一行,放一个数字“1”进去,直接按行进行累加</p><p><strong>count(*):</strong> InnoDB引擎并不会把全部字段取出来,而是专门做了优化,不取值,服务层直接按行进行累加</p><p>按照效率排序的话:count(字段)<count(主键)<count(1)<count(*),所以尽量使用count(*)</p><h2 id="七、update优化"><a href="#七、update优化" class="headerlink" title="七、update优化"></a>七、update优化</h2><p>InnoDB的行锁是针对索引加的锁,不是针对记录加的锁,并且该索引不能失效,否则会从行锁升级为表锁</p><font color=#9932CD>在执行update语句时一定要根据索引字段去更新,避免行锁升级为表锁会降低数据库的并发访问性能</font>]]></content>
<summary type="html"><h2 id="一、插入数据"><a href="#一、插入数据" class="headerlink" title="一、插入数据"></a>一、插入数据</h2><p><strong>批量插入</strong></p>
<p><code>insert into tb_text</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>索引</title>
<link href="http://example.com/posts/6640.html"/>
<id>http://example.com/posts/6640.html</id>
<published>2023-10-14T03:25:00.000Z</published>
<updated>2023-10-14T08:21:53.295Z</updated>
<content type="html"><![CDATA[<h2 id="一、索引概述"><a href="#一、索引概述" class="headerlink" title="一、索引概述"></a>一、索引概述</h2><p>索引(index)是帮助MySQL高效获取数据的<font color=#9932CD>数据结构</font>(有序)</p><p>在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法,这种数据结构就是索引</p><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723124120600.png" alt="image-20230723124120600"></p><div class="table-container"><table><thead><tr><th>优势</th><th>劣势</th></tr></thead><tbody><tr><td>提高数据检索的效率,降低数据库的IO成本</td><td>索引列也是要占用空间的</td></tr><tr><td>通过索引列对数据进行排序,降低数据排序的成本,降低CPU的消耗</td><td>索引大大提供了查询效率,同时却也降低更新表的速度,如对表进行INSERT、UPDATE、DELETE时,效率降低</td></tr></tbody></table></div><h2 id="二、索引结构"><a href="#二、索引结构" class="headerlink" title="二、索引结构"></a>二、索引结构</h2><p>MySQL的索引是在存储引擎层实现的,不同的存储引擎有不同的结构,主要包含以下几种:</p><div class="table-container"><table><thead><tr><th>索引结构</th><th>描述</th></tr></thead><tbody><tr><td>B+Tree索引</td><td>最常见的索引类型,大部分引擎都支持B+树索引</td></tr><tr><td>Hash索引</td><td>底层数据结构是用哈希表实现的,只有精确匹配索引列的查询才有效,不支持范围查询</td></tr><tr><td>R-tree(空间索引)</td><td>空间索引是MyISAM引擎的一个特殊索引类型,主要用于地理空间数据类型,通常使用较少</td></tr><tr><td>Full-text(全文索引)</td><td>是一种通过建立倒排索引,快速匹配文档的方式。类似于Lucene,Solr,ES</td></tr></tbody></table></div><div class="table-container"><table><thead><tr><th>索引</th><th>InnoDB</th><th>MyISAM</th><th>Memory</th></tr></thead><tbody><tr><td>B+tree索引</td><td>✔</td><td>✔</td><td>✔</td></tr><tr><td>Hash索引</td><td>🗙</td><td>🗙</td><td>✔</td></tr><tr><td>R-tree索引</td><td>🗙</td><td>✔</td><td>🗙</td></tr><tr><td>Full-text</td><td>5.6版之后支持</td><td>✔</td><td>🗙</td></tr></tbody></table></div><p>常用索引如果没有特指,都是指B+树结构组织的索引</p><p><strong>二叉树:</strong> 顺序插入时,会形成一个链表,查询性能大大降低。大数据量情况下,层级较深,检索速度慢</p><p><strong>红黑树:</strong> 大数据量情况下,层级较深,检索速度慢</p><blockquote><p>红黑树是一种自平衡的二叉查找树,同时满足以下特性:</p><ol><li>节点非黑即红</li><li>根节点是黑色</li><li>叶子节点(NIL,该节点没有后续的子节点)都是黑色</li><li>红色节点的父节点和子节点都必须是黑色的</li><li>从根节点到叶子节点的每条路径上,黑色节点的数量相同</li></ol></blockquote><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723212306829.png" alt="image-20230723212306829"></p><p><strong>B-Tree:</strong> 是一种多路平衡查找树,其中的B表示balance的意思</p><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/%E7%B4%A2%E5%BC%95%E7%BB%93%E6%9E%84B-Tree.png" alt="索引结构B-Tree"></p><blockquote><p>树的度数指的是一个节点的子节点个数</p></blockquote><p>可通过<a href="https://www.cs.usfca.edu/~galles/visualization/Algorithms.html">数据结构可视化</a>网站演示B-Tree树演变过程,下面以插入==100 65 169 368 900 556 780 35 215 1200 234 888 158 90 1000 88 120 268 250==数据为例进行演示</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723221716304.png" alt="image-20230723221716304"></p><p><strong>B+Tree:</strong>是一种多叉树,类似于二叉树,但每个节点可以有更多的子节点</p><blockquote><p>B+Tree所具有的特点:</p><ol><li>所有的数据都存储在叶子节点上,而非叶子节点只存储索引信息</li><li>叶子节点通过指针连接在一起</li><li>所有的叶子节点具有相同的深度,使得查询的时间复杂度保持在对数级别</li><li>非叶子节点中的索引值可以重复,这样可以提高查询效率</li></ol></blockquote><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/B%2BTree.png" alt="B+Tree"></p><p>可通过<a href="https://www.cs.usfca.edu/~galles/visualization/Algorithms.html">数据结构可视化</a>网站演示B-Tree树演变过程,下面以插入==100 65 169 368 900 556 780 35 215 1200 234 888 158 90 1000 88 120 268 250==数据为例进行演示</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724112037449.png" alt="image-20230724112037449"></p><blockquote><p>MySQL索引数据结构对经典的B+Tree进行了优化。在原B+Tree的基础上,增加一个指向相邻叶子节点的链表指针,这就形成了带有顺序指针的B+Tree,提高区间访问的性能</p></blockquote><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/B%5EMTree%E4%BC%98%E5%8C%96.png" alt="B^MTree优化"></p><p><strong>Hash索引:</strong> 采用一定的hash算法,将键值换算成新的hash值,映射到对应的槽位上,然后存储在hash表中</p><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724153225293.png" alt="image-20230724153225293"></p><blockquote><p>Hash索引特点:</p><ol><li>Hash索引只能用于对等比较(=,in),不支持范围查询(between,>,<,…)</li><li>无法利用索引完成排序操作</li><li>查询效率高,通常只需要一次检索就可以了,效率通常要高于B+Tree索引</li></ol></blockquote><p>在MySQL中,支持Hash索引的是Memory引擎,而InnoDB中具有自适应hash功能,hash索引是存储引擎根据B+Tree索引在指定条件下自动构建的</p><h3 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h3><p>⛦为什么InnoDB存储引擎选择使用B+Tree索引结构?</p><ul><li>相对于二叉树,层级更少,搜索效率高</li><li>对于B-Tree,无论是叶子节点还是非叶子节点,都会保存数据,这样导致一页中存储的键值减少,指针跟着减少,要同样保存大量数据,只能增加树的层级,导致性能降低</li><li>相对Hash索引,B+Tree支持范围匹配和排序操作</li></ul><h2 id="三、索引分类"><a href="#三、索引分类" class="headerlink" title="三、索引分类"></a>三、索引分类</h2><div class="table-container"><table><thead><tr><th>分类</th><th>含义</th><th>特点</th><th>关键字</th></tr></thead><tbody><tr><td>主键索引</td><td>针对于表中主键创建的索引</td><td>默认自动创建,只能有一个</td><td>PRIMARY</td></tr><tr><td>唯一索引</td><td>避免同一个表中某数据列中的值重复</td><td>可以有多个</td><td>UNIQUE</td></tr><tr><td>常规索引(单利索引)</td><td>快速定位特定数据</td><td>可以有多个</td><td></td></tr><tr><td>全文索引</td><td>全文索引查找的是文本中的关键字,而不是比较索引中的值</td><td>可以有多个</td><td>FULLTEXT</td></tr></tbody></table></div><p>在InnoDB存储引擎中,根据索引的存储形式,又可以分为以下两种</p><div class="table-container"><table><thead><tr><th>分类</th><th>含义</th><th>特点</th></tr></thead><tbody><tr><td>聚集索引(Clustered Index)</td><td>将数据存储与索引放到了一块,索引结构的叶子节点保存了行数据</td><td>必须有且只有一个</td></tr><tr><td>二级索引(Secondary Index)</td><td>将数据与索引分开存储,索引结构的叶子节点关联的是对应的主键</td><td>可以存在多个</td></tr></tbody></table></div><p>聚集索引选取规则</p><ul><li>如果存在主键,主键索引就是聚集索引</li><li>如果不存在主键,将使用第一个唯一(UNIQUE)索引作为聚集索引</li><li>如果没有主键或没有合适的唯一索引,则InnoDB会自动生成一个rowid作为隐藏的聚集索引</li></ul><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/clusteredIndex1.PNG" alt="clusteredIndex1"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/clusteredIndex2.PNG" alt="clusteredIndex2"></p><h3 id="思考-1"><a href="#思考-1" class="headerlink" title="思考"></a>思考</h3><p>以下SQL语句,哪个执行效率高?为什么?</p><p><code>select \* from user where id = 10;</code></p><p><code>select \* from user where name = 'Arm';</code></p><p><strong>备注:</strong> id为主键,name字段有对应的二级索引</p><p>InnoDB主键索引的B+Tree高度为多高呢?</p><p>假设:一行数据大小为1k,一页可以存储16行这样的数据。InnoDB的指针占用6个字节空间,主键即使为bigint,占用字节数为8</p><p>当高度为2:</p><p>n*8+(n+1)*6 = 16*1024,算出n约为1170</p><p>1171*16 = 18736,可以算出第2层子节点的个数</p><p>当高度为3:</p><p>1171*1171*16 = 21939856,可以算出第3层子节点的个数</p><h2 id="四、索引语法"><a href="#四、索引语法" class="headerlink" title="四、索引语法"></a>四、索引语法</h2><p><strong>创建索引</strong></p><p><code>CREATE [UNIQUE|FULLTEXT] INDEX index_name ON table_name (index_col_name,...);</code></p><p><strong>查看索引</strong></p><p><code>SHOW INDEX FROM table_name;</code></p><p><strong>删除索引</strong></p><p><code>DROP INDEX index_name ON table_name;</code></p><p><strong>准备工作</strong></p><p>在Linux上创建名为itcast的mysql数据库,然后再创建一张表 tb_user,顺便插入准备好的数据</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></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 class="keyword">INSERT</span> <span class="keyword">INTO</span> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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><blockquote><h3 id="按照下列的需求,完成索引的创建"><a href="#按照下列的需求,完成索引的创建" class="headerlink" title="按照下列的需求,完成索引的创建"></a>按照下列的需求,完成索引的创建</h3><ol><li>name字段的值可能会重复,为该字段创建索引</li><li>phone字段的值非空且唯一,为该字段创建唯一索引</li><li>为profession、age、status创建联合索引</li><li>为email建立合适的索引来提升查询效率</li></ol></blockquote><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724200953582.png" alt="image-20230724200953582"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724211338021.png" alt="image-20230724211338021"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724211238555.png" alt="image-20230724211238555"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724211153207.png" alt="image-20230724211153207"></p><h2 id="五、SQL性能分析"><a href="#五、SQL性能分析" class="headerlink" title="五、SQL性能分析"></a>五、SQL性能分析</h2><h3 id="SQL执行频率"><a href="#SQL执行频率" class="headerlink" title="SQL执行频率"></a>SQL执行频率</h3><p>MySQL客户端连接成功后,通过show[session|global] status命令可以提供服务器状态信息。通过如下指令,可以查看当前数据库的INSERT、UPDATE、DELETE、SELECT的访问频次</p><p><code>SHOW GLOBAL STATUS LIKE 'Com_______';</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724212607961.png" alt="image-20230724212607961"></p><p>通过查询SQL执行频率为SQL优化作支撑</p><h3 id="慢查询日志"><a href="#慢查询日志" class="headerlink" title="慢查询日志"></a>慢查询日志</h3><p>慢查询日志记录了所有执行时间超过指定参数(long_query_time,单位:秒,默认10秒)的所有SQL语句的日志</p><p>MySQL的慢查询日志默认没有开启,需要在MySQL的配置文件(/etc/my.cnf)中配置如下信息:</p><blockquote><p># 开启MySQL慢查询日志开关</p><p>slow_query_log=1</p><p># 设置慢日志的时间为2秒,SQL语句执行时间超过2秒,就会视为慢查询,记录慢查询日志</p><p>long_query_time=2</p></blockquote><p>配置完毕之后,通过以下指令重新启动MySQL服务器进行测试,查看慢日志文件中记录的信息 /var/lib/mysql/localhost-slow.log</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724213641592.png" alt="image-20230724213641592"></p><p>配置完后重启mysql服务:<code>systemctl restart mysqld</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724213757966.png" alt="image-20230724213757966"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724213916544.png" alt="image-20230724213916544"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724214104094.png" alt="image-20230724214104094"></p><p>通过检查慢查询日志可以发现,只记录超过预设时间的SQL,而这样就可以定位出执行效率比较低的SQL,从而针对性进行优化</p><h3 id="profile详情"><a href="#profile详情" class="headerlink" title="profile详情"></a>profile详情</h3><p>show profiles能够在做SQL优化时帮助了解执行时间都耗费在那一块儿</p><p>通过have_profiling参数能够看到当前MySQL是否支持profile操作:<code>SELECT @@have_profiling;</code></p><p>默认profiling是关闭的,可以通过set语句在session/global级别开启profiling:<code>SET profiling=1;</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724215149130.png" alt="image-20230724215149130"></p><p>执行一系列的业务SQL操作,然后通过如下指令查看指令的执行耗时</p><blockquote><p># 查看每一条SQL的耗时基本情况</p><p>show profiles;</p><p># 查看指定query_id的SQL语句各个阶段的耗时情况</p><p>show profile for query query_id;</p><p># 查看指定query_id的SQL语句CPU的使用情况</p><p>show profile cpu for query query_id;</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724215728906.png" alt="image-20230724215728906"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230724220042985.png" alt="image-20230724220042985"></p><h3 id="explain"><a href="#explain" class="headerlink" title="explain"></a>explain</h3><p>EXPLAIN 或者 DESC命令获取MySQL如何执行SELECT语句的信息,包括在SELECT语句执行过程中表如何连接和连接的顺序</p><blockquote><p># 直接在select语句之前加上关键字explain/desc</p><p>EXPLAIN SELECT 字段列表 FROM 表名 WHERE 条件;</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726091809789.png" alt="image-20230726091809789"></p><p>EXPLAIN 执行计划各字段含义:</p><ul><li>id:select查询的序列号,表示查询中执行select字句或操作表的顺序(id相同,执行顺序从上到下;id不同,值越大,越先执行)</li><li>select_type:表示SELECT的类型,常见的取值有SIMPLE(简单表即不使用表连接或子查询)、PRIMARY(主查询即外层查询)、UNION(UNION中的第二个或后面的查询语句)、SUBQUERY(SELECT/WHERE之后包含了子查询)等</li><li>type:表示连接类型,性能由好到差的连接类型为NULL、system、const、eq_ref、ref、range、index、all</li><li>possible_keys:显示可能应用在这张表上的索引,一个或多个</li><li>key:实际使用的索引,如果为NULL,则没有使用索引</li><li>key_len:表示索引中使用的字节数,该值为索引字段最大可能长度,并非实际长度,在不损失精确性前提下,长度越短越好</li><li>rows:MySQL认为必须要执行查询的行数,在Innodb引擎的表中,是一个预估值,并不总是准确的</li><li>filtered:表示返回结果的行数占需读取行数的百分比,filtered值越大越好</li></ul><p>【准备工作】</p><p>通过student表、course表和student_course表实现多表查询进行演示,首先需要创建并导入数据</p><p>因为在本地机上建立过三表且有对应数据,因而不必复写建表及插入操作,直接复制sql语句并运行即可</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726101901077.png" alt="image-20230726101901077"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726102024236.png" alt="image-20230726102024236"></p><p>为了同步老师所讲案例,对student_course表中的部分数据进行修改和增添</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="keyword">CREATE</span> <span class="keyword">TABLE</span> `course` (</span><br><span class="line"> `id` <span class="type">int</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line"> `name` <span class="type">varchar</span>(<span class="number">20</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb4 <span class="keyword">COLLATE</span> utf8mb4_0900_ai_ci <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> <span class="keyword">PRIMARY</span> KEY (`id`) <span class="keyword">USING</span> BTREE,</span><br><span class="line"> <span class="keyword">UNIQUE</span> INDEX `name`(`name`) <span class="keyword">USING</span> BTREE</span><br><span class="line">) ENGINE <span class="operator">=</span> InnoDB AUTO_INCREMENT <span class="operator">=</span> <span class="number">4</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> <span class="operator">=</span> utf8mb4 <span class="keyword">COLLATE</span> <span class="operator">=</span> utf8mb4_0900_ai_ci ROW_FORMAT <span class="operator">=</span> <span class="keyword">Dynamic</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `course` <span class="keyword">VALUES</span> (<span class="number">4</span>, <span class="string">'Hadoop'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `course` <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'Java'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `course` <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="string">'MySQL'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `course` <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">'PHP'</span>);</span><br></pre></td></tr></table></figure><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="keyword">CREATE</span> <span class="keyword">TABLE</span> `student` (</span><br><span class="line"> `id` <span class="type">int</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line"> `name` <span class="type">varchar</span>(<span class="number">20</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb3 <span class="keyword">COLLATE</span> utf8mb3_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `<span class="keyword">no</span>` <span class="type">varchar</span>(<span class="number">20</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8mb3 <span class="keyword">COLLATE</span> utf8mb3_general_ci <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> <span class="keyword">PRIMARY</span> KEY (`id`) <span class="keyword">USING</span> BTREE,</span><br><span class="line"> <span class="keyword">UNIQUE</span> INDEX `name`(`name`) <span class="keyword">USING</span> BTREE</span><br><span class="line">) ENGINE <span class="operator">=</span> InnoDB AUTO_INCREMENT <span class="operator">=</span> <span class="number">4</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> <span class="operator">=</span> utf8mb3 <span class="keyword">COLLATE</span> <span class="operator">=</span> utf8mb3_general_ci ROW_FORMAT <span class="operator">=</span> <span class="keyword">Dynamic</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">'黛绮丝'</span>, <span class="string">'2000100101'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">'谢逊'</span>, <span class="string">'2000100102'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="string">'殷天正'</span>, <span class="string">'2000100103'</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student` <span class="keyword">VALUES</span> (<span class="number">4</span>, <span class="string">'韦一笑'</span>, <span class="string">'2000100104'</span>);</span><br></pre></td></tr></table></figure><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="keyword">CREATE</span> <span class="keyword">TABLE</span> `student_course` (</span><br><span class="line"> `id` <span class="type">int</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line"> `studentid` <span class="type">int</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> `courseid` <span class="type">int</span> <span class="keyword">NULL</span> <span class="keyword">DEFAULT</span> <span class="keyword">NULL</span>,</span><br><span class="line"> <span class="keyword">PRIMARY</span> KEY (`id`) <span class="keyword">USING</span> BTREE,</span><br><span class="line"> INDEX `fk_studentid`(`studentid`) <span class="keyword">USING</span> BTREE,</span><br><span class="line"> INDEX `fk_courseid`(`courseid`) <span class="keyword">USING</span> BTREE,</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 class="keyword">ON</span> <span class="keyword">DELETE</span> RESTRICT <span class="keyword">ON</span> <span class="keyword">UPDATE</span> RESTRICT,</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 class="keyword">ON</span> <span class="keyword">DELETE</span> RESTRICT <span class="keyword">ON</span> <span class="keyword">UPDATE</span> RESTRICT</span><br><span class="line">) ENGINE <span class="operator">=</span> InnoDB AUTO_INCREMENT <span class="operator">=</span> <span class="number">5</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> <span class="operator">=</span> utf8mb4 <span class="keyword">COLLATE</span> <span class="operator">=</span> utf8mb4_0900_ai_ci ROW_FORMAT <span class="operator">=</span> <span class="keyword">Dynamic</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student_course` <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student_course` <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student_course` <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="number">1</span>, <span class="number">3</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student_course` <span class="keyword">VALUES</span> (<span class="number">4</span>, <span class="number">2</span>, <span class="number">2</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student_course` <span class="keyword">VALUES</span> (<span class="number">5</span>, <span class="number">2</span>, <span class="number">3</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> `student_course` <span class="keyword">VALUES</span> (<span class="number">6</span>, <span class="number">3</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726102707749.png" alt="image-20230726102707749"></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726104557679.png" alt="image-20230726104557679"></p><p>查询选修了 MySQL 课程的学生(子查询实现)</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726105430826.png" alt="image-20230726105430826"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726105451933.png" alt="image-20230726105451933"></p><p>查看不同sql语句的type</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726110917441.png" alt="image-20230726110917441"></p><h2 id="六、索引使用"><a href="#六、索引使用" class="headerlink" title="六、索引使用"></a>六、索引使用</h2><h3 id="验证索引效率"><a href="#验证索引效率" class="headerlink" title="验证索引效率"></a>验证索引效率</h3><p>在未建立索引之前,执行如下SQL语句,查看SQL耗时</p><p><code>SELECT * FROM tb_sku WHERE sn = '100000003145001';</code></p><p>针对字段创建索引</p><p><code>create index ind_sku_sn on tb_sku(sn);</code></p><p>然后再次执行相同sql语句,再次查看SQL耗时</p><p><code>SELECT * FROM tb_sku WHERE sn = '100000003145001';</code></p><h3 id="索引使用规则"><a href="#索引使用规则" class="headerlink" title="索引使用规则"></a>索引使用规则</h3><h4 id="最左前缀法则"><a href="#最左前缀法则" class="headerlink" title="最左前缀法则"></a>最左前缀法则</h4><p>如果索引了多列(联合索引),要遵守最左前缀法则。最左前缀法则指的是查询从索引的最左列开始,并且不跳过索引中的列;如果跳跃某一列,<font color=#9932CD>索引将部分失效(后面的字段索引失效)</font></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726191704644.png" alt="image-20230726191704644"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726192027462.png" alt="image-20230726192027462"></p><h4 id="范围查询"><a href="#范围查询" class="headerlink" title="范围查询"></a>范围查询</h4><p>联合索引中,出现范围查询(>,<),<font color=#9932CD>范围查询右侧的列索引失效</font></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726192615058.png" alt="image-20230726192615058"></p><h4 id="索引列运算"><a href="#索引列运算" class="headerlink" title="索引列运算"></a>索引列运算</h4><p>不要在索引列上进行运算操作,<font color=red>否则索引会失效</font></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726193321089.png" alt="image-20230726193321089"></p><h4 id="字符串不加引号"><a href="#字符串不加引号" class="headerlink" title="字符串不加引号"></a>字符串不加引号</h4><p>字符串类型字段使用时,不加引号,<font color=red>索引会失效</font></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726193731218.png" alt="image-20230726193731218"></p><h4 id="模糊查询"><a href="#模糊查询" class="headerlink" title="模糊查询"></a>模糊查询</h4><p>如果仅仅是尾部模糊匹配,索引不会失效。如果是头部模糊匹配,<font color=red>索引会失效</font></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726194131975.png" alt="image-20230726194131975"></p><h4 id="or连接的条件"><a href="#or连接的条件" class="headerlink" title="or连接的条件"></a>or连接的条件</h4><p>用or分割开的条件,如果or之前的条件中的列有索引,而后面的列中没有索引,那么涉及的索引都不会被用到</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726194559617.png" alt="image-20230726194559617"></p><p>由于age没有索引,所以即使id、phone有索引,索引也会失效,所以需要针对于age建立索引</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726194926417.png" alt="image-20230726194926417"></p><h4 id="数据分布影响"><a href="#数据分布影响" class="headerlink" title="数据分布影响"></a>数据分布影响</h4><p>如果MySQL评估使用索引比全表更慢,则不使用索引</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726195632680.png" alt="image-20230726195632680"></p><h4 id="SQL提示-覆盖索引"><a href="#SQL提示-覆盖索引" class="headerlink" title="SQL提示 + 覆盖索引"></a>SQL提示 + 覆盖索引</h4><p>SQL提示是优化数据库的一个重要手段,简单来说,就是在SQL语句中加入一些人为的提示来达到优化操作的目的</p><p>use index/ignore index/force index</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726200525573.png" alt="image-20230726200525573"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726200900163.png" alt="image-20230726200900163"></p><p>尽量使用覆盖索引(查询使用了索引,并且需要返回的列,在该索引中已经全部能够找到),减少select *</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726202039565.png" alt="image-20230726202039565"></p><p><code>using index condition:</code> 查找使用了索引,但需要回表查询数据</p><p><code>using where;using index:</code> 查找使用了索引,但需要的数据都在索引列中能找到,所以不需要回表查询</p><p><strong>思考:</strong> 一张表,有四个字段(id,username,password,status),由于数据量大,需要对以下SQL语句进行优化,如何优化才是最佳方案?</p><p><code>select id,username,password from tb_user where username = 'itcast';</code></p><p>建立username和password这两个字段的联合索引</p><h4 id="前缀索引"><a href="#前缀索引" class="headerlink" title="前缀索引"></a>前缀索引</h4><p>当字段类型为字符串(varchar,text等)时,有时候需要索引很长的字符串,这会让索引变得很大,查询时,浪费大量磁盘IO,影响查询效率。此时可以知将字符串的一部分前缀,建立索引,这样可以大大节约索引空间,从而提高索引效率</p><p><strong>语法:</strong> <code>create index idx_xxx on table_name(column(n));</code></p><p><strong>前缀长度:</strong> 可以根据索引的选择性来决定,而选择性是指不重复的索引值(基数)和数据表的记录总数的比值,索引选择性越高则查询效率越高,唯一索引的选择性是1,这是最好的索引选择性,性能也是最好的</p><p><code>select count(distinct email)/count(*) from tb_user;</code></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726204226996.png" alt="image-20230726204226996"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726204619695.png" alt="image-20230726204619695"></p><p>前缀索引的查询流程</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/qz_index.jpg" alt="qz_index"></p><h4 id="单列索引与联合索引"><a href="#单列索引与联合索引" class="headerlink" title="单列索引与联合索引"></a>单列索引与联合索引</h4><p>单列索引:即一个索引只包含单个列</p><p>联合索引:即一个索引包含了多个列</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230726210438727.png" alt="image-20230726210438727"></p><p>在业务场景中,如果存在多个查询条件,考虑针对于查询字段建立索引时,建议建立联合索引,而非单列索引</p><font color=#9932CD>多条件联合查询时,MySQL优化器会评估哪个字段的索引效率更高,会选择该索引完成本次查询</font><p>联合索引情况</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/lh_index.png" alt="lh_index"></p><h2 id="七、索引设计原则"><a href="#七、索引设计原则" class="headerlink" title="七、索引设计原则"></a>七、索引设计原则</h2><ol><li>针对于数据量较大且查询比较频繁的表建立索引</li><li>针对于常作为查询条件(where)、排序(order by)、分组(group by)操作的字段建立索引</li><li>尽量选择区分度高的列作为索引,尽量建立唯一索引,区分度越高,使用索引的效率越高</li><li>如果是字符串类型的字段,字段的长度较长,可以针对于字段的特点,建立前缀索引</li><li>尽量使用联合索引,减少单列索引,查询时,联合索引很多时候可以覆盖索引,节省存储空间,避免回表,提高查询效率</li><li>要控制索引的数量,索引并不是多多益善,索引越多,维护索引结构的代价就越大,会影响增删改的效率</li><li>如果索引不能存储NULL值,请在创建表时使用NOT NULL约束它。当优化器知道每列是否包含NULL值时,它可以更好确定哪个索引可以最有效地用于查询</li></ol>]]></content>
<summary type="html"><h2 id="一、索引概述"><a href="#一、索引概述" class="headerlink" title="一、索引概述"></a>一、索引概述</h2><p>索引(index)是帮助MySQL高效获取数据的<font color=#9932CD>数据结构</font</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>Linux</title>
<link href="http://example.com/posts/6639.html"/>
<id>http://example.com/posts/6639.html</id>
<published>2023-10-13T11:23:00.000Z</published>
<updated>2023-10-13T11:22:34.676Z</updated>
<content type="html"><![CDATA[<h2 id="视频地址"><a href="#视频地址" class="headerlink" title="视频地址"></a>视频地址</h2><p>B站黑马:<a href="https://b23.tv/av213091208/p118">缘起</a> —— <a href="https://b23.tv/av213091208/p141">缘灭</a></p><h2 id="一、Linux-简介"><a href="#一、Linux-简介" class="headerlink" title="一、Linux 简介"></a>一、Linux 简介</h2><p>Linux 是一套免费使用和自由传播的类 Unix 操作系统,是一个基于 POSIX 和 UNIX 的多用户、多任务、支持多线程和多 CPU 的操作系统</p><p>Linux 不仅系统性能稳定,还开放了源码</p><p><strong>桌面操作系统</strong></p><ul><li>Windows(用户数量最多)</li><li>Mac OS</li><li>Linux</li></ul><p><strong>服务器操作系统</strong></p><ul><li>UNIX</li><li>Linux</li><li>Windows Server</li></ul><p><strong>移动设备操作系统</strong></p><ul><li>Android(基于Linux、开源)</li><li>IOS(苹果公司开发、不开源)</li></ul><p><strong>嵌入式操作系统</strong></p><ul><li>Linux(机顶盒、路由器、交换机)</li></ul><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721112920496.png" alt="image-20230721112920496"></p><h3 id="Linux-系统版本"><a href="#Linux-系统版本" class="headerlink" title="Linux 系统版本"></a>Linux 系统版本</h3><p><strong>内核版</strong></p><ul><li>由Linus Torvalds及其团队开发、维护</li><li>开源免费</li><li>负责控制硬件</li></ul><p><strong>发行版</strong></p><ul><li>基于Linux内核版进行扩展</li><li>由各个Linux厂商开发和维护的</li><li>有收费版本和免费版本</li></ul><p>市面上较为知名的Linux发行版如下:</p><ul><li>Ubuntu:以桌面应用为主</li><li>RedHat:应用最广泛、收费</li><li>CentOS:RedHat的社区版、免费</li><li>openSUSE:对个人完全免费、图形界面华丽</li><li>Fedora:功能完备、快速更新、免费</li><li>红旗Linux:北京中科红旗软件技术有限公司开发(中科红旗承诺,红旗Linux桌面操作系统 v11及后续迭代版本永久向个人及家庭用户免费提供)</li></ul><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721151343628.png" alt="image-20230721151343628"></p><h2 id="二、Linux-安装"><a href="#二、Linux-安装" class="headerlink" title="二、Linux 安装"></a>二、Linux 安装</h2><p>Linux系统安装方式</p><ul><li>物理机安装:直接将操作系统安装到服务器硬件上</li><li>虚拟机安装:通过虚拟机软件安装</li></ul><p><strong>虚拟机</strong>是指通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统。虚拟机技术的核心思想是将物理计算机的资源(如处理器、内存、存储等)进行虚拟化,使多个虚拟机可以共享这些资源,并在其上运行不同的操作系统和应用程序</p><p>可以通过虚拟机软件来模拟计算机硬件</p><p>常用的虚拟机软件:VMWare、VirtualBox…</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721182825817.png" alt="image-20230721182825817"></p><h2 id="三、安装SSH连接工具"><a href="#三、安装SSH连接工具" class="headerlink" title="三、安装SSH连接工具"></a>三、安装SSH连接工具</h2><p>SSH(Secure Shell)是一种网络协议,用于在不安全的网络中安全地进行远程登录和数据传输。它通过加密和身份验证机制,确保数据在传输过程中的安全性</p><p>常用的SSH连接工具:putty、filezilla、xshell、finalshell</p><p>通过SSH连接工具就可以实现从本地连接到远程的Linux服务器</p><p>这里选择使用finalshell作为SSH连接工具</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721184522862.png" alt="image-20230721184522862"></p><p>然后使用FinalShell连接Linux</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721184801284.png" alt="image-20230721184801284"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721185020990.png" alt="image-20230721185020990"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721185105224.png" alt="image-20230721185105224"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721185159433.png" alt="image-20230721185159433"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721185246363.png" alt="image-20230721185246363"></p><h2 id="四、Linux目录结构"><a href="#四、Linux目录结构" class="headerlink" title="四、Linux目录结构"></a>四、Linux目录结构</h2><p>在Linux系统中,/是所有目录的顶点</p><blockquote><ul><li>bin 存放二进制可执行文件</li><li>boot 存放系统引导时使用的各种文件</li><li>dev 存放设备文件</li><li>etc 存放系统配置文件</li><li>home 存放系统用户的文件</li><li>lib 存放程序运行所需的共享库和内核模块</li><li>opt 额外安装的可选应用程序包所放置的位置</li><li>root 超级用户目录</li><li>sbin 存放二进制可执行文件,只有root用户才能访问</li><li>tmp 存放临时文件</li><li>usr 存放系统应用程序</li><li>var 存放运行时需要改变数据的文件,例如日志文件</li></ul></blockquote><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721193044740.png" alt="image-20230721193044740"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721194624566.png" alt="image-20230721194624566"></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230721195140040.png" alt="image-20230721195140040"></p><h2 id="五、Linux-常用命令"><a href="#五、Linux-常用命令" class="headerlink" title="五、Linux 常用命令"></a>五、Linux 常用命令</h2><ul><li>文件目录操作命令</li><li>拷贝移动命令</li><li>打包压缩命令</li><li>文本编辑命令</li><li>查找命令</li></ul><p>常用命令列表</p><div class="table-container"><table><thead><tr><th>类别</th><th>命令</th><th>英文全称</th><th>含义</th><th>语法</th><th>示例</th></tr></thead><tbody><tr><td>文件目录操作</td><td>cd</td><td>change directory</td><td>切换用户当前工作目录</td><td>`cd [-L</td><td>[-P [-e]]] [dir]`</td><td><code>cd /</code></td></tr><tr><td>文件目录操作</td><td>cp</td><td>copy</td><td>将源文件或目录复制到目标文件或目录中</td><td><code>cp(选项)(参数)</code></td><td><code>cp file /usr/men/tmp/file1</code></td></tr><tr><td>文件目录操作</td><td>rm</td><td>remove</td><td>用于删除给定的文件和目录</td><td><code>rm(选项)(参数)</code></td><td><code>rm -rf testdir</code></td></tr><tr><td>文件目录操作</td><td>mv</td><td>move</td><td>对文件或目录移动或重命名</td><td><code>mv(选项)(参数)</code></td><td><code>mv file_1.txt /home/office/</code></td></tr><tr><td>文件目录操作</td><td>rmdir</td><td>remove directory</td><td>用来删除空目录</td><td><code>rmdir(选项)(参数)</code></td><td><code>rmdir -p www/Test</code></td></tr><tr><td>文件目录操作</td><td>touch</td><td>touch</td><td>更新文件的时间标签和创建新的空文件</td><td><code>touch(选项)(参数)</code></td><td><code>touch ex2</code></td></tr><tr><td>文件目录操作</td><td>mkdir</td><td>make directory</td><td>用来创建目录</td><td><code>mkdir(选项)(参数)</code></td><td><code>mkdir -m 700 /usr/meng/test</code></td></tr><tr><td>文件目录操作</td><td>clear</td><td>clear</td><td>清除当前屏幕终端上的任何信息</td><td><code>clear</code></td><td>输入clear后当前终端上的任何信息就会被清除</td></tr><tr><td>文件目录操作</td><td>li</td><td>list</td><td>显示目录内容列表</td><td><code>ls [选项] [文件名...]</code></td><td><code>ls -l</code></td></tr><tr><td>文件目录操作</td><td>tree</td><td>tree</td><td>树状图列出目录的内容</td><td><code>tree(选项)(参数)</code></td><td><code>tree /private/ -L 1</code></td></tr><tr><td>文件目录操作</td><td>cat</td><td>cat</td><td>显示文件内容和连接多个文件并打印到标准输出</td><td><code>cat [OPTION]... [FILE]...</code></td><td><code>cat -s test.log</code></td></tr><tr><td>文件目录操作</td><td>pwd</td><td>print work directory</td><td>显示当前工作目录的绝对路径</td><td><code>pwd [-LP]</code></td><td><code>pwd</code></td></tr><tr><td>文件目录操作</td><td>man</td><td>man</td><td>查看Linux中的指令帮助</td><td><code>man(选项)(参数)</code></td><td><code>man 3 sleep</code></td></tr></tbody></table></div><p>🔥Linux命令有很多,如何快速了解相关命令,可通过查询<a href="https://wangchujiang.com/linux-command/hot.html">命令列表</a>详细快速了解</p><p>🔥Linux命令有很多,如何快速找到具有某项功能的命令,可通过查询<a href="https://www.linuxcool.com">Linux命令大全</a>详细快速了解</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722151611969.png" alt="image-20230722151611969"></p><h3 id="Linux-使用技巧"><a href="#Linux-使用技巧" class="headerlink" title="Linux 使用技巧"></a>Linux 使用技巧</h3><ul><li><p>按下<kbd>Tab</kbd>键可自动补全</p></li><li><p>连按两次<kbd>Tab</kbd>键可给出操作提示</p></li><li>使用上下箭头快速调出曾经使用过的命令</li><li>使用clear命令或者<code>ctrl+l</code>快捷键实现清屏</li><li>使用<code>ctrl+a</code>实现定位到命令行行首,使用<code>ctrl+e</code>实现定位到命令行行尾</li></ul><h3 id="Linux-命令格式"><a href="#Linux-命令格式" class="headerlink" title="Linux 命令格式"></a>Linux 命令格式</h3><p><code>command [-options][parameter]</code></p><p><i>说明:</i></p><ul><li>command:命令名</li><li>[-options]:选项,可用来对命令进行控制,可省略</li><li>[parameter]:参数,可以是零个或多个</li></ul><p><strong>注意:</strong> 命令名、选项和参数用空格分隔,<code>[]</code>代表可选</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722153728560.png" alt="image-20230722153728560"></p><h3 id="文件目录操作"><a href="#文件目录操作" class="headerlink" title="文件目录操作"></a>文件目录操作</h3><p><code>ls</code>显示指定目录下的内容,语法:<code>ls [-al][dir]</code></p><p><strong>说明:</strong> -a 显示所有文件及目录(.开头的隐藏文件也会列出),-l 除文件名称外,同时将文件型态(d开头表示目录、-开头表示文件)、权限、拥有者、文件大小等信息详细列出</p><p><strong>注意:</strong> 由于使用ls命令时经常需要加入<code>-l</code>选项,所以Linux为<code>ls -l</code>命令提供了一种简写方式,即<code>ll</code></p><p><code>cd</code>用于切换当前工作目录,即进入指定目录,语法:<code>cd [dirName]</code></p><p><strong>说明:</strong> <code>~</code>表示用户的home目录,<code>.</code>表示当前所在目录,<code>..</code>表示当前目录的上一级目录</p><p><code>cat</code>用于显示文件内容,语法:<code>cat [-n] fileName</code></p><p><strong>说明:</strong> -n:由1开始对所有输出的行数编号</p><p><code>more</code>以分页的形式显示文件内容,语法:<code>more fileName</code></p><p><strong>操作说明:</strong> 回车键即向下滚动一行,空格键即向下滚动一屏,b即返回上一屏,q或者ctrl+c即退出more</p><p><code>tail</code>查看文件末尾的内容,语法:<code>tail [-f] fileName</code></p><p><strong>说明:</strong> -f动态读取文件末尾内容并显示,通常用于日志文件的内容输出</p><p><code>mkdir</code>用于创建目录,语法:<code>mkdir [-p] fileName</code></p><p><strong>说明:</strong> -p是确保目录名称存在,不存在就创建一个。通过此选项,可以实现多层目录同时创建</p><p><code>rmdir</code>删除空目录,语法:<code>rmdir [-p] dirName</code></p><p><strong>说明:</strong> -p是确保当子目录被删除后使父目录为空目录的话则一并删除</p><p><code>rm</code>删除文件或目录,语法:<code>rm [-rf] name</code></p><p><strong>说明:</strong> -r:将目录及目录中所有文件(目录)逐一删除,即递归删除,-f:无需确认,直接删除</p><h3 id="拷贝移动命令"><a href="#拷贝移动命令" class="headerlink" title="拷贝移动命令"></a>拷贝移动命令</h3><p><code>cp</code>用于复制文件或目录,语法:<code>cp [-r] source dest</code></p><p><strong>说明:</strong> -r:如果复制的是目录需要使用此选项,此时将复制该目录下所有的子目录和文件</p><p><strong>案例:</strong> <code>cp -r itcast/ itheima/</code>是将itcast目录和目录下所有目录及文件复制到itheima目录下,<code>cp -r itcast/* itheima/</code>是将itcast目录下所有文件复制到itheima目录下</p><p><code>mv</code>为文件或目录重命名,也可以将文件或目录进行移动,语法:<code>mv source dest</code></p><p><strong>案例:</strong> <code>mv hello.txt hi.txt</code>是将hello.txt改名为hi.txt,<code>mv hi.txt itheima/</code>是将文件hi.txt移动到itheima目录中,<code>mv hi.txt itheima/hello.txt</code>是将hi.txt移动到itheima目录中并改名为itheima,<code>mv itcast/ itheima/</code>是当itheima目录不存在,将itcast目录改名为itheima,<code>mv itcast/ itheima/</code>是当itheima目录存在,将itcast目录移动到itheima目录中</p><h3 id="打包压缩命令"><a href="#打包压缩命令" class="headerlink" title="打包压缩命令"></a>打包压缩命令</h3><p><code>tar</code>对文件进行打包、解包、压缩或解压缩,语法:<code>tar [-zcxvf] fileName [files]</code></p><p><strong>说明:</strong> 包文件名缀为.tar表示只是完成了打包,并没有压缩;包文件后缀为.tar.gz表示完成了打包还进行了压缩</p><p><strong>参数选项:</strong> </p><ul><li>-z:z代表的是gzip,通过gzip命令处理文件,gzip可以对文件压缩或解压</li><li>-c:c代表的是create,即创建新的包文件</li><li>-x:x代表的是extract,实现从包文件中还原文件</li><li>-v:v代表的是verbose,显示命令的执行过程</li><li>-f:f代表的是file,用于指定包文件的名称</li></ul><p><strong>案例:</strong><code>tar -zxvf hello.tar.gz</code>将hello.tar.gz文件进行解压并将解压后的文件放在当前目录, <code>tar -zxvf hello.tar.gz -C /usr/local</code>将hello.tar.gz文件进行解压并将解压后的文件放在/usr/local目录下</p><h3 id="文本编辑命令"><a href="#文本编辑命令" class="headerlink" title="文本编辑命令"></a>文本编辑命令</h3><p><code>vi</code>是Linux命令提供的一个文本编辑工具,可以对文件内容进行编辑,类似于Windows记事本,语法:<code>vi fileName</code></p><p><code>vim</code>是从vi发展来的一个功能更加强大的文本编辑工具,在编辑文件时可以对文件内容进行着色,方便对文件进行编辑处理,所以实际中vim更加常用,而要使用vim命令则需要安装:<code>yum install vim</code></p><p><code>vim</code>对文件内容进行编辑,语法:<code>vim fileName</code></p><p><strong>说明:</strong> 在使用vim命令编辑文件时,如果指定的文件存在则直接打开此文件,如果指定的文件不存在则新建文件;vim进行文本编辑时共分为三种模式,分别是命令行模式、插入模式和底行模式,这三种模式可以互相切换,在使用vim时一定要注意当前所处模式</p><p><strong>命令模式</strong></p><ul><li>通过vim命令打开文件后默认进入命令模式,另外两种模式需要首先进入命令模式才能进入彼此</li><li>命令模式下可以查看文件内容、移动光标(上下左右箭头、gg、G)</li></ul><p><strong>插入模式</strong></p><ul><li>插入模式下可以对文件内容进行编辑</li><li>在命令模式下按下[i,a,o]任意一个,可以进入插入模式,进入插入模式后,下方会出现【insert】字样</li><li>在插入模式下按下<kbd>ESC</kbd>键即可回到命令模式</li></ul><p><strong>底行模式</strong></p><ul><li>底行模式下可以通过命令对文件内容进行查找、显示行号、退出等操作</li><li>在命令模式下按下[:,/]任意一个就可进入底行模式</li><li>通过/方式进入底行模式后,可以对文件内容进行查找</li><li>通过:方式进入底行模式后,可以输入wq(保存并退出)、q!(不保存退出)、set nu(显示行号)</li></ul><h3 id="查找命令"><a href="#查找命令" class="headerlink" title="查找命令"></a>查找命令</h3><p><code>find</code>在指定目录下查找文件,语法:<code>find dirName -option fileName</code></p><p><strong>案例:</strong> <code>find . -name "*.java"</code>即在当前目录及其子目录下查找.java结尾文件,<code>find /itcast -name "*.java"</code>在/itcast目录及其子目录下查找.java结尾的文件</p><p><code>grep</code>从指定文件中查找指定的文件内容,语法:<code>gerp content fileName</code></p><p><strong>案例:</strong> <code>grep Hello HelloWorld.java</code>即查找HelloWorld.java文件中出现的Hello字符串的位置,<code>grep hello *.java</code>即查找当前目录中所有.java结尾的文件中包含hello字符串的位置</p><h2 id="六、软件安装"><a href="#六、软件安装" class="headerlink" title="六、软件安装"></a>六、软件安装</h2><ul><li>软件安装方式</li><li>安装jdk</li><li>安装Tomcat</li><li>安装MySQL</li><li>安装lrzsz</li></ul><h3 id="软件安装方式"><a href="#软件安装方式" class="headerlink" title="软件安装方式"></a>软件安装方式</h3><p><strong>二进制发布包安装:</strong> 软件已经针对具体平台编译打包发布,只要解压,修改配置即可</p><p><strong>rpm安装:</strong> 软件已经按照redhat的包管理规范进行打包,使用rpm命令进行安装,不能自行解决库依赖问题</p><p><strong>yum安装:</strong> 一种在线软件安装方式,本质上还是rpm安装,会自动下载安装包并安装,安装过程中自动解决库依赖问题</p><p><strong>源码编译安装:</strong> 软件以源码工程的形式发布,需要自己编译打包</p><h3 id="安装jdk"><a href="#安装jdk" class="headerlink" title="安装jdk"></a>安装jdk</h3><p><strong>第一步</strong> 从<a href="https://repo.huaweicloud.com/java/jdk">HUAWEI镜像源</a>下载Linux版JDK安装包</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722200024421.png" alt="image-20230722200024421"></p><p><strong>第二步</strong> 使用FinalShell自带的上传功能将jdk的二进制发布包上传到Linux</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722200944039.png" alt=""></p><p><strong>第三步</strong> 解压安装包,命令为<code>tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722201903688.png" alt="image-20230722201903688"></p><p><strong>第四步</strong> 配置环境变量,使用vim命令修改/etc/profile文件,在文件末尾添加如下配置</p><figure class="highlight plaintext"><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">JAVA_HOME=/usr/local/jdk1.8.0_171</span><br><span class="line">PATH=$JAVA_HOME/bin:$PATH</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722202606590.png" alt="image-20230722202606590"></p><p><strong>第五步</strong> 重新加载profile文件,使更改的配置立即生效,命令:<code>source /etc/profile</code>;检查安装是否成功,命令:<code>java -version</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722202750491.png" alt="image-20230722202750491"></p><h3 id="安装Tomcat"><a href="#安装Tomcat" class="headerlink" title="安装Tomcat"></a>安装Tomcat</h3><p><strong>第一步</strong> 从<a href="https://tomcat.apache.org/download-80.cgi">官方网站</a>下载Tomcat二进制发布包</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722203613052.png" alt="image-20230722203613052"></p><p><strong>第二步</strong> 使用FinalShell自带的上传工具将Tomcat的二进制发布包上传到Linux</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722204945129.png" alt="image-20230722204945129"></p><p><strong>第三步</strong> 解压安装包,命令为<code>tar -zxvf apache-tomcat-8.5.91.tar.gz -C /usr/local</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722205136431.png" alt="image-20230722205136431"></p><p><strong>第四步</strong> 进入Tomcat的bin目录启动服务,命令为<code>sh startup.sh</code>或<code>./startup.sh</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722205422438.png" alt="image-20230722205422438"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722205602577.png" alt="image-20230722205602577"></p><h4 id="验证Tomcat启动"><a href="#验证Tomcat启动" class="headerlink" title="验证Tomcat启动"></a>验证Tomcat启动</h4><p><strong>查看启动日志</strong></p><p><code>more /usr/local/apache-tomcat-8.5.91/logs/catalina.out</code></p><p><code>tail -50 /usr/local/apache-tomcat-8.5.91/logs/catalina.out</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722210219464.png" alt="image-20230722210219464"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722210300100.png" alt="image-20230722210300100"></p><p><strong>查看进程</strong></p><p><code>ps -ef|grep tomcat</code></p><ul><li>ps命令是Linux下非常强大的进程查看命令,通过ps -ef可以查看当前运行的所有进程的详细信息</li><li>”|“在Linux中称为管道符,可以将前一个命令的结果输出给后一个命令作为输入</li><li>使用ps命令查看进程时,经常配合管道符和查找命令grep一起使用,来查看特定进程</li></ul><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722210847479.png" alt="image-20230722210847479"></p><p>在本地机上测试连接Linux上的tomcat服务器,会发现连接失败</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722211120084.png" alt="image-20230722211120084"></p><h4 id="防火墙操作"><a href="#防火墙操作" class="headerlink" title="防火墙操作"></a>防火墙操作</h4><ul><li>查看防火墙状态:<code>systemctl status firewalld</code>或<code>firewall-cmd --state</code></li><li>暂时关闭防火墙:<code>systemctl stop firewalld</code></li><li>永久关闭防火墙:<code>systemctl disable firewalld</code></li><li>开启防火墙:<code>systemctl start firewalld</code></li><li>开发指定端口:<code>firewall-cmd --zone=public --add-port=8080/tcp --permanent</code></li><li>关闭指定端口:<code>firewall-cmd --zone=public --remove-port=8080/tcp --permanent</code></li><li>立即生效:<code>firewall-cmd --reload</code></li><li>查看开放的端口:<code>firewall-cmd --zone=public --list-ports</code></li></ul><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722211840689.png" alt="image-20230722211840689"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722211851853.png" alt="image-20230722211851853"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722212313150.png" alt="image-20230722212313150"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722212325381.png" alt="image-20230722212325381"></p><p><strong>注意:</strong> systemctl是管理Linux中服务的命令,可对服务进行启动、停止、重启、查看状态等操作;firewall-cmd是Linux中专门用于控制防火墙的命令;为了保证系统安全,服务器的防火墙不建议关闭</p><h4 id="关闭Tomcat服务"><a href="#关闭Tomcat服务" class="headerlink" title="关闭Tomcat服务"></a>关闭Tomcat服务</h4><p><strong>第一种</strong></p><p>运行Tomcat下的bin目录中提供停止服务的脚本文件shutdown.sh,命令:<code>sh shutdown.sh</code>或<code>./shutdown.sh</code></p><p><strong>第二种</strong></p><p>查看Tomcat进程,获得进程id,然后执行结束进程的命令:<code>kill -9 进程号</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722213508170.png" alt="image-20230722213508170"></p><h3 id="安装MySQL"><a href="#安装MySQL" class="headerlink" title="安装MySQL"></a>安装MySQL</h3><p>先检测当前系统中是否已安装MySQL数据库,如果已安装,那么在安装则会失败,同时CentOS7自带的mariadb会与MySQL数据库冲突</p><p><code>rpm -qa</code>:查询当前系统中安装的所有软件</p><p><code>rpm -qa|grep mysql</code>:查询当前系统中安装的软件名称中是否有mysql</p><p><code>rpm -qa|grep mariadb</code>:查询当前系统中安装的软件名称中是否有mariadb</p><p>RPM(Red-Hat Package Manager)是软件包管理器,是红帽Linux用于管理和安装软件的工具</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722214231594.png" alt="image-20230722214231594"></p><p>卸载已安装的冲突软件</p><p><code>rpm -e --nodeps 软件名称</code>:卸载软件</p><p><code>rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722214708954.png" alt="image-20230722214708954"></p><p>到这里准备工作完成了,接下来就是正式安装</p><p><strong>第一步</strong> 到<a href="https://downloads.mysql.com/archives/community">官网</a>下载Linux版MySQL,这里选择MySQL的版本和本地机相同</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722215310980.png" alt="image-20230722215310980"></p><p><strong>第二步</strong> 使用FinalShell自带的上传工具将MySQL压缩包上传到Linux</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722215829742.png" alt="image-20230722215829742"></p><p><strong>第三步</strong> 移动mysql压缩包到创建的目录下</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722220105983.png" alt="image-20230722220105983"></p><p><strong>第四步</strong> 解压mysql压缩包</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722220429858.png" alt="image-20230722220429858"></p><p><strong>第五步</strong> 安装rpm软件包</p><p><code>rpm -ivh *.rpm --nodeps --force</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722221603126.png" alt="image-20230722221603126"></p><p><strong>第六步</strong> 启动mysql</p><p><code>systemctl status mysqld</code>:查看mysql服务状态</p><p><code>systemctl start mysqld</code>:启动mysql服务</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722222026605.png" alt="image-20230722222026605"></p><p><strong>说明:</strong> 可以设置开机时启动mysql服务,避免每次开机启动mysql</p><p><code>systemctl enable mysqld</code>:开机启动mysql服务</p><p><code>netstat -tunlp</code>:查看已启动的服务</p><p><code>netstat -tunlp|grep mysql</code></p><p><code>ps -ef|grep mysql</code>:查看mysql进程</p><p><strong>注意:</strong> 需要安装net-tools</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722222408872.png" alt="image-20230722222408872"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722222614009.png" alt="image-20230722222614009"></p><h4 id="登录MySQL数据库"><a href="#登录MySQL数据库" class="headerlink" title="登录MySQL数据库"></a>登录MySQL数据库</h4><p>查阅临时密码</p><p><code>cat /var/log/mysqld.log</code>:查看文件内容</p><p><code>cat /var/log/mysqld.log|grep password</code>:查看文件内容中包含password的行信息</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722223333466.png" alt="image-20230722223333466"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722223624852.png" alt="image-20230722223624852"></p><p><strong>修改临时密码</strong></p><p>先修改密码在设置校验规则与密码长度</p><p><code>alter user 'root'@'localhost' identified with mysql_native_password by 'Root_12root';</code>:修改的密码中必须有数字、大小写字母和特殊字符且长度在8以上</p><p><code>set global validate_password.policy=0;</code>:设置密码安全等级低,便于密码修改</p><p><code>set global validate_password.length=4;</code>:设置密码长度最低位数</p><p><code>ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';</code>:修改密码为简单密码</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722225828959.png" alt="image-20230722225828959"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722225859441.png" alt="image-20230722225859441"></p><p><strong>开启访问权限</strong></p><p>先创建用户,在对该用户分配用户权限,然后在刷新权限</p><p><code>CREATE USER 'root'@'%' IDENTIFIED BY '123456';</code></p><p><code>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;</code></p><p><code>FLUSH PRIVILEGES;</code></p><p><strong>注意:</strong> <code>CREATE USER 'root'@'%' IDENTIFIED BY '123456';</code>中<code>'123456'</code>为远程连接的访问密码</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723101038751.png" alt="image-20230723101038751"></p><p>开放3306端口:<code>firewall-cmd --zone=public --add-port=3306/tcp --permanent</code>、<code>firewall-cmd --reload</code>、<code>firewall-cmd --zone=public --list-ports</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230722231453261.png" alt="image-20230722231453261"></p><p>使用navicat测试远程连接Linux上的mysql,也可以使用DataGrip测试远程连接Linux上的mysql</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723101742200.png" alt="image-20230723101742200"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723101801312.png" alt="image-20230723101801312"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723110728145.png" alt="image-20230723110728145"></p><h3 id="安装lrzsz"><a href="#安装lrzsz" class="headerlink" title="安装lrzsz"></a>安装lrzsz</h3><p>使用Yum安装</p><blockquote><p>Yum(全称为Yellow dog Updater,Modified)是一个在Fedora和RedHat以及CentOS中的Shell前端软件包管理器。基于RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动出来依赖关系,并且一次安装所有依赖的软件包,无序繁琐地一次次下载、安装</p></blockquote><p>搜索lrzsz安装包,命令:<code>yum list lrzsz</code>,然后使用yum命令在线安装,命令:<code>yum install lrzsz.x86_64</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723102656892.png" alt="image-20230723102656892"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230723102822687.png" alt="image-20230723102822687"></p>]]></content>
<summary type="html"><h2 id="视频地址"><a href="#视频地址" class="headerlink" title="视频地址"></a>视频地址</h2><p>B站黑马:<a href="https://b23.tv/av213091208/p118">缘起</a> —— <a hr</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>MySQL进阶篇</title>
<link href="http://example.com/posts/6638.html"/>
<id>http://example.com/posts/6638.html</id>
<published>2023-10-13T11:20:00.000Z</published>
<updated>2023-10-13T11:21:29.587Z</updated>
<content type="html"><![CDATA[<h2 id="整体脉络"><a href="#整体脉络" class="headerlink" title="整体脉络"></a>整体脉络</h2><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230802202743259.png" alt="image-20230802202743259"></p><h2 id="存储引擎"><a href="#存储引擎" class="headerlink" title="存储引擎"></a>存储引擎</h2><h3 id="MySQL体系结构"><a href="#MySQL体系结构" class="headerlink" title="MySQL体系结构"></a>MySQL体系结构</h3><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720213328191.png" alt="image-20230720213328191"></p><p><strong>连接层</strong><br>主要完成一些类似于连接处理、授权认证及相关的安全方案</p><p><strong>服务层</strong><br>主要完成大多数核心服务功能,如SQL接口、缓存查询、SQL分析与优化、部分内置函数执行。所有跨存储引擎的功能也在这一层实现,如过程、函数等</p><p><strong>引擎层</strong><br>真正负责MySQL中数据的存储和提取,服务器通过API和存储引擎进行通信。不同存储引擎具有不同功能,这样就可以根据需要,选取适合的存储引擎</p><p><strong>存储层</strong><br>将数据库的数据和日志存储在文件系统之上,并完成与存储引擎的交互,是文件的物理存储层</p><h3 id="存储引擎简介"><a href="#存储引擎简介" class="headerlink" title="存储引擎简介"></a>存储引擎简介</h3><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><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="keyword">CREATE</span> <span class="keyword">TABLE</span> 表名(</span><br><span class="line">字段<span class="number">1</span> 字段<span class="number">1</span>类型 [COMMENT 字段<span class="number">1</span>注释],</span><br><span class="line"> ...</span><br><span class="line"> 字段n 字段n类型 [COMMENT 字段n注释]</span><br><span class="line">) ENGINE <span class="operator">=</span> INNODB [COMMENT 表注释];</span><br></pre></td></tr></table></figure><p><strong>查看当前数据库支持的存储引擎</strong><br><code>SHOW ENGINES;</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720215013148.png" alt="image-20230720215013148"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720215709339.png" alt="image-20230720215709339"></p><h3 id="存储引擎特点"><a href="#存储引擎特点" class="headerlink" title="存储引擎特点"></a>存储引擎特点</h3><h4 id="InnoDB"><a href="#InnoDB" class="headerlink" title="InnoDB"></a>InnoDB</h4><p>InnoDB是一种兼顾高可靠性和高性能的通用存储引擎,在MySQL 5.5之后,InnoDB是默认的MySQL存储引擎</p><p><strong>特点</strong></p><ul><li>DML操作遵循ACID模型,支持<font color=#9932CD>事务</font></li><li><font color=#9932CD>行级锁</font>,提高并发访问性能</li><li>支持<font color=#9932CD>外键</font> FOREIGN KEY约束,保证数据的完整性和正确性</li></ul><p><strong>文件</strong><br>xxx.ibd:xxx代表的是表名,InnoDB引擎的每张表都会对应这样一个表空间文件,存储该表的表结构(frm、sdi)、数据和索引</p><p>参数:innodb_file_per_table</p><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720225610794.png" alt="image-20230720225610794"></p><h4 id="MyISAM"><a href="#MyISAM" class="headerlink" title="MyISAM"></a>MyISAM</h4><p>MyISAM是MySQL早期的默认存储引擎</p><p><strong>特点</strong></p><ul><li>不支持事务,不支持外键</li><li>支持表锁,不支持行锁</li><li>访问速度快</li></ul><p><strong>文件</strong><br>xxx.sdi:存储表结构信息</p><p>xxx.MYD:存储数据</p><p>xxx.MYI:存储索引</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720230022387.png" alt="image-20230720230022387"></p><h4 id="Memory"><a href="#Memory" class="headerlink" title="Memory"></a>Memory</h4><p>Memory引擎的表数据是存储在内存中的,由于受到硬件问题或断电问题的影响,只能将这些表作为临时表或缓存使用</p><p><strong>特点</strong></p><ul><li>内存存放,访问速度快</li><li>hash索引(默认)</li></ul><p><strong>文件</strong><br>xxx.sdi:存储表结构信息</p><div class="table-container"><table><thead><tr><th style="text-align:center">特点</th><th style="text-align:center">InnoDB</th><th style="text-align:center">MyIASM</th><th style="text-align:center">Memory</th></tr></thead><tbody><tr><td style="text-align:center">存储限制</td><td style="text-align:center">64TB</td><td style="text-align:center">有</td><td style="text-align:center">有</td></tr><tr><td style="text-align:center">事务安全</td><td style="text-align:center"><font color=#9932CD>支持</font></td><td style="text-align:center">-</td><td style="text-align:center">-</td></tr><tr><td style="text-align:center">锁机制</td><td style="text-align:center"><font color=#9932CD>行锁</font></td><td style="text-align:center">表锁</td><td style="text-align:center">表锁</td></tr><tr><td style="text-align:center">B+tree索引</td><td style="text-align:center">支持</td><td style="text-align:center">支持</td><td style="text-align:center">支持</td></tr><tr><td style="text-align:center">Hash索引</td><td style="text-align:center">-</td><td style="text-align:center">-</td><td style="text-align:center">支持</td></tr><tr><td style="text-align:center">全文索引</td><td style="text-align:center">支持(5.6版本之后)</td><td style="text-align:center">支持</td><td style="text-align:center">-</td></tr><tr><td style="text-align:center">空间使用</td><td style="text-align:center">高</td><td style="text-align:center">低</td><td style="text-align:center">N/A</td></tr><tr><td style="text-align:center">内存使用</td><td style="text-align:center">高</td><td style="text-align:center">低</td><td style="text-align:center">中等</td></tr><tr><td style="text-align:center">批量插入速度</td><td style="text-align:center">低</td><td style="text-align:center">高</td><td style="text-align:center">高</td></tr><tr><td style="text-align:center">支持外键</td><td style="text-align:center"><font color=#9932CD>支持</font></td><td style="text-align:center">-</td><td style="text-align:center">-</td></tr></tbody></table></div><h3 id="存储引擎选择"><a href="#存储引擎选择" class="headerlink" title="存储引擎选择"></a>存储引擎选择</h3><p>在选择存储引擎时,应该根据应用系统的特点选择合适的存储引擎。对于复杂的应用系统,还可以根据实际情况选择多种存储引擎进行组合</p><p><strong>InnoDB:</strong> 是MySQL的默认存储引擎,支持事务、外键。如果应用对事务的完整性有比较高的要求,在并发条件下要求数据的一致性,数据操作除了插入和查询之外,还包含很多的更新、删除操作,那么InnoDB存储引擎是比较合适的选择</p><p><strong>MyISAM:</strong> 如果应用是以读操作和插入操作为主,只有很少的更新和删除操作,并且对事务的完整性、并发性要求不是很高,那么选择这个存储引擎是非常合适的</p><p><strong>MEMORY:</strong> 将所有数据保存在内存中,访问速度快,通常用于临时表及缓存。MEMORY的缺陷就是对表的大小有限制,太大的表无法缓存在内存中,而且无法保障数据的安全性</p>]]></content>
<summary type="html"><h2 id="整体脉络"><a href="#整体脉络" class="headerlink" title="整体脉络"></a>整体脉络</h2><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.co</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>事务</title>
<link href="http://example.com/posts/6637.html"/>
<id>http://example.com/posts/6637.html</id>
<published>2023-10-13T11:10:00.000Z</published>
<updated>2023-10-13T11:18:18.962Z</updated>
<content type="html"><![CDATA[<h2 id="事务简介"><a href="#事务简介" class="headerlink" title="事务简介"></a>事务简介</h2><p>事务用于管理对数据库的一组操作,这组操作要么全部成功执行,要么全部回滚(即取消)。事务的目的是保证数据库的一致性和完整性</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720170911464.png" alt="image-20230720170911464"></p><p>在MySQL中,只有InnoDB数据库引擎才支持事务</p><h2 id="事务操作"><a href="#事务操作" class="headerlink" title="事务操作"></a>事务操作</h2><p><strong>查看/设置事务提交方式</strong><br><code>SELECT @@autocommit;</code></p><p><code>SET @@autocommit;</code></p><p><strong>提交事务</strong><br><code>COMMIT;</code></p><p><strong>回滚事务</strong><br><code>ROLLBACK;</code></p><p><strong>开启事务</strong><br><code>START TRANSACTION 或 BEGIN;</code></p><p><strong>提交事务</strong><br><code>COMMIT;</code></p><p><strong>回滚事务</strong><br><code>ROLLBACK;</code></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><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></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> account(</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">10</span>) comment <span class="string">'姓名'</span>,</span><br><span class="line"> money <span class="type">int</span> comment <span class="string">'余额'</span></span><br><span class="line">) <span class="type">character</span> <span class="keyword">set</span> utf8 comment <span class="string">'账户表'</span>;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> account(name, money) <span class="keyword">values</span> (<span class="string">'学校'</span>, <span class="number">2000</span>),(<span class="string">'慕容婉'</span>, <span class="number">2000</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> <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><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><br><span class="line">有内鬼,终止交易...</span><br><span class="line"></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 class="comment">-- 方式一</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@autocommit</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">set</span> @<span class="variable">@autocommit</span> <span class="operator">=</span> <span class="number">0</span>; <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">commit</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 回滚事务</span></span><br><span class="line"><span class="keyword">rollback</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">set</span> @<span class="variable">@autocommit</span> <span class="operator">=</span> <span class="number">1</span>; <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">start</span> transaction;</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> <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><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><br><span class="line">有内鬼,终止交易...</span><br><span class="line"></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 class="comment">-- 提交事务</span></span><br><span class="line"><span class="keyword">commit</span> ;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 回滚事务</span></span><br><span class="line"><span class="keyword">rollback</span> ;</span><br></pre></td></tr></table></figure><h2 id="事务四大特性"><a href="#事务四大特性" class="headerlink" title="事务四大特性"></a>事务四大特性</h2><p>一般来说,事务是必须满足4个条件(ACID):原子性(<strong>A</strong>tomicity,或称不可分割性)、一致性(<strong>C</strong>onsistency)、隔离性(<strong>I</strong>solation,又称独立性)、持久性(<strong>D</strong>urability)</p><ul><li>原子性:事务是不可分割的最小操作单元,要么全部成功,要么全部失败</li><li>一致性:事务完成时,必须使所有的数据都保持一致状态</li><li>隔离性:数据库系统提供的隔离机制,保证事务在不受外部并发操作影响的独立环境下运行</li><li>持久性:事务一旦提交或回滚,它对数据库中数据的改变是永久的</li></ul><h2 id="并发事务问题"><a href="#并发事务问题" class="headerlink" title="并发事务问题"></a>并发事务问题</h2><p>多个事务在执行过程中所出现的问题</p><div class="table-container"><table><thead><tr><th>问题</th><th>描述</th><th>举例</th></tr></thead><tbody><tr><td>脏读</td><td>一个事务读到另一个事务还没有提交的数据</td><td>事务A修改了数据但还未提交,事务B读取到了事务A修改的数据,这时候事务B读取到的数据就是脏的,称为脏读</td></tr><tr><td>不可重复读</td><td>一个事务先后读取同一条记录,但两次读取的数据不同</td><td>事务A读取了一条数据之后,事务B修改了这条数据并提交了事务,然后事务A再次读取这条数据,就会发现两次结果不一致,这就是不可重复读</td></tr><tr><td>幻读</td><td>在同一个事务中,由于其他事务的插入、更新或删除操作,导致同一个查询语句返回不同的结果集</td><td>事务A使用了一定条件查询,然后事务B增加了符合条件的记录,当事务A再次查询时,发现两次查询结果集不一样,好像产生了幻觉</td></tr></tbody></table></div><h2 id="事务隔离级别"><a href="#事务隔离级别" class="headerlink" title="事务隔离级别"></a>事务隔离级别</h2><p>用来解决并发事务问题的</p><div class="table-container"><table><thead><tr><th>隔离级别</th><th>脏读</th><th>不可重复读</th><th>幻读</th></tr></thead><tbody><tr><td>Read uncommitted</td><td>会</td><td>会</td><td>会</td></tr><tr><td>Read committed</td><td>不会</td><td>会</td><td>会</td></tr><tr><td>Repeatable Read(默认)</td><td>不会</td><td>不会</td><td>会</td></tr><tr><td>Serializable</td><td>不会</td><td>不会</td><td>不会</td></tr></tbody></table></div><p><strong>查看事务隔离级别</strong></p><p><code>SELECT @@TRANSACTION_ISOLATION;</code></p><p><strong>设置事务隔离级别</strong></p><p><code>SET [SESSION][GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITED | READ COMMITED | REPEATABLE READ | SERIALIZABLE}</code></p><p><strong>注意:</strong> 事务隔离级别越高,数据越安全,但性能越低</p>]]></content>
<summary type="html"><h2 id="事务简介"><a href="#事务简介" class="headerlink" title="事务简介"></a>事务简介</h2><p>事务用于管理对数据库的一组操作,这组操作要么全部成功执行,要么全部回滚(即取消)。事务的目的是保证数据库的一致性和完整性</</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>多表查询</title>
<link href="http://example.com/posts/6636.html"/>
<id>http://example.com/posts/6636.html</id>
<published>2023-10-13T11:07:00.000Z</published>
<updated>2023-10-13T11:08:10.379Z</updated>
<content type="html"><![CDATA[<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>在MySQL中,多表查询是指在一个查询语句中涉及多个表的操作。这种查询可以通过连接(join)操作将多个表关联起来,从而实现在多个表之间进行数据的检索和处理</p><h2 id="多表关系"><a href="#多表关系" class="headerlink" title="多表关系"></a>多表关系</h2><p>项目开发中,在进行数据库表结构设计时,会根据业务需求与业务模块之间的关系,分析并设计表结构,由于业务之间相互关联,所以各个表结构之间也存在各种联系,基本分为三类:</p><ul><li>一对多(多对一)</li><li>多对多</li><li>一对一</li></ul><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719172334300.png" alt="image-20230719172334300"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719214108948.png" alt="image-20230719214108948"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719191339826.png" alt="image-20230719191339826"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719193800824.png" alt="image-20230719193800824"></p><h2 id="多表查询概述"><a href="#多表查询概述" class="headerlink" title="多表查询概述"></a>多表查询概述</h2><p>多表查询即从多张表中查询数据</p><p><strong>笛卡尔积:</strong> 笛卡尔积是指在数学中,两个集合所有组合情况(<font color=#008c8c>在多表查询时,需要消除无效笛卡尔积</font>)</p><p>新建emp表</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="keyword">create</span> <span class="keyword">table</span> emp(</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">20</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> unsigned 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">) <span class="type">character</span> <span class="keyword">set</span> utf8 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> emp(name, age, job, salary, entrydate, dept_id) <span class="keyword">values</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="number">5</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> emp(name, age, job, salary, entrydate, managerid, dept_id) <span class="keyword">values</span></span><br><span class="line"> (<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="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-05'</span>, <span class="number">2</span>, <span class="number">1</span>),</span><br><span class="line"> (<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="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">11000</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="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="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">1</span>),</span><br><span class="line"> (<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="string">'丁敏君'</span>, <span class="number">23</span>, <span class="string">'出纳'</span>, <span class="number">5250</span>, <span class="string">'2009-05-12'</span>, <span class="number">7</span>, <span class="number">3</span>),</span><br><span class="line"> (<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="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="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="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="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="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="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="keyword">insert</span> <span class="keyword">into</span> emp(name, age, salary, entrydate, managerid) <span class="keyword">values</span> (<span class="string">'陈友谅'</span>, <span class="number">42</span>, <span class="number">2000</span>, <span class="string">'2011-10,12'</span>, <span class="number">1</span>); </span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719221412000.png" alt="image-20230719221412000"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719222545708.png" alt="image-20230719222545708"></p><h3 id="多表查询分类"><a href="#多表查询分类" class="headerlink" title="多表查询分类"></a>多表查询分类</h3><p><strong>连接查询</strong></p><p>交叉连接:返回两张表的笛卡尔积 <code>cross join</code></p><p>内连接:相当于查询两张表交集部分数据</p><p>外连接:</p><ul><li>左外连接:查询左表所有数据包含交集部分数据</li><li>右外连接:查询右表所有数据包含交集部分数据</li></ul><p>自连接:当前表与自身的连接查询,自连接必须使用表别名</p><p><strong>子查询</strong></p><h2 id="内连接"><a href="#内连接" class="headerlink" title="内连接"></a>内连接</h2><p>内连接查询语法有两种</p><p><strong>隐式内连接</strong></p><p><code>SELECT 字段列表 FROM 表1,表2 WHERE 条件...;</code></p><p><strong>显示内连接</strong></p><p><code>SELECT 字段列表 FROM 表1 [INNER] JOIN 表2 ON 连接条件...;</code></p><p>内连接查询的是两张表的交集部分</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719224031607.png" alt="image-20230719224031607"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719224247221.png" alt="image-20230719224247221"></p><h2 id="外连接"><a href="#外连接" class="headerlink" title="外连接"></a>外连接</h2><p><strong>左外连接</strong></p><p><code>SELECT 字段列表 FROM 表1 LEFT [OUTER] JOIN 表2 ON 条件...;</code></p><p><strong>右外连接</strong><br><code>SELECT 字段列表 FROM 表1 RIGHT [OUTER] JOIN 表2 ON 条件...;</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719224936057.png" alt="image-20230719224936057"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719225314761.png" alt="image-20230719225314761"></p><p>在项目开发当中,左外连接用的多一些,因为右外连接可以改为左外连接</p><h2 id="自连接"><a href="#自连接" class="headerlink" title="自连接"></a>自连接</h2><p><strong>自连接查询</strong></p><p><code>SELECT 字段列表 FROM 表A 别名A JOIN 表A 别名B ON 条件...;</code></p><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><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">-- 1. 查询员工 及其 所属领导的名字</span></span><br><span class="line"><span class="keyword">select</span> e.name, p.name <span class="keyword">from</span> emp e <span class="keyword">join</span> emp p <span class="keyword">on</span> e.managerid <span class="operator">=</span> p.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 2. 查询所有员工 emp 及其领导的名字 emp , 如果员工没有领导,也需要查询出来</span></span><br><span class="line"><span class="keyword">select</span> e.name <span class="string">'员工'</span>, p.name <span class="string">'直属领导'</span> <span class="keyword">from</span> emp e <span class="keyword">left</span> <span class="keyword">join</span> emp p <span class="keyword">on</span> e.managerid <span class="operator">=</span> p.id;</span><br></pre></td></tr></table></figure><h2 id="联合查询-union-union-all"><a href="#联合查询-union-union-all" class="headerlink" title="联合查询 - union, union all"></a>联合查询 - union, union all</h2><p>对于union查询,就是把多次查询的结果合并起来,形成一个新的查询结果集</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="keyword">FROM</span> 表A...</span><br><span class="line"><span class="keyword">UNION</span>[<span class="keyword">ALL</span>]</span><br><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表B...;</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719231744466.png" alt="image-20230719231744466"></p><p><strong>注意:</strong> 联合查询多张表是列数必须保持一致,字段类型也需要保持一致;union all会将全部数据直接合并在一起,union会对合并后的数据去重</p><h2 id="子查询介绍"><a href="#子查询介绍" class="headerlink" title="子查询介绍"></a>子查询介绍</h2><p>SQL语句中嵌套SELECT语句,称为嵌套查询,又称子查询</p><p><code>SELECT * FROM t1 WHERE column1 = (SELECT column2 FROM t2);</code></p><p>子查询外部的语句可以是INSERT / UPDATE / DELETE /SELECT 中的任一个</p><p>根据子查询结果不同,分为</p><ul><li>标量子查询(子查询结果为单个值即一行一列)</li><li>列子查询(子查询结果为一列)</li><li>行子查询(子查询结果为一行)</li><li>表子查询(子查询结果为多行多列)</li></ul><p>根据子查询位置,分为:WHERE之后、FROM之后、SELECT之后</p><h2 id="标量子查询"><a href="#标量子查询" class="headerlink" title="标量子查询"></a>标量子查询</h2><p><u>子查询返回的是单行单列的数据也就是单个值</u>,这也是子查询中最简单的返回形式</p><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><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></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">-- a. 查询“销售部”部门ID</span></span><br><span class="line"><span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> name <span class="operator">=</span> "销售部";</span><br><span class="line"></span><br><span class="line"><span class="comment">-- b. 根据销售部部门ID,查询员工信息</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> dept_id <span class="operator">=</span> <span class="number">4</span>;</span><br><span class="line"></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> dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> name <span class="operator">=</span> "销售部");</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 2. 查询在“东方白”入职之后的员工信息</span></span><br><span class="line"><span class="comment">-- a. 查询 东方白 的 入职日期</span></span><br><span class="line"><span class="keyword">select</span> entrydate <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">-- b.查询指定入职日期之后入职的员工信息</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> entrydate <span class="operator">></span> <span class="string">'2009-02-12'</span>;</span><br><span class="line"></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> entrydate <span class="operator">></span> (<span class="keyword">select</span> entrydate <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'东方白'</span>);</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720100159150.png" alt="image-20230720100159150"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720100734293.png" alt="image-20230720100734293"></p><h2 id="列子查询"><a href="#列子查询" class="headerlink" title="列子查询"></a>列子查询</h2><p>子查询返回的结果是一列多行,通常来自对表的某个字段查询的返回,这种子查询称为<font color=#9932CD>列子查询</font></p><p>常用操作符:IN、 NOT IN、ANY、SOME、ALL</p><div class="table-container"><table><thead><tr><th>操作符</th><th>描述</th></tr></thead><tbody><tr><td>IN</td><td>在指定集合范围内,多选一</td></tr><tr><td>NOT IN</td><td>不在指定集合范围之内</td></tr><tr><td>ANY</td><td>子查询返回列表中,有任意一个满足即可</td></tr><tr><td>SOME</td><td>与ANY等同,使用SOME的地方都可以使用ANY</td></tr><tr><td>ALL</td><td>子查询返回列表的所有值都必须满足</td></tr></tbody></table></div><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></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">-- a. 查询“销售部” 和 “市场部”部门ID</span></span><br><span class="line"><span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> 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">-- b. 根据部门ID,查询员工信息</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> dept_id <span class="keyword">in</span> (<span class="number">2</span>,<span class="number">4</span>);</span><br><span class="line"></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> dept_id <span class="keyword">in</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> 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">-- 2. 查询比 财务部 所有人工资都高的员工信息</span></span><br><span class="line"><span class="comment">-- a. 查询“财务部”部门ID</span></span><br><span class="line"><span class="keyword">select</span> id <span class="keyword">from</span> dept <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">-- b. 根据部门ID查询财务部的最高工资</span></span><br><span class="line"><span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> dept_id <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <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">-- c. 查询比指定工资高的所有员工信息</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> salary <span class="operator">></span> <span class="keyword">all</span> (<span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <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">-- 3. 查询比研发部中任意一人工资高的员工信息</span></span><br><span class="line"><span class="comment">-- a. 查询研发部部门ID</span></span><br><span class="line"><span class="keyword">select</span> id <span class="keyword">from</span> dept <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">-- b. 查询研发部所有人工资</span></span><br><span class="line"><span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <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">-- c.查询比指定工资高的所有员工信息</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> salary <span class="operator">></span> <span class="keyword">any</span> (<span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'研发部'</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> salary <span class="operator">></span> <span class="keyword">some</span> (<span class="keyword">select</span> salary <span class="keyword">from</span> emp <span class="keyword">where</span> dept_id <span class="operator">=</span> (<span class="keyword">select</span> id <span class="keyword">from</span> dept <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'研发部'</span>));</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720102746919.png" alt="image-20230720102746919"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720104112176.png" alt="image-20230720104112176"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720105302951.png" alt="image-20230720105302951"></p><h2 id="行子查询"><a href="#行子查询" class="headerlink" title="行子查询"></a>行子查询</h2><p>子查询返回的结果是一行多列</p><p>常用操作符:=、<>、IN、NOT、IN</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></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">-- a. 查询“张无忌”的薪酬与直属领导</span></span><br><span class="line"><span class="keyword">select</span> salary, managerid <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">-- b. 查询与“张无忌”薪酬及直属领导相同的员工信息</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> (salary, managerid) <span class="operator">=</span> (<span class="number">12500</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 class="keyword">from</span> emp <span class="keyword">where</span> (salary, managerid) <span class="operator">=</span> (<span class="keyword">select</span> salary, managerid <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'张无忌'</span>);</span><br></pre></td></tr></table></figure><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720110618741.png" alt="image-20230720110618741"></p><h2 id="表子查询"><a href="#表子查询" class="headerlink" title="表子查询"></a>表子查询</h2><p>子查询返回的结果是多行多列</p><p>常用的操作符:IN</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></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">-- a. 查询“鹿杖客”、“宋远桥”的职位和薪酬</span></span><br><span class="line"><span class="keyword">select</span> job, salary <span class="keyword">from</span> emp <span class="keyword">where</span> 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">-- b. 查询与“鹿杖客"、”宋远桥“职位和薪酬相同的员工信息</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> (job, salary) <span class="keyword">in</span> ((<span class="string">'职位'</span>, <span class="number">3750</span>), (<span class="string">'销售'</span>, <span class="number">4600</span>));</span><br><span class="line"></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> (job, salary) <span class="keyword">in</span> (<span class="keyword">select</span> job, salary <span class="keyword">from</span> emp <span class="keyword">where</span> 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 class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> (job, salary) <span class="keyword">in</span> (<span class="keyword">select</span> job, salary <span class="keyword">from</span> emp <span class="keyword">where</span> name <span class="keyword">in</span> (<span class="string">'鹿杖客'</span>, <span class="string">'宋远桥'</span>));</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 2. 查询入职日期是 "2006-01-01" 之后的员工信息及其部门信息</span></span><br><span class="line"><span class="comment">-- a. 查询入职日期是“2006-01-01”之后的员工信息</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> entrydate <span class="operator">></span> <span class="string">'2006-01-01'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- b.查询入职日期是 "2006-01-01" 之后的员工信息及其部门信息</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, d.name <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">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><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720111726193.png" alt="image-20230720111726193"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720112622882.png" alt="image-20230720112622882"></p><h2 id="综合实践"><a href="#综合实践" class="headerlink" title="综合实践"></a>综合实践</h2><blockquote><h3 id="根据需求,完成SQL语句编写"><a href="#根据需求,完成SQL语句编写" class="headerlink" title="根据需求,完成SQL语句编写"></a>根据需求,完成SQL语句编写</h3><ol><li>查询员工姓名、年龄、职位、部门信息</li><li>查询年龄小于30岁的员工姓名、年龄、职位、部门信息</li><li>查询拥有员工的部门ID、部门名称</li><li>查询所有年龄大于40岁的员工及其归属的部门名称;如果员工没有分配部门,也需要展示出来</li><li>查询所有员工的工资等级</li><li>查询“研发部”所有员工的信息及工资等级</li><li>查询“研发部”员工的平均工资</li><li>查询工资比“灭绝”高的员工信息</li><li>查询比平均薪酬高的员工信息</li><li>查询低于本部门平均工资的员工信息</li><li>查询所有部门信息并统计部门员工人数</li><li>查询所有学生的选课情况,展示出学生名称、学号、课程名称</li></ol></blockquote><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><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></pre></td><td class="code"><pre><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.<span class="operator">*</span> <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">-- 2. 查询年龄小于30岁的员工姓名、年龄、职位、部门信息(显示内连接)</span></span><br><span class="line"><span class="keyword">select</span> e.name, e.age, e.job, d.name <span class="keyword">from</span> emp e <span class="keyword">join</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id <span class="keyword">where</span> 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="keyword">select</span> <span class="keyword">distinct</span> d.id, 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">-- 4. 查询所有年龄大于40岁的员工及其归属的部门名称:如果员工没有分配部门,也需要展示出来</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, 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 class="keyword">where</span> e.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="comment">-- 表: emp, salgrade</span></span><br><span class="line"><span class="comment">-- 连接条件: emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal</span></span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, s.grade <span class="keyword">from</span> emp e, salgrade s <span class="keyword">where</span> e.salary <span class="operator">>=</span> s.losal <span class="keyword">and</span> e.salary <span class="operator"><=</span> s.hisal;</span><br><span class="line"><span class="keyword">select</span> e.<span class="operator">*</span>, s.grade <span class="keyword">from</span> emp e, salgrade s <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">-- 表:emp、dept、salgrade</span></span><br><span class="line"><span class="comment">-- 连接条件:emp.dept_id = dept.id emp.salary >= salgrade.losal and emp.salary <= salgrade.hisal</span></span><br><span class="line"><span class="comment">-- 查询条件:dept.name = '研发部'</span></span><br><span class="line"><span class="keyword">select</span> p.<span class="operator">*</span>, s.grade, s.losal, s.hisal <span class="keyword">from</span> (</span><br><span class="line"> <span class="keyword">select</span> e.<span class="operator">*</span> <span class="keyword">from</span> emp e, (<span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> dept <span class="keyword">where</span> name <span class="operator">=</span> <span class="string">'研发部'</span>) d <span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id</span><br><span class="line"> ) p, salgrade s <span class="keyword">where</span> p.salary <span class="keyword">between</span> s.losal <span class="keyword">and</span> s.hisal;</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><br><span class="line"><span class="comment">-- 7. 查询“研发部”员工的平均工资</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> <span class="built_in">avg</span>(e.salary), 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 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="comment">-- 表:emp</span></span><br><span class="line"><span class="comment">-- a.查询“灭绝”的薪资</span></span><br><span class="line"><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">-- b.查询比指定薪资高的员工信息</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> 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="comment">-- 表:emp</span></span><br><span class="line"><span class="comment">-- a.查询平均薪资</span></span><br><span class="line"><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">-- b.查询比平均薪资高的员工信息</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> 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">-- 表: emp</span></span><br><span class="line"><span class="comment">-- a. 查询指定部门平均工资</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">avg</span>(salary) <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- b.查询低于本部门平均工资的员工信息</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.salary <span class="operator"><</span> (<span class="keyword">select</span> <span class="built_in">avg</span>(salary) <span class="keyword">from</span> emp e <span class="keyword">where</span> e.dept_id <span class="operator">=</span> e1.dept_id);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 11. 查询所有的部门信息并统计部门的员工人数</span></span><br><span class="line"><span class="comment">-- 表:emp, dept</span></span><br><span class="line"><span class="keyword">select</span> id, 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 class="keyword">from</span> dept d;</span><br><span class="line"></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 class="keyword">where</span> dept_id <span class="operator">=</span> <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">select</span> d.<span class="operator">*</span>, <span class="built_in">count</span>(<span class="operator">*</span>) <span class="string">'人数'</span> <span class="keyword">from</span> emp e, dept d <span class="keyword">where</span> e.dept_id <span class="operator">=</span> d.id <span class="keyword">group</span> <span class="keyword">by</span> e.dept_id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 12. 查询所有学生的选课情况,展示出学生名称,学号,课程名称</span></span><br><span class="line"><span class="comment">-- 表:student, course, student_course</span></span><br><span class="line"><span class="comment">-- 连接条件:student.id = student_course.studentid, course.id = student_course.courseid</span></span><br><span class="line"><span class="keyword">select</span> s.name, s.no, c.name <span class="keyword">from</span> student s, student_course sc, course c <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><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720120107900.png" alt="image-20230720120107900"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720114053450.png" alt="image-20230720114053450"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720114543617.png" alt="image-20230720114543617"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720115326139.png" alt="image-20230720115326139"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720115708088.png" alt="image-20230720115708088"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720144513701.png" alt="image-20230720144513701"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720150027047.png" alt="image-20230720150027047"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720150517486.png" alt="image-20230720150517486"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720150813071.png" alt="image-20230720150813071"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720151129196.png" alt="image-20230720151129196"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720151955676.png" alt="image-20230720151955676"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720154721320.png" alt="image-20230720154721320"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720155713524.png" alt="image-20230720155713524"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230720155749063.png" alt="image-20230720155749063"></p>]]></content>
<summary type="html"><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>在MySQL中,多表查询是指在一个查询语句中涉及多个表的操作。这种查询可以通过连接(join)操作将多个表关联起来,从而实现在多个表之间进行</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>MySQL5.7升为8.0新指南</title>
<link href="http://example.com/posts/6635.html"/>
<id>http://example.com/posts/6635.html</id>
<published>2023-10-13T06:50:00.000Z</published>
<updated>2023-10-13T06:48:52.687Z</updated>
<content type="html"><![CDATA[<h2 id="一、效果图"><a href="#一、效果图" class="headerlink" title="一、效果图"></a>一、效果图</h2><p>俗话说的好:<u>有图有真相</u></p><p>升级前</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718152235046.png" alt="image-20230718152235046"></p><p>升级后</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718123656618.png" alt="image-20230718123656618"></p><h2 id="二、核心思路"><a href="#二、核心思路" class="headerlink" title="二、核心思路"></a>二、核心思路</h2><p>以官方教程为主,各种博文为辅,实践出真知</p><p>官方教程网址:<a href="https://dev.mysql.com/doc/refman/8.0/en/windows-upgrading.html">https://dev.mysql.com/doc/refman/8.0/en/windows-upgrading.html</a></p><p>重点内容重新解读</p><p>对于5.7升为8.0更建议<font color=#9932CD>使用Windows ZIP 存档分发方式升级</font>,同时在升级前应该做好备份工作</p><h2 id="三、备份"><a href="#三、备份" class="headerlink" title="三、备份"></a>三、备份</h2><p>这里采用mysqldump命令来备份</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/mysql_update.jpg" alt=""></p><h3 id="mysqldum介绍"><a href="#mysqldum介绍" class="headerlink" title="mysqldum介绍"></a>mysqldum介绍</h3><p><u>mysqldump 工具一般会随着 MySQL 数据库的安装而自动安装即安装完 MySQL 服务器后, 您可以直接使用 mysqldump 工具</u>。它能将一个或者多个数据库,或者数据库中的一个或者多个表导出为一个 SQL 文件,包括 DDL 语句和 DML 语句</p><p>第一步 找到mysql数据库安装目录</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717225400814.png" alt="image-20230717225400814"></p><p>第二步 使用<code>--all-databases</code>选项备份所有MySQL数据库,命令示例:<code>mysqldump -u root -p --all-databases > all_databases.sql</code></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717230915711.png" alt="image-20230717230915711"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717231104613.png" alt="image-20230717231104613"></p><p>密码为root用户的登录密码</p><p>备份延伸:备份单个数据库和备份多个数据库,便于分享</p><p><strong>单个数据库备份</strong></p><p>命令示例:<code>mysqldump -u root -p database_name > database_name.sql</code></p><p>解释:通过<code>root</code>用户将名为<code>database_name</code>的数据库进行备份即 将其保存到名为<code>database_name.sql</code>的文件中</p><p><strong>多个数据库备份</strong></p><p>命令示例:<code>mysqldump -u root -p --databases database_name_a database_name_b > databases_a_b.sql</code></p><p>解释:通过<code>root</code>用户将要备份的数据库列表即<font color=#9932CD>用空格分隔的数据库名称</font>创建为一个包含数据库列表的转储文件</p><h2 id="四、卸载MySQL旧版"><a href="#四、卸载MySQL旧版" class="headerlink" title="四、卸载MySQL旧版"></a>四、卸载MySQL旧版</h2><p>卸载四步走</p><h3 id="第一步"><a href="#第一步" class="headerlink" title="第一步"></a>第一步</h3><p>停止MySQL服务并移除MySQL服务</p><p>以管理员身份运行cmd</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718105427105.png" alt="image-20230718105427105"></p><h3 id="第二步"><a href="#第二步" class="headerlink" title="第二步"></a>第二步</h3><p>删除MySQL安装目录,默认是安装在C盘中的Program Files目录下</p><p>我的安装目录在D:\Mysql下,因而来这里删除</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718110237249.png" alt="image-20230718110237249"></p><p>删除后</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718110257511.png" alt="image-20230718110257511"></p><h3 id="第三步"><a href="#第三步" class="headerlink" title="第三步"></a>第三步</h3><p>删除相关注册表</p><p>通过吾爱大佬@wszjf提供的注册表编辑器==RegCool==进行删除</p><p>总共有三处需要查看并删除</p><ul><li><code>HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\EventLog\Application\MySQL</code></li><li><code>HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Services\Eventlog\Application\MySQL</code></li><li><code>HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\MySQL</code></li></ul><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718111619197.png" alt="image-20230718111619197"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718111731284.png" alt="image-20230718111731284"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718111834964.png" alt="image-20230718111834964"></p><h3 id="第四步"><a href="#第四步" class="headerlink" title="第四步"></a>第四步</h3><p>关机重启再安装</p><h2 id="五、MySQL新版下载"><a href="#五、MySQL新版下载" class="headerlink" title="五、MySQL新版下载"></a>五、MySQL新版下载</h2><p>来到MySQL官方提供的下载页:<a href="https://www.mysql.com/cn/downloads">https://www.mysql.com/cn/downloads</a>,滑至底部找到<a href="https://dev.mysql.com/downloads/mysql">MySQL Community Server</a>,然后进入详情页下载</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718113643389.png" alt="image-20230718113643389"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718114301978.png" alt="image-20230718114301978"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718114415335.png" alt="image-20230718114415335"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718114441313.png" alt="image-20230718114441313"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718114937600.png" alt="image-20230718114937600"></p><h2 id="六、MySQL新版安装"><a href="#六、MySQL新版安装" class="headerlink" title="六、MySQL新版安装"></a>六、MySQL新版安装</h2><p>跟着博文教程傻瓜安装,需要注意部分会重点强调</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718115216666.png" alt="image-20230718115216666"></p><p> <strong>选择自定义安装路径</strong></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718115900587.png" alt="image-20230718115900587"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718120530431.png" alt="image-20230718120530431"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718120623665.png" alt="image-20230718120623665"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718120810077.png" alt="image-20230718120810077"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718120903663.png" alt="image-20230718120903663"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718120924109.png" alt="image-20230718120924109"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718120957789.png" alt="image-20230718120957789"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718121459582.png" alt="image-20230718121459582"></p><blockquote><p>选项一:使用强密码加密进行身份验证(升级触发的新功能)</p><p>选项二:使用传统身份验证方法(兼容5.x旧版本)</p><p>如果MySQL可以采用强加密进行身份验证,但图形化管理工具并没有采用强密码加密,就会导致图形化管理工具无法连接MySQL</p></blockquote><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718121838558.png" alt="image-20230718121838558"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718121905229.png" alt="image-20230718121905229"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718121958296.png" alt="image-20230718121958296"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718122027790.png" alt="image-20230718122027790"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718122150715.png" alt="image-20230718122150715"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718122211667.png" alt="image-20230718122211667"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718122231218.png" alt="image-20230718122231218"></p><p>到此就安装完毕了</p><h2 id="七、MySQL-8-0连接测试"><a href="#七、MySQL-8-0连接测试" class="headerlink" title="七、MySQL 8.0连接测试"></a>七、MySQL 8.0连接测试</h2><p>在开始菜单可以看到8.0所提供的命令行客户端</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718122507932.png" alt="image-20230718122507932"></p><p>输入安装时设置的密码进行登录<img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718122719402.png" alt="image-20230718122719402"></p><p>然后输入<code>exit</code>退出即可</p><h2 id="八、配置环境变量"><a href="#八、配置环境变量" class="headerlink" title="八、配置环境变量"></a>八、配置环境变量</h2><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718123336062.png" alt="image-20230718123336062"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718123512456.png" alt="image-20230718123512456"></p><h2 id="九、恢复备份(错误)"><a href="#九、恢复备份(错误)" class="headerlink" title="九、恢复备份(错误)"></a>九、恢复备份(错误)</h2><font color=red>**注意:** 这里是不同版本系列下的数据库迁移,不要迁移 information_schema, mysql,sys and performance_schema 这几个数据库,不然肯定会遇到问题</font><font color=#008c8c>以下恢复备份方式没有问题,问题在于将5.7下内置的数据库覆盖了8.0内置的数据库</font><p>如果熟悉Navicat导入sql文件那就使用Navicat,这里我选择使用DataGrip,正好实践下DataGrip如何导入sql文件的</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718150609795.png" alt="image-20230718150609795"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718150715446.png" alt="image-20230718150715446"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718150803345.png" alt="image-20230718150803345"></p><p>这时候在使用检查约束(8.0.16版之后支持)就执行成功了</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718150933535.png" alt="image-20230718150933535"></p><h2 id="十、报1449错误代码"><a href="#十、报1449错误代码" class="headerlink" title="十、报1449错误代码"></a>十、报1449错误代码</h2><p>在使用<code>show databases;</code>时报错:<code>ERROR 1449 (HY000): The user specified as a definer ('mysql.infoschema'@'localhost') does not exist</code></p><p>通过几小时搜寻与整理发现应该是与昨天将MySQL5.7的所有数据库导入到MySQL8.0有关,在stack overflow有<a href="https://stackoverflow.com/questions/70287223/mysql-the-user-specified-as-a-definer-mysql-infoschemalocalhost-does-not">相关解答</a></p><blockquote><p>问题可能出在 mysql 进行迁移或备份时的数据库。 不应该对 information_schema, mysql,sys and performance_schema 这几个数据库进行备份和导入覆盖新服务器数据库</p><p>如果用户很重要,您可以插入它。 转储来自旧服务器的数据库mysql下的user表</p></blockquote><p>这时候没有别的办法,对MySQL 8.0进行重装</p><h3 id="MySQL-8-0卸载"><a href="#MySQL-8-0卸载" class="headerlink" title="MySQL 8.0卸载"></a>MySQL 8.0卸载</h3><p>两步走,第一步:停止MySQL 服务;第二步,用geek卸载MySQL相关程序</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719160037112.png" alt="image-20230719160037112"></p><p>重装后可以看到MySQL 8.0中myql数据库下的user表的默认用户</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719161211946.png" alt="image-20230719161211946"></p><h3 id="恢复备份"><a href="#恢复备份" class="headerlink" title="恢复备份"></a>恢复备份</h3><p>因为做的是全库备份,而现在需要恢复除内置数据库以外的数据库,换言之,如何从全库备份中恢复一个或多个数据库呢?</p><p>在搜寻一番无果后,结合自己的情况找到了解决办法</p><p>通过EditPlus打开全库备份文件,然后搜索<code>CREATE DATABASE</code>发现了有11处在创建数据库和表以及插入数据</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719164103381.png" alt="image-20230719164103381"></p><p>然后便是新建一个文件bookstore.sql,在全库备份文件中找到恢复bookstore数据库的代码(通过关键字搜索,找到一个含CREATE DATABASE <code>bookstore</code>和下一个CREATE DATABASE中间的代码),然后复制到bookstore.sql,之后先通过navicat运行下sql文件,发现可行便知道解决了</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719164750771.png" alt="image-20230719164750771"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719164828461.png" alt="image-20230719164828461"></p><font size=5>通过上述手段就可以恢复全部数据库,这一次吃了没经验的亏,还有部分原因是网上教程存在问题</font><h2 id="十一、注册MySQL到服务中"><a href="#十一、注册MySQL到服务中" class="headerlink" title="十一、注册MySQL到服务中"></a>十一、注册MySQL到服务中</h2><p>今天发现使用<code>net stop mysql</code>会提示服务名无效</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719111030804.png" alt="image-20230719111030804"></p><p>也就是没有将MySQL注入到服务中,下面进行注入</p><p>来到MySQL的安装路径下bin目录下,然后打开命令行窗口</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719111405078.png" alt="image-20230719111405078"></p><p>然后运行<code>mysqld --install</code>命令</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719111608474.png" alt="image-20230719111608474"></p><p>如果出现==Install/Remove of the Service Denied!==则表示不成功,需要用管理员身份运行cmd</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719112256268.png" alt="image-20230719112256268"></p><p>然后又有新问题了:MySQL 服务无法启动</p><p>通过<kbd>win</kbd>+<kbd>r</kbd>输入<code>services.msc</code>发现了有MySQL本地系统和MySQL80网络服务</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719114121287.png" alt="image-20230719114121287"></p><blockquote><p>MySQL本地系统和MySQL80网络服务是两种不同的MySQL部署方式。</p><p>MySQL本地系统是指将MySQL数据库安装在本地计算机上,作为本地应用程序使用。这种部署方式适用于个人开发者或小型团队,他们可以在自己的计算机上安装和管理MySQL数据库。本地系统通常用于开发和测试目的,因为它提供了更高的灵活性和控制权。</p><p>MySQL80网络服务是指将MySQL数据库安装在一个专门的服务器上,并通过网络提供服务。这种部署方式适用于需要多个用户访问和共享数据库的情况,例如企业或大型组织。网络服务允许多个客户端通过网络连接到MySQL服务器,并同时访问数据库。这种部署方式通常需要专门的服务器硬件和网络基础设施来支持高并发和可靠性。</p><p>总结来说,MySQL本地系统适用于个人开发和测试,而MySQL80网络服务适用于多用户访问和共享数据库的情况。选择哪种部署方式取决于你的具体需求和资源情况。</p></blockquote><p>所以答案很简单,关闭网络服务,开启本地系统</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719132446141.png" alt="image-20230719132446141"></p><p>这样就欧了</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230719114610558.png" alt="image-20230719114610558"></p>]]></content>
<summary type="html"><h2 id="一、效果图"><a href="#一、效果图" class="headerlink" title="一、效果图"></a>一、效果图</h2><p>俗话说的好:<u>有图有真相</u></p>
<p>升级前</p>
<p> <img src="https://mk</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>约束</title>
<link href="http://example.com/posts/6634.html"/>
<id>http://example.com/posts/6634.html</id>
<published>2023-10-13T06:47:00.000Z</published>
<updated>2023-10-13T06:48:01.308Z</updated>
<content type="html"><![CDATA[<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>约束是<u>用于限制表中数据的规则或条件</u>,确保数据的完整性和统一性</p><p>常见约束类型如下:</p><div class="table-container"><table><thead><tr><th>约束</th><th>描述</th><th>关键字</th></tr></thead><tbody><tr><td>非空约束</td><td>确保字段的值不能为null</td><td>NOT NULL</td></tr><tr><td>唯一约束</td><td>确保字段的值是唯一的,但允许为空值</td><td>UNIQUE</td></tr><tr><td>主键约束</td><td>用于唯一标识表中的每一行数据,要求非空且唯一</td><td>PRIMARY KEY</td></tr><tr><td>默认约束</td><td>如果插入数据未提供字段值,则采用默认值</td><td>DEFAULT</td></tr><tr><td>检查约束(8.0.16版之后支持)</td><td>确保插入或更新的数据满足指定条件</td><td>CHECK</td></tr><tr><td>外键约束</td><td>用于建立表之间的关联关系。将一个表的字段与另一个表的主键或唯一约束关联起来</td><td>FOREIGN KEY</td></tr></tbody></table></div><p><strong>注意:</strong> 约束是作用于表中字段上的,可以在创建或修改表时添加约束</p><h2 id="案例演示"><a href="#案例演示" class="headerlink" title="案例演示"></a>案例演示</h2><blockquote><h3 id="根据需求,完成表结构的创建"><a href="#根据需求,完成表结构的创建" class="headerlink" title="根据需求,完成表结构的创建"></a>根据需求,完成表结构的创建</h3><p>字段名 字段含义 字段类型 约束条件 约束关键字</p><p>id ID唯一标识 int 主键且自增 PRIMARY KEY,AUTO_INCREMENT</p><p>name 姓名 varchar(10) 不为空且唯一 NOT NULL,UNIQUE</p><p>age 年龄 int 大于0且小于等于120 CHECK</p><p>status 状态 char(1) 没有指定值默认为1 DEFAULT </p><p>sex 性别 char(1) 无 </p></blockquote><p>检查约束报错 - 其实已有心理准备,因为我使用的mysql版本为5.7</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717202824324.png" alt="image-20230717202824324"></p><p>既然如此,那就卸载5.7版安装8.0版</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718155918768.png" alt="image-20230718155918768"></p><p>又是表的编码格式报错,这里更改表的编码格式为utf8</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718160837230.png" alt="image-20230718160837230"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718161020566.png" alt="image-20230718161020566"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718161756916.png" alt="image-20230718161756916"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718161805957.png" alt="image-20230718161805957"></p><h2 id="外键约束"><a href="#外键约束" class="headerlink" title="外键约束"></a>外键约束</h2><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718221142880.png" alt="image-20230718221142880"></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></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> 表名(</span><br><span class="line">字段名 数据类型,</span><br><span class="line"> ...</span><br><span class="line"> [CONSTRNINT][外键名称] <span class="keyword">FOREIGN</span> KEY(外键字段名) <span class="keyword">REFERENCES</span> 主表(主表列名)</span><br><span class="line">);</span><br></pre></td></tr></table></figure><p><code>ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY(外键字段名) REFERENCES 主表(主表列名);</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718223411844.png" alt="image-20230718223411844"></p><p>添加外键时报错</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718224156342.png" alt="image-20230718224156342"></p><blockquote><p>此处错误是因为子表中关联的字段dept_id的值在父表也就是dept中没有,建立外键约束必须保证关联子表中的字段值在父表中存在,这里是子表的dept_id值为1时父表没有与之对应的id值存在</p></blockquote><p>解决方案:将删除的行给加回来然后在执行外键约束</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718225236211.png" alt="image-20230718225236211"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718225318819.png" alt="image-20230718225318819"></p><p>删除外键</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718225526666.png" alt="image-20230718225526666"></p><h2 id="外键删除更新行为"><a href="#外键删除更新行为" class="headerlink" title="外键删除更新行为"></a>外键删除更新行为</h2><div class="table-container"><table><thead><tr><th>行为</th><th>说明</th></tr></thead><tbody><tr><td>NO ACTION</td><td>当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。(与RESTRICT一致)</td></tr><tr><td>RESTRICT</td><td>当在父表中删除/更新对应记录时,首先检查该记录是否有对应外键,如果有则不允许删除/更新。(与NO ACTION一致)</td></tr><tr><td>CASCADE</td><td>当在父表中删除/更新记录时,首先检查该记录是否有对应外键,如果有则也删除/更新外键在子表中的记录</td></tr><tr><td>SET NULL</td><td>当在父表中删除对应记录时,首先检查该记录是否有对应外键,如果有则设置子表中该外键值为null(这就要求该外键允许为null)</td></tr><tr><td>SET DEFAULT</td><td>父表有变更时,子表将外键列设置成一个默认的值(Innodb不支持)</td></tr></tbody></table></div><p><code>ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段) REFRENCES 主表名(主表字段名) ON UPDATE CASCADE ON DELETE CASCAD;</code></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230718231752477.png" alt="image-20230718231752477"></p>]]></content>
<summary type="html"><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>约束是<u>用于限制表中数据的规则或条件</u>,确保数据的完整性和统一性</p>
<p>常见约束类型如下:</p>
<div class=</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>函数</title>
<link href="http://example.com/posts/6633.html"/>
<id>http://example.com/posts/6633.html</id>
<published>2023-10-12T02:16:00.000Z</published>
<updated>2023-10-12T02:15:47.111Z</updated>
<content type="html"><![CDATA[<h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717170923795.png" alt="image-20230717170923795"></p><h2 id="字符串函数"><a href="#字符串函数" class="headerlink" title="字符串函数"></a>字符串函数</h2><p>MySQL中内置了很多字符串函数,常用的几个如下:</p><div class="table-container"><table><thead><tr><th>函数</th><th>功能</th></tr></thead><tbody><tr><td>CONCAT(S1,S2,…Sn)</td><td>字符串拼接,将S1,S2,…Sn拼接成一个字符串</td></tr><tr><td>LOWER(str)</td><td>将字符串str全部转为小写</td></tr><tr><td>UPPER(str)</td><td>将字符串str全部转为大写</td></tr><tr><td>LPAD(str,n,pad)</td><td>左填充,用字符串pad对str的左边进行填充,达到n个字符串长度</td></tr><tr><td>RPAD(str,n,pad)</td><td>右填充,用字符串pad对str的右边进行填充,达到n个字符串长度</td></tr><tr><td>TRIM(str)</td><td>去掉字符串头部和尾部的空格</td></tr><tr><td>SUBSTRING(str,start,len)</td><td>返回从字符串str从start位置起的len个长度的字符串</td></tr></tbody></table></div><p>更改下DataGrip的外观</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717171730990.png" alt="image-20230717171730990"></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717173319342.png" alt="image-20230717173319342"></p><blockquote><h3 id="根据需求完成如下SQL编写"><a href="#根据需求完成如下SQL编写" class="headerlink" title="根据需求完成如下SQL编写"></a>根据需求完成如下SQL编写</h3><p>由于业务需求变更,企业员工的工号,统一为5位数,目前不足5位数的全部在前面补0。比如:1号员工的工号应该为00001</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717174542404.png" alt="image-20230717174542404"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717174420315.png" alt="image-20230717174420315"></p><h2 id="数值函数"><a href="#数值函数" class="headerlink" title="数值函数"></a>数值函数</h2><p>常见的数值函数如下:</p><div class="table-container"><table><thead><tr><th>函数</th><th>功能</th></tr></thead><tbody><tr><td>CEIL(x)</td><td>向上取整</td></tr><tr><td>FLOOR(x)</td><td>向下取整</td></tr><tr><td>MOD(x,y)</td><td>返回x/y的模</td></tr><tr><td>RAND()</td><td>返回0~1内的随机数</td></tr><tr><td>ROUND(x,y)</td><td>求参数x的四舍五入的值,保留y位小数</td></tr></tbody></table></div><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717185509491.png" alt="image-20230717185509491"></p><blockquote><h3 id="根据需求完成如下SQL编写-1"><a href="#根据需求完成如下SQL编写-1" class="headerlink" title="根据需求完成如下SQL编写"></a>根据需求完成如下SQL编写</h3><p>通过数据库的函数,生成一个六位数的随机验证码</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717190316692.png" alt="image-20230717190316692"></p><h2 id="日期函数"><a href="#日期函数" class="headerlink" title="日期函数"></a>日期函数</h2><p>常见的日期函数如下:</p><div class="table-container"><table><thead><tr><th>函数</th><th>功能</th></tr></thead><tbody><tr><td>CURDAT()</td><td>返回当前日期</td></tr><tr><td>CURTIME()</td><td>返回当前时间</td></tr><tr><td>NOW()</td><td>返回当前日期和时间</td></tr><tr><td>YEAR(date)</td><td>获取指定date的年份</td></tr><tr><td>MONTH(date)</td><td>获取指定date的月份</td></tr><tr><td>DAY(date)</td><td>获取指定date的日期</td></tr><tr><td>DATE_ADD(date,INTERVAL expr type)</td><td>返回一个日期/时间值加上一个时间间隔expr后的时间值</td></tr><tr><td>DATEDIFF(date1,date2)</td><td>返回起始时间date1和结束时间date2之间的天数</td></tr></tbody></table></div><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717191507864.png" alt="image-20230717191507864"></p><blockquote><h3 id="根据需求完成如下SQL编写-2"><a href="#根据需求完成如下SQL编写-2" class="headerlink" title="根据需求完成如下SQL编写"></a>根据需求完成如下SQL编写</h3><p>查询所有员工的入职天数,并根据入职天数倒序排序</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717192140469.png" alt="image-20230717192140469"></p><h2 id="流程函数"><a href="#流程函数" class="headerlink" title="流程函数"></a>流程函数</h2><p>流程函数也是很常用的一类函数,可以在SQL语句中实现条件过滤,从而提供语句的效率</p><div class="table-container"><table><thead><tr><th>函数</th><th>功能</th></tr></thead><tbody><tr><td>IF(value,t,f)</td><td>如果value为true,则返回t,否则返回f</td></tr><tr><td>IFNULL(value1,value2)</td><td>如果value1不为空,返回value1,否则返回value2</td></tr><tr><td>CASE WHEN [val1] THEN [res1] … ELSE [default] END</td><td>如果val1为true,返回res1, … 否则返回default默认值</td></tr><tr><td>CASE [expr] WHEN [val1] THEN [res1] … ELES [default] END</td><td>如果expr的值等于val1,返回res1,… 否则返回default默认值</td></tr></tbody></table></div><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717193544637.png" alt="image-20230717193544637"></p><blockquote><h3 id="统计班级各个学员的成绩"><a href="#统计班级各个学员的成绩" class="headerlink" title="统计班级各个学员的成绩"></a>统计班级各个学员的成绩</h3><p>成绩 大于等于 85为优秀,成绩 大于等于 60为及格,否则为不及格</p></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717194503112.png" alt="image-20230717194503112"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717195128979.png" alt="image-20230717195128979"></p>]]></content>
<summary type="html"><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>【图解】</p>
<p><img src="https://mk-image-article.oss-cn-hangzhou.aliyunc</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
<entry>
<title>SQL</title>
<link href="http://example.com/posts/6632.html"/>
<id>http://example.com/posts/6632.html</id>
<published>2023-10-12T02:10:00.000Z</published>
<updated>2023-10-12T02:09:56.078Z</updated>
<content type="html"><![CDATA[<h2 id="一、SQL通用语法"><a href="#一、SQL通用语法" class="headerlink" title="一、SQL通用语法"></a>一、SQL通用语法</h2><ul><li>SQL语句单行或多行书写,以分号结尾</li><li>SQL语句可以使用空格/缩进来增强语句的可读性</li><li>MySQL数据库的SQL语句不区分大小写,关键字建议使用大写</li><li>注释<ul><li>单行注释:— 注释内容 或 # 注释内容(MySQL特有)</li><li>多行注释:/<em> 注释内容 </em>/ </li></ul></li></ul><h2 id="二、SQL分类"><a href="#二、SQL分类" class="headerlink" title="二、SQL分类"></a>二、SQL分类</h2><div class="table-container"><table><thead><tr><th style="text-align:center">分类</th><th style="text-align:center">全称</th><th style="text-align:center">说明</th></tr></thead><tbody><tr><td style="text-align:center">DDL</td><td style="text-align:center">Data Definition Language</td><td style="text-align:center">数据定义语言,用来定义数据库对象(数据库、表、字段)。常见语句有CREATE、ALTER和DROP等</td></tr><tr><td style="text-align:center">DML</td><td style="text-align:center">Data Manipulation Language</td><td style="text-align:center">数据操作语言,用于对数据库中表的数据记录进行操作,包括插入、更新和删除数据。常见语句有INSERT、UPDATE和DELETE等</td></tr><tr><td style="text-align:center">DQL</td><td style="text-align:center">Data Query Language</td><td style="text-align:center">数据查询语言,用于查询数据库中的数据。SELECT语句可以根据条件过滤数据、排序数据、分组数据等</td></tr><tr><td style="text-align:center">DCL</td><td style="text-align:center">Data Control Language</td><td style="text-align:center">数据控制语言,用于控制数据库的访问权限和安全性。常见语句有GRANT和REVOKE等</td></tr></tbody></table></div><h2 id="三、DDL"><a href="#三、DDL" class="headerlink" title="三、DDL"></a>三、DDL</h2><p><strong>查询</strong></p><ul><li>查询所有数据库:<code>SHOW DATABASES;</code></li><li>查询当前数据库:<code>SELECT DATABASE();</code></li></ul><p><strong>创建</strong></p><ul><li><code>CREATE DATABASE [IF NOT EXISTS] 数据库名 [DEFAULT CHARSET 字符集] [COLLATE 排序规则];</code></li></ul><p><strong>删除</strong></p><ul><li><code>DROP DATABASE [IF EXSITS] 数据库名;</code></li></ul><p><strong>使用</strong></p><ul><li><code>USE 数据库名;</code></li></ul><p><strong>查询当前数据库所有表</strong></p><ul><li><code>SHOW TABLES;</code></li></ul><p><strong>查询表结构</strong></p><ul><li><code>DESC 表名;</code></li></ul><p><strong>查询指定表的建表语句</strong></p><ul><li><code>SHOW CREATE TABLE 表名;</code></li></ul><p><strong>添加字段</strong></p><p><code>ALTER TABLE 表名 ADD 字段名 类型(长度)[COMMENT注释][约束]</code></p><p><strong>修改数据类型</strong></p><p><code>ALTER TABLE 表名 MODIFY 字段名 新数据类型(长度);</code></p><p><strong>修改字段名和字段类型</strong></p><p><code>ALTER TABLE 表名 CHANGE 旧字段名 新字段名 类型(长度)[COMMENT 注释][约束];</code> </p><p><strong>删除字段</strong></p><p><code>ALTER TABLE 表名 DROP 字段名;</code></p><p><strong>修改表名</strong></p><p><code>ALTER TABLE 表名 RENAME TO 新表名;</code></p><p><strong>删除表</strong></p><p><code>DROP TABLE[IF EXISTS] 表名;</code></p><p><strong>删除指定表并重新创建该表</strong></p><p><code>TRUNCATE TABLE 表名;</code></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></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> 表名(</span><br><span class="line">字段<span class="number">1</span> 字段<span class="number">1</span>类型[COMMENT 字段<span class="number">1</span>注释],</span><br><span class="line"> 字段<span class="number">2</span> 字段<span class="number">2</span>类型[COMMENT 字段<span class="number">2</span>注释],</span><br><span class="line"> 字段<span class="number">3</span> 字段<span class="number">3</span>类型[COMMENT 字段<span class="number">3</span>注释],</span><br><span class="line"> ...</span><br><span class="line"> 字段n 字段n类型[COMMENT 字段n注释]</span><br><span class="line">)[COMMENT 表注释];</span><br></pre></td></tr></table></figure><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230704202442658.png" alt="image-20230704202442658"></p><blockquote><h3 id="根据需求创建表(设计合理的数据类型、长度)"><a href="#根据需求创建表(设计合理的数据类型、长度)" class="headerlink" title="根据需求创建表(设计合理的数据类型、长度)"></a>根据需求创建表(设计合理的数据类型、长度)</h3><p>设计一张员工信息表,要求如下</p><ol><li>编号(纯数字)</li><li>员工工号(字符串类型,长度不超过10位)</li><li>员工姓名(字符串类型,长度不超过10位)</li><li>性别(男/女,存储一个汉字)</li><li>年龄(正常人年龄,不可能存储负数)</li><li>身份证号(二代身份证号均为18位,身份证中有X这样的字符)</li><li>入职时间(取值年月日即可)</li></ol></blockquote><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230712212739573.png" alt="image-20230712212739573"></p><blockquote><h3 id="表操作"><a href="#表操作" class="headerlink" title="表操作"></a>表操作</h3><ol><li>为emp增加一个新的字段”昵称”即nickname,类型为varchar(20)</li><li>将emp表的nickname字段修改为username,类型为varchar(30)</li><li>将emp表的字段username删除</li><li>将emp表的表名修改为employee</li></ol></blockquote><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230713212941479.png" alt="image-20230713212941479"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230713213445629.png" alt="image-20230713213445629"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230713213536879.png" alt="image-20230713213536879"></p><h3 id="小结"><a href="#小结" class="headerlink" title="小结"></a>小结</h3><p>分为数据库操作和表操作</p><h2 id="四、DML"><a href="#四、DML" class="headerlink" title="四、DML"></a>四、DML</h2><h3 id="添加数据(INSERT)"><a href="#添加数据(INSERT)" class="headerlink" title="添加数据(INSERT)"></a>添加数据(INSERT)</h3><p><strong>给指定字段添加数据</strong></p><p><code>INSERT INTO 表名(字段名1, 字段名2, ...) VALUES(值1,值2,...);</code></p><p><strong>给全部字段添加数据</strong></p><p><code>INSERT INTO 表名 VALUES(值1,值2,...);</code></p><p><strong>批量添加数据</strong></p><p><code>INSERT INTO 表名(字段名1,字段名2,...) VALUES(值1,值2,...),(值1,值2,...),(值1,值2,...);</code></p><p><code>INSERT INTO 表名 VALUES(值1,值2,...),(值1,值2,...),(值1,值2,...);</code></p><p><strong>注意:</strong></p><ul><li>插入数据时,指定的字段顺序需要与值的顺序是一一对应的</li><li>字符串和日期型数据应该包含在引号中</li><li>插入的数据大小,应该在字段的规定范围内</li></ul><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716120141997.png" alt="image-20230716120141997"></p><p>更改表的编码格式为utf8</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716121320050.png" alt="image-20230716121320050"></p><p>超出字段范围</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716121637293.png" alt="image-20230716121637293"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716122323370.png" alt="image-20230716122323370"></p><h3 id="修改数据(UPDATE)"><a href="#修改数据(UPDATE)" class="headerlink" title="修改数据(UPDATE)"></a>修改数据(UPDATE)</h3><p><code>UPDATE 表名 SET 字段名1=值1,字段名2=值2,...[WHERE 条件];</code></p><p><strong>注意:</strong> 修改语句的条件可以有,也可以没有,如果没有条件,则修改整张表的所有数据</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716151825688.png" alt="image-20230716151825688"></p><h3 id="删除数据(DELETE"><a href="#删除数据(DELETE" class="headerlink" title="删除数据(DELETE)"></a>删除数据(DELETE)</h3><p><code>DELETE FROM 表名[WHERE 条件];</code></p><p><strong>注意:</strong> DELETE语句的条件可以有,也可以没有,如果没有,则会删除整张表的所有数据;DELETE语句不能删除某一个字段的值(可以使用UPDATE)</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716152553280.png" alt="image-20230716152553280"></p><h2 id="五、DQL"><a href="#五、DQL" class="headerlink" title="五、DQL"></a>五、DQL</h2><p>【图解】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716155531060.png" alt="image-20230716155531060"></p><h3 id="基本查询"><a href="#基本查询" class="headerlink" title="基本查询"></a>基本查询</h3><p><strong>查询多个字段</strong></p><p><code>SELECT 字段1,字段2,字段3... FROM 表名;</code></p><p><code>SELECT * FROM 表名;</code></p><p><strong>设置别名</strong></p><p><code>SELECT 字段1 [AS 别名1],字段2 [AS 别名2]... FROM 表名;</code></p><p><strong>去除重复记录</strong></p><p><code>SELECT DISTINCT 字段列表 FROM 表名;</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716162627810.png" alt="image-20230716162627810"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716162854515.png" alt="image-20230716162854515"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716163259094.png" alt="image-20230716163259094"></p><p>查询去重</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716163601392.png" alt="image-20230716163601392"></p><h3 id="条件查询"><a href="#条件查询" class="headerlink" title="条件查询"></a>条件查询</h3><p><code>SELECT 字段列表 FROM 表名 WHERE 条件列表;</code></p><div class="table-container"><table><thead><tr><th>比较运算符</th><th>功能</th></tr></thead><tbody><tr><td>></td><td>大于</td></tr><tr><td>>=</td><td>大于等于</td></tr><tr><td><</td><td>小于</td></tr><tr><td><=</td><td>小于等于</td></tr><tr><td>=</td><td>等于</td></tr><tr><td><> 或 !=</td><td>不等于</td></tr><tr><td>BETWEEN … AND …</td><td>在某个范围之内(含最小、最大值)</td></tr><tr><td>IN(…)</td><td>在in之后的列表中的值,多选一</td></tr><tr><td>LIKE 占位符</td><td>模糊匹配(_匹配单个字符,%匹配任意个字符)</td></tr><tr><td>IS NULL</td><td>是NULL</td></tr></tbody></table></div><div class="table-container"><table><thead><tr><th>逻辑运算符</th><th>功能</th></tr></thead><tbody><tr><td>AND 或 &&</td><td>并且(多个条件同时成立)</td></tr><tr><td>OR 或 \</td><td>\</td><td></td><td>或者(多个条件任意一个成立)</td></tr><tr><td>NOT 或 !</td><td>非,不是</td></tr></tbody></table></div><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 条件查询</span></span><br><span class="line"><span class="comment">-- 1. 查询年龄等于 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="comment">-- 2. 查询年龄小于 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">-- 3. 查询年龄小于等于 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">-- 4.查询没有身份证号的员工信息</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">-- 5. 查询有身份证号的员工信息</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">not</span> <span class="keyword">null</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 6.查询年龄不等于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">-- 7. 查询年龄在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><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><br><span class="line"></span><br><span class="line"><span class="comment">-- 8. 查询性别为 女 且 年龄小于 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> sex <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">-- 9. 查询年龄等于18 或 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> 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">40</span>;</span><br><span class="line"></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">40</span>);</span><br><span class="line"></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 <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">-- 11. 查询身份证最后一位是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><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716170419800.png" alt="image-20230716170419800"></p><h3 id="聚合函数"><a href="#聚合函数" class="headerlink" title="聚合函数"></a>聚合函数</h3><p>聚合函数用于对数据进行计算和汇总</p><div class="table-container"><table><thead><tr><th>函数</th><th>功能</th><th>举例</th></tr></thead><tbody><tr><td>COUNT</td><td>计算指定列或行的数量</td><td>SELECT COUNT(*) FROM table_name;</td></tr><tr><td>SUM</td><td>计算指定列的总和</td><td>SELECT SUM(column_name) FROM table_name;</td></tr><tr><td>AVG</td><td>计算指定列的平均值</td><td>SELECT AVG(column_name) FROM table_name;</td></tr><tr><td>MAX</td><td>找到指定列的最大值</td><td>SELECT MAX(column_name) FROM table_name;</td></tr><tr><td>MIN</td><td>找到指定列的最小值</td><td>SELECT MIN(column_name) FROM table_name;</td></tr><tr><td>GROUP</td><td>将指定列的值连接成一个字符</td><td>SELECT GROUP_COUNT(column_name) FROM table_name;</td></tr></tbody></table></div><p><code>SELECT 聚会函数(字段列表) FROM 表名;</code></p><p><strong>注意:</strong> null不参与所有聚合函数运算</p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716172019259.png" alt="image-20230716172019259"></p><h3 id="分组查询"><a href="#分组查询" class="headerlink" title="分组查询"></a>分组查询</h3><p><code>SELECT 字段列表 FROM 表名 [WHERE 条件] GROUP BY 分组字段名 [HAVING 分组后过滤条件];</code></p><blockquote><p>where 与 having 区别</p><ol><li>执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤</li><li>判断条件不同:where不能对聚合函数进行判断,而having可以</li></ol></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716173358604.png" alt="image-20230716173358604"></p><p><strong>注意:</strong> 执行顺序:where > 聚合函数 > having,分组之后,查询的字段一般为聚合函数或分组字段,查询其他字段无任何意义</p><h3 id="排序查询"><a href="#排序查询" class="headerlink" title="排序查询"></a>排序查询</h3><p><code>SELECT 字段列表 FROM 表名 ORDER BY 字段1 排序方式, 字段2 排序方式2;</code></p><p><strong>说明:</strong> 默认为升序排序即ASC,也可以降序排序DESC</p><p><strong>注意:</strong> 如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716210231328.png" alt="image-20230716210231328"></p><h3 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h3><p><code>SELECT 字段列表 FROM 表名 LIMIT 起始索引,查询记录数;</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716210939381.png" alt="image-20230716210939381"></p><p><strong>注意:</strong> </p><ul><li><p>起始索引从0开始,起始索引 = (查询页码 - 1)* 每页显示记录数</p></li><li><p>分页查询是数据库的方言,不同的数据库有不同的实现,MySQL中是LIMIT</p></li><li>如果查询的是第一页数据,起始索引可以省略,直接简写为 limit 10</li></ul><h3 id="综合练习"><a href="#综合练习" class="headerlink" title="综合练习"></a>综合练习</h3><blockquote><h3 id="按要求完成如下DQL语句编写"><a href="#按要求完成如下DQL语句编写" class="headerlink" title="按要求完成如下DQL语句编写"></a>按要求完成如下DQL语句编写</h3><ol><li>查询年龄为20,21,22,23岁的员工信息</li><li>查询性别为男,且年龄在20-40岁(含)以内的姓名为三个字的员工</li><li>统计员工表中,年龄小于60岁的,男性员工和女性员工的人数</li><li>查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序</li><li>查询性别为男,且年龄再20-40岁(含)以内的前5个员工信息,对查询的结果按年龄升序排序,年龄相同的按入职时间升序排序</li></ol></blockquote><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716213636876.png" alt="image-20230716213636876"></p><p>【图解 编写顺序 - 执行顺序】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716214505861.png" alt="image-20230716214505861"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716215121693.png" alt="image-20230716215121693"></p><h2 id="六、DCL"><a href="#六、DCL" class="headerlink" title="六、DCL"></a>六、DCL</h2><h3 id="用户管理"><a href="#用户管理" class="headerlink" title="用户管理"></a>用户管理</h3><p><strong>查询用户</strong></p><p><code>USE mysql;SELECT * FROM user;</code></p><p><strong>创建用户</strong></p><p><code>CREATE USER '用户名'@'主机名' IDENTIFIED BY '密码';</code></p><p><strong>修改用户密码</strong></p><p><code>ALTER USER '用户名'@'主机名' IDENTIFIED WITH mysql_native_password BY '新密码';</code></p><p><strong>删除用户</strong></p><p><code>DROP USER '用户名'@'主机名';</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716221401550.png" alt="image-20230716221401550"></p><p><strong>注意:</strong> 主机名可以使用%通配,这类SQL开发人员操作的比较少,主要是DBA(Database Administrator 数据库管理员)使用</p><h3 id="权限控制"><a href="#权限控制" class="headerlink" title="权限控制"></a>权限控制</h3><p>MySQL定义了很多种权限,但常用的就以下几种:</p><div class="table-container"><table><thead><tr><th>权限</th><th>说明</th></tr></thead><tbody><tr><td>ALL,ALL PRIVILEGES</td><td>所有权限</td></tr><tr><td>SELECT</td><td>查询数据</td></tr><tr><td>INSERT</td><td>插入数据</td></tr><tr><td>UPDATE</td><td>修改数据</td></tr><tr><td>DELETE</td><td>删除数据</td></tr><tr><td>ALTER</td><td>修改表</td></tr><tr><td>DROP</td><td>删除数据库/表/视图</td></tr><tr><td>CREATE</td><td>创建数据库/表</td></tr></tbody></table></div><p>其他权限描述及含义,可直接参考官方文档:<a href="https://dev.mysql.com/doc">https://dev.mysql.com/doc</a>;5.7版官方文档:<a href="https://dev.mysql.com/doc/refman/5.7/en">https://dev.mysql.com/doc/refman/5.7/en</a>,8.0版官方文档:<a href="https://dev.mysql.com/doc/refman/8.0/en">https://dev.mysql.com/doc/refman/8.0/en</a></p><p><strong>查询权限</strong></p><p><code>SHOW GRANTS FOR '用户名'@'主机名';</code></p><p><strong>授予权限</strong></p><p><code>GRANT 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';</code></p><p><strong>撤销权限</strong></p><p><code>REVOKE 权限列表 ON 数据库名.表名 FROM '用户名'@'主机名';</code></p><p>【实践图】</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717152542625.png" alt="image-20230717152542625"></p><p>授予权限报错</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717154632394.png" alt="image-20230717154632394"></p><p><strong>排错过程</strong></p><p>第一步 将授权的权限打开,然后重新加载并刷新权限表</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717161705785.png" alt="image-20230717161705785"></p><p>第二步 重启mysql服务,以管理员身份打开命令行运行</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717161802317.png" alt="image-20230717161802317"></p><p>第三步 重新执行授权语句</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717161935610.png" alt="image-20230717161935610"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717162132530.png" alt="image-20230717162132530"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717162253361.png" alt="image-20230717162253361"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230717162345655.png" alt="image-20230717162345655"></p><p><strong>注意:</strong> 多个权限之间使用逗号分隔;授权时,数据库名和表名可以使用*进行通配,代表所有</p><h3 id="小结-1"><a href="#小结-1" class="headerlink" title="小结"></a>小结</h3><p>用户管理:主要用来管理哪些用户可以访问mysql数据库</p><p>权限控制:用来控制当前用户连接上mysql之后可以访问哪些数据库哪些表,可以对数据库对象(数据库、表、字段)执行什么样的操作</p><h2 id="七、数据类型"><a href="#七、数据类型" class="headerlink" title="七、数据类型"></a>七、数据类型</h2><ul><li>MySQL中有多种数据类型可用于定义表中的列,主要包括以下五大类:<ul><li>整数类型:TINYINT、INT、BIGINT</li><li>浮点数类型:FLOAT、DOUBLE、DECIMAL</li><li>字符串类型:CHAR、VARCHAR、TEXT</li><li>日期类型:DATE、TIME、DATETIME、TIMESTAMP</li><li>其他类型:BOOLEAN、BLOB</li></ul></li></ul><div class="table-container"><table><thead><tr><th>类型</th><th>大小</th><th>有符合(SIGNED)范围</th><th>无符号(UNSIGNED)范围</th><th>描述</th></tr></thead><tbody><tr><td>TINYINT</td><td>1 byte</td><td>(-128,127)</td><td>(0,255)</td><td>小整数值</td></tr><tr><td>INT</td><td>4 bytes</td><td>(-2^31,2^31-1)</td><td>(0,2^32-1)</td><td>整数值</td></tr><tr><td>BIGINT</td><td>8 bytes</td><td>(-2^63,2^63-1)</td><td>(0,2^64-1)</td><td>极大整数值</td></tr><tr><td>FLOAT</td><td>4 bytes</td><td></td><td></td><td>单精度浮点数</td></tr><tr><td>DOUBLE</td><td>8 bytes</td><td></td><td></td><td>双精度浮点数</td></tr><tr><td>DECIMAL</td><td></td><td>依赖于M(精度)和D(标度)的值</td><td>依赖于M(精度)和D(标度)的值</td><td>小数值(精确定点数)</td></tr><tr><td>CHAR</td><td>0-255 bytes</td><td></td><td></td><td>定长字符串</td></tr><tr><td>VARCHAR</td><td>0-65535 bytes</td><td></td><td></td><td>变长字符串</td></tr><tr><td>TEXT</td><td>0-65535 bytes</td><td></td><td></td><td>长文本数据</td></tr></tbody></table></div><p>注:CHAR(1)数据类型表示一个固定长度为1个字符的字符串</p><div class="table-container"><table><thead><tr><th>类型</th><th>大小</th><th>范围</th><th>格式</th><th>描述</th></tr></thead><tbody><tr><td>DATE</td><td>3 bytes</td><td>1000-01-01 至 9999-12-31</td><td>YYYY-MM-DD</td><td>日期值</td></tr><tr><td>TIME</td><td>3 bytes</td><td>-838:59:59 至 838:59:59</td><td>HH:MM:SS</td><td>时间值或持续时间</td></tr><tr><td>DATETIME</td><td>8 bytes</td><td>1000-01-01 00:00:00 至 9999-12-31 23:59:59</td><td>YYYY-MM-DD HH:MM:SS</td><td>混合日期和时间值</td></tr><tr><td>TIMESTAMP</td><td>4 bytes</td><td>1970-01-01 00:00:01 至 2038-01-19 03:14:07</td><td>YYYY-MM-DD HH:MM:SS</td><td>混合日期和时间值,时间戳</td></tr><tr><td>BLOB</td><td>0-65535 bytes</td><td></td><td></td><td>二进制字符串,可以用于存储图像、音频、视频等任意二进制数据</td></tr></tbody></table></div><blockquote><p>在MySQL中并没有布尔数据类型,即不支持boolean类型,因而采用TINYINT(1)或ENUM(‘0’, ‘1’)来表示布尔值,其中1对应true,0则对应false</p></blockquote><h2 id="八、MySQL图形化界面工具"><a href="#八、MySQL图形化界面工具" class="headerlink" title="八、MySQL图形化界面工具"></a>八、MySQL图形化界面工具</h2><p>之前一直使用Navicat作为MySQL的图形化界面工具,而黑马课程主要以DataGrip作为MySQL图形化界面工具作为演示,<u>正好借这次机会熟悉下DataGrip的使用,毕竟技多不压身</u></p><h3 id="DataGrip简介"><a href="#DataGrip简介" class="headerlink" title="DataGrip简介"></a>DataGrip简介</h3><blockquote><p>DataGrip是一种功能强大的数据库集成开发环境(IDE),由JetBrains公司开发。旨在提供一个统一的界面,用于管理和开发各种类型的数据库,包括关系型数据库(如MySQL、PostgreSQL、Oracle、SQL Server等)和NoSQL数据库(如MongoDB、Cassandra等)。</p><p>DataGrip具有许多强大的功能,使得数据库开发变得更加高效和方便。以下是一些主要特点:</p><ol><li><p>多数据库支持:DataGrip支持多种类型的数据库,使得开发人员可以在一个工具中管理和查询不同类型的数据库。</p></li><li><p>数据库导航:DataGrip提供了一个直观的界面,用于浏览和导航数据库对象,如表、视图、存储过程等。开发人员可以快速找到并编辑所需的数据库对象。</p></li><li><p>SQL编辑器:DataGrip内置了一个强大的SQL编辑器,具有语法高亮、代码补全、代码导航等功能。开发人员可以轻松地编写和调试复杂的SQL查询。</p></li><li><p>数据库管理:DataGrip提供了一套全面的数据库管理工具,包括表格和索引的创建和修改、数据导入和导出、数据库备份和恢复等。</p></li><li><p>数据查询和可视化:DataGrip支持高级的数据查询和可视化功能,可以轻松地执行复杂的查询操作,并将结果以表格、图表等形式展示。</p></li><li><p>版本控制集成:DataGrip集成了常用的版本控制系统,如Git,使得开发人员可以方便地管理和跟踪数据库脚本的变更。</p></li></ol><p>总的来说,DataGrip是一个功能强大的数据库开发工具,可以帮助开发人员更高效地管理和开发各种类型的数据库。无论是初学者还是经验丰富的数据库开发人员,都可以从DataGrip的各种功能和工具中受益。</p></blockquote><h3 id="DataGrip安装及激活"><a href="#DataGrip安装及激活" class="headerlink" title="DataGrip安装及激活"></a>DataGrip安装及激活</h3><p>官方最新版本: <a href="https://www.jetbrains.com/datagrip/download/#section=windows">https://www.jetbrains.com/datagrip/download/#section=windows</a></p><p>官方其他版本:<a href="https://www.jetbrains.com/datagrip/download/other.html">https://www.jetbrains.com/datagrip/download/other.html</a></p><p><strong>第一步 安装</strong></p><p>这里选择2022.2.1版本进行安装使用</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714220423682.png" alt="image-20230714220423682"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714212000179.png" alt="image-20230714212000179"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714220906265.png" alt="image-20230714220906265"></p><p><strong>第二步 激活</strong></p><p>双击运行脚本install-all-users.vbs</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714222615375.png" alt="image-20230714222615375"></p><p>会提示安装补丁需要等待数秒。点击【确定】按钮后,过程大概 10 - 30 秒,如看到弹框提示 <code>Done</code> 时,表示激活破解成功</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714221601567.png" alt="image-20230714221601567"></p><p>启动JetBrains产品,许可证激活选择激活码方式(Activate code),获取激活码<a href="https://3.jetbra.in">https://3.jetbra.in</a> ,可用站点需要具备使用Google搜索引擎的网络</p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714222927954.png" alt="image-20230714222927954"></p><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714223138876.png" alt="image-20230714223138876"></p><h3 id="DataGrip初使用"><a href="#DataGrip初使用" class="headerlink" title="DataGrip初使用"></a>DataGrip初使用</h3><p> <img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714223417991.png" alt="image-20230714223417991"></p><p>默认新建项目放在C盘,所以这里设置文件路径<code>D:\DataGrip\DataGripProjects\mysql-base</code></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714224059661.png" alt="image-20230714224059661"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714224139485.png" alt="image-20230714224139485"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714224237640.png" alt="image-20230714224237640"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714231011781.png" alt="image-20230714231011781"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230714231036560.png" alt="image-20230714231036560"></p><p>删除无用数据库,然后创建数据库创建表并修改表,部分效果图如下</p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716113942561.png" alt="image-20230716113942561"></p><p><img src="https://mk-image-article.oss-cn-hangzhou.aliyuncs.com/img/image-20230716114050425.png" alt="image-20230716114050425"></p>]]></content>
<summary type="html"><h2 id="一、SQL通用语法"><a href="#一、SQL通用语法" class="headerlink" title="一、SQL通用语法"></a>一、SQL通用语法</h2><ul>
<li>SQL语句单行或多行书写,以分号结尾</li>
<li>SQL语句可以使</summary>
<category term="MySQL" scheme="http://example.com/categories/MySQL/"/>
</entry>
</feed>