-
Notifications
You must be signed in to change notification settings - Fork 0
/
atom.xml
496 lines (248 loc) · 334 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>萌傀儡</title>
<subtitle>珍惜现在,趁早摸鱼!</subtitle>
<link href="https://blhorizon.github.io/atom.xml" rel="self"/>
<link href="https://blhorizon.github.io/"/>
<updated>2024-05-02T18:04:20.590Z</updated>
<id>https://blhorizon.github.io/</id>
<author>
<name>萌傀儡</name>
</author>
<generator uri="https://hexo.io/">Hexo</generator>
<entry>
<title>Mysql数据库基础(六)</title>
<link href="https://blhorizon.github.io/posts/aabb.html"/>
<id>https://blhorizon.github.io/posts/aabb.html</id>
<published>2024-05-02T16:52:07.000Z</published>
<updated>2024-05-02T18:04:20.590Z</updated>
<content type="html"><![CDATA[<h2 id="事务概述"><a href="#事务概述" class="headerlink" title="事务概述"></a>事务概述</h2><p>事务是<strong>一组操作的集合</strong>,它是一个不可分割的工作单位。事务会把所有的操作作为一个整体一起向系统提交或撤消操作请求,即这些操作<strong>要么同时完成,要么同时失败</strong></p><p>Mysql的事务是默认<strong>自动提交</strong>的,即执行DML语句时就会<strong>立即</strong>隐式的提交事务</p><h2 id="事务操作"><a href="#事务操作" class="headerlink" title="事务操作"></a>事务操作</h2><p>先创建 account表,并插入数据:</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> account(</span><br><span class="line">id <span class="type">int</span> auto_increment <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'主键ID'</span>,</span><br><span class="line">name <span class="type">VARCHAR</span>(<span class="number">10</span>) COMMENT <span class="string">'姓名'</span>,</span><br><span class="line">money <span class="type">int</span> COMMENT <span class="string">'余额'</span></span><br><span class="line">)COMMENT <span class="string">'账户表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> account <span class="keyword">VALUES</span>(<span class="keyword">NULL</span>,<span class="string">'张三'</span>,<span class="number">2000</span>),(<span class="keyword">NULL</span>,<span class="string">'李四'</span>,<span class="number">2000</span>);</span><br></pre></td></tr></table></figure><p>因为Mysql是<strong>立即自动提交</strong>DML语句的,所以我们可以将这个自动更改为<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="comment">-- 查看当前提交方式</span></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="comment">-- 更改提交方式为手动</span></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><br></pre></td></tr></table></figure><p>将其更改为手动后,执行DML语句是不会提交到数据库的,这时可以执行下面两个SQL语句来操作事务:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><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><p>一般大部分情况下,我们都不会更改Mysql的提交方式的,这时我看可以通过该SQL语句来<strong>开启事务</strong>:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">START</span> TRANSACTION 或 <span class="keyword">BEGIN</span>;</span><br></pre></td></tr></table></figure><h2 id="四大特性"><a href="#四大特性" class="headerlink" title="四大特性"></a>四大特性</h2><p>如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性(<strong>ACID</strong>):</p><ul><li>原子性(Atomicity):指事务包含的所有操作要么全部成功,要么全部失败回滚,因此事务的操作如果成功就必须要完全应用到数据库,如果操作失败则不能对数据库有任何影响</li><li>一致性(Consistency):指事务必须使数据库从一个一致性状态变换到另一个一致性状态,也就是说一个事务执行之前和执行之后都必须处于一致性状态<blockquote><p>拿转账来说,假设用户A和用户B两者的钱加起来一共是5000,那么不管A和B之间如何转账,转几次账,事务结束后两个用户的钱相加起来应该还得是5000,这就是事务的一致性</p></blockquote></li><li>隔离性(Isolation):当多个用户并发访问数据库时,比如操作同一张表时,数据库为每一个用户开启的事务,不能被其他事务的操作所干扰,多个并发事务之间要相互隔离<blockquote><p>要达到这么一种效果:对于任意两个并发的事务T1和T2,在事务T1看来,T2要么在T1开始之前就已经结束,要么在T1结束之后才开始,这样每个事务都感觉不到有其他事务在并发地执行</p></blockquote></li><li>持久性(Durability):指一个事务一旦被提交了,那么对数据库中的数据的改变就是永久性的,即便是在数据库系统遇到故障的情况下也不会丢失提交事务的操作<blockquote><p>如我们在使用JDBC操作数据库时,在提交事务方法后,提示用户事务操作完成,当我们程序执行完成直到看到提示后,就可以认定事务以及正确提交,即使这时候数据库出现了问题,也必须要将我们的事务完全执行完成,否则就会造成我们看到提示事务处理完毕,但是数据库因为故障而没有执行事务的重大错误</p></blockquote></li></ul><h2 id="并发事务"><a href="#并发事务" class="headerlink" title="并发事务"></a>并发事务</h2><p>总的说,数据库事务无非就两种:读取事务(select)、修改事务(update,insert)。在没有事务隔离控制的时候,多个事务在同一时刻对同一数据的操作可能就会影响到最终期望的结果,通常有四种情况:</p><ol><li>两个更新事务同时修改一条数据时,很显然这种情况是最严重的了,程序中无论如何也不能出现这种情况,因为它会造成更新的丢失</li><li>一个更新事务更新一条数据时,另一个读取事务读取了还没提交的更新,这种情况下会出现读取到脏数据</li><li>一个读取事务读取一条数据时,另一个更新事务修改了这条数据,这时就会出现不可重现的读取</li><li>一个读取事务读取时,另一个插入事务(注意此处时插入)插入了一条新数据,这样就可能多读出一条数据,出现幻读</li></ol><p>以上四种情况描述完毕,相信大家也发现规律了,前三种是对同一条数据的并发操作,对程序的结果可能产生致命影响,尤其是金融等实时性,准确性要求极高的系统,绝不容许这三中情况的出现,相比第四种情况不会影响数据的真实性,在很多情况下是允许的,如社交论坛等实时性要求不高的系统</p><p>综上四个情况,我们可以大致这样简单的理解(最初说的两种事务的自由组合2*2=4):</p><ol><li>修改时允许修改(丢失更新)</li><li>修改时允许读取(脏读)</li><li>读取时允许修改(不可重复读)</li><li>读取时允许插入(幻读)</li></ol><p>从上到下问题越来越不严重,但所需的性能开销却越大。因为不同的系统允许不同级别的情况,所以就出现了事务隔离这么一个东东,来允许我们设定数据库的并发行为</p><p>总结下如果不考虑事务的隔离性,会发生的几种问题:</p><ol><li><p>脏读:指在一个事务处理过程里读取了另一个未提交的事务中的数据</p><blockquote><p>当一个事务正在多次修改某个数据,而在这个事务中这多次的修改都还未提交,这时一个并发的事务来访问该数据,就会造成两个事务得到的数据不一致<br>例如:用户A向用户B转账100元,对应SQL命令如下:<br> update account set money=money+100 where name=’B’; (此时A通知B)<br> update account set money=money - 100 where name=’A’;<br>当只执行第一条SQL时,A通知B查看账户,B发现确实钱已到账(此时即发生了脏读),而之后无论第二条SQL是否执行,只要该事务不提交,则所有操作都将回滚,那么当B以后再次查看账户时就会发现钱其实并没有转</p></blockquote></li><li><p>不可重复读:指在对于数据库中的某个数据,一个事务范围内多次查询却返回了不同的数据值,这是由于在查询间隔,被另一个事务修改并提交了</p><blockquote><p>例如事务T1在读取某一数据,而事务T2立马修改了这个数据并且提交事务给数据库,事务T1再次读取该数据就得到了不同的结果,发送了不可重复读<br>不可重复读和脏读的区别是,脏读是某一事务读取了另一个事务未提交的脏数据,而不可重复读则是读取了前一事务提交的数据<br>在某些情况下,不可重复读并不是问题,比如我们多次查询某个数据当然以最后查询得到的结果为主。但在另一些情况下就有可能发生问题,例如对于同一个数据A和B依次查询就可能不同,A和B就可能打起来了……</p></blockquote></li><li><p>虚读(幻读):事务非独立执行时发生的一种现象</p><blockquote><p>例如事务T1对一个表中所有的行的某个数据项做了从“1”修改为“2”的操作,这时事务T2又对这个表中插入了一行数据项,而这个数据项的数值还是为“1”并且提交给数据库。而操作事务T1的用户如果再查看刚刚修改的数据,会发现还有一行没有修改,其实这行是从事务T2中添加的,就好像产生幻觉一样,这就是发生了幻读<br>幻读和不可重复读都是读取了另一条已经提交的事务(这点就脏读不同),所不同的是不可重复读查询的都是同一个数据项,而幻读针对的是一批数据整体(比如数据的个数)</p></blockquote></li></ol><h2 id="隔离级别"><a href="#隔离级别" class="headerlink" title="隔离级别"></a>隔离级别</h2><p>SQL标准定义了4类隔离级别,包括了一些具体规则,用来限定事务内外的哪些改变是可见的,哪些是不可见的。低级别的隔离级一般支持更高的并发处理,并拥有更低的系统开销</p><ol><li><p>Read Uncommitted(读取未提交内容):在该隔离级别,所有事务都可以看到其他未提交事务的执行结果</p><blockquote><p>本隔离级别很少用于实际应用,因为它的性能也不比其他级别好多少。读取未提交的数据,也被称之为脏读(Dirty Read)</p></blockquote></li><li><p>Read Committed(读取提交内容): 这是大多数数据库系统的默认隔离级别(但不是MySQL默认的)</p><blockquote><p>它满足了隔离的简单定义:一个事务只能看见已经提交事务所做的改变。这种隔离级别 也支持所谓的不可重复读(Nonrepeatable Read),因为同一事务的其他实例在该实例处理其间可能会有新的commit,所以同一select可能返回不同结果</p></blockquote></li><li><p>Repeatable Read(可重读):这是MySQL的默认事务隔离级别,它确保同一事务的多个实例在并发读取数据时,会看到同样的数据行</p><blockquote><p>不过理论上,这会导致另一个棘手的问题:幻读 (Phantom Read)。简单的说,幻读指当用户读取某一范围的数据行时,另一个事务又在该范围内插入了新行,当用户再读取该范围的数据行时,会发现有新的“幻影” 行<br>InnoDB和Falcon存储引擎通过多版本并发控制(MVCC,Multiversion Concurrency Control)机制解决了该问题</p></blockquote></li><li><p>Serializable(可串行化):这是最高的隔离级别,它通过强制事务排序,使之不可能相互冲突,从而解决幻读问题。简言之,它是在每个读的数据行上加上共享锁。在这个级别,可能导致大量的超时现象和锁竞争</p><blockquote><p> 这四种隔离级别采取不同的锁类型来实现,若读取的是同一个数据的话,就容易发生问题。例如:</p><ul><li>脏读(Drity Read):某个事务已更新一份数据,另一个事务在此时读取了同一份数据,由于某些原因,前一个RollBack了操作,则后一个事务所读取的数据就会是不正确的</li><li>不可重复读(Non-repeatable read):在一个事务的两次查询之中数据不一致,这可能是两次查询过程中间插入了一个事务更新的原有的数据</li><li>幻读(Phantom Read):在一个事务的两次查询中数据笔数不一致,例如有一个事务查询了几列(Row)数据,而另一个事务却在此时插入了新的几列数据,先前的事务在接下来的查询中,就会发现有几列数据是它先前所没有的</li></ul></blockquote></li></ol><p><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-03_01-54-46.webp"><br>以上四种隔离级别最高的是 Serializable 级别,最低的是 Read uncommitted 级别,当然<strong>级别越高,执行效率就越低,数据越安全</strong>。像 Serializable 这样的级别,就是以锁表的方式(类似于Java多线程中的锁)使得其他的线程只能在锁外等待,所以平时<strong>选用何种隔离级别应该根据实际情况</strong>。在<strong>MySQL数据库中默认的隔离级别为Repeatable read (可重复读)</strong></p><blockquote><p>在 Oracle 数据库中,只支持 Serializable (串行化)级别和 Read committed (读已提交)这两种级别,其中默认的为 Read committed 级别</p></blockquote><p>在MySQL数据库中查看当前事务的隔离级别: <strong>select @@tx_isolation;</strong></p><p>在MySQL数据库中设置事务的隔离 级别:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> [glogal <span class="operator">|</span> session] transaction isolation level 隔离级别名称;</span><br><span class="line"></span><br><span class="line"><span class="keyword">set</span> tx_isolation<span class="operator">=</span>’隔离级别名称;’</span><br></pre></td></tr></table></figure><p>注意:<strong>设置数据库的隔离级别一定要是在开启事务之前!</strong></p><p>隔离级别的设置只对当前链接有效。对于使用MySQL命令窗口而言,一个窗口就相当于一个链接,当前窗口设置的隔离级别只对当前窗口中的事务有效</p>]]></content>
<summary type="html">事务</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Mysql数据库基础" scheme="https://blhorizon.github.io/tags/Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Mysql数据库基础(五)</title>
<link href="https://blhorizon.github.io/posts/e2a2.html"/>
<id>https://blhorizon.github.io/posts/e2a2.html</id>
<published>2024-05-02T04:01:22.000Z</published>
<updated>2024-05-02T16:50:40.005Z</updated>
<content type="html"><![CDATA[<h2 id="多表关系"><a href="#多表关系" class="headerlink" title="多表关系"></a>多表关系</h2><p>在项目开发进行数据库结构设计时,根据业务需求和业务模块之间的关系进行分享并设计表的结构,而由于业务之间是相互关联的,所以各个表结构之间也存在着各种联系,主要分为 <strong>一对多(多对一)</strong>、<strong>多对多</strong>、<strong>一对一</strong> 这三种</p><h3 id="一对多(多对一)"><a href="#一对多(多对一)" class="headerlink" title="一对多(多对一)"></a>一对多(多对一)</h3><p>例如部门与员工的关系:一个部门可以对应多个员工,而一个员工则对应一个部门,这种可以通过<strong>在多的一方建立外键,指向一的一方的主键</strong></p><h3 id="多对多"><a href="#多对多" class="headerlink" title="多对多"></a>多对多</h3><p>例如学生与课程的关系:一个学生可以选修多门课程,而一个课程可以被多名学生选修,这种可以通过<strong>建立第三张中间表,该表至少包含两个外键,分别关联两方主键</strong></p><p>创建学生表并插入数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><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="keyword">CREATE</span> <span class="keyword">TABLE</span> student (</span><br><span class="line"> id <span class="type">INT</span> auto_increment <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'主键ID'</span>,</span><br><span class="line"> name <span class="type">VARCHAR</span> ( <span class="number">10</span> ) COMMENT <span class="string">'姓名'</span>,</span><br><span class="line"> <span class="keyword">no</span> <span class="type">VARCHAR</span> ( <span class="number">10</span> ) COMMENT <span class="string">'学号'</span> </span><br><span class="line">)COMMENT <span class="string">' 学生表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student</span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>,<span class="string">'黛绮丝'</span>,<span class="string">'2000100101'</span>),(<span class="keyword">null</span>,<span class="string">'谢逊'</span>,<span class="string">'2000100102'</span>),(<span class="keyword">null</span>,<span class="string">'殷天正'</span>,<span class="string">'2000100103'</span>),(<span class="keyword">null</span>,<span class="string">'韦一笑'</span>,<span class="string">'2000100104'</span>);</span><br></pre></td></tr></table></figure><p>创建课程表并插入数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> course(</span><br><span class="line">id <span class="type">int</span> auto_increment <span class="keyword">primary</span> key comment <span class="string">'主键ID'</span>,</span><br><span class="line">name <span class="type">varchar</span>(<span class="number">10</span>) comment <span class="string">'课程名称'</span></span><br><span class="line">)comment <span class="string">'课程表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course </span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>,<span class="string">'Java'</span>),(<span class="keyword">null</span>,<span class="string">'PHP'</span>),(<span class="keyword">null</span>,<span class="string">'Mysql'</span>),(<span class="keyword">null</span>,<span class="string">'Hadoop'</span>);</span><br></pre></td></tr></table></figure><p>上述两张表没有关联,可以通过<strong>中间表</strong>进行关联:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></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> auto_increment comment <span class="string">'主键'</span> <span class="keyword">primary</span> key,</span><br><span class="line">studentid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'学生ID'</span>,</span><br><span class="line">courseid <span class="type">int</span> <span class="keyword">not</span> <span class="keyword">null</span> comment <span class="string">'课程ID'</span>,</span><br><span class="line"><span class="keyword">constraint</span> fk_courseid <span class="keyword">foreign</span> key (courseid) <span class="keyword">references</span> course(id),</span><br><span class="line"><span class="keyword">constraint</span> fk_studentid <span class="keyword">foreign</span> key (studentid) <span class="keyword">references</span> student(id)</span><br><span class="line">)comment <span class="string">'学生课程中间表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student_course </span><br><span class="line"><span class="keyword">values</span> (<span class="keyword">null</span>,<span class="number">1</span>,<span class="number">1</span>),(<span class="keyword">null</span>,<span class="number">1</span>,<span class="number">2</span>),(<span class="keyword">null</span>,<span class="number">1</span>,<span class="number">3</span>),(<span class="keyword">null</span>,<span class="number">2</span>,<span class="number">3</span>),(<span class="keyword">null</span>,<span class="number">3</span>,<span class="number">4</span>);</span><br></pre></td></tr></table></figure><h3 id="一对一"><a href="#一对一" class="headerlink" title="一对一"></a>一对一</h3><p>例如用户与用户详情的关系。一对一的关系多用于<strong>单表拆分</strong>,将一张表的基础字段放在一张表中,其他详情字段则放在另一张表中,用以提升操作效率</p><p>这种关系跟一对多(多对一)的处理类似,区别在于该<strong>外键是唯一(UNIQUE)</strong>的</p><p>创建用户基本信息表并插入数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><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">CREATE</span> <span class="keyword">TABLE</span> tb_user (</span><br><span class="line">id <span class="type">INT</span> AUTO_INCREMENT <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'主键ID'</span>,</span><br><span class="line">name <span class="type">VARCHAR</span>(<span class="number">10</span>) COMMENT <span class="string">'姓名'</span>,</span><br><span class="line">age <span class="type">INT</span> COMMENT <span class="string">'年龄'</span>,</span><br><span class="line">gender <span class="type">CHAR</span>(<span class="number">1</span>) COMMENT <span class="string">'1: 男,2: 女'</span>,</span><br><span class="line">phone <span class="type">CHAR</span>(<span class="number">11</span>) COMMENT <span class="string">'手机号'</span></span><br><span class="line">) COMMENT <span class="string">'用户基本信息表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> tb_user(id, name, age, gender, phone) </span><br><span class="line"><span class="keyword">VALUES</span>(<span class="keyword">null</span>, <span class="string">'黄渤'</span>, <span class="number">45</span>, <span class="string">'1'</span>, <span class="string">'18800001111'</span>),</span><br><span class="line">(<span class="keyword">null</span>, <span class="string">'冰冰'</span>, <span class="number">352</span>, <span class="string">'1'</span>, <span class="string">'18800002222'</span>),</span><br><span class="line">(<span class="keyword">null</span>, <span class="string">'码云'</span>, <span class="number">55</span>, <span class="string">'1'</span>, <span class="string">'18800008888'</span>),</span><br><span class="line">(<span class="keyword">null</span>, <span class="string">'李彦宏'</span>, <span class="number">50</span>, <span class="string">'1'</span>, <span class="string">'18800009999'</span>);</span><br></pre></td></tr></table></figure><p>创建用户详情表并插入数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><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">TABLE</span> tb_user_edu(</span><br><span class="line">id <span class="type">INT</span> AUTO_INCREMENT <span class="keyword">PRIMARY</span> KEY COMMENT <span class="string">'主键ID'</span>,</span><br><span class="line">degree <span class="type">VARCHAR</span>(<span class="number">20</span>) COMMENT <span class="string">'学历'</span>,</span><br><span class="line">major <span class="type">VARCHAR</span>(<span class="number">50</span>) COMMENT <span class="string">'专业'</span>,</span><br><span class="line">primaryschool <span class="type">VARCHAR</span>(<span class="number">50</span>) COMMENT <span class="string">'小学'</span>,</span><br><span class="line">middleschool <span class="type">VARCHAR</span>(<span class="number">50</span>) COMMENT <span class="string">'中学'</span>,</span><br><span class="line">university <span class="type">VARCHAR</span>(<span class="number">50</span>) COMMENT <span class="string">'大学'</span>,</span><br><span class="line">userid <span class="type">INT</span> <span class="keyword">UNIQUE</span> COMMENT <span class="string">'用户ID'</span>,</span><br><span class="line"><span class="keyword">CONSTRAINT</span> fk_userid <span class="keyword">FOREIGN</span> KEY (userid) <span class="keyword">REFERENCES</span> tb_user(id)</span><br><span class="line">) COMMENT <span class="string">'用户教育信息表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> tb_user_edu(id, degree, major, primaryschool, middleschool, university, userid) </span><br><span class="line"><span class="keyword">VALUES</span>(<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'舞蹈'</span>, <span class="string">'静安区第一小学'</span>, <span class="string">'静安区第一中学'</span>, <span class="string">'北京舞蹈学院'</span>, <span class="number">1</span>),</span><br><span class="line">(<span class="keyword">null</span>, <span class="string">'硕士'</span>, <span class="string">'表演'</span>, <span class="string">'朝阳区第一小学'</span>, <span class="string">'朝阳区第一中学'</span>, <span class="string">'北京电影学院'</span>, <span class="number">2</span>),</span><br><span class="line">(<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'英语'</span>, <span class="string">'杭州市第一小学'</span>, <span class="string">'杭州市第一中学'</span>, <span class="string">'杭州师范大学'</span>, <span class="number">3</span>),</span><br><span class="line">(<span class="keyword">null</span>, <span class="string">'本科'</span>, <span class="string">'应用数学'</span>, <span class="string">'阳泉第一小学'</span>, <span class="string">'阳泉区第一中学'</span>, <span class="string">'清华大学'</span>, <span class="number">4</span>);</span><br></pre></td></tr></table></figure><p>可以看出对 userid 使用了唯一 UNIQUE</p><h2 id="多表查询"><a href="#多表查询" class="headerlink" title="多表查询"></a>多表查询</h2><p>从多张表中查询数据。按照 <strong>SELECT * FROM A表,B表</strong> 来进行查询会造成<strong>数据冗余</strong>,即笛卡尔积。很明显,这种数据并不是我们所需要的,所以在进行多表查询时我们需要消除无效的笛卡尔积</p><blockquote><p>笛卡尔积:两个集合的所有组合情况,即A集合有2条数据,B集合有3条数据,那么总共会形成6条数据</p></blockquote><p>多表查询主要分为 <strong>连接查询</strong> 和 <strong>子查询</strong></p><ul><li>连接查询分为 <strong>内连接</strong>、<strong>外连接</strong>、<strong>自连接</strong></li><li>子查询,又被称为嵌套查询,分为 <strong>标量子查询</strong>、<strong>列子查询</strong>、<strong>行子查询</strong>、<strong>表子查询</strong></li></ul><h2 id="连接查询"><a href="#连接查询" class="headerlink" title="连接查询"></a>连接查询</h2><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><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 隐式内连接</span></span><br><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表<span class="number">1</span>,表<span class="number">2</span> <span class="keyword">WHERE</span> 条件;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显式内连接</span></span><br><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表<span class="number">1</span> [<span class="keyword">INNER</span>] <span class="keyword">JOIN</span> 表<span class="number">2</span> <span class="keyword">ON</span> 连接条件;</span><br></pre></td></tr></table></figure><p>查询员工所对应的部门:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 隐式内连接</span></span><br><span class="line"><span class="keyword">SELECT</span> emp.name <span class="keyword">as</span> <span class="string">'员工'</span> , dept.name <span class="keyword">as</span> <span class="string">'部门'</span> <span class="keyword">FROM</span> emp,dept <span class="keyword">WHERE</span> emp.dept_id <span class="operator">=</span> dept.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 显式内连接</span></span><br><span class="line"><span class="keyword">SELECT</span> emp.name <span class="keyword">as</span> <span class="string">'员工'</span> , dept.name <span class="keyword">as</span> <span class="string">'部门'</span> <span class="keyword">FROM</span> emp <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept <span class="keyword">ON</span> emp.dept_id <span class="operator">=</span> dept.id;</span><br></pre></td></tr></table></figure><h3 id="外连接"><a href="#外连接" class="headerlink" title="外连接"></a>外连接</h3><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 左外连接:表1数据 + 表1和表2交集</span></span><br><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表<span class="number">1</span> <span class="keyword">LEFT</span> [<span class="keyword">OUTER</span>] <span class="keyword">JOIN</span> 表<span class="number">2</span> <span class="keyword">ON</span> 条件;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 右外连接:表2数据 + 表1和表2交集</span></span><br><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表<span class="number">1</span> <span class="keyword">RIGHT</span> [<span class="keyword">OUTER</span>] <span class="keyword">JOIN</span> 表<span class="number">2</span> <span class="keyword">ON</span> 条件;</span><br></pre></td></tr></table></figure><p>练习:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- emp表的所有数据和其对应的部门信息</span></span><br><span class="line"><span class="keyword">SELECT</span> e.<span class="operator">*</span>,d.name <span class="keyword">as</span> <span class="string">'部门'</span> <span class="keyword">FROM</span> emp e <span class="keyword">LEFT</span> <span class="keyword">JOIN</span> dept d <span class="keyword">ON</span> e.dept_id <span class="operator">=</span> d.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- dept表的所有数据与其所在的员工</span></span><br><span class="line"><span class="keyword">SELECT</span> e.name <span class="keyword">as</span> <span class="string">'员工'</span>, d.<span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">RIGHT</span> <span class="keyword">JOIN</span> dept d <span class="keyword">ON</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure><h3 id="自连接"><a href="#自连接" class="headerlink" title="自连接"></a>自连接</h3><p>自连接可以是内连接,也可以是外连接</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表A 别名A <span class="keyword">JOIN</span> 表A 别名B <span class="keyword">ON</span> 条件;</span><br></pre></td></tr></table></figure><p>练习:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 员工的所属领导</span></span><br><span class="line"><span class="keyword">SELECT</span> ework.name <span class="keyword">as</span> <span class="string">'员工'</span>,emana.name <span class="keyword">as</span> <span class="string">'所属领导'</span> <span class="keyword">FROM</span> emp ework <span class="keyword">JOIN</span> emp emana <span class="keyword">ON</span> ework.managerid <span class="operator">=</span> emana.id;</span><br><span class="line"><span class="keyword">SELECT</span> ework.name <span class="keyword">as</span> <span class="string">'员工'</span>,emana.name <span class="keyword">as</span> <span class="string">'所属领导'</span> <span class="keyword">FROM</span> emp ework , emp emana <span class="keyword">WHERE</span> ework.managerid <span class="operator">=</span> emana.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 员工的所属领导,没有领导也要查出来</span></span><br><span class="line"><span class="keyword">SELECT</span> ework.name <span class="keyword">as</span> <span class="string">'员工'</span>,emana.name <span class="keyword">as</span> <span class="string">'所属领导'</span> <span class="keyword">FROM</span> emp ework <span class="keyword">LEFT</span> <span class="keyword">JOIN</span> emp emana <span class="keyword">ON</span> ework.managerid <span class="operator">=</span> emana.id;</span><br></pre></td></tr></table></figure><h2 id="联合查询"><a href="#联合查询" class="headerlink" title="联合查询"></a>联合查询</h2><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></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>练习:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 薪资低于10000的员工和年龄大于30岁的员工</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">30</span></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="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> salary <span class="operator"><</span> <span class="number">10000</span>;</span><br></pre></td></tr></table></figure><p>多张表的<strong>列数必须保持一致</strong>,字段<strong>类型也必须要保持一致</strong></p><p>UNION ALL 会将全部查询返回的数据直接合并在一起,而 UNION 会<strong>对合并之后的数据去重</strong></p><h2 id="子查询"><a href="#子查询" class="headerlink" title="子查询"></a>子查询</h2><p>子查询语法:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> t1 <span class="keyword">WHERE</span> column1 <span class="operator">=</span> (<span class="keyword">SELECT</span> column1 <span class="keyword">FROM</span> t2)</span><br></pre></td></tr></table></figure><p>该外部语句除了可以是查询外,还能是增删改其中一种</p><h3 id="标量子查询"><a href="#标量子查询" class="headerlink" title="标量子查询"></a>标量子查询</h3><p>所返回的结果是<strong>单个值</strong>(字符串、数字、日期等),是最简单的形式</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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 杨逍之后入职的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span 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><h3 id="列子查询"><a href="#列子查询" class="headerlink" title="列子查询"></a>列子查询</h3><p>子查询所返回的结果是<strong>一列(也可以是多行)</strong>,这种子查询称为列子查询</p><p>常见的操作符如下图所示:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_22-34-34.webp"></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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询销售部和市场部的所有员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span 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="operator">||</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">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> 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">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">-- 查询比研发部其中任意一人工资高的员工信息</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><h3 id="行子查询"><a href="#行子查询" class="headerlink" title="行子查询"></a>行子查询</h3><p>子查询所返回的结果是<strong>一行(也可以是多列)</strong>,这种子查询称为行子查询</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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询与'张无忌'的薪资及直属领导相同的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span 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><h3 id="行子查询-1"><a href="#行子查询-1" class="headerlink" title="行子查询"></a>行子查询</h3><p>子查询所返回的结果是<strong>多行多列</strong>,这种子查询称为表子查询</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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询与'杨逍'薪资和工作相同的员工信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> (salary,job) <span class="keyword">IN</span> (<span class="keyword">SELECT</span> salary,job <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">-- 查询在2004-09-07后入职的员工部门信息</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> (<span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> entrydate <span class="operator">></span> <span class="string">'2004-09-07'</span>) e <span class="keyword">LEFT</span> <span class="keyword">JOIN</span> dept d <span class="keyword">ON</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure><h2 id="案例练习"><a href="#案例练习" class="headerlink" title="案例练习"></a>案例练习</h2><h3 id="建表"><a href="#建表" class="headerlink" title="建表"></a>建表</h3><p>上述练习没有删除 emp表 和 dept表 的数据,但是基本操作相同,除了数据</p><p>现在重新创建 dept表,并插入相应数据:</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">CREATE</span> <span class="keyword">TABLE</span> dept ( id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY auto_increment, NAME <span class="type">VARCHAR</span> ( <span class="number">10</span> ) );</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'研发部'</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'市场部'</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'财务部'</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'销售部'</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'总经办'</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'人事部'</span> );</span><br></pre></td></tr></table></figure><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><span class="line">30</span><br><span class="line">31</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,</span><br><span class="line">NAME <span class="type">VARCHAR</span> ( <span class="number">10</span> ),</span><br><span class="line">age <span class="type">INT</span>,</span><br><span class="line">job <span class="type">VARCHAR</span> ( <span class="number">10</span> ),</span><br><span class="line">salary <span class="type">INT</span>,</span><br><span class="line">entrydate <span class="type">date</span>,</span><br><span class="line">managerid <span class="type">INT</span>,</span><br><span class="line">dept_id <span class="type">INT</span>,</span><br><span class="line"><span class="keyword">CONSTRAINT</span> fk_dept <span class="keyword">FOREIGN</span> KEY ( dept_id ) <span class="keyword">REFERENCES</span> dept ( id ) </span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">NULL</span>, <span class="number">5</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'灭绝'</span>, <span class="number">60</span>, <span class="string">'财务总监'</span>, <span class="number">8500</span>, <span class="string">'2002-09-12'</span>, <span class="number">1</span>, <span class="number">3</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'周芷若'</span>, <span class="number">19</span>, <span class="string">'会计'</span>, <span class="number">4800</span>, <span class="string">'2006-06-02'</span>, <span class="number">7</span>, <span class="number">3</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'丁敏君'</span>, <span class="number">23</span>, <span class="string">'出纳'</span>, <span class="number">5250</span>, <span class="string">'2009-05-13'</span>, <span class="number">7</span>, <span class="number">3</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'赵敏'</span>, <span class="number">20</span>, <span class="string">'市场部总监'</span>, <span class="number">12500</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">2</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'鹿杖客'</span>, <span class="number">56</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2006-10-03'</span>, <span class="number">10</span>, <span class="number">2</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'鹤笔翁'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">3750</span>, <span class="string">'2007-05-09'</span>, <span class="number">10</span>, <span class="number">2</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'方东白'</span>, <span class="number">19</span>, <span class="string">'职员'</span>, <span class="number">5500</span>, <span class="string">'2009-02-12'</span>, <span class="number">10</span>, <span class="number">2</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'张三丰'</span>, <span class="number">88</span>, <span class="string">'销售总监'</span>, <span class="number">14000</span>, <span class="string">'2004-10-12'</span>, <span class="number">1</span>, <span class="number">4</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'俞莲舟'</span>, <span class="number">38</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'宋远桥'</span>, <span class="number">40</span>, <span class="string">'销售'</span>, <span class="number">4600</span>, <span class="string">'2004-10-12'</span>, <span class="number">14</span>, <span class="number">4</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'陈友谅'</span>, <span class="number">42</span>, <span class="keyword">NULL</span>, <span class="number">2000</span>, <span class="string">'2011-10-12'</span>, <span class="number">1</span>, <span class="keyword">NULL</span> );</span><br></pre></td></tr></table></figure><p>创建 salgrade表,并插入相应数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> salgrade ( grade <span class="type">INT</span>, losal <span class="type">INT</span>, hisal <span class="type">INT</span> ) 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> salgrade</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">( <span class="number">1</span>, <span class="number">0</span>, <span class="number">3000</span> ),</span><br><span class="line">( <span class="number">2</span>, <span class="number">3001</span>, <span class="number">5000</span> ),</span><br><span class="line">( <span class="number">3</span>, <span class="number">5001</span>, <span class="number">8000</span> ),</span><br><span class="line">( <span class="number">4</span>, <span class="number">8001</span>, <span class="number">10000</span> ),</span><br><span class="line">( <span class="number">5</span>, <span class="number">10001</span>, <span class="number">15000</span> ),</span><br><span class="line">( <span class="number">6</span>, <span class="number">15001</span>, <span class="number">20000</span> ),</span><br><span class="line">( <span class="number">7</span>, <span class="number">20001</span>, <span class="number">25000</span> ),</span><br><span class="line">( <span class="number">8</span>, <span class="number">25001</span>, <span class="number">30000</span> );</span><br></pre></td></tr></table></figure><h3 id="练习"><a href="#练习" class="headerlink" title="练习"></a>练习</h3><ul><li><p>1、查询员工的姓名、年龄、职位、部门信息(隐式内连接)</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.name,e.age,e.job,d.name <span class="keyword">as</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><br></pre></td></tr></table></figure></li><li><p>2、查询年龄小于30岁的员工姓名、年龄、职位、部门信息(显示式内连接)</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.name,e.age,e.job,d.name <span class="keyword">as</span> <span class="string">'部门'</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">ON</span> e.dept_id <span class="operator">=</span> d.id <span class="operator">&&</span> e.age <span class="operator"><</span> <span class="number">30</span>;</span><br></pre></td></tr></table></figure></li><li><p>3、查询所有员工的部门ID、部门名称</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="keyword">DISTINCT</span> d.id,d.name <span class="keyword">as</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><br></pre></td></tr></table></figure></li><li><p>4、查询所有年龄大于40岁的员工及其所属部门名称,如果没有也需要展示出来</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.<span class="operator">*</span>,d.name <span class="keyword">as</span> <span class="string">'部门'</span> <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></pre></td></tr></table></figure></li><li><p>5、查询所有员工的工资等级</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.<span class="operator">*</span>,s.grade <span class="keyword">AS</span> <span class="string">'薪资等级'</span> <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></pre></td></tr></table></figure></li><li><p>6、查询’研发部’所有员工的信息及工资等级</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.<span class="operator">*</span>,s.grade <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> emp.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>)) 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></pre></td></tr></table></figure></li><li><p>7、查询’研发部’的平均工资</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> d.name <span class="keyword">as</span> <span class="string">'部门'</span>,<span class="built_in">AVG</span>(e.salary) <span class="keyword">AS</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">and</span> d.name <span class="operator">=</span> <span class="string">'研发部'</span>;</span><br></pre></td></tr></table></figure></li><li><p>8、查询工资比’灭绝’高的员工信息</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="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> emp.name <span class="operator">=</span> <span class="string">'灭绝'</span>);</span><br></pre></td></tr></table></figure></li><li><p>9、查询比平均薪资高的员工信息</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="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>(emp.salary) <span class="keyword">FROM</span> emp);</span><br></pre></td></tr></table></figure></li><li><p>10、查询低于本部门平均薪资的员工信息</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(e.salary) <span class="keyword">FROM</span> emp e <span class="keyword">WHERE</span> e.dept_id <span class="operator">=</span> <span class="number">3</span>;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e2 <span class="keyword">WHERE</span> salary <span class="operator"><</span> (<span class="keyword">SELECT</span> <span class="built_in">AVG</span>(e.salary) <span class="keyword">FROM</span> emp e <span class="keyword">WHERE</span> e.dept_id <span class="operator">=</span> e2.dept_id);</span><br></pre></td></tr></table></figure></li><li><p>11、查询所有的部门信息,并统计部门的人数</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="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 class="keyword">SELECT</span> d.id,d.name,(<span class="keyword">SELECT</span> <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> emp e <span class="keyword">WHERE</span> e.dept_id <span class="operator">=</span> d.id) <span class="keyword">AS</span> <span class="string">'人数'</span> <span class="keyword">FROM</span> dept d;</span><br></pre></td></tr></table></figure></li><li><p>12、查询所有学生的选课情况,展示学生名称、学号、课程名称</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 所涉及表: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,course c,student_course sc <span class="keyword">WHERE</span> s.id <span class="operator">=</span> sc.studentid <span class="keyword">AND</span> c.id <span class="operator">=</span> sc.courseid;</span><br></pre></td></tr></table></figure></li></ul>]]></content>
<summary type="html">多表查询</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Mysql数据库基础" scheme="https://blhorizon.github.io/tags/Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Mysql数据库基础(四)</title>
<link href="https://blhorizon.github.io/posts/201c.html"/>
<id>https://blhorizon.github.io/posts/201c.html</id>
<published>2024-05-02T02:23:03.000Z</published>
<updated>2024-05-02T03:28:10.436Z</updated>
<content type="html"><![CDATA[<h2 id="约束概述"><a href="#约束概述" class="headerlink" title="约束概述"></a>约束概述</h2><p>概念:作用于表中字段的<strong>规则</strong>,用于限制存储在表中的数据</p><p>目的:保证数据库中数据的正确,有效性和完整性</p><p>约束有以下几种分类:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_10-27-19.webp"></p><p>因为约束时作用于表中字段的,所以可以在<strong>创建表/修改表</strong>时进行添加</p><p>根据需求,完成该 user 表的创建:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_10-37-29.webp"></p><p>上述SQL语句:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="keyword">USER</span> (</span><br><span class="line">id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY auto_increment COMMENT <span class="string">'ID唯一表示'</span>,</span><br><span class="line">NAME <span class="type">VARCHAR</span> ( <span class="number">10</span> ) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span> COMMENT <span class="string">'姓名'</span>,</span><br><span class="line">age <span class="type">INT</span> <span class="keyword">CHECK</span> ( age <span class="operator">></span> <span class="number">0</span> <span class="operator">&&</span> age <span class="operator"><=</span> <span class="number">120</span> ) COMMENT <span class="string">'年龄'</span>,</span><br><span class="line">STATUS <span class="type">CHAR</span> ( <span class="number">1</span> ) <span class="keyword">DEFAULT</span> <span class="string">'1'</span> COMMENT <span class="string">'状态'</span>,</span><br><span class="line">gender <span class="type">CHAR</span> ( <span class="number">1</span> ) COMMENT <span class="string">'性别'</span></span><br><span class="line">) COMMENT <span class="string">'约束练习'</span>;</span><br></pre></td></tr></table></figure><h2 id="外键约束"><a href="#外键约束" class="headerlink" title="外键约束"></a>外键约束</h2><p>外键是用来<strong>建立两张表数据之间的连接</strong>,从而保证数据的一致性和完整性</p><p>具有外键的表我们称之为<strong>子表(从表)</strong>,而其关联的另一张表称之为<strong>父表(主表)</strong></p><p>添加外键:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> emp <span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> 外键名称 <span class="keyword">FOREIGN</span> KEY(外键字段名) <span class="keyword">REFERENCES</span> 主表(主表列名);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 也可以在创建表时添加</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> 表名 (</span><br><span class="line"> 字段名 数据类型,</span><br><span class="line"><span class="keyword">CONSTRAINT</span> 外键名称 <span class="keyword">FOREIGN</span> KEY (外键字段名) <span class="keyword">REFERENCES</span> 主表(主表列名)</span><br><span class="line">);</span><br></pre></td></tr></table></figure><p>删除外键:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> 表名 <span class="keyword">DROP</span> <span class="keyword">FOREIGN</span> KEY 外键名称;</span><br></pre></td></tr></table></figure><p>外键默认是不允许更新或删除的,这与其更新/删除<strong>行为</strong>有关,主要有以下几种:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_11-22-34.webp"></p><p>可以在外键相关SQL语句后面更新该行为,如:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> emp <span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> 外键名称 <span class="keyword">FOREIGN</span> KEY(外键字段名) <span class="keyword">REFERENCES</span> 主表(主表列名) <span class="keyword">ON</span> <span class="keyword">UPDATE</span> 行为 <span class="keyword">ON</span> <span class="keyword">DELETE</span> 行为;</span><br></pre></td></tr></table></figure><h3 id="案例练习"><a href="#案例练习" class="headerlink" title="案例练习"></a>案例练习</h3><p>将之前的 emp(员工)表删除,创建一个 dept(部门) 表和 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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- dept表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> dept ( id <span class="type">INT</span> auto_increment COMMENT <span class="string">'ID'</span> <span class="keyword">PRIMARY</span> KEY, 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> ) COMMENT <span class="string">'部门表'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- emp表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> emp (</span><br><span class="line">id <span class="type">INT</span> AUTO_INCREMENT COMMENT <span class="string">'ID'</span> <span class="keyword">PRIMARY</span> KEY,</span><br><span class="line">NAME <span class="type">VARCHAR</span> ( <span class="number">50</span> ) <span class="keyword">NOT</span> <span class="keyword">NULL</span> COMMENT <span class="string">'姓名'</span>,</span><br><span class="line">age <span class="type">INT</span> COMMENT <span class="string">'年龄'</span>,</span><br><span class="line">job <span class="type">VARCHAR</span> ( <span class="number">20</span> ) COMMENT <span class="string">'职位'</span>,</span><br><span class="line">salary <span class="type">INT</span> COMMENT <span class="string">'薪资'</span>,</span><br><span class="line">entrydate <span class="type">date</span> COMMENT <span class="string">'入职时间'</span>,</span><br><span class="line">managerid <span class="type">INT</span> COMMENT <span class="string">'直属领导ID'</span>,</span><br><span class="line">dept_id <span class="type">INT</span> COMMENT <span class="string">'部门ID'</span> </span><br><span class="line">) COMMENT <span class="string">'员工表'</span>;</span><br></pre></td></tr></table></figure><p>插入相应的数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- dept表</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept ( id, NAME ) <span class="keyword">VALUES</span>( <span class="number">1</span>, <span class="string">'研发部'</span> ),(<span class="number">2</span>,<span class="string">'市场部'</span> ),(<span class="number">3</span>,<span class="string">'财务部'</span> ),(<span class="number">4</span>,<span class="string">'销售部'</span> ),(<span class="number">5</span>,<span class="string">'总经办'</span> );</span><br><span class="line"></span><br><span class="line"><span class="comment">-- emp表</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'金庸'</span>, <span class="number">66</span>, <span class="string">'总裁'</span>, <span class="number">20000</span>, <span class="string">'2000-01-01'</span>, <span class="keyword">NULL</span>, <span class="number">5</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'张无忌'</span>, <span class="number">20</span>, <span class="string">'项目经理'</span>, <span class="number">12500</span>, <span class="string">'2005-12-05'</span>, <span class="number">1</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'杨逍'</span>, <span class="number">33</span>, <span class="string">'开发'</span>, <span class="number">8400</span>, <span class="string">'2000-11-03'</span>, <span class="number">2</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'韦一笑'</span>, <span class="number">48</span>, <span class="string">'开发'</span>, <span class="number">11000</span>, <span class="string">'2002-02-05'</span>, <span class="number">2</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'常遇春'</span>, <span class="number">43</span>, <span class="string">'开发'</span>, <span class="number">10500</span>, <span class="string">'2004-09-07'</span>, <span class="number">3</span>, <span class="number">1</span> ),</span><br><span class="line">( <span class="keyword">NULL</span>, <span class="string">'小昭'</span>, <span class="number">19</span>, <span class="string">'程序员鼓励师'</span>, <span class="number">6600</span>, <span class="string">'2004-10-12'</span>, <span class="number">2</span>, <span class="number">1</span> );</span><br></pre></td></tr></table></figure><p>给 emp 表添加外键:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> emp <span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> fk_emp_dept_id <span class="keyword">FOREIGN</span> KEY (dept_id) <span class="keyword">REFERENCES</span> dept(id);</span><br></pre></td></tr></table></figure><p>当成功添加外键后,这时我们想删除部门表的研发部时会报错,提示存在外键。我们可以删除该外键:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> emp <span class="keyword">drop</span> <span class="keyword">FOREIGN</span> key fk_emp_dept_id;</span><br></pre></td></tr></table></figure><p>这时就可以将研发部给删除掉了</p><p>给外键添加 <strong>CASCADE</strong> 行为后,可以看出员工表一旦发生变化,那么 emp 表其对应的部门 id 也会跟随着其改变</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> emp <span class="keyword">ADD</span> <span class="keyword">CONSTRAINT</span> fk_emp_dept_id <span class="keyword">FOREIGN</span> KEY (dept_id) <span class="keyword">REFERENCES</span> dept(id);</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">Mysql的约束</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Mysql数据库基础" scheme="https://blhorizon.github.io/tags/Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Mysql数据库基础(三)</title>
<link href="https://blhorizon.github.io/posts/4edd.html"/>
<id>https://blhorizon.github.io/posts/4edd.html</id>
<published>2024-05-02T01:26:56.000Z</published>
<updated>2024-05-02T02:21:00.468Z</updated>
<content type="html"><![CDATA[<p><strong>函数</strong>是指一段可以被另一个程序直接调用的程序或代码</p><h2 id="字符串函数"><a href="#字符串函数" class="headerlink" title="字符串函数"></a>字符串函数</h2><p>常见的字符串函数如下:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_09-44-54.webp"></p><p>可以通过 <strong>SELECT 函数(参数)</strong> 来使用这些函数</p><h2 id="数值函数"><a href="#数值函数" class="headerlink" title="数值函数"></a>数值函数</h2><p>常见的数值函数如下:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_09-52-33.webp"></p><p>1.1 向上取整是 2,向下取整则是 1;4/3 取模则是 1</p><h2 id="日期函数"><a href="#日期函数" class="headerlink" title="日期函数"></a>日期函数</h2><p>常见的数值函数如下:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_10-03-12.webp"></p><h2 id="流程函数"><a href="#流程函数" class="headerlink" title="流程函数"></a>流程函数</h2><p>流畅函数是一种常用的函数,常用于<strong>SQL语句的条件筛选</strong>,从而提高其效率<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_10-07-02.webp"></p><p>创建 score 表:</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> score (</span><br><span class="line">id <span class="type">INT</span> COMMENT <span class="string">'编号'</span>,</span><br><span class="line">NAME <span class="type">VARCHAR</span> ( <span class="number">20</span> ) COMMENT <span class="string">'姓名'</span>,</span><br><span class="line">math <span class="type">INT</span> COMMENT <span class="string">'数学'</span>,</span><br><span class="line">english <span class="type">INT</span> COMMENT <span class="string">'英语'</span>,</span><br><span class="line">chinese <span class="type">INT</span> COMMENT <span class="string">'语文'</span> </span><br><span class="line">) COMMENT <span class="string">'学员成绩表'</span>;</span><br></pre></td></tr></table></figure><p>插入相应的数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> score</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">( <span class="number">1</span>, <span class="string">'Tom'</span>, <span class="number">67</span>, <span class="number">88</span>, <span class="number">95</span> ),</span><br><span class="line">( <span class="number">2</span>, <span class="string">'Rose'</span>, <span class="number">23</span>, <span class="number">66</span>, <span class="number">90</span> ),</span><br><span class="line">( <span class="number">3</span>, <span class="string">'Jack'</span>, <span class="number">56</span>, <span class="number">98</span>, <span class="number">76</span> );</span><br></pre></td></tr></table></figure><p>成绩大于等于85的为优秀,大于等于60的为合格,否则为不及格:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span></span><br><span class="line">id,</span><br><span class="line">name,</span><br><span class="line">( <span class="keyword">CASE</span> <span class="keyword">WHEN</span> math <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">THEN</span> <span class="string">'优秀'</span> <span class="keyword">WHEN</span> math <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">THEN</span> <span class="string">'及格'</span> <span class="keyword">ELSE</span> <span class="string">'不及格'</span> <span class="keyword">END</span> ) <span class="keyword">AS</span> <span class="string">'数学成绩'</span>,</span><br><span class="line">( <span class="keyword">CASE</span> <span class="keyword">WHEN</span> english <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">THEN</span> <span class="string">'优秀'</span> <span class="keyword">WHEN</span> english <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">THEN</span> <span class="string">'及格'</span> <span class="keyword">ELSE</span> <span class="string">'不及格'</span> <span class="keyword">END</span> ) <span class="keyword">AS</span> <span class="string">'英语成绩'</span>,</span><br><span class="line">( <span class="keyword">CASE</span> <span class="keyword">WHEN</span> chinese <span class="operator">>=</span> <span class="number">85</span> <span class="keyword">THEN</span> <span class="string">'优秀'</span> <span class="keyword">WHEN</span> chinese <span class="operator">>=</span> <span class="number">60</span> <span class="keyword">THEN</span> <span class="string">'及格'</span> <span class="keyword">ELSE</span> <span class="string">'不及格'</span> <span class="keyword">END</span> ) <span class="keyword">AS</span> <span class="string">'语文成绩'</span> </span><br><span class="line"><span class="keyword">FROM</span></span><br><span class="line">score;</span><br></pre></td></tr></table></figure>]]></content>
<summary type="html">Mysql内置函数</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Mysql数据库基础" scheme="https://blhorizon.github.io/tags/Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Mysql数据库基础(二)</title>
<link href="https://blhorizon.github.io/posts/afc9c419.html"/>
<id>https://blhorizon.github.io/posts/afc9c419.html</id>
<published>2024-04-29T02:20:32.000Z</published>
<updated>2024-05-02T01:43:02.010Z</updated>
<content type="html"><![CDATA[<h2 id="SQL概述"><a href="#SQL概述" class="headerlink" title="SQL概述"></a>SQL概述</h2><p>SQL的通用语法:</p><ul><li>语句可以单行或多行书写,以<strong>分号</strong>结尾</li><li>可以通过空格/缩进来增强语句的可读性,空格可以是多个</li><li>MySQL数据库的SQL语句不区分大小写,但关键字建议使用大写区分开来</li></ul><p>SQL的分类:</p><ul><li>DDL(Data Definition Language):数据定义语言,用来定义数据库对象:数据库、表、字段等</li><li>DML(Data Manipulation Language):数据操作语言,用来对数据库表中的数据进行增删改</li><li>DCL(Data Control Language):数据控制语言,用来创建数据库用户、控制数据库的访问权限</li><li>DQL(Data Query Language):数据查询语言,用来查询数据库中表的记录</li></ul><h2 id="DDL"><a href="#DDL" class="headerlink" title="DDL"></a>DDL</h2><p>DDL(Data Definition Language):数据定义语言,用来定义数据库对象:数据库、表、字段等</p><p>数据库操作:</p><ul><li><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询所有的数据库</span></span><br><span class="line"><span class="keyword">SHOW</span> DATABASES;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询当前的数据库</span></span><br><span class="line"><span class="keyword">SELECT</span> DATABASE();</span><br></pre></td></tr></table></figure></li><li><p>创建</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建数据库</span></span><br><span class="line"><span class="keyword">CREATE</span> DATABASE [IF <span class="keyword">NOT</span> <span class="keyword">EXISTS</span>] 数据库名 [<span class="keyword">DEFAULT</span> CHARSET 字符集] [<span class="keyword">COLLATE</span> 排序规则];</span><br></pre></td></tr></table></figure><blockquote><p>utf8 长度只有三个字符,有一些特殊的字符有四个字符,可以使用<strong>utf8mb4</strong></p></blockquote></li><li><p>删除</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 删除数据库</span></span><br><span class="line"><span class="keyword">DROP</span> DATABASE [IF <span class="keyword">EXISTS</span>] 数据库名;</span><br></pre></td></tr></table></figure></li><li><p>使用</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">USE 数据库名;</span><br></pre></td></tr></table></figure></li></ul><hr><p>表操作:</p><ul><li><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></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> 表名(</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">)[COMMENT 表注释];</span><br></pre></td></tr></table></figure><blockquote><p>注意:<strong>最后一个字段</strong>后面没有逗号;在Mysql中字符串是<strong>varchar(长度)</strong></p></blockquote></li><li><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询当前数据库所有表</span></span><br><span class="line"><span class="keyword">SHOW</span> TABLES;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看表结构</span></span><br><span class="line"><span class="keyword">DESC</span> 表名;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询指定表的建表语句</span></span><br><span class="line"><span class="keyword">SHOW</span> <span class="keyword">CREATE</span> <span class="keyword">TABLE</span> 表名;</span><br></pre></td></tr></table></figure></li></ul><p>在这里就需要提及Mysql的数据类型了,其主要分为三类:<strong>数值类型</strong>、<strong>字符串类型</strong>、<strong>日期时间类型</strong></p><ul><li><p>数值类型<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-04-21_10-09-07.webp"></p><blockquote><p>例如 <strong>123.45</strong>,那么它的<strong>精度(M)则是它的长度5</strong>,<strong>标度(D)则是它的小数点后位数2</strong></p></blockquote></li><li><p>字符串类型<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-04-21_10-16-37.webp"></p><blockquote><ul><li>二进制数据例如文件、压缩包等,一般都会有一个专门的文件服务器来存储,因为存储在数据库中会影响其性能</li><li>变长字符串会根据你的存储内容来计算占用空间,性能较差;定长字符串则都会占用指定的空间,性能较好</li></ul></blockquote></li><li><p>日期时间类型<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-04-21_10-25-03.webp"></p></li><li><p>添加字段</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建表</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> 表名 <span class="keyword">ADD</span> 字段名 字段类型 [COMMENT 注释] [约束];</span><br></pre></td></tr></table></figure></li><li><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 修改数据类型</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> 表名 MODIFY 字段名 新数据类型;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改字段名和其数据类型</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> 表名 CHANGE 旧字段名 新字段名 新数据类型 [COMMENT 注释] [约束];</span><br></pre></td></tr></table></figure></li><li><p>删除字段</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 删除字段</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> 表名 <span class="keyword">DROP</span> 字段名;</span><br></pre></td></tr></table></figure></li><li><p>修改表名</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 修改表名</span></span><br><span class="line"><span class="keyword">ALTER</span> <span class="keyword">TABLE</span> 表名 RENAME <span class="keyword">TO</span> 新表名;</span><br></pre></td></tr></table></figure></li><li><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 删除表</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> [IF <span class="keyword">EXISTS</span>] 表名;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 只删除表中的数据</span></span><br><span class="line"><span class="keyword">TRUNCATE</span> <span class="keyword">TABLE</span> 表名;</span><br></pre></td></tr></table></figure></li></ul><h2 id="DML"><a href="#DML" class="headerlink" title="DML"></a>DML</h2><p>DML(Data Manipulation Language):数据操作语言,用来对数据库表中的数据进行增删改</p><ul><li><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 给指定字段添加数据</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> 表名 (字段<span class="number">1</span>,字段<span class="number">2</span>,...) <span class="keyword">VALUES</span>(值<span class="number">1</span>,值<span class="number">2</span>,...);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 给所有字段添加数据</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> 表名 <span class="keyword">VALUES</span>(值<span class="number">1</span>,值<span class="number">2</span>,...);</span><br></pre></td></tr></table></figure><blockquote><ol><li>如果想批量添加数据,可以在关键字<strong>VALUES</strong>后面<strong>再增加,(值1,值2,…)【以逗号分隔开】</strong></li><li>插入的数据要<strong>一一对应</strong>且注意<strong>该字段的数据类型</strong></li></ol></blockquote></li><li><p>更新数据</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">UPDATE</span> 表名 <span class="keyword">SET</span> 字段<span class="number">1</span><span class="operator">=</span>值<span class="number">1</span>,字段<span class="number">2</span><span class="operator">=</span>值<span class="number">2</span>,... [<span class="keyword">WHERE</span> 条件];</span><br></pre></td></tr></table></figure><blockquote><p>没有添加条件则会更新所有数据!</p></blockquote></li><li><p>删除数据</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> 表名 [<span class="keyword">WHERE</span> 条件];</span><br></pre></td></tr></table></figure><blockquote><p>没有添加条件则会删除所有数据!同时delete是不能删除一个字段的值的</p></blockquote></li></ul><p>数据库操作最基本也是最重要的是<strong>查询</strong>操作</p><h2 id="DQL"><a href="#DQL" class="headerlink" title="DQL"></a>DQL</h2><p>DQL(Data Query Language):数据查询语言,用来查询数据库中表的记录</p><h3 id="基本查询"><a href="#基本查询" class="headerlink" title="基本查询"></a>基本查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询指定字段</span></span><br><span class="line"><span class="keyword">SELECT</span> 字段<span class="number">1</span> [<span class="keyword">AS</span> 别名<span class="number">1</span>],字段<span class="number">2</span> [<span class="keyword">AS</span> 别名<span class="number">2</span>],... <span class="keyword">FROM</span> 表名;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询所有</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> 表名;</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="keyword">DISTINCT</span> 字段列表 <span class="keyword">FROM</span> 表名;</span><br></pre></td></tr></table></figure><h3 id="条件查询"><a href="#条件查询" class="headerlink" title="条件查询"></a>条件查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 <span class="keyword">WHERE</span> 条件列表;</span><br></pre></td></tr></table></figure><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-04-29_10-29-19.webp"></p><h3 id="聚合函数"><a href="#聚合函数" class="headerlink" title="聚合函数"></a>聚合函数</h3><p>将一列数据作为一个整体,进行纵向计算。聚合函数一般配合分组查询<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-04-29_10-48-49.webp"></p><blockquote><p> 聚合函数是<strong>作用于字段</strong>的,一般<strong>NULL值是不参与任</strong>何聚合函数的运算</p></blockquote><h3 id="分组查询"><a href="#分组查询" class="headerlink" title="分组查询"></a>分组查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 [<span class="keyword">WHERE</span> 条件列表] <span class="keyword">GROUP</span> <span class="keyword">BY</span> 分组字段名 [<span class="keyword">HAVING</span> 分组后过滤的条件];</span><br></pre></td></tr></table></figure><p>WHERE 和 HAVING 的区别:</p><ul><li>执行时机不同:where 是在<strong>分组之前进行过滤</strong>,不满足 where 条件的不参与分组;而 having 是在<strong>分组之后对结果进行过滤</strong></li><li>判断条件不足:where 不能对聚合函数进行判断,而 having 可以</li></ul><p>需要注意的是:</p><ul><li>执行顺序:where > 聚合函数 > having</li><li>分组之后,查询的字段一般是聚合函数和分组字段,查询其他字段毫无意义</li></ul><h3 id="排序查询"><a href="#排序查询" class="headerlink" title="排序查询"></a>排序查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 <span class="keyword">ORDER</span> <span class="keyword">BY</span> 字段<span class="number">1</span> 排序方式<span class="number">1</span> , 字段<span class="number">2</span> 排序方式<span class="number">2</span>;</span><br></pre></td></tr></table></figure><p>默认排序方式是 <strong>升序(ASC)</strong>。同时还有一种排序方式,即 <strong>降序(DESC)</strong></p><blockquote><p>当进行多字段排序,当<strong>第一个字段值相同</strong>时,才会根据第二个字段进行排序</p></blockquote><h3 id="分页查询"><a href="#分页查询" class="headerlink" title="分页查询"></a>分页查询</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段列表 <span class="keyword">FROM</span> 表名 LIMIT 起始索引 , 查询记录数;</span><br></pre></td></tr></table></figure><p>起始索引的计算方式:<strong>(查询页码 - 1) * 每页显示记录数</strong>。当查询的是第一页数据时,起始索引可以省略掉</p><p>分页查询是是数据库的方言,即不同数据库有不同的实现方式,比如 Mysql 就是 LIMIT 的了</p><h3 id="案例练习"><a href="#案例练习" class="headerlink" title="案例练习"></a>案例练习</h3><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></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> COMMENT <span class="string">'编号'</span>,</span><br><span class="line">workno <span class="type">VARCHAR</span> ( <span class="number">10</span> ) COMMENT <span class="string">'工号'</span>,</span><br><span class="line">NAME <span class="type">VARCHAR</span> ( <span class="number">10</span> ) COMMENT <span class="string">'名字'</span>,</span><br><span class="line">gender <span class="type">CHAR</span> COMMENT <span class="string">'性别'</span>,</span><br><span class="line">age TINYINT UNSIGNED COMMENT <span class="string">'年龄'</span>,</span><br><span class="line">idcard <span class="type">CHAR</span> ( <span class="number">18</span> ) COMMENT <span class="string">'身份证号'</span>,</span><br><span class="line">workaddress <span class="type">VARCHAR</span> ( <span class="number">50</span> ) COMMENT <span class="string">'工作地址'</span>,</span><br><span class="line">entrydate <span class="type">date</span> COMMENT <span class="string">'入职时间'</span> </span><br><span class="line">) COMMENT <span class="string">'员工表'</span>;</span><br></pre></td></tr></table></figure><p>插入相应的数据:</p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><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="keyword">INSERT</span> <span class="keyword">INTO</span> emp ( id, workno, NAME, gender, age, idcard, workaddress, entrydate )</span><br><span class="line"><span class="keyword">VALUES</span></span><br><span class="line">( <span class="number">1</span>, <span class="number">1</span>, <span class="string">'柳岩'</span>, <span class="string">'女'</span>, <span class="number">20</span>, <span class="string">'123456789012345678'</span>, <span class="string">'北京'</span>, <span class="string">'2000-01-01'</span> ),</span><br><span class="line">( <span class="number">2</span>, <span class="number">2</span>, <span class="string">'张无忌'</span>, <span class="string">'男'</span>, <span class="number">18</span>, <span class="string">'123456789012345670'</span>, <span class="string">'北京'</span>, <span class="string">'2005-09-01'</span> ),</span><br><span class="line">( <span class="number">3</span>, <span class="number">3</span>, <span class="string">'韦一笑'</span>, <span class="string">'男'</span>, <span class="number">38</span>, <span class="string">'123456789712345670'</span>, <span class="string">'上海'</span>, <span class="string">'2005-08-01'</span> ),</span><br><span class="line">( <span class="number">4</span>, <span class="number">4</span>, <span class="string">'赵敏'</span>, <span class="string">'女'</span>, <span class="number">18</span>, <span class="string">'123456757123845670'</span>, <span class="string">'北京'</span>, <span class="string">'2009-12-01'</span> ),</span><br><span class="line">( <span class="number">5</span>, <span class="number">5</span>, <span class="string">'小昭'</span>, <span class="string">'女'</span>, <span class="number">16</span>, <span class="string">'123456769012345678'</span>, <span class="string">'上海'</span>, <span class="string">'2007-07-01'</span> ),</span><br><span class="line">( <span class="number">6</span>, <span class="number">6</span>, <span class="string">'杨道'</span>, <span class="string">'男'</span>, <span class="number">28</span>, <span class="string">'12345678931234567X'</span>, <span class="string">'北京'</span>, <span class="string">'2006-01-01'</span> ),</span><br><span class="line">( <span class="number">7</span>, <span class="number">7</span>, <span class="string">'范骚'</span>, <span class="string">'男'</span>, <span class="number">40</span>, <span class="string">'123456789212345670'</span>, <span class="string">'北京'</span>, <span class="string">'2005-05-01'</span> ),</span><br><span class="line">( <span class="number">8</span>, <span class="number">8</span>, <span class="string">'黛绮丝'</span>, <span class="string">'女'</span>, <span class="number">38</span>, <span class="string">'123456157123645670'</span>, <span class="string">'天津'</span>, <span class="string">'2015-05-01'</span> ),</span><br><span class="line">( <span class="number">9</span>, <span class="number">9</span>, <span class="string">'范凉凉'</span>, <span class="string">'女'</span>, <span class="number">45</span>, <span class="string">'123156789012345678'</span>, <span class="string">'北京'</span>, <span class="string">'2010-04-01'</span> ),</span><br><span class="line">( <span class="number">10</span>, <span class="number">10</span>, <span class="string">'陈友凉'</span>, <span class="string">'男'</span>, <span class="number">53</span>, <span class="string">'123456789012345670'</span>, <span class="string">'上海'</span>, <span class="string">'2011-01-01'</span> ),</span><br><span class="line">( <span class="number">11</span>, <span class="number">11</span>, <span class="string">'张士诚'</span>, <span class="string">'男'</span>, <span class="number">55</span>, <span class="string">'123567897123465670'</span>, <span class="string">'江苏'</span>, <span class="string">'2015-05-01'</span> ),</span><br><span class="line">( <span class="number">12</span>, <span class="number">12</span>, <span class="string">'常遇春'</span>, <span class="string">'男'</span>, <span class="number">32</span>, <span class="string">'123446757152345670'</span>, <span class="string">'北京'</span>, <span class="string">'2004-02-01'</span> ),</span><br><span class="line">( <span class="number">13</span>, <span class="number">13</span>, <span class="string">'张三丰'</span>, <span class="string">'男'</span>, <span class="number">88</span>, <span class="string">'123656789012345678'</span>, <span class="string">'江苏'</span>, <span class="string">'2020-11-01'</span> ),</span><br><span class="line">( <span class="number">14</span>, <span class="number">14</span>, <span class="string">'灭绝'</span>, <span class="string">'女'</span>, <span class="number">65</span>, <span class="string">'123456719012345670'</span>, <span class="string">'西安'</span>, <span class="string">'2019-05-01'</span> ),</span><br><span class="line">( <span class="number">15</span>, <span class="number">15</span>, <span class="string">'胡青牛'</span>, <span class="string">'男'</span>, <span class="number">70</span>, <span class="string">'12345674971234567X'</span>, <span class="string">'西安'</span>, <span class="string">'2018-04-01'</span> ),</span><br><span class="line">( <span class="number">16</span>, <span class="number">16</span>, <span class="string">'周芷若'</span>, <span class="string">'女'</span>, <span class="number">18</span>, <span class="keyword">NULL</span>, <span class="string">'北京'</span>, <span class="string">'2012-06-01'</span> );</span><br></pre></td></tr></table></figure><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></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询年龄为20,21,22,23的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">WHERE</span> age <span class="keyword">IN</span>(<span class="number">20</span>,<span class="number">21</span>,<span class="number">22</span>,<span class="number">23</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询性别男,年龄在20~40岁(含)以内的姓名为三个字的员工信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">WHERE</span> gender <span class="operator">=</span> <span class="string">'男'</span> <span class="operator">&&</span> age <span class="keyword">BETWEEN</span> <span class="number">20</span> <span class="keyword">AND</span> <span class="number">40</span> <span class="operator">&&</span> name <span class="keyword">like</span> <span class="string">'___'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 统计员工表中,年龄小于60的男女员工人数</span></span><br><span class="line"><span class="keyword">select</span> gender,<span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">from</span> emp <span class="keyword">WHERE</span> age <span class="operator"><</span> <span class="number">60</span> <span class="keyword">GROUP</span> <span class="keyword">BY</span> gender;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄小于等于35岁的员工信息,并根据其年龄进行升序,如果年龄相同,则按照入职时间降序</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">from</span> emp <span class="keyword">where</span> age <span class="operator"><=</span> <span class="number">35</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> age,entrydate <span class="keyword">DESC</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询年龄在20~40岁(含)以内的前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> gender <span class="operator">=</span> <span class="string">'男'</span> <span class="operator">&&</span> age <span class="keyword">BETWEEN</span> <span class="number">20</span> <span class="keyword">and</span> <span class="number">40</span> <span class="keyword">ORDER</span> <span class="keyword">BY</span> age,entrydate <span class="keyword">desc</span> LIMIT <span class="number">5</span>;</span><br></pre></td></tr></table></figure><p>SELECT 的多种查询方式是有先后执行顺序的,具体如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-04-30_09-46-47.webp"></p><h2 id="DCL"><a href="#DCL" class="headerlink" title="DCL"></a>DCL</h2><p>DCL(Data Control Language):数据控制语言,用来管理数据库用户、控制数据库的访问权限</p><h3 id="管理用户"><a href="#管理用户" class="headerlink" title="管理用户"></a>管理用户</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询用户,用户信息存储在系统数据库mysql的user表中</span></span><br><span class="line">use mysql;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> <span class="keyword">user</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建用户</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">USER</span> <span class="string">'用户名'</span>@<span class="string">'主机名'</span> IDENTIFIED <span class="keyword">BY</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">ALTER</span> <span class="keyword">USER</span> <span class="string">'用户名'</span>@<span class="string">'主机名'</span> IDENTIFIED <span class="keyword">WITH</span> mysql_native_password <span class="keyword">BY</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">DROP</span> <span class="keyword">USER</span> <span class="string">'用户名'</span>@<span class="string">'主机名'</span>;</span><br></pre></td></tr></table></figure><p>当主机名为 <strong>%</strong> 时,则表示在<strong>任意主机</strong>都可以访问到</p><p>新创建的用户默认是<strong>没有任何权限</strong>的,当前<strong>只能访问到information_schema</strong>该数据库</p><p>这类SQL开发人员操作的比较少,主要是DBA(Database Administrator 数据库管理员)会使用</p><h3 id="权限控制"><a href="#权限控制" class="headerlink" title="权限控制"></a>权限控制</h3><p>在mysql中定义了很多种权限,但是常用的就以下几种:<br><img src="/images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Mysql%E5%9F%BA%E7%A1%80/Snipaste_2024-05-02_08-55-40.webp"></p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询权限</span></span><br><span class="line"><span class="keyword">SHOW</span> GRANTS <span class="keyword">FOR</span> <span class="string">'用户名'</span>@<span class="string">'主机名'</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 授予权限</span></span><br><span class="line"><span class="keyword">GRANT</span> 权限列表 <span class="keyword">ON</span> 数据库名.表名 <span class="keyword">TO</span> <span class="string">'用户名'</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">REVOKE</span> 权限列表 <span class="keyword">ON</span> 数据库名.表名 <span class="keyword">FROM</span> <span class="string">'用户名'</span>@<span class="string">'主机名'</span>;</span><br></pre></td></tr></table></figure><p>授予多个权限时,多个权限之间以<strong>逗号</strong>分隔开;授权时数据库和表名以<code>*</code>表示,则代表所有</p>]]></content>
<summary type="html">SQL的几种分类</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Mysql数据库基础" scheme="https://blhorizon.github.io/tags/Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>搭建个人博客</title>
<link href="https://blhorizon.github.io/posts/5d85.html"/>
<id>https://blhorizon.github.io/posts/5d85.html</id>
<published>2024-04-20T12:39:34.000Z</published>
<updated>2024-05-02T19:59:51.544Z</updated>
<content type="html"><![CDATA[<h2 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h2><p>Hexo 是一个快速、简洁且高效的 静态 博客框架,它是<strong>基于 Node.js 运行</strong>的,也就是说它是可以 <strong>不需要服务器</strong> 的</p><div class="tabs" id="准备工作"><ul class="nav-tabs"><li class="tab active"><button type="button" data-href="#准备工作-1">安装 Node.js</button></li><li class="tab"><button type="button" data-href="#准备工作-2">安装 Vscode</button></li></ul><div class="tab-contents"><div class="tab-item-content active" id="准备工作-1"><p>在 <a href="https://nodejs.org/en">Node.js官网</a> 中下载并安装。安装完成后可以通过如下指令检测<strong>是否成功安装</strong>:</p><figure class="highlight bash"><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"># 查看Node.js版本</span></span><br><span class="line">node -v</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看npm版本</span></span><br><span class="line">npm -v</span><br></pre></td></tr></table></figure><blockquote><p>如果你想要安装<strong>其他版本</strong>的 Node.js,可以前往此处:<a href="https://nodejs.org/zh-cn/download/releases">以往的版本</a></p></blockquote><p>Node.js 会自带 <strong>npm</strong>,该 npm 会用于后续为Hexo博客安装相应的<strong>插件</strong>。关于npm的<strong>镜像源</strong>有如下操作:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 自带的镜像源对于国内有时下载插件会很慢,可以更换为淘宝的镜像源</span></span><br><span class="line">npm config <span class="built_in">set</span> registry https://registry.npm.taobao.org</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看当前npm的镜像源</span></span><br><span class="line">npm config get registry</span><br><span class="line"></span><br><span class="line"><span class="comment"># 取消镜像</span></span><br><span class="line">npm config delete registry</span><br></pre></td></tr></table></figure><button type="button" class="tab-to-top" aria-label="scroll to top"><i class="fas fa-arrow-up"></i></button></div><div class="tab-item-content" id="准备工作-2"><p><a href="https://code.visualstudio.com/">Visual Studio Code</a> 是一款免费的、轻量级且可扩展的<strong>代码编辑器</strong>,适用于构建 Web、桌面和移动应用</p><p>安装完 VSCode 后是不能立马使用<strong>终端</strong>来对Hexo博客输入命令的,需要在<strong>终端</strong>中执行如下操作:</p><ol><li>鼠标右键以 <strong>管理员身份</strong> 运行 Visual Studio Code,在<strong>终端</strong>中执行 <strong>get-ExecutionPolicy</strong>,显示 <strong>Restricted(禁止)</strong></li><li>执行 <strong>set-ExecutionPolicy RemoteSigned</strong> 后再执行 get-ExecutionPolicy,显示 <strong>RemoteSigned(解锁)</strong></li></ol><p>Visual Studio Code 默认<strong>文件夹合并</strong>,可以在<strong>文件 - 首选项 - 设置</strong>上进行如下操作:<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/newblog/foldercompat.webp"></p><blockquote><p>小技巧:可以对 VSCode 下载 <a href="https://marketplace.visualstudio.com/items?itemName=vscode-icons-team.vscode-icons">vscode-icons</a> 来美化默认的文件夹样式</p></blockquote><button type="button" class="tab-to-top" aria-label="scroll to top"><i class="fas fa-arrow-up"></i></button></div></div></div><h2 id="安装-Hexo"><a href="#安装-Hexo" class="headerlink" title="安装 Hexo"></a>安装 Hexo</h2><p>Windows上可以按下快捷键 <strong>Win + R</strong> 打开运行 <strong>cmd</strong> 来启动<strong>命令提示符窗口</strong>,执行如下指令:</p><figure class="highlight bash"><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"># 安装Hexo</span></span><br><span class="line">npm install -g hexo-cli</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看Hexo版本</span></span><br><span class="line">hexo -v</span><br></pre></td></tr></table></figure><p>这时我们就可以利用如下指令在本地电脑上新建一个 HexoBlog 文件夹,里面就有运行Hexo博客的文件了:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 初始化Hexo博客</span></span><br><span class="line">hexo init HexoBlog</span><br></pre></td></tr></table></figure><p>初始化完成后,会生成相应的文件夹。下面对这些文件夹进行简单的介绍:</p><blockquote><p><strong>node_modules</strong>:依赖包<br><strong>public</strong>:存放生成的页面<br><strong>scaffolds</strong>:生成文章的一些模板<br><strong>source</strong>:用来存放你的文章<br><strong>themes</strong>:主题<br><strong>.gitignore</strong>:发布时忽略的文件(可忽略)<br><strong>_config.landscape.yml</strong>:主题的配置文件<br><strong>config.yml</strong>:博客的配置文件<br><strong>package.json</strong>:项目名称、描述、版本、运行和开发等信息</p></blockquote><p>此时依次执行如下指令(<strong>Hexo三连</strong>):</p><blockquote><p><strong>hexo clean</strong>:清除缓存文件(db.json)和已生成的静态文件(public),缩写为 <strong>hexo cl</strong><br><strong>hexo generate</strong>:生成静态文件,缩写为 <strong>hexo g</strong><br><strong>hexo server</strong>:启动服务器,缩写为 <strong>hexo s</strong></p></blockquote><p>在浏览器中打开此<a href="http://localhost:4000/">链接</a>就可以访问到Hexo博客默认的<strong>landscape主题</strong>了<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/newblog/hexobuild.webp"></p><h2 id="挂载博客"><a href="#挂载博客" class="headerlink" title="挂载博客"></a>挂载博客</h2><p>在 <a href="https://git-scm.com/downloads">Git官网</a> 上根据你的操作系统下载安装。安装好 Git 后,鼠标右键就可以看到 Git BASH Here,执行如下指令:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置你的用户名,需要跟Github相同</span></span><br><span class="line">git config --global user.name <span class="string">"你的用户名"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置你的邮箱,需要跟Github相同</span></span><br><span class="line">git config --global user.email <span class="string">"你的邮箱"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看配置的是否成功</span></span><br><span class="line">git config --global --list</span><br></pre></td></tr></table></figure><p>在 <a href="https://github.com/">Github官网</a> 上点击右上角的 <strong>+</strong> 按钮,选择 <strong>New repository</strong>,创建一个 <strong><用户名>.github.io</strong> 的仓库<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/newblog/repositorybuild.webp"></p><p>创建完成后,需要一个<strong>密钥</strong>来让你的计算机可以<strong>连接 Github</strong>,可以在命令提示符窗口通过如下指令来生成:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成密钥</span></span><br><span class="line">ssh-keygen -t rsa -C <span class="string">"你的邮箱"</span></span><br></pre></td></tr></table></figure><p>在你的<strong>C盘用户文件夹内</strong>生成 <strong>.ssh</strong> 这个文件夹。其中 <strong>id_rsa.pub</strong> 里的内容就是我们要在Github上配置的<strong>密钥</strong></p><p>在 Github 右上角头选择 <strong>Settings</strong>,进入设置页后选择 <strong>SSH and GPG keys</strong>,然后点击 <strong>New SSH key</strong> 配置密钥<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/newblog/idrsapub.webp"></p><p>执行如下指令,如果你看到 <strong>You’ve successfully authenticated</strong> 出现,就说明你计算机与 Github 连接成功了</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看密钥是否配置成功</span></span><br><span class="line">ssh -T [email protected]</span><br></pre></td></tr></table></figure><p>在 <a href="https://vercel.com/">Vercel官网</a> 中通过<strong>授权 Github</strong> 的方式注册一个账号。点击此<a href="https://vercel.com/new">链接</a>导入 Github 上的 Hexo博客 仓库,导入后点击 <strong>Deploy</strong><br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/newblog/vercelbuild.webp"></p><blockquote><p>上图的 <strong>Domains</strong> 可以自定义你的博客<strong>域名</strong></p></blockquote><p>在 VSCode 上的<strong>终端</strong>上执行如下命令安装该插件,该插件可以将本地的博客文件上传到 GitHub仓库 中</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install hexo-deployer-git --save</span><br></pre></td></tr></table></figure><p>在博客配置文件 <strong>_config.yml</strong> 中的 <strong>deploy</strong> 填写如下信息:</p><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></pre></td><td class="code"><pre><span class="line"><span class="attr">deploy:</span></span><br><span class="line"> <span class="attr">type:</span> <span class="string">git</span></span><br><span class="line"> <span class="attr">repository:</span> <span class="string">[email protected]:Fomalhaut-Blog/Fomalhaut-Blog.github.io.git</span> <span class="comment"># 填写仓库的SSH</span></span><br><span class="line"> <span class="attr">branch:</span> <span class="string">main</span></span><br></pre></td></tr></table></figure><p>在这之后就可以通过 <strong>hexo deploy</strong> 或 <strong>hexo d</strong> 来部署我们的网站了。这时通过 <strong><用户名>.github.io</strong> 就可以访问Hexo博客 了</p><blockquote><p><strong><用户名></strong> 替换为你的 Github用户名</p></blockquote>]]></content>
<summary type="html">搭建自已的Hexo博客</summary>
<category term="Hexo博客" scheme="https://blhorizon.github.io/categories/Hexo%E5%8D%9A%E5%AE%A2/"/>
<category term="搭建" scheme="https://blhorizon.github.io/tags/%E6%90%AD%E5%BB%BA/"/>
</entry>
<entry>
<title>Mysql数据库基础(一)</title>
<link href="https://blhorizon.github.io/posts/6c7926d0.html"/>
<id>https://blhorizon.github.io/posts/6c7926d0.html</id>
<published>2024-04-17T02:06:36.000Z</published>
<updated>2024-05-02T01:24:54.776Z</updated>
<content type="html"><![CDATA[<blockquote><p>高效的管理和存储数据才是软件系统的核心!</p></blockquote><h2 id="Mysql概述"><a href="#Mysql概述" class="headerlink" title="Mysql概述"></a>Mysql概述</h2><p>数据库(Database)是按照数据结构来组织、存储和管理数据的仓库</p><p>数据库管理系统(Database Management System)是一种操纵和管理数据库的大型软件,用于建立、使用和维护数据库,简称DBMS。它对数据库进行统一的管理和控制,以保证数据库的安全性和完整性</p><p>关系型数据库是建立在关系模型基础上,由多张相互连接的<strong>二维表</strong>组成的数据库,其特点:</p><ul><li>表结构进行数据存储,格式统一,便于维护</li><li>使用SQL语言操作,标准统一,使用方便<blockquote><p>SQL 是用于访问和处理关系型数据库的<strong>标准</strong>的计算机语言</p></blockquote></li></ul><h2 id="Mysql安装"><a href="#Mysql安装" class="headerlink" title="Mysql安装"></a>Mysql安装</h2><p>下载Mysql的安装包:<a href="https://dev.mysql.com/downloads/installer/">8.0.36</a>,具体安装步骤可参考:<a href="https://zhuanlan.zhihu.com/p/687170328">Mysql 8.0 – 最新版本安装(保姆级教程)</a></p><p>可以通过命令 <strong>net start mysql80</strong> 启动mysql,也可以通过命令 <strong>net stop mysql80</strong> 来停止mysql</p><blockquote><p>mysql80 是在安装mysql时注册到Windows上的服务名称</p></blockquote><p>在设置好环境变量后,可以在 cmd窗口 以 <strong>mysql [-h localhost] [-P 3306] -u root -p</strong> 来连接mysql</p><p>点击<a href="https://kkdaj.lanzouq.com/iCJfT04o3pkh">链接</a>(密码:ec23)下载 <strong>Navicate</strong> 及其破解软件,具体破解流程可参考:<a href="https://www.cnblogs.com/kkdaj/p/16260681.html">NavicatPremium16破解</a></p>]]></content>
<summary type="html"><blockquote>
<p>高效的管理和存储数据才是软件系统的核心!</p>
</blockquote>
<h2 id="Mysql概述"><a href="#Mysql概述" class="headerlink" title="Mysql概述"></a>Mysql概述</h</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Mysql数据库基础" scheme="https://blhorizon.github.io/tags/Mysql%E6%95%B0%E6%8D%AE%E5%BA%93%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Linux简单操作(五)</title>
<link href="https://blhorizon.github.io/posts/cd6ed668.html"/>
<id>https://blhorizon.github.io/posts/cd6ed668.html</id>
<published>2024-04-16T09:31:20.000Z</published>
<updated>2024-04-16T09:42:52.256Z</updated>
<content type="html"><![CDATA[<p>所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在。但是目前我们使用比较多的是 vim 编辑器。</p><p>vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性,方便程序设计</p><p>Vim 是从 vi 发展出来的一个文本编辑器。代码补全、编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用。</p><p>简单的来说, vi 是老式的字处理器,不过功能已经很齐全了,但是还是有可以进步的地方。 vim 则可以说是程序开发者的一项很好用的工具。</p><p>以下是普通模式常用的几个命令:</p><ul><li>i – 切换到输入模式,在光标当前位置开始输入文本。</li><li>x – 删除当前光标所在处的字符。</li><li>: – 切换到底线命令模式,以在最底一行输入命令。</li><li>a – 进入插入模式,在光标下一个位置开始输入文本。</li><li>o:在当前行的下方插入一个新行,并进入插入模式。</li><li>O – 在当前行的上方插入一个新行,并进入插入模式。</li><li>dd – 剪切当前行。</li><li>yy – 复制当前行。</li><li>p(小写) – 粘贴剪贴板内容到光标下方。</li><li>P(大写)– 粘贴剪贴板内容到光标上方。</li><li>u – 撤销上一次操作。</li><li>Ctrl + r – 重做上一次撤销的操作。</li><li>:w – 保存文件。</li><li>:q – 退出 Vim 编辑器。</li><li>:q! – 强制退出Vim 编辑器,不保存修改。</li></ul><blockquote><p>在命令模式下按下 i 就进入了输入模式,使用 Esc 键可以返回到普通模式</p></blockquote><p>在输入模式中,可以使用以下按键:</p><ul><li>字符按键以及Shift组合,输入字符</li><li>ENTER,回车键,换行</li><li>BACK SPACE,退格键,删除光标前一个字符</li><li>DEL,删除键,删除光标后一个字符</li><li>方向键,在文本中移动光标</li><li>HOME/END,移动光标到行首/行尾</li><li>Page Up/Page Down,上/下翻页</li><li>Insert,切换光标为输入/替换模式,光标将变成竖线/下划线</li><li>ESC,退出输入模式,切换到命令模式</li></ul><p>在命令模式下按下 :(英文冒号)就进入了底线命令模式:</p><ul><li>:w:保存文件。</li><li>:q:退出 Vim 编辑器。</li><li>:wq:保存文件并退出 Vim 编辑器。</li><li>:q!:强制退出Vim编辑器,不保存修改。</li></ul><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Linux%E7%AE%80%E5%8D%95%E4%BD%A0%E6%93%8D%E4%BD%9C/vim-vi-workmodel.png"></p>]]></content>
<summary type="html"><p>所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在。但是目前我们使用比较多的是 vim 编辑器。</p>
<p>vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正确性,方便程序设计</p>
<p>Vim 是从 vi 发展出来</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Linux简单操作" scheme="https://blhorizon.github.io/tags/Linux%E7%AE%80%E5%8D%95%E6%93%8D%E4%BD%9C/"/>
</entry>
<entry>
<title>Linux简单操作(四)</title>
<link href="https://blhorizon.github.io/posts/cbc58115.html"/>
<id>https://blhorizon.github.io/posts/cbc58115.html</id>
<published>2024-04-15T04:09:27.000Z</published>
<updated>2024-04-16T09:28:29.039Z</updated>
<content type="html"><![CDATA[<h2 id="ln-命令"><a href="#ln-命令" class="headerlink" title="ln 命令"></a>ln 命令</h2><p>ln(英文全拼:link files)命令是一个非常重要命令,它的功能是<strong>为某一个文件在另外一个位置建立一个同步的链接</strong></p><blockquote><p>当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在某个固定的目录,放上该文件,然后在其它的目录下用ln命令链接(link)它就可以,不必重复的占用磁盘空间</p></blockquote><p>语法: <strong>ln [参数][源文件或目录][目标文件或目录]</strong><br>必要参数:</p><ul><li>–backup[=CONTROL] 备份已存在的目标文件</li><li>-b 类似 –backup ,但不接受参数</li><li>-d 允许超级用户制作目录的硬链接</li><li>-f 强制执行</li><li>-i 交互模式,文件存在则提示用户是否覆盖</li><li>-n 把符号链接视为一般目录</li><li><strong>-s</strong> 软链接(符号链接)</li><li>-v 显示详细的处理过程<br>选择参数:</li><li>-S “-S<字尾备份字符串> “或 “–suffix=<字尾备份字符串>”</li><li>-V “-V<备份方式>”或”–version-control=<备份方式>”</li><li>–help 显示帮助信息</li><li>–version 显示版本信息</li></ul><p>Linux链接分两种,一种被称为<strong>硬链接</strong>(Hard Link),另一种被称为<strong>符号链接</strong>(Symbolic Link)。默认情况下,ln命令产生硬链接</p><ul><li><p>符号连接(Symbolic Link),也叫软连接。软链接文件有类似于Windows的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有<strong>另一文件的位置信息</strong></p><figure class="highlight shell"><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">[moe@master ~]$ ln -s d.txt d_soft_link</span><br><span class="line">[moe@master ~]$ ll</span><br><span class="line">总用量 16</span><br><span class="line">...</span><br><span class="line">drwxrwxr-x. 4 moe moe 54 4月 15 11:04 dongman</span><br><span class="line">lrwxrwxrwx 1 moe moe 5 4月 15 15:59 d_soft_link -> d.txt</span><br><span class="line">-rw-rw-r-- 1 moe moe 22 4月 15 10:43 d.txt</span><br><span class="line">...</span><br><span class="line">[moe@master ~]$ cat d_soft_link </span><br><span class="line">你好我是测试者</span><br><span class="line">[moe@master ~]$ cat d.txt </span><br><span class="line">你好我是测试者</span><br><span class="line">[moe@master ~]$ gedit d.txt </span><br><span class="line">[moe@master ~]$ cat d_soft_link </span><br><span class="line">你好我是测试者22</span><br><span class="line">[moe@master ~]$ cat d.txt </span><br><span class="line">你好我是测试者22</span><br></pre></td></tr></table></figure><p>上面脚本是在相同目录创建这个软链接。当在不同目录时,需要<strong>使用绝对路径</strong>,否则会报红,不能使用,因为软链接记录的是文件的位置信息</p><figure class="highlight shell"><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">[moe@master ~]$ ln -s ~/d.txt dongman/d2_soft_link</span><br><span class="line">[moe@master ~]$ ll dongman/</span><br><span class="line">总用量 4</span><br><span class="line">-rw-rw-r-- 1 moe moe 0 4月 15 10:59 a.txt</span><br><span class="line">-rw-rw-r-- 1 moe moe 22 4月 15 10:44 c.txt</span><br><span class="line">lrwxrwxrwx 1 moe moe 15 4月 15 16:00 d2_soft_link -> /home/moe/d.txt</span><br><span class="line">drwxrwxr-x 2 moe moe 6 4月 15 11:04 nv</span><br><span class="line">drwxrwxr-x 5 moe moe 43 4月 15 11:10 test</span><br><span class="line">[moe@master ~]$ ln -s d.txt dongman/d3_soft_link</span><br><span class="line">[moe@master ~]$ ll dongman/</span><br><span class="line">总用量 4</span><br><span class="line">-rw-rw-r-- 1 moe moe 0 4月 15 10:59 a.txt</span><br><span class="line">-rw-rw-r-- 1 moe moe 22 4月 15 10:44 c.txt</span><br><span class="line">lrwxrwxrwx 1 moe moe 15 4月 15 16:00 d2_soft_link -> /home/moe/d.txt</span><br><span class="line">lrwxrwxrwx 1 moe moe 5 4月 15 16:04 d3_soft_link -> d.txt(报红)</span><br><span class="line">drwxrwxr-x 2 moe moe 6 4月 15 11:04 nv</span><br><span class="line">drwxrwxr-x 5 moe moe 43 4月 15 11:10 test</span><br><span class="line">[moe@master ~]$ cat dongman/d3_soft_link </span><br><span class="line">cat: dongman/d3_soft_link: 没有那个文件或目录</span><br></pre></td></tr></table></figure><blockquote><p>可以给目录创建一个软链接,同时需要注意的是软链接只记录文件的位置信息,所以它占用的空间不会跟原文件相同</p></blockquote></li><li><p>硬连接指通过<strong>索引节点</strong>来进行连接。在Linux的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在Linux中,多个文件名指向同一索引节点是存在的。一般这种连接就是硬连接。硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,<strong>文件真正删除的条件是与之相关的所有硬连接文件均被删除</strong></p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">[moe@master dongman]$ ln c.txt c_hard_link</span><br><span class="line">[moe@master dongman]$ ll</span><br><span class="line">总用量 8</span><br><span class="line">-rw-rw-r-- 1 moe moe 0 4月 15 10:59 a.txt</span><br><span class="line">-rw-rw-r-- 2 moe moe 22 4月 15 10:44 c_hard_link</span><br><span class="line">-rw-rw-r-- 2 moe moe 22 4月 15 10:44 c.txt</span><br><span class="line">lrwxrwxrwx 1 moe moe 15 4月 15 16:00 d2_soft_link -> /home/moe/d.txt</span><br><span class="line">drwxrwxr-x 2 moe moe 6 4月 15 11:04 nv</span><br><span class="line">drwxrwxr-x 5 moe moe 43 4月 15 11:10 test</span><br><span class="line">[moe@master dongman]$ ln c.txt c1_hard_link</span><br><span class="line">[moe@master dongman]$ ll</span><br><span class="line">总用量 12</span><br><span class="line">-rw-rw-r-- 1 moe moe 0 4月 15 10:59 a.txt</span><br><span class="line">-rw-rw-r-- 3 moe moe 22 4月 15 10:44 c1_hard_link</span><br><span class="line">-rw-rw-r-- 3 moe moe 22 4月 15 10:44 c_hard_link</span><br><span class="line">-rw-rw-r-- 3 moe moe 22 4月 15 10:44 c.txt</span><br><span class="line">lrwxrwxrwx 1 moe moe 15 4月 15 16:00 d2_soft_link -> /home/moe/d.txt</span><br><span class="line">drwxrwxr-x 2 moe moe 6 4月 15 11:04 nv</span><br><span class="line">drwxrwxr-x 5 moe moe 43 4月 15 11:10 test</span><br><span class="line">[moe@master dongman]$ ln nv nv_hard_link</span><br><span class="line">ln: "nv": 不允许将硬链接指向目录</span><br></pre></td></tr></table></figure><blockquote><p>可以看出这个<strong>2和3</strong>代表该文件目前有几个索引,只有当这些索引链接的文件都被删除时该文件才会被真正删除,相当于一个<strong>实时的备份文件(并不是简单拷贝)</strong>,防止误删。同时需要注意<strong>硬链接不能链接目录</strong></p></blockquote></li></ul><h2 id="grep-命令"><a href="#grep-命令" class="headerlink" title="grep 命令"></a>grep 命令</h2><p>Linux系统中 grep(Global Regular Expression Print) 命令是一种<strong>强大的文本搜索工具</strong>,它能使用正则表达式搜索文本,并把匹配的行打印出来</p><p>语法:<strong>grep [options] pattern [files]</strong><br>常用选项::</p><ul><li><strong>-i</strong>:忽略大小写进行匹配。</li><li>-v:反向查找,只打印不匹配的行。</li><li><strong>-n</strong>:显示匹配行的行号。</li><li>-r:递归查找子目录中的文件。</li><li>-l:只打印匹配的文件名。</li><li>-c:只打印匹配的行数。</li></ul><figure class="highlight shell"><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">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link 公共 视频 文档 音乐</span><br><span class="line">b b.txt dongman1 d.txt 模板 图片 下载 桌面</span><br><span class="line">[moe@master ~]$ grep -ni 你好 b.txt</span><br><span class="line">3:hello你好</span><br><span class="line">[moe@master ~]$ grep -ni 你好 . -r</span><br><span class="line">匹配到二进制文件 ./.cache/tracker/meta.db-wal</span><br><span class="line">./dongman/c.txt:1:你好我是测试者</span><br><span class="line">./dongman/c_hard_link:1:你好我是测试者</span><br><span class="line">./dongman/c1_hard_link:1:你好我是测试者</span><br><span class="line">./b.txt:3:hello你好</span><br><span class="line">./d.txt:1:你好我是测试者22</span><br><span class="line">./b_hard_link:3:hello你好</span><br></pre></td></tr></table></figure><blockquote><p>拓展:<a href="https://blog.csdn.net/qq_51010919/article/details/130714120">Linux-grep的详细使用</a></p></blockquote><h2 id="find-命令"><a href="#find-命令" class="headerlink" title="find 命令"></a>find 命令</h2><p>find 命令用于在指定目录下查找文件和目录</p><p>语法:<strong>find [路径] [匹配条件] [动作]</strong><br>参数说明 :<br>路径 是要查找的目录路径,可以是一个目录或文件名,也可以是多个路径,多个路径之间用空格分隔,如果未指定路径,则默认为当前目录。expression 是可选参数,用于指定查找的条件,可以是文件名、文件类型、文件大小等等。匹配条件 中可使用的选项有二三十个之多,以下列出最常用的部份:</p><ul><li><strong>-name</strong> pattern:按文件名查找,支持使用通配符 * 和 ?。</li><li>-type type:按文件类型查找,可以是 f(普通文件)、d(目录)、l(符号链接)等。</li><li>-size [+-]size[cwbkMG]:按文件大小查找,支持使用 + 或 - 表示大于或小于指定大小,单位可以是 c(字节)、w(字数)、b(块数)、k(KB)、M(MB)或 G(GB)。</li><li>-mtime days:按修改时间查找,支持使用 + 或 - 表示在指定天数前或后,days 是一个整数表示天数。</li><li>-user username:按文件所有者查找。</li><li>-group groupname:按文件所属组查找。</li></ul><figure class="highlight shell"><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">[moe@master ~]$ find -name b.txt</span><br><span class="line">./b.txt</span><br><span class="line">[moe@master ~]$ find ~ -name b.txt</span><br><span class="line">/home/moe/b.txt</span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">使用单引号包含</span></span><br><span class="line">[moe@master ~]$ find ~ -name '*txt'</span><br></pre></td></tr></table></figure><h2 id="tar-命令"><a href="#tar-命令" class="headerlink" title="tar 命令"></a>tar 命令</h2><p>tar(英文全拼:tape archive )命令用于<strong>备份文件</strong>。是创建和解压归档文件的命令行工具,可以用来将多个文件和目录组合成一个单一的归档文件,也可以用来解压已经存在的归档文件<br>简单语法:tar [选项] 压缩包名 压缩文件 [-C] [存放目录]<br>常用选项:</p><ul><li>c或–create 建立新的备份文件</li><li>-C<目的目录>或–directory=<目的目录> 将压缩包指定到目的目录</li><li>-v或–verbose 显示指令执行过程</li><li>-x或–extract或–get 从备份文件中还原文件,即解压</li><li>-t或–list 列出备份文件的内容</li><li>-f<备份文件>或–file=<备份文件> 指定备份文件</li></ul><figure class="highlight shell"><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">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link 公共 视频 文档 音乐</span><br><span class="line">b b.txt dongman1 d.txt 模板 图片 下载 桌面</span><br><span class="line">[moe@master ~]$ tar -cf txt.tar b.txt d.txt </span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link txt.tar(红色显示) 模板 图片 下载 桌面</span><br><span class="line">b b.txt dongman1 d.txt 公共 视频 文档 音乐</span><br><span class="line">[moe@master ~]$ tar -cf txt.tar b.txt d.txt -C dongman</span><br><span class="line">[moe@master ~]$ ls dongman</span><br><span class="line">a.txt c1_hard_link c_hard_link c.txt d2_soft_link nv test</span><br><span class="line">[moe@master ~]$ tar -xf txt.tar -C dongman</span><br><span class="line">[moe@master ~]$ ls dongman</span><br><span class="line">a.txt b.txt c1_hard_link c_hard_link c.txt d2_soft_link d.txt nv test</span><br></pre></td></tr></table></figure><blockquote><p>可以看出只能将解压的文件移动到指定目录中去。其中<strong>f</strong>这个选项必须放在后面。拓展:<a href="https://www.runoob.com/linux/linux-comm-tar.html">tar命令</a></p></blockquote><h2 id="gzip-命令"><a href="#gzip-命令" class="headerlink" title="gzip 命令"></a>gzip 命令</h2><p>gzip命令用于<strong>压缩文件</strong>。是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多出”<strong>.gz</strong>“的扩展名<br>简单语法:<strong>gzip [选项] 压缩包</strong><br>常用选项:</p><ul><li>d 解压</li><li>-r或–recursive 递归处理,将指定目录下的所有文件及子目录一并处理</li></ul><figure class="highlight shell"><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">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link txt.tar 模板 图片 下载 桌面</span><br><span class="line">b b.txt dongman1 d.txt 公共 视频 文档 音乐</span><br><span class="line">[moe@master ~]$ gzip -d txt.tar</span><br><span class="line">gzip: txt.tar: unknown suffix -- ignored</span><br><span class="line">[moe@master ~]$ gzip -r txt.tar</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link txt.tar.gz 模板 图片 下载 桌面</span><br><span class="line">b b.txt dongman1 d.txt 公共 视频 文档 音乐</span><br><span class="line">[moe@master ~]$ gzip -d txt.tar.gz </span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link txt.tar 模板 图片 下载 桌面</span><br><span class="line">b b.txt dongman1 d.txt 公共 视频 文档 音乐</span><br></pre></td></tr></table></figure><blockquote><p>拓展:<a href="https://www.runoob.com/linux/linux-comm-gzip.html">gzip命令</a></p></blockquote><p>一步到位:</p><ul><li>以gzip的方式打包且压缩:<strong>tar czf 名称.tar.gz 目标文件</strong><figure class="highlight shell"><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">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link txt.tar 模板 图片 下载 桌面</span><br><span class="line">b b.txt dongman1 d.txt 公共 视频 文档 音乐</span><br><span class="line">[moe@master ~]$ tar czf test.tar.gz b.txt d.txt </span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b_hard_link dongman d_soft_link test.tar.gz 公共 视频 文档 音乐</span><br><span class="line">b b.txt dongman1 d.txt txt.tar 模板 图片 下载 桌面</span><br></pre></td></tr></table></figure></li><li>以gzip的方式解压并解包:<strong>tar zxf 名称.tar.gz -C 目标目录</strong><figure class="highlight shell"><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">[moe@master ~]$ tar zxf test.tar.gz -C dongman1</span><br><span class="line">[moe@master ~]$ ls dongman1</span><br><span class="line">a.txt b.txt c1_hard_link c_hard_link c.txt d2_soft_link d.txt nv test</span><br></pre></td></tr></table></figure></li></ul><h2 id="bzip2-命令"><a href="#bzip2-命令" class="headerlink" title="bzip2 命令"></a>bzip2 命令</h2><p>bzip2命令是 <strong>.bz2文件</strong> 的压缩程序。bzip2采用新的压缩演算法,压缩效果比传统的LZ77/LZ78压缩演算法来得好。若没有加上任何参数,bzip2压缩完文件后会产生.bz2的压缩文件,并删除原始的文件</p><p>与gzip用法类似,这里也可以一步到位:</p><ul><li>以bzip2的方式打包且压缩:<strong>tar cjf 名称.tar.gz 目标文件</strong><figure class="highlight shell"><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">[moe@master ~]$ mkdir bzip2</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt dongman1 test.tar.gz 模板 文档 桌面</span><br><span class="line">b bzip2 d_soft_link txt.tar 视频 下载</span><br><span class="line">b_hard_link dongman d.txt 公共 图片 音乐</span><br><span class="line">[moe@master ~]$ tar cjf test.tar.bz2 b.txt d.txt </span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt dongman1 test.tar.bz2 公共 图片 音乐</span><br><span class="line">b bzip2 d_soft_link test.tar.gz 模板 文档 桌面</span><br><span class="line">b_hard_link dongman d.txt txt.tar 视频 下载</span><br></pre></td></tr></table></figure></li><li>以bzip2的方式解压并解包:<strong>tar zxf 名称.tar.gz -C 目标目录</strong><figure class="highlight shell"><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">[moe@master ~]$ tar xjf test.tar.bz2 -C bzip2/</span><br><span class="line">[moe@master ~]$ ls bzip2/</span><br><span class="line">b.txt d.txt</span><br></pre></td></tr></table></figure></li></ul><h2 id="zip命令"><a href="#zip命令" class="headerlink" title="zip命令"></a>zip命令</h2><p>zip 命令用于压缩文件。zip 是个使用广泛的压缩程序,压缩后的文件后缀名为 <strong>.zip</strong><br>通过 <strong>zip -r 压缩名称(不需要写后缀码) 压缩文件</strong> 来压缩文件</p><figure class="highlight shell"><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">[moe@master ~]$ zip -r zz b.txt d.txt </span><br><span class="line"> adding: b.txt (deflated 15%)</span><br><span class="line"> adding: d.txt (stored 0%)</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt dongman1 test.tar.bz2 zz.zip 视频 下载</span><br><span class="line">b bzip2 d_soft_link test.tar.gz 公共 图片 音乐</span><br><span class="line">b_hard_link dongman d.txt txt.tar 模板 文档 桌面</span><br></pre></td></tr></table></figure><p>又可以通过 <strong>unzip -d 压缩后的文件目录 压缩文件</strong> 来解压文件</p><figure class="highlight shell"><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">[moe@master ~]$ unzip -d zz zz.zip </span><br><span class="line">Archive: zz.zip</span><br><span class="line"> inflating: zz/b.txt </span><br><span class="line"> extracting: zz/d.txt </span><br><span class="line">[moe@master ~]$ ls zz</span><br><span class="line">b.txt d.txt</span><br></pre></td></tr></table></figure><blockquote><p>压缩后的文件目录如果没有会自动创建</p></blockquote><p>上述三种压缩方式的<strong>压缩率</strong>对比:zip < gzip < bzip2,通用性则是:zip > gzip > bzip2,所以最终常用的压缩方式取中间的 gzip 即可,在Windows中下载的般都是以 tar.gz 为后缀名的文件</p><h2 id="which-命令"><a href="#which-命令" class="headerlink" title="which 命令"></a>which 命令</h2><p>which命令用于查找文件。which指令会在环境变量$PATH设置的目录里查找符合条件的文件</p><p>语法:<strong>which [文件…]</strong><br>参数:</p><ul><li>-n<文件名长度> 指定文件名长度,指定的长度必须大于或等于所有文件中最长的文件名。</li><li>-p<文件名长度> 与-n参数相同,但此处的<文件名长度>包括了文件的路径。</li><li>-w 指定输出时栏位的宽度。</li><li>-V 显示版本信息。</li></ul><p>寻找命令 tar 所在的文件在哪</p><figure class="highlight shell"><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">[moe@master ~]$ which tar</span><br><span class="line">/usr/bin/tar</span><br></pre></td></tr></table></figure><p>切换到<strong>root(超级管理员)</strong>下,可以使用 <strong>su [用户]</strong> 命令</p><figure class="highlight shell"><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">[moe@master ~]$ su</span><br><span class="line">密码:</span><br><span class="line">[root@master moe]# </span><br></pre></td></tr></table></figure><p>修改用户的密码,可以使用 **passwd [用户]**,此时修改的密码不能简单的使用123456来设置</p><figure class="highlight shell"><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">[root@master moe]# passwd root</span><br><span class="line">更改用户 root 的密码 。</span><br><span class="line">新的 密码:</span><br></pre></td></tr></table></figure><blockquote><p>可以<strong>在root用户下</strong>修改其他用户的密码,此时密码可以用简单的数字表示</p></blockquote><p><strong>exit 命令</strong>可以退出当前用户到上一个用户。如果没有,则关闭终端窗口</p><p><strong>who 命令</strong>可以查看当前登录的用户</p><figure class="highlight shell"><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">[moe@master ~]$ who</span><br><span class="line">moe :0 2024-04-16 16:22 (:0)</span><br><span class="line">moe pts/0 2024-04-16 16:27 (:0)</span><br><span class="line">moe pts/1 2024-04-16 17:10 (:0)</span><br></pre></td></tr></table></figure><blockquote><p>pts:代表终端用户、tty:代表操作系统用户。可以通过 pkill -kill -t [pts/tty]</p></blockquote><p>reboot 和 shutdown 命令:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Linux%E7%AE%80%E5%8D%95%E4%BD%A0%E6%93%8D%E4%BD%9C/Snipaste_2024-04-16_17-15-23.png"></p><h2 id="chmod-命令"><a href="#chmod-命令" class="headerlink" title="chmod 命令"></a>chmod 命令</h2><p>chmod(英文全拼:change mode)命令是控制用户<strong>对文件的权限</strong>的命令<br>其中:</p><ul><li>u 表示该文件的拥有者,g 表示与该文件的拥有者属于同一个群体(group)者,o 表示其他以外的人,a 表示这三者皆是。</li><li><strong>+</strong> 表示增加权限、- 表示取消权限、= 表示唯一设定权限。</li><li>r 表示可读取(数字代号为’4’),w 表示可写入(数字代号为’2’),x 表示可执行(数字代号为’1’),- 表示不具有任何权限(数字代号为’0’)<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[moe@master ~]$ ll d.txt</span><br><span class="line">-rw-rw-r-- 1 moe moe 50 4月 15 16:39 d.txt</span><br><span class="line">[moe@master ~]$ chmod u+x d.txt </span><br><span class="line">[moe@master ~]$ ll d.txt</span><br><span class="line">-rwxrw-r-- 1 moe moe 50 4月 15 16:39 d.txt</span><br><span class="line">[moe@master ~]$ chmod u=- d.txt </span><br><span class="line">[moe@master ~]$ ll d.txt </span><br><span class="line">----rw-r-- 1 moe moe 50 4月 15 16:39 d.txt</span><br></pre></td></tr></table></figure><blockquote><p>可以直接通过 <strong>=-</strong> 来取消所有权限</p></blockquote></li></ul>]]></content>
<summary type="html"><h2 id="ln-命令"><a href="#ln-命令" class="headerlink" title="ln 命令"></a>ln 命令</h2><p>ln(英文全拼:link files)命令是一个非常重要命令,它的功能是<strong>为某一个文件在另外一个位置建</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Linux简单操作" scheme="https://blhorizon.github.io/tags/Linux%E7%AE%80%E5%8D%95%E6%93%8D%E4%BD%9C/"/>
</entry>
<entry>
<title>Linux简单操作(三)</title>
<link href="https://blhorizon.github.io/posts/31866e1a.html"/>
<id>https://blhorizon.github.io/posts/31866e1a.html</id>
<published>2024-04-15T02:31:14.000Z</published>
<updated>2024-04-15T04:10:15.037Z</updated>
<content type="html"><![CDATA[<h2 id="cp-命令"><a href="#cp-命令" class="headerlink" title="cp 命令"></a>cp 命令</h2><p>cp(英文全拼:copy file)命令主要用于<strong>复制文件或目录</strong></p><p>语法:<strong>cp [选项] 源文件 目标文件</strong></p><blockquote><p>source(源文件)表示要复制的文件或目录的路径,dest(目标文件)表示复制后的文件或目录的路径</p></blockquote><p>选项说明:</p><ul><li><strong>-a</strong>:此选项通常在复制目录时使用,它保留链接、文件属性,并复制目录下的所有内容。其作用等于 dpR 参数组合。</li><li>-d:复制时保留链接。这里所说的链接相当于 Windows 系统中的快捷方式。</li><li><strong>-r</strong> 或 –recursive:用于复制目录及其所有的子目录和文件,如果要复制目录,需要使用该选项。</li><li>-i 或 –interactive:在复制前提示确认,如果目标文件已存在,则会询问是否覆盖,回答 y 时目标文件将被覆盖。。</li><li>-u 或 –update:仅复制源文件中更新时间较新的文件。</li><li>-v 或 –verbose:显示详细的复制过程。</li><li>-p 或 –preserve:保留源文件的权限、所有者和时间戳信息。</li><li>-f 或 –force:强制复制,即使目标文件已存在也会覆盖,而且不给出提示。</li><li>-l:不复制文件,只是生成链接文件。</li></ul><figure class="highlight shell"><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">[moe@master ~]$ cp a.txt dongman</span><br><span class="line">[moe@master ~]$ ls dongman/</span><br><span class="line">a.txt</span><br></pre></td></tr></table></figure><figure class="highlight shell"><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">[moe@master ~]$ ls test/</span><br><span class="line">dongwu ren</span><br><span class="line">[moe@master ~]$ cp test dongman -r</span><br><span class="line">[moe@master ~]$ ls dongman/</span><br><span class="line">a.txt test</span><br><span class="line">[moe@master ~]$ ls dongman/test</span><br><span class="line">dongwu ren</span><br></pre></td></tr></table></figure><figure class="highlight shell"><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">[moe@master ~]$ cp a.txt d.txt -a</span><br><span class="line">[moe@master ~]$ ll</span><br><span class="line">总用量 20</span><br><span class="line">-rw-rw-r--. 1 moe moe 6 4月 15 00:04 a</span><br><span class="line">-rw-rw-r-- 1 moe moe 22 4月 15 10:43 a.txt</span><br><span class="line">-rw-rw-r--. 1 moe moe 7 4月 15 00:07 b</span><br><span class="line">-rw-rw-r-- 1 moe moe 0 4月 15 09:26 b.txt</span><br><span class="line">-rw-rw-r-- 1 moe moe 22 4月 15 10:44 c.txt</span><br><span class="line">drwxrwxr-x. 2 moe moe 19 4月 15 10:43 dongman</span><br><span class="line">-rw-rw-r-- 1 moe moe 22 4月 15 10:43 d.txt</span><br><span class="line">drwxrwxr-x. 4 moe moe 31 4月 14 23:46 test</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 22:32 公共</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 18:57 模板</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 18:57 视频</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 18:57 图片</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 18:57 文档</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 18:57 下载</span><br><span class="line">drwxr-xr-x. 2 moe moe 6 4月 14 18:57 音乐</span><br><span class="line">drwxr-xr-x. 2 moe moe 40 4月 14 20:46 桌面</span><br></pre></td></tr></table></figure><h2 id="mv-命令"><a href="#mv-命令" class="headerlink" title="mv 命令"></a>mv 命令</h2><p>mv(英文全拼:move file)命令用来为文件或目录改名、或将文件或目录<strong>移入</strong>其它位置</p><p>语法:<strong>mv [options] source dest</strong><br>选项说明:</p><ul><li>-b: 当目标文件或目录存在时,在执行覆盖前,会为其创建一个备份。</li><li>-i: 如果指定移动的源目录或文件与目标的目录或文件同名,则会先询问是否覆盖旧文件,输入 y 表示直接覆盖,输入 n 表示取消该操作。</li><li>-f: 如果指定移动的源目录或文件与目标的目录或文件同名,不会询问,直接覆盖旧文件(默认)。</li><li>-n: 不要覆盖任何已存在的文件或目录。</li><li>-u:当源文件比目标文件新或者目标文件不存在时,才执行移动操作。</li></ul><p>执行命令: <strong>mv source_file(文件) dest_file(文件)</strong> ,会将源文件名 source_file 改为目标文件名 dest_file</p><figure class="highlight shell"><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">[moe@master ~]$ mv a.txt e.txt</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt dongman e.txt 公共 视频 文档 音乐</span><br><span class="line">b c.txt d.txt test 模板 图片 下载 桌面</span><br><span class="line">[moe@master ~]$ gedit e.txt </span><br><span class="line">[moe@master ~]$ mv e.txt d.txt</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt dongman test 模板 图片 下载 桌面</span><br><span class="line">b c.txt d.txt 公共 视频 文档 音乐</span><br></pre></td></tr></table></figure><blockquote><p>如果 <strong>dest_file文件</strong> 存在,会被覆盖掉</p></blockquote><p>执行命令: <strong>mv source_file(文件) dest_directory(目录)</strong> ,将文件 source_file 移动到目标目录 dest_directory 中</p><figure class="highlight shell"><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">[moe@master ~]$ ls dongman</span><br><span class="line">a.txt test</span><br><span class="line">[moe@master ~]$ gedit dongman/a.txt </span><br><span class="line">[moe@master ~]$ touch a.txt</span><br><span class="line">[moe@master ~]$ mv a.txt dongman/a.txt </span><br><span class="line">[moe@master ~]$ gedit dongman/a.txt </span><br><span class="line"></span><br><span class="line">[moe@master ~]$ mv c.txt dongman</span><br><span class="line">[moe@master ~]$ ls dongman/</span><br><span class="line">a.txt c.txt test</span><br></pre></td></tr></table></figure><blockquote><p>如果 <strong>dest_directory(目录)</strong> 中存在同名的 **source_file(文件)**,会将其覆盖掉</p></blockquote><p>执行命令: <strong>mv source_directory(目录) dest_directory(目录)</strong> ,目录名 dest_directory 不存在则 source_directory 改名为目录名 dest_directory</p><figure class="highlight shell"><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">[moe@master ~]$ mv test dongman/</span><br><span class="line">mv: 无法将"test" 移动至"dongman/test": 文件已存在</span><br><span class="line"></span><br><span class="line">[moe@master ~]$ mkdir nv</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt d.txt test 模板 图片 下载 桌面</span><br><span class="line">b dongman nv 公共 视频 文档 音乐</span><br><span class="line">[moe@master ~]$ mv nv dongman/</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b.txt d.txt 公共 视频 文档 音乐</span><br><span class="line">b dongman test 模板 图片 下载 桌面</span><br><span class="line">[moe@master ~]$ ls dongman/</span><br><span class="line">a.txt c.txt nv test</span><br></pre></td></tr></table></figure><blockquote><p>不能将一个目录移动到已存在该目录的目录中去</p></blockquote><p>执行命令:<strong>mv source_directory(目录) dest_file(文件)</strong> ,错误</p><figure class="highlight shell"><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">[moe@master ~]$ mv dongman/ d.txt</span><br><span class="line">mv: 无法以目录"dongman/" 来覆盖非目录"d.txt"</span><br></pre></td></tr></table></figure><h2 id="输出重定向命令"><a href="#输出重定向命令" class="headerlink" title="输出重定向命令"></a>输出重定向命令</h2><p>Linux允许将命令执行结果重定向到一个文件(不存在则新建一个)中,即本应显示到终端上的执行结果会保存到指定文件中去</p><figure class="highlight shell"><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">[moe@master ~]$ pwd >b.txt</span><br><span class="line">[moe@master ~]$ pwd >>b.txt</span><br></pre></td></tr></table></figure><blockquote><p><strong>></strong> 输出重定向会覆盖原来的内容, <strong>>></strong> 输出重定向会追加到文件的尾部</p></blockquote><h2 id="cat-命令"><a href="#cat-命令" class="headerlink" title="cat 命令"></a>cat 命令</h2><p>cat(英文全拼:concatenate)命令用于将文件的内容<strong>显示到终端界面</strong>上</p><p>语法:<strong>cat [-AbeEnstTuv] [–help] [–version] fileName</strong><br>选项说明:</p><ul><li>-n 或 –number:由 1 开始对所有输出的行数编号。</li><li>-b 或 –number-nonblank:和 -n 相似,只不过对于空白行不编号。</li><li>-s 或 –squeeze-blank:当遇到有连续两行以上的空白行,就代换为一行的空白行。</li><li>-v 或 –show-nonprinting:使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外。</li><li>-E 或 –show-ends : 在每行结束处显示 $。</li><li>-T 或 –show-tabs: 将 TAB 字符显示为 ^I。</li><li>-A, –show-all:等价于 -vET。</li><li>-e:等价于”-vE”选项;</li><li>-t:等价于”-vT”选项;</li></ul><figure class="highlight shell"><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">[moe@master ~]$ cat b.txt </span><br><span class="line">/home/moe</span><br><span class="line">/home/moe</span><br><span class="line">[moe@master ~]$ cat d.txt </span><br><span class="line">你好我是测试者</span><br><span class="line">[moe@master ~]$ cat b.txt d.txt > a.txt</span><br><span class="line">[moe@master ~]$ cat a.txt </span><br><span class="line">/home/moe</span><br><span class="line">/home/moe</span><br><span class="line">你好我是测试者</span><br></pre></td></tr></table></figure><blockquote><p><strong>dev/null</strong>:在类 Unix 系统中,/dev/null 称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操作成功),读取它则会立即得到一个 EOF</p></blockquote><h2 id="more-命令"><a href="#more-命令" class="headerlink" title="more 命令"></a>more 命令</h2><p>more 命令类似 cat ,不过会以一页一页的形式显示,更方便使用者<strong>逐页阅读</strong>,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会往回(back)一页显示,而且还有搜寻字串的功能(与 vi 相似),使用中的说明文件,请按 h </p><p>语法:<strong>more [-dlfpcsu] [-num] [+/pattern] [+linenum] [fileNames..]</strong><br>参数:</p><ul><li>-num 一次显示的行数</li><li>-d 提示使用者,在画面下方显示 [Press space to continue, ‘q’ to quit.] ,如果使用者按错键,则会显示 [Press ‘h’ for instructions.] 而不是 ‘哔’ 声</li><li>-l 取消遇见特殊字元 ^L(送纸字元)时会暂停的功能</li><li>-f 计算行数时,以实际上的行数,而非自动换行过后的行数(有些单行字数太长的会被扩展为两行或两行以上)</li><li>-p 不以卷动的方式显示每一页,而是先清除萤幕后再显示内容</li><li>-c 跟 -p 相似,不同的是先显示内容再清除其他旧资料</li><li>-s 当遇到有连续两行以上的空白行,就代换为一行的空白行</li><li>-u 不显示下引号 (根据环境变数 TERM 指定的 terminal 而有所不同)</li><li>+/pattern 在每个文档显示前搜寻该字串(pattern),然后从该字串之后开始显示</li><li>+num 从第 num 行开始显示</li><li>fileNames 欲显示内容的文档,可为复数个数</li></ul><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[moe@master ~]$ more a.txt </span><br></pre></td></tr></table></figure><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Linux%E7%AE%80%E5%8D%95%E4%BD%A0%E6%93%8D%E4%BD%9C/Snipaste_2024-04-15_11-57-44.png"></p><p>常用操作命令:</p><ul><li>Enter 向下n行,需要定义。默认为1行</li><li>Ctrl+F 向下滚动一屏</li><li>空格键 向下滚动一屏</li><li>Ctrl+B 返回上一屏</li><li>= 输出当前行的行号</li><li>:f 输出文件名和当前行的行号</li><li>V 调用vi编辑器</li><li>!命令 调用Shell,并执行命令</li><li>q 退出more</li></ul><h2 id="管道"><a href="#管道" class="headerlink" title="管道"></a>管道</h2><p>管道:一个命令的输出可以通过管道作为另一个命令的输入</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[moe@master ~]$ ls ~ -al | more</span><br></pre></td></tr></table></figure><blockquote><p>以 <strong>|</strong> 作为这个管道</p></blockquote>]]></content>
<summary type="html"><h2 id="cp-命令"><a href="#cp-命令" class="headerlink" title="cp 命令"></a>cp 命令</h2><p>cp(英文全拼:copy file)命令主要用于<strong>复制文件或目录</strong></p>
<p>语法</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Linux简单操作" scheme="https://blhorizon.github.io/tags/Linux%E7%AE%80%E5%8D%95%E6%93%8D%E4%BD%9C/"/>
</entry>
<entry>
<title>Linux简单操作(二)</title>
<link href="https://blhorizon.github.io/posts/4f754e31.html"/>
<id>https://blhorizon.github.io/posts/4f754e31.html</id>
<published>2024-04-14T13:27:55.000Z</published>
<updated>2024-04-15T03:59:21.438Z</updated>
<content type="html"><![CDATA[<h2 id="cd命令"><a href="#cd命令" class="headerlink" title="cd命令"></a>cd命令</h2><p>cd(英文全拼:change directory)命令用于<strong>改变当前工作目录</strong>的命令,切换到指定的路径</p><p>若目录名称省略,则变换至使用者的 home 目录 (也就是刚 login 时所在的目录)。另外, <strong>~</strong> 也表示为 home 目录 的意思, <strong>.</strong> 则是表示目前所在的目录, <strong>..</strong> 则表示目前目录位置的上一层目录</p><ul><li><p>cd [dirName]</p><blockquote><p>dirName:要切换的目标目录,可以是相对路径或绝对路径</p></blockquote><figure class="highlight shell"><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="meta prompt_"># </span><span class="language-bash">换到绝对路径:指定完整的目录路径来切换到目标目录</span></span><br><span class="line">[moe@master ~]$ cd /dev/block</span><br><span class="line">[moe@master block]$ pwd</span><br><span class="line">/dev/block</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">切换到相对路径:指定相对于当前目录的路径来切换到目标目录</span></span><br><span class="line">[moe@master dev]$ cd disk/by-id/</span><br><span class="line">[moe@master by-id]$ pwd</span><br><span class="line">/dev/disk/by-id</span><br></pre></td></tr></table></figure></li><li><p>切换到上级目录:使用 .. 表示上级目录,可以通过连续多次使用 .. 来切换到更高级的目录</p><figure class="highlight shell"><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">[moe@master by-id]$ cd ..</span><br><span class="line">[moe@master disk]$ cd ..</span><br><span class="line">[moe@master dev]$ pwd</span><br><span class="line">/dev</span><br></pre></td></tr></table></figure></li><li><p>切换到用户主目录(home):使用 ~ 表示当前用户的主目录,可以使用 cd 命令直接切换到主目录</p><figure class="highlight shell"><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">[moe@master dev]$ cd ~</span><br><span class="line">[moe@master ~]$ pwd</span><br><span class="line">/home/moe</span><br></pre></td></tr></table></figure></li><li><p>切换到上次访问的目录:使用 <strong>cd -</strong> 可以切换到上次访问的目录</p><figure class="highlight shell"><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">[moe@master ~]$ cd -</span><br><span class="line">/dev</span><br></pre></td></tr></table></figure><blockquote><p>拓展:<a href="https://blog.csdn.net/qq_45112637/article/details/134323473?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-134323473-blog-130321172.235%5Ev43%5Epc_blog_bottom_relevance_base2&spm=1001.2101.3001.4242.2&utm_relevant_index=4">Centos7安装配置中文输入法</a></p></blockquote></li></ul><h2 id="ls-命令"><a href="#ls-命令" class="headerlink" title="ls 命令"></a>ls 命令</h2><p>ls(英文全拼: list directory contents)命令用于显示指定工作目录下之内容(<strong>列出目前工作目录所含的文件及子目录</strong>)</p><p>语法:<strong>ls [-alrtAFR] [name…]</strong></p><p>参数 :</p><ul><li>-a 显示所有文件及目录 (. 开头的隐藏文件也会列出)</li><li>-d 只列出目录(不递归列出目录内的文件)</li><li>-l 以长格式显示文件和目录信息,包括权限、所有者、大小、创建时间等</li><li>-r 倒序显示文件和目录</li><li>-t 将按照修改时间排序,最新的文件在最前面</li><li>-A 同 -a ,但不列出 “.” (目前目录) 及 “..” (父目录)</li><li>-F 在列出的文件名称后加一符号;例如可执行档则加 “*”, 目录则加 “/“</li><li>-R 递归显示目录中的所有文件和子目录</li></ul><figure class="highlight shell"><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">ls -l # 以长格式显示当前目录中的文件和目录,可简写为 ll</span><br><span class="line">ls -a # 显示当前目录中的所有文件和目录,包括隐藏文件</span><br><span class="line">ls -lh # 以人类可读的方式显示当前目录中的文件和目录大小</span><br><span class="line">ls -t # 按照修改时间排序显示当前目录中的文件和目录</span><br><span class="line">ls -R # 递归显示当前目录中的所有文件和子目录</span><br><span class="line">ls -l /etc/passwd # 显示/etc/passwd文件的详细信息</span><br></pre></td></tr></table></figure><p>列出根目录()下的所有目录:</p><figure class="highlight shell"><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">[moe@master /]$ ls /</span><br><span class="line">bin dev home lib64 mnt proc run srv tmp var</span><br><span class="line">boot etc lib media opt root sbin sys usr</span><br></pre></td></tr></table></figure><p>将 /bin 目录以下所有目录及文件详细资料列出:</p><figure class="highlight shell"><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">[moe@master /]$ ls -lR /bin</span><br><span class="line">lrwxrwxrwx. 1 root root 7 4月 14 18:50 /bin -> usr/bin</span><br></pre></td></tr></table></figure><p>当文件名包含空格、特殊字符或者开始字符为破折号时,可以使用反斜杠(\)进行转义,或者使用引号将文件名括起来。例如:</p><figure class="highlight shell"><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">ls "my file.txt" # 列出文件名为"my file.txt"的文件</span><br><span class="line">ls my\ file.txt # 列出文件名为"my file.txt"的文件</span><br><span class="line">ls -- -filename # 列出文件名为"-filename"的文件</span><br></pre></td></tr></table></figure><p>ls 命令还可以使用通配符进行模式匹配,例如 * 表示匹配任意字符,? 表示匹配一个字符,[…] 表示匹配指定范围内的字符。例如:</p><figure class="highlight shell"><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">ls *.txt # 列出所有扩展名为.txt的文件</span><br><span class="line">ls file?.txt # 列出文件名为file?.txt的文件,其中?表示任意一个字符</span><br><span class="line">ls [abc]*.txt # 列出以a、b或c开头、扩展名为.txt的文件</span><br></pre></td></tr></table></figure><p>列出目前工作目录下所有名称是 s 开头的文件或目录,越新的排越后面:</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ls -ltr s*</span><br></pre></td></tr></table></figure><p>在使用 ls -l 命令时,第一列的字符表示文件或目录的类型和权限。其中第一个字符表示文件类型,例如:</p><ul><li><strong>-</strong> 表示普通文件</li><li>d 表示目录</li><li>l 表示符号链接</li><li>c 表示字符设备文件</li><li>b 表示块设备文件</li><li>s 表示套接字文件</li><li>p 表示管道文件</li></ul><p>在使用 ls -l 命令时,第一列的其余 9 个字符表示文件或目录的访问权限,分别对应三个字符一组的 rwx 权限。例如:</p><ul><li>表示读取权限</li><li>w 表示写入权限</li><li>x 表示执行权限</li><li><strong>-</strong> 表示没有对应权限</li></ul><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">drwxr-xr-x. 144 root root 0 4月 14 18:56 module</span><br></pre></td></tr></table></figure><p>从上面可以看到,每一行都有7列,分别是:</p><ul><li>第一列共10位,第1位表示文档类型,d表示目录,-表示文件,l表示链接文件,d表示可随机存取的设备,如U盘等,c表示一次性读取设备,如鼠标、键盘等。后9位,依次对应三种身份所拥有的权限,身份顺序为:owner、group、others,权限顺序为:readable、writable、executable。如:-r-xr-x—的含义为当前文档是一个文件,拥有者可读、可执行,同一个群组下的用户,可读、可执行,其他人没有任何权限</li><li>第二列表示链接数,表示有多少个文件链接到inode号码</li><li>第三列表示拥有者</li><li>第四列表示所属群组</li><li>第五列表示文档容量大小,单位字节</li><li>第六列表示文档最后修改时间,注意不是文档的创建时间哦</li><li>第七列表示文档名称。以点(.)开头的是隐藏文档</li></ul><h2 id="mkdir-命令"><a href="#mkdir-命令" class="headerlink" title="mkdir 命令"></a>mkdir 命令</h2><p>mkdir(英文全拼:make directory)命令用于<strong>创建目录</strong></p><p>语法:<strong>mkdir [-p] dirName</strong></p><ul><li>-p 确保目录名称存在,不存在的就建一个</li></ul><figure class="highlight shell"><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">[moe@master ~]$ ls</span><br><span class="line">公共 模板 视频 图片 文档 下载 音乐 桌面</span><br><span class="line">[moe@master ~]$ mkdir test</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br></pre></td></tr></table></figure><p>可以同时创建两个目录,以空格格式隔开</p><figure class="highlight shell"><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">[moe@master dongman]$ cd ~</span><br><span class="line">[moe@master ~]$ mkdir test/ren test/dongwu</span><br><span class="line">[moe@master ~]$ cd test</span><br><span class="line">[moe@master test]$ ls</span><br><span class="line">dongwu ren</span><br></pre></td></tr></table></figure><p>如果是在同一个目录下再创建多个目录,可以用 <strong>{}</strong> 来表示,并以<strong>英文逗号</strong>隔开</p><figure class="highlight shell"><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">[moe@master test]$ cd ~</span><br><span class="line">[moe@master ~]$ mkdir dongman/{xuni,fengjing,xiaz}</span><br><span class="line">[moe@master ~]$ cd dongman/</span><br><span class="line">[moe@master dongman]$ ls</span><br><span class="line">daojian fengjing xiaz xuni</span><br></pre></td></tr></table></figure><p>可以通过 <strong>.dirname</strong> 来创建一个隐藏的目录</p><figure class="highlight shell"><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">[moe@master dongman]$ cd ~</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">dongman test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br><span class="line">[moe@master ~]$ mkdir .yinc</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">dongman test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br><span class="line">[moe@master ~]$ ls -a</span><br><span class="line">. .shell_profile .dbus .local .yinc 图片 桌面</span><br><span class="line">.. .shellrc dongman .mozilla 公共 文档</span><br><span class="line">.shell_history .cache .esd_auth .pki 模板 下载</span><br><span class="line">.shell_logout .config .ICEauthority test 视频 音乐</span><br></pre></td></tr></table></figure><blockquote><p>每个目录都会有 <strong>.(当前目录)</strong> 和 <strong>..(上一级目录)</strong> 这两个存在</p></blockquote><h2 id="touch-命令"><a href="#touch-命令" class="headerlink" title="touch 命令"></a>touch 命令</h2><p>touch 命令用于修改文件或者目录的时间属性,包括存取时间和更改时间。若文件不存在,系统会<strong>建立一个新的文件</strong></p><p>语法:<strong>touch [-acfm][-d<日期时间>][-r<参考文件或目录>] [-t<日期时间>][–help][–version][文件或目录…]</strong><br>参数说明:</p><ul><li>a 改变档案的读取时间记录。</li><li>m 改变档案的修改时间记录。</li><li>c 假如目的档案不存在,不会建立新的档案。与 –no-create 的效果一样。</li><li>f 不使用,是为了与其他 unix 系统的相容性而保留。</li><li>r 使用参考档的时间记录,与 –file 的效果一样。</li><li>d 设定时间与日期,可以使用各种不同的格式。</li><li>t 设定档案的时间记录,格式与 date 指令相同。</li><li>–no-create 不会建立新档案。</li><li>–help 列出指令格式。</li><li>–version 列出版本讯息。</li></ul><p>使用指令”touch”时,如果指定的文件不存在,则将创建一个新的空白文件</p><figure class="highlight shell"><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">[moe@master ~]$ touch a</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a dongman test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br></pre></td></tr></table></figure><p>可以通过 <strong>.filename</strong> 来创建一个隐藏的目录创建一个隐藏的文件</p><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">[moe@master ~]$ touch .a</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a dongman test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br><span class="line">[moe@master ~]$ ls -a</span><br><span class="line">. .shell_history .cache .esd_auth .pki 模板 下载</span><br><span class="line">.. .shell_logout .config .ICEauthority test 视频 音乐</span><br><span class="line">a .shell_profile .dbus .local .yinc 图片 桌面</span><br><span class="line">.a .shellrc dongman .mozilla 公共 文档</span><br></pre></td></tr></table></figure><blockquote><p>一般touch命令用的较少,只有想要创建一个空文件时才会使用</p></blockquote><p>使用 <strong>gedit</strong> 命令可以创建(没有的情况下)并打开文件进行编辑</p><figure class="highlight shell"><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">[moe@master ~]$ ls</span><br><span class="line">a dongman test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br><span class="line">[moe@master ~]$ gedit b</span><br><span class="line">[moe@master ~]$ ls</span><br><span class="line">a b dongman test 公共 模板 视频 图片 文档 下载 音乐 桌面</span><br></pre></td></tr></table></figure><h2 id="rm-命令"><a href="#rm-命令" class="headerlink" title="rm 命令"></a>rm 命令</h2><p>rm(英文全拼:remove)命令用于<strong>删除一个文件或者目录</strong></p><p>语法:<strong>rm [options] name…</strong></p><ul><li>-i 删除前逐一询问确认</li><li>-f 即使原档案(文件)属性设为唯读,亦直接删除,无需逐一确认</li><li>-r 将目录及以下之档案(文件)亦逐一删除</li></ul><figure class="highlight shell"><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">[moe@master dongman]$ touch a.txt</span><br><span class="line">[moe@master dongman]$ ls</span><br><span class="line">a.txt daojian fengjing xiaz xuni</span><br><span class="line">[moe@master dongman]$ rm a.txt</span><br><span class="line">[moe@master dongman]$ ls</span><br><span class="line">daojian fengjing xiaz xuni</span><br><span class="line">[moe@master dongman]$ rm daojian</span><br><span class="line">rm: 无法删除"daojian": 是一个目录</span><br><span class="line">[moe@master dongman]$ rm daojian -r</span><br><span class="line">[moe@master dongman]$ ls</span><br><span class="line">fengjing xiaz xuni</span><br><span class="line">[moe@master dongman]$ rm * -r</span><br><span class="line">[moe@master dongman]$ ls -a</span><br><span class="line">. ..</span><br></pre></td></tr></table></figure><blockquote><p>可以看出 <strong>.</strong> 和 <strong>..</strong> 两个目录是删除不了的,同时<strong>rm * -r</strong>是不能<strong>直接</strong>删除隐藏文件的,需要手动逐个删除</p></blockquote><h2 id="clear-命令"><a href="#clear-命令" class="headerlink" title="clear 命令"></a>clear 命令</h2><p>clear 命令用于清除屏幕。本质上是给你翻屏了,以前的命令向上翻就可以看到了</p><blockquote><p>通过 <strong>tab</strong> 键可以自动补全。<strong>ctrl + c</strong>终止当前命令</p></blockquote><h2 id="终端命令格式"><a href="#终端命令格式" class="headerlink" title="终端命令格式"></a>终端命令格式</h2><p>格式:<strong>command [-options] [parameter]</strong></p><ul><li>commmand:命令名,相应功能的英文单词或其缩写</li><li>-options:选项,对命令进行控制,可以省略</li><li>parameter:传给命令的参数,可以零个或多个</li></ul><p>一般终端自带的命令,如<strong>cd</strong>和<strong>pwd</strong>等,通过 <strong>–help</strong>是查看不到帮助文档的,这时可以通过 <strong>man命令</strong> 来查看<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/Linux%E7%AE%80%E5%8D%95%E4%BD%A0%E6%93%8D%E4%BD%9C/Snipaste_2024-04-15_09-06-23.png"></p>]]></content>
<summary type="html"><h2 id="cd命令"><a href="#cd命令" class="headerlink" title="cd命令"></a>cd命令</h2><p>cd(英文全拼:change directory)命令用于<strong>改变当前工作目录</strong>的命令,切换到指</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Linux简单操作" scheme="https://blhorizon.github.io/tags/Linux%E7%AE%80%E5%8D%95%E6%93%8D%E4%BD%9C/"/>
</entry>
<entry>
<title>Linux简单操作(一)</title>
<link href="https://blhorizon.github.io/posts/e044d553.html"/>
<id>https://blhorizon.github.io/posts/e044d553.html</id>
<published>2024-04-14T09:40:21.000Z</published>
<updated>2024-04-14T14:43:52.235Z</updated>
<content type="html"><![CDATA[<h2 id="操作系统"><a href="#操作系统" class="headerlink" title="操作系统"></a>操作系统</h2><p>操作系统(Operation System, OS)是指控制和管理整个计算机系统的硬件和软件资源,并合理的组织和调度计算机的工作和资源的分配,以提供给用户和其它软件方便的接口和环境,它是计算机系统中最基本也是最重要的<strong>系统软件</strong>。</p><blockquote><p>拓展:<a href="https://blog.csdn.net/Royalic/article/details/119999404">计算机操作系统知识点总结(有这一篇就够了!!!)</a>、<a href="https://zhuanlan.zhihu.com/p/105945721">什么是操作系统?</a></p></blockquote><p>不同领域的操作系统有:桌面操作系统、服务器操作系统、嵌入式操作系统、移动设备操作系统</p><h2 id="安装CentOS7"><a href="#安装CentOS7" class="headerlink" title="安装CentOS7"></a>安装CentOS7</h2><p>VMware17虚拟机下载教程:<a href="https://www.bilibili.com/video/BV1Zz4y157AN/?spm_id_from=333.337.search-card.all.click&vd_source=8abceb502969e7de8c2eb9bc66a1d6e3">【2023新版】VMware17虚拟机的下载安装指南</a>,许可证:<strong>MC60H-DWHD5-H80U9-6V85M-8280D</strong><br>在阿里这里下载CentOS7镜像:<a href="https://developer.aliyun.com/mirror/?spm=a2c6h.13651104.mirror-detail.d1002.7162320c71ugFr">CentOS7镜像</a>,选择<strong>OS镜像</strong><br>安装步骤可参考:<a href="https://blog.csdn.net/weixin_43849575/article/details/102996790">CentOS7(Linux)详细安装教程(图文详解)</a></p><h2 id="Linux的文件和目录"><a href="#Linux的文件和目录" class="headerlink" title="Linux的文件和目录"></a>Linux的文件和目录</h2><p>在Windows中文件会存放在一个个<strong>驱动器盘符</strong>中,但是在Linux中则没有这个概念,其所有的文件都是存放在一个<strong>根目录</strong>中的。在CentOS7中根目录有:</p><figure class="highlight shell"><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">[root@master moe]# ls /</span><br><span class="line">bin dev home lib64 mnt proc run srv tmp var</span><br><span class="line">boot etc lib media opt root sbin sys usr</span><br></pre></td></tr></table></figure><p>系统启动必须:</p><ul><li>/boot:放置linux系统启动时用到的核心文件,自己的安装别放在这里。建议单独分区,分区大小100M即可</li><li>/etc:存放所有的系统需要的配置文件和子目录列表,更改目录下的文件可能会导致系统不能启动</li><li>/lib:存放基本代码库(比如c++库),其作用类似于Windows里的DLL文件。几乎所有的应用程序都需要用到这些共享库</li><li>/sys: 这是linux2.6内核的一个很大的变化。该目录下安装了2.6内核中新出现的一个文件系统 sysfs 。sysfs文件系统集成了下面3种文件系统的信息:针对进程信息的proc文件系统、针对设备的devfs文件系统以及针对伪终端的devpts文件系统。该文件系统是内核设备树的一个直观反映。当一个内核对象被创建的时候,对应的文件和目录也在内核对象子系统中</li></ul><p>指令集合:</p><ul><li>/bin:可执行二进制文件的目录,存放着常用的命令ls、tar、mv、cat等</li><li>/sbin:s表示Super User,放置系统管理员使用的可执行命令,如fdisk、shutdown、mount等。与/bin不同的是,这几个目录是给系统管理员root使用的命令,一般用户只能”查看”而不能设置和使用</li></ul><p>外部文件管理:</p><ul><li>/dev :Device(设备)的缩写, 存放的是Linux的外部设备。注意:在Linux中访问设备和访问文件的方式是相同的</li><li>/media:类windows的其他设备,例如U盘、光驱等等,识别后linux会把设备放到这个目录下</li><li>/mnt:临时挂载别的文件系统的,我们可以将光驱挂载在/mnt/上,然后进入该目录就可以查看光驱里的内容了</li></ul><p>临时文件:</p><ul><li>/run:是一个临时文件系统,存储系统启动以来的信息。当系统重启时,这个目录下的文件应该被删掉或清除。如果你的系统上有 /var/run 目录,应该让它指向 run</li><li>/lost+found:一般情况下为空的,系统非法关机后,这里就存放一些文件</li><li>/tmp:这个目录是用来存放一些临时文件的</li></ul><p>账户:</p><ul><li>/root:系统管理员root的家目录,系统第一个启动的分区为/,所以最好将/root和/放置在一个分区下</li><li>/home:存放普通用户的家目录,新增用户账号时,用户的家目录都存放在此目录下。建议单独分区,并设置较大的磁盘空间,方便用户存放数据</li><li>/usr:用户的很多应用程序和文件都放在这个目录下,类似于windows下的program files目录。建议单独分区,设置较大的磁盘空间</li><li>/usr/bin:系统用户使用的应用程序与指令</li><li>/usr/sbin:超级用户使用的比较高级的管理程序和系统守护程序</li><li>/usr/src:内核源代码默认的放置目录</li></ul><p>运行过程中要用:</p><ul><li>/var:存放经常修改的数据,比如程序运行的日志文件(/var/log 目录下)。建议单独分区,设置较大的磁盘空间</li><li>/proc:管理内存空间!虚拟的目录,是系统内存的映射,我们可以直接访问这个目录来,获取系统信息。这个目录的内容不在硬盘上而是在内存里,我们也可以直接修改里面的某些文件来做修改。由于数据都存放于内存中,所以不占用磁盘空间</li></ul><p>扩展用的:</p><ul><li>/opt:给主机额外安装软件所摆放的目录,比如你安装oracle数据库就可以放倒这个目录下,默认是空的。以前的 Linux 系统中,习惯放置在 /usr/local 目录下</li><li>/srv:存放服务启动后需要访问的数据(不用服务器就是空)</li></ul><p>参考:<a href="https://www.runoob.com/linux/linux-system-contents.html">Linux 系统目录结构</a>、 <a href="https://cnblogs.com/heimatengyun/p/12161873.html">linux入门系列2–CentOs图形界面操作及目录结构</a></p>]]></content>
<summary type="html"><h2 id="操作系统"><a href="#操作系统" class="headerlink" title="操作系统"></a>操作系统</h2><p>操作系统(Operation System, OS)是指控制和管理整个计算机系统的硬件和软件资源,并合理的组织和调度计算机的</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="Linux简单操作" scheme="https://blhorizon.github.io/tags/Linux%E7%AE%80%E5%8D%95%E6%93%8D%E4%BD%9C/"/>
</entry>
<entry>
<title>软件测试基础(五)</title>
<link href="https://blhorizon.github.io/posts/f83f843e.html"/>
<id>https://blhorizon.github.io/posts/f83f843e.html</id>
<published>2024-04-14T06:22:37.000Z</published>
<updated>2024-04-14T14:44:02.911Z</updated>
<content type="html"><![CDATA[<h2 id="项目介绍"><a href="#项目介绍" class="headerlink" title="项目介绍"></a>项目介绍</h2><ul><li><p>项目背景<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-34-28.png"></p><blockquote><p>软件是因为什么而来的</p></blockquote></li><li><p>产品定位<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-38-37.png"></p><blockquote><p>这个软件开发出来是什么样子的</p></blockquote></li><li><p>项目目标<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-40-25.png"></p></li><li><p>产品功能架构<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-43-17.png"></p></li></ul><h2 id="项目功能测试"><a href="#项目功能测试" class="headerlink" title="项目功能测试"></a>项目功能测试</h2><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-45-23.png"></p><h3 id="登录功能测试"><a href="#登录功能测试" class="headerlink" title="登录功能测试"></a>登录功能测试</h3><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-46-43.png"></p><ul><li><p>登录需求<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-49-32.png"><br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_14-50-24.png"></p></li><li><p>提取测试点<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_15-12-58.png"><br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_15-25-14.png"></p></li><li><p>设计用例<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_16-29-42.png"><br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_16-30-22.png"></p><blockquote><p>滑块属于验证码这边的<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/Snipaste_2024-04-14_16-42-11.png"></p></blockquote></li></ul><blockquote><p>拓展:<a href="https://zhuanlan.zhihu.com/p/102911463">五大主流浏览器及四大内核</a></p></blockquote><p>还有个发布文章的功能测试:<a href="https://www.bilibili.com/video/BV1TP4y1J7BD?p=71&vd_source=8abceb502969e7de8c2eb9bc66a1d6e3">发布文章需求梳理</a>,这里感觉老师讲的不咋样</p><h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>到这里软件测试基础的已经算结束了。总的来说:</p><ol><li>要学会<strong>根据需求来编写好测试用例</strong></li><li>测试点要<strong>覆盖好</strong></li><li>编写的测试用例不一定要有多好,但是需要<strong>条理清晰易懂</strong>,能够讲明白</li></ol><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%94%E5%A4%A9/1.webp"></p>]]></content>
<summary type="html"><h2 id="项目介绍"><a href="#项目介绍" class="headerlink" title="项目介绍"></a>项目介绍</h2><ul>
<li><p>项目背景<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="软件测试基础" scheme="https://blhorizon.github.io/tags/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>软件测试基础(四)</title>
<link href="https://blhorizon.github.io/posts/b5d9d04a.html"/>
<id>https://blhorizon.github.io/posts/b5d9d04a.html</id>
<published>2024-04-04T03:33:22.000Z</published>
<updated>2024-04-04T14:36:05.834Z</updated>
<content type="html"><![CDATA[<blockquote><p>学习Html是为了未来给<strong>Web项目功能测试</strong>打基础的</p></blockquote><h2 id="认知Html"><a href="#认知Html" class="headerlink" title="认知Html"></a>认知Html</h2><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_11-40-10.png"></p><blockquote><p>Html:<strong>超文本标记语言</strong></p></blockquote><p><strong>Html5基本骨架</strong>组成:</p><figure class="highlight html"><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="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>网页标题<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> 网页内容编写区域</span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><h2 id="常用标签"><a href="#常用标签" class="headerlink" title="常用标签"></a>常用标签</h2><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_11-53-51.png"></p><p>注释:不会被程序执行,用来解释代码的含义。快捷键:<strong>Ctrl + /</strong></p><blockquote><p>在前端中,页面发布上线之前,需要检查所有的注释是否由不恰当的文字出现或去除掉注释</p></blockquote><ul><li><p>标题标签</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>标题标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> 通过 h1 ~ h6 来定义标题,其中 h1 最大</span></span><br><span class="line"><span class="comment"> h1 一般用来代表整个网页的主标题,只能出现一次,利于seo优化</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h1</span>></span>标题1<span class="tag"></<span class="name">h1</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>标题2<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h3</span>></span>标题3<span class="tag"></<span class="name">h3</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h4</span>></span>标题5<span class="tag"></<span class="name">h4</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h5</span>></span>标题5<span class="tag"></<span class="name">h5</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">h6</span>></span>标题6<span class="tag"></<span class="name">h6</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_12-15-15.png"></p></li><li><p>段落标签</p><figure class="highlight html"><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="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>段落标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> 通过 p 来定义段落</span></span><br><span class="line"><span class="comment"> 浏览器会自动在每个 <p> 元素前后添加一个空行</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>Lorem ipsum dolor sit amet consectetur adipisicing elit. A, officiis!<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">p</span>></span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur, libero!<span class="tag"></<span class="name">p</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_12-21-31.png"></p><blockquote><p>在Vscode中可以输入<strong>lorenm+数字</strong>来快速生成一段英文</p></blockquote></li><li><p>超链接标签</p><figure class="highlight html"><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="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>超链接标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><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"> 其中最重要的是 href 属性,它指示链接的目的地</span></span><br><span class="line"><span class="comment"> target 属性指定窗口的打开方式,_blank 新建窗口、_self 在当前窗口</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">a</span> <span class="attr">href</span>=<span class="string">"https://www.bilibili.com/"</span> <span class="attr">target</span>=<span class="string">"_blank"</span>></span>哔哩哔哩<span class="tag"></<span class="name">a</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_12-31-16.png"></p></li><li><p>图片标签</p><figure class="highlight html"><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="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>图片标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> 通过 img 来定义图片,用于在 HTML 页面中嵌入图像</span></span><br><span class="line"><span class="comment"> 从技术上讲,实际上并没有将图像插入到网页中,而是将图像链接到了网页,<img> 标签创建了一个容器,用于引用图像</span></span><br><span class="line"><span class="comment"> 用 src 属性来规定图像的路径,用 alt 属性来显示因某种原因无法显示图像的替代文本</span></span><br><span class="line"><span class="comment"> 可以通过 width、height 来规定图片的长高,否则会以图片的本身大小显示</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">img</span> <span class="attr">src</span>=<span class="string">"../source/images/logo.png"</span> <span class="attr">alt</span>=<span class="string">"显示不出"</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_13-05-24.png"></p><blockquote><p><strong>绝对路径</strong>:目标文件在硬盘上的真实路径(最精确路径)<br><strong>相对路径</strong>:相对当前执行文件所在位置作为起点。其中 <strong>../</strong> 为上一级, <strong>./</strong> 为同级(可以省略)</p></blockquote></li><li><p>换行标签和空格</p><figure class="highlight html"><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="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>换行标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> 通过 br 来定义换行,用于在 HTML 页面中插入单行换行符</span></span><br><span class="line"><span class="comment"> 代码的换行在浏览器中只会当作空格来显示</span></span><br><span class="line"><span class="comment"> <br> 标签只是简单地开始新的一行,而当浏览器遇到 <p> 标签时,通常会在相邻的段落之间插入一些垂直的间距</span></span><br><span class="line"><span class="comment"> 注意:请使用 <br> 标签来插入换行符,而不是用它来增加段落之间的空白。</span></span><br><span class="line"><span class="comment"> <br> 标签是空标签(意味着它没有结束标签,因此这是错误的:<br></br>)</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> 如需<span class="tag"><<span class="name">br</span>></span>在文本中强制<span class="tag"><<span class="name">p</span>></span>换行<span class="tag"><<span class="name">br</span>></span>,请使用 <span class="tag"><<span class="name">br</span>></span>br<span class="tag"><<span class="name">br</span>></span> 元素<span class="tag"></<span class="name">p</span>></span>。</span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_21-04-14.png"></p><blockquote><p>在浏览器中,不论你在<strong>两个字间</strong>有多少空格,都只会显示一个。想要显示多个空格,需要借用 <code>&nbsp;</code></p></blockquote></li><li><p>div和span标签</p><figure class="highlight html"><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></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>div标签和span标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> <div> 标签定义 HTML 文档中的分割或部分(分区或小节),把文档分割为独立的、不同的部分</span></span><br><span class="line"><span class="comment"> <div> 是块级元素。这意味着它的内容自动地开始一个新行</span></span><br><span class="line"><span class="comment"> 对同一个 <div> 元素应用 class 或 id 属性,但是更常见的情况是只应用其中一种</span></span><br><span class="line"><span class="comment"> 这两者的主要差异是,class 用于元素组(类似的元素,或者可以理解为某一类元素),而 id 用于标识单独的唯一的元素</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>div标签<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span>Lorem ipsum dolor sit amet.<span class="tag"></<span class="name">div</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">div</span>></span>Lorem ipsum dolor sit amet consectetur.<span class="tag"></<span class="name">div</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> <span> 标签是一个行内容器,用于标记文本的一部分,或文档的一部分</span></span><br><span class="line"><span class="comment"> <span> 是行内元素。不会自动开始一个新行</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>span标签<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>Lorem ipsum dolor sit amet consectetur.<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">span</span>></span>Lorem ipsum dolor sit, amet consectetur adipisicing.<span class="tag"></<span class="name">span</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_21-27-51.png"></p></li><li><p>列表标签</p><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>列表标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> <ol> 标签定义有序列表。有序列表可以是数字或字母顺序</span></span><br><span class="line"><span class="comment"> <ul> 标签定义无序(带项目符号)列表</span></span><br><span class="line"><span class="comment"> <li> 标签定义列表项,用于有序列表 (<ol>)、无序列表 (<ul>) 和菜单列表 (<menu>) 中</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>有序列表<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">ol</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span>水果<span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span>西瓜<span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">ol</span>></span></span><br><span class="line"></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>无序列表<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">ul</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span>哔哩哔哩<span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">li</span>></span>百度<span class="tag"></<span class="name">li</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">ul</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_21-43-06.png"></p></li><li><p>表单标签</p><figure class="highlight html"><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="meta"><!DOCTYPE <span class="keyword">html</span>></span></span><br><span class="line"><span class="tag"><<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>></span></span><br><span class="line"><span class="tag"><<span class="name">head</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">meta</span> <span class="attr">name</span>=<span class="string">"viewport"</span> <span class="attr">content</span>=<span class="string">"width=device-width, initial-scale=1.0"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">title</span>></span>表单标签<span class="tag"></<span class="name">title</span>></span></span><br><span class="line"><span class="tag"></<span class="name">head</span>></span></span><br><span class="line"><span class="tag"><<span class="name">body</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> <form> 标签用于为接收用户输入创建 HTML 表单,用以将表单信息提交到指定地方</span></span><br><span class="line"><span class="comment"> <input> 标签是行内元素,用于定义输入字段,用户可以在其中输入数据</span></span><br><span class="line"><span class="comment"> <input> 元素是最重要的表单元素,可以以多种方式显示,具体取决于 type 属性</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">h2</span>></span>表单列表<span class="tag"></<span class="name">h2</span>></span></span><br><span class="line"> <span class="comment"><!-- </span></span><br><span class="line"><span class="comment"> get: 提交参数显示在Url上且对长度有限制,但提交速度快。一般用来提交数据</span></span><br><span class="line"><span class="comment"> post:提交参数不显示在Url上且没有长度限制,但提交速度慢。在F12的Network中可以看到,一般用来发送数据</span></span><br><span class="line"><span class="comment"> --></span></span><br><span class="line"> <span class="tag"><<span class="name">form</span> <span class="attr">action</span>=<span class="string">""</span> <span class="attr">method</span>=<span class="string">"get"</span>></span></span><br><span class="line"> 文本框:<span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">name</span>=<span class="string">"username"</span>></span><span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> 密码框:<span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"password"</span> <span class="attr">name</span>=<span class="string">"password"</span>></span><span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="comment"><!-- 单选框和多选框需要同一组才能实现,通过 name 属性来分组 --></span></span><br><span class="line"> 性别:<span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"radio"</span> <span class="attr">name</span>=<span class="string">"sex"</span> <span class="attr">value</span>=<span class="string">"男"</span>></span>男 <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"radio"</span> <span class="attr">name</span>=<span class="string">"sex"</span> <span class="attr">value</span>=<span class="string">"女"</span>></span>女<span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> 爱好:<span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">name</span>=<span class="string">"hobby"</span> <span class="attr">value</span>=<span class="string">"跑步"</span>></span>跑步 <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">name</span>=<span class="string">"hobby"</span> <span class="attr">value</span>=<span class="string">"动漫"</span>></span>动漫 <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">name</span>=<span class="string">"hobby"</span> <span class="attr">value</span>=<span class="string">"睡觉"</span>></span>睡觉<span class="tag"><<span class="name">br</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"提交"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"reset"</span> <span class="attr">value</span>=<span class="string">"重置"</span>></span></span><br><span class="line"> <span class="tag"><<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span> <span class="attr">value</span>=<span class="string">"点击验证"</span>></span></span><br><span class="line"> <span class="tag"></<span class="name">form</span>></span></span><br><span class="line"><span class="tag"></<span class="name">body</span>></span></span><br><span class="line"><span class="tag"></<span class="name">html</span>></span></span><br></pre></td></tr></table></figure><p>浏览器页面显示如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E5%9B%9B%E5%A4%A9/Snipaste_2024-04-04_22-36-00.png"></p><blockquote><p>对于单选框和多选框的文本内容,服务端是无法获取的,而真正操作的是 <strong>name</strong> 和 <strong>value</strong> 属性</p></blockquote></li></ul>]]></content>
<summary type="html"><blockquote>
<p>学习Html是为了未来给<strong>Web项目功能测试</strong>打基础的</p>
</blockquote>
<h2 id="认知Html"><a href="#认知Html" class="headerlink" title="认知H</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="软件测试基础" scheme="https://blhorizon.github.io/tags/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>软件测试基础(三)</title>
<link href="https://blhorizon.github.io/posts/4d73c4c.html"/>
<id>https://blhorizon.github.io/posts/4d73c4c.html</id>
<published>2024-03-26T01:48:56.000Z</published>
<updated>2024-04-04T03:17:08.984Z</updated>
<content type="html"><![CDATA[<p>测试用例需要在软件开发完成前做好。在将软件成品交付给你进行测试时,通过测试用例去验证该软件</p><p><strong>缺陷</strong>:实际测试结果与测试用例的预期结果不一致</p><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-27_09-53-02.png"></p><h2 id="缺陷"><a href="#缺陷" class="headerlink" title="缺陷"></a>缺陷</h2><p>软件在使用的过程中出现的任何<strong>问题</strong>都叫软件的缺陷,俗称<strong>bug</strong>。其判定的标准如下:</p><ul><li><strong>少功能</strong>:软件未实现需求(规格)说明书明确要求的功能</li><li><strong>功能错误</strong>:软件出现了需求(规格)说明书中指明不应该出现的错误</li><li><strong>多功能</strong>:软件实现的功能超出需求(规格)说明书指明的范围</li><li><strong>缺少隐性功能</strong>:软件未实现需求(规格)说明书虽未明确指明但应该实现的功能</li><li><strong>不易使用</strong>:软件难以理解,不易使用,运行缓慢,用户体验不好</li></ul><p>缺陷产生的原因如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-27_10-25-59.png"></p><p>软件缺陷的生命周期如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-27_10-29-50.png"></p><p>当一个缺陷被发现后,要懂得如何去描述这个缺陷,并知道根据缺陷类型和优先级等提交给谁来去解决,同时公司一般会有一个缺陷管理工具来记录这些缺陷。</p><p>软件缺陷的核心内容如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-28_09-53-17.png"></p><p>缺陷的提交要素如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-28_10-00-39.png"></p><p>缺陷的类型如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-28_10-02-43.png"></p><p>下图上的<strong>缺陷示例</strong>是在Excel上写的,真实在公司内部肯定会有一个专门的软件来写这个<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_09-50-18.png"></p><p>测试与开发在整个软件开发过程中的交流过程如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_09-54-55.png"></p><blockquote><p>知道测试和开发流程中涉及的工作即可</p></blockquote><p>提交缺陷的注意事项如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_09-56-58.png"></p><blockquote><p>面试题:当你发现缺陷后,首先你会怎么办? -> <strong>检查该缺陷是否可重现、再确定是否重复</strong></p></blockquote><p>缺陷的编写规范如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_10-04-35.png"></p><blockquote><p>有时A模块和B模块同时出现同一个缺陷,这时一般就只会处理一个就好了</p></blockquote><h2 id="缺陷管理工具"><a href="#缺陷管理工具" class="headerlink" title="缺陷管理工具"></a>缺陷管理工具</h2><p>一个国产、免费、开源、简单、轻量级的工具,该工具还支持三管融合(产品经理、项目管理、质量管理)。其地址为<a href="https://zentao.demo.qucheng.cc/user-login.html">禅道</a><br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_10-14-27.png"></p><blockquote><p>一般常用的还有<strong>JIRA</strong>、<strong>TFS</strong></p></blockquote><p>禅道本质上是一个<strong>项目管理工具</strong>,只是这里用其测试的地方来作为缺陷管理工具<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_10-18-29.png"></p><p>公司每一个角色的权限如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-03-29_10-23-54.png"></p><p>禅道使用流程:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-04-04_10-29-11.png"></p><blockquote><p>重点是对缺陷的管理</p></blockquote><p>提交Bug的图示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-04-04_10-46-40.png"><br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-04-04_10-50-41.png"></p><blockquote><p>将Bug指定给某个开发,在开发处理完该Bug后会指派给你这个Bug,告诉你当前状态后你<strong>验证</strong>该Bug即可</p></blockquote><p>禅道也可以创建用例,但是一般不好用。企业基本都是购买开发版,并将在Excel中写好的测试用例导入进去<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-04-04_11-01-53.png"></p><p>缺陷标题实现:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%89%E5%A4%A9/Snipaste_2024-04-04_11-14-51.png"></p><blockquote><p>本质就是按照规范去写,从而让人明白</p></blockquote>]]></content>
<summary type="html"><p>测试用例需要在软件开发完成前做好。在将软件成品交付给你进行测试时,通过测试用例去验证该软件</p>
<p><strong>缺陷</strong>:实际测试结果与测试用例的预期结果不一致</p>
<p><img src="/../images/%E8%BD%AF%E4%BB%</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="软件测试基础" scheme="https://blhorizon.github.io/tags/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>软件测试基础(二)</title>
<link href="https://blhorizon.github.io/posts/7a241c67.html"/>
<id>https://blhorizon.github.io/posts/7a241c67.html</id>
<published>2024-03-19T04:07:43.000Z</published>
<updated>2024-05-12T03:28:32.804Z</updated>
<content type="html"><![CDATA[<p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_12-07-09.png"></p><h2 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h2><p><strong>用例</strong>:用户使用的<strong>案例</strong>。<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%80%E5%A4%A9/Snipaste_2024-03-19_11-14-21.png"></p><p><strong>测试用例</strong>:(模拟用户使用情景,进行测试)是为测试项目而设计的<strong>执行文档</strong>。</p><p>测试用例的<strong>作用</strong>:</p><ul><li>防止漏测</li><li>实施测试的标准:测试人员不仅要关注功能是否符合,还需要关注用户使用的体验感等</li></ul><p>测试用例的的编写格式,如下图所示:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%80%E5%A4%A9/Snipaste_2024-03-19_11-20-37.png"></p><ul><li><strong>用例编号</strong>:该用例的唯一标识符。格式:<strong>项目_模块_编号</strong></li><li><strong>用例标题</strong>:该用例的测试场景。格式:<strong>预期结果(测试点)</strong></li><li><strong>项目/模块</strong>:该用例是属于哪个项目/模块的。格式:<strong>所属项目或模块</strong></li><li><strong>优先级</strong>:表示用例的重要程度或影响力。格式:<strong>P0~P4(P0最高,即用户使用频率最高的功能)</strong></li><li><strong>前置条件</strong>:执行此条用例有哪些前置操作</li><li><strong>测试步骤</strong>:执行此条用例如何去操作</li><li><strong>测试数据</strong>:执行此条用例所需要使用的数据,没有可以为空</li><li><strong>预期结果</strong>:期望达到的结果</li></ul><h2 id="测试用例编写案例"><a href="#测试用例编写案例" class="headerlink" title="测试用例编写案例"></a>测试用例编写案例</h2><p>练习如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%80%E5%A4%A9/Snipaste_2024-03-19_11-38-20.png"></p><p>参考答案如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%B8%80%E5%A4%A9/Snipaste_2024-03-19_13-36-12.png"></p><h2 id="等价类"><a href="#等价类" class="headerlink" title="等价类"></a>等价类</h2><blockquote><p>解决数据过多的问题</p></blockquote><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_12-14-02.png"><br>在<strong>无穷多的数据</strong>中,<strong>有效数据中只取其一</strong>、<strong>无效数据中只取其一</strong>,最关键的是符合<strong>某种特征的集合</strong></p><p>练习如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_13-38-34.png"></p><p>参考答案如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_13-40-27.png"><br>这里重点关注<strong>类型</strong>、<strong>长度</strong>和<strong>规则</strong>。</p><p>适用场景:针对有大量数据测试输入,但是没法穷举测试的地方。其中最经常见的是<strong>页面的输入框类测试</strong></p><ul><li>输入框</li><li>下拉列表</li><li>单选复选框</li></ul><blockquote><p>重点:<br> 1、正向用例:一条尽可能覆盖多条<br> 2、逆向用例:每一条数据,都是一条单独用例。</p></blockquote><h2 id="边界值"><a href="#边界值" class="headerlink" title="边界值"></a>边界值</h2><blockquote><p>解决边界位数限制问题</p></blockquote><p>边界范围节点:选取<strong>正好等于</strong>、<strong>刚好大于</strong>、<strong>刚好小于</strong>边界的值作为测试数据</p><ul><li><strong>上点</strong>:边界上的点(正好等于)</li><li><strong>离点</strong>:距离上点最近的点(刚好大于、刚好小于)</li><li><strong>内点</strong>:范围内的点(区间范围内的数据)<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_17-24-17.png"></li></ul><p>步骤:<strong>明确需求</strong> -》 <strong>确定有效等价和无效等价类</strong> -》 <strong>确定边界范围值</strong> -》 <strong>提取数据编写测试用例</strong></p><p>练习如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_17-36-49.png"></p><p>参考答案如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-19_18-05-50.png"></p><p>边界值优化:将7个优化位<strong>5个</strong></p><ul><li>上点:必选(不考虑区间开闭)</li><li>内点:必选(建议选取中间范围)</li><li>离点:开内闭外(考虑开闭区间,开区间选择内部离点,闭区间选择外部离点)</li></ul><p>使用场景:</p><ul><li>在等价类的基础上针对边界范围内的测试数据输入的地方(重点关注边界)</li><li>常见词语描述:大小、尺寸、重量、最大、最小、至少、至多等修饰词语</li><li>典型代表:有边界范围的输入框测试</li></ul><blockquote><p>面试题:最常用的用例设计方法有哪些?–等价类、边界值</p></blockquote><h2 id="判定表法"><a href="#判定表法" class="headerlink" title="判定表法"></a>判定表法</h2><blockquote><p>适合条件组合数量较少的情况(比如4个以下)。当超过4个时,就不符合覆盖所有条件,应采用<strong>正交法</strong>来解决</p></blockquote><p>等价边界值分析法主要关注<strong>单个输入类条件</strong>的测试,并未考虑<strong>输入条件的各种组合</strong>、<strong>输入条件与输出结果之间有相互制约关系</strong></p><p>定义:以<strong>表格</strong>的形式表达<strong>多种条件逻辑判断</strong>的工具。主要由如下组成:</p><ul><li><strong>条件桩</strong>:无关次序,列出问题中的所有条件</li><li><strong>动作桩</strong>:排序没有约束,列出问题中可能采取的操作</li><li><strong>条件项</strong>:列出条件对应的取值,所有可能情况下的真假值</li><li><strong>动作项</strong>:列出条件项的、各种取值情况下应该采取的动作结果</li></ul><p>以案例:验证”若用户欠费或关机,则不允许主被叫”功能的测试:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-20_17-05-12.png"></p><p>所得出的规则:</p><ul><li>判定表中贯穿条件项与动作项的一列就是一条<strong>规则</strong></li><li>假设由n个条件,每个条件的取值有两个(0,1),全组合由2的n次方个规则</li></ul><p>设计用例的步骤:</p><ol><li>明确需求</li><li>画出判定表:<strong>列出条件桩和动作桩</strong>、<strong>列出条件项并将填写的条件进行全组合</strong>、<strong>根据条件项的组合确定动作项</strong>、<strong>简化合并相似规则</strong></li><li>根据规则编写测试用例</li></ol><p>练习如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-20_17-41-31.png"></p><p>参考答案如下图:<br><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-20_17-40-09.png"></p><h2 id="场景法"><a href="#场景法" class="headerlink" title="场景法"></a>场景法</h2><blockquote><p>根据实际的应用场景,测试人员一般优先测试<strong>业务用例</strong></p></blockquote><p>通过<strong>流程图</strong>来表达程序或业务的走向,其作用如下:</p><ul><li>通过看懂流程图来设计业务用例</li><li>当需求文档信息不全时,能根据需求梳理出流程</li></ul><p>场景法也可以叫做<strong>流程图法</strong>,通过流程图来描述用户的使用场景,然后覆盖流程路径来设计测试用例。其所具有的意义:</p><ul><li>用户使用角度:用户平时使用的不是单个功能,而是多个功能组合起来一起使用</li><li>测试人员角度:平时测试的都是单个功能点进行测试,容易忽略多个功能的组合测试</li></ul><h2 id="错误推荐法"><a href="#错误推荐法" class="headerlink" title="错误推荐法"></a>错误推荐法</h2><blockquote><p>应用场景:当项目用例都执行完毕,且BUG修复完成,离上线还有一段时间,在这段时间中可是使用错误推荐法复测主要业务或测试未覆盖的功能。</p></blockquote><p>先保证优先级高的业务流程正常,优先级低的业务上架以后慢慢测</p><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4%A9/Snipaste_2024-03-21_10-03-21.png"></p>]]></content>
<summary type="html"><p><img src="/../images/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/%E7%AC%AC%E4%BA%8C%E5%A4</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="软件测试基础" scheme="https://blhorizon.github.io/tags/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>软件测试基础(一)</title>
<link href="https://blhorizon.github.io/posts/d5158705.html"/>
<id>https://blhorizon.github.io/posts/d5158705.html</id>
<published>2024-03-19T01:13:00.000Z</published>
<updated>2024-05-12T03:41:38.447Z</updated>
<content type="html"><![CDATA[<h2 id="软件测试的概念"><a href="#软件测试的概念" class="headerlink" title="软件测试的概念"></a>软件测试的概念</h2><p><strong>软件的产生过程</strong>,如下图所示:<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/softwareget.webp"></p><p>软件测试是指<strong>在预定的条件下执行程序,以此发现潜在的缺陷,并确保软件系统按照设计和预期的方式运行</strong>。测试的目标不仅仅是找出错误,更是为了验证软件是否符合规格和满足用户需求。</p><p>软件测试的目的是确保软件系统符合预期的需求,并能在不同的使用情境下表现良好。因为,我们需要<strong>通过不同的软件测试类型,全面验证软件的功能性、性能、安全性和兼容性</strong>,为用户提供一个可信赖的软件产品。</p><blockquote><p>评判一个软件是否优秀可从<strong>功能性</strong>、<strong>性能</strong>、<strong>兼容性</strong>、<strong>易用性</strong>、可靠性、<strong>安全</strong>、可维护性、可移植性这 8 个维度来衡量</p></blockquote><p>软件测试主流包含<strong>功能测试</strong>、<strong>接口测试</strong>、<strong>自动化测试</strong>和<strong>性能测试</strong>。</p><p>在进行软件测试时,我们要遵循一些基本原则,以确保测试的有效性和全面性。这些原则包括:</p><ul><li><strong>全面性(Exhaustiveness):</strong>测试应该覆盖软件的所有功能和情况,确保每个方面都经过验证。</li><li><strong>独立性(Independence):</strong>测试过程应该独立于开发过程,以确保独立的视角和避免潜在的偏见。</li><li><strong>可重复性(Repeatability):</strong>测试过程应该是可重复的,以便在发现问题时能够准确地重现并分析。</li><li><strong>尽早测试(Early Testing):</strong>测试应该在开发早期开始,以便在问题变得更加显著和昂贵之前就能够发现和解决它们。</li></ul><p>测试生命周期是指测试活动<strong>从计划阶段到测试执行、分析和报告</strong>的整个过程。它包括以下阶段:</p><ul><li><strong>计划和准备阶段:</strong>在项目开始之前,确定测试的范围、目标和资源需求,并制定测试计划。</li><li><strong>设计阶段:</strong>开发测试用例和测试数据,确保测试覆盖到所有关键功能和场景。</li><li><strong>执行阶段:</strong>执行测试用例,记录测试结果,并识别潜在的缺陷。</li><li><strong>分析和报告阶段:</strong>分析测试结果,生成测试报告,并协助开发团队解决发现的问题。</li></ul><p>通过理解这些基本概念,我们能够更好地理解软件测试的整体框架,为后续深入讨论不同软件测试的分类和方法奠定基础。</p><h2 id="软件测试模型"><a href="#软件测试模型" class="headerlink" title="软件测试模型"></a>软件测试模型</h2><p>软件测试模型是<strong>软件测试和开发阶段的对应关系</strong>,能够用来指导软件测试的整个过程,而常见的软件测试模型包括:V 模型、W 模型、H 模型、X 模型和前置模型,其中V 模型是最具代表性的软件测试模型,需要掌握,其余了解即可。</p><h3 id="V-模型"><a href="#V-模型" class="headerlink" title="V 模型"></a>V 模型</h3><p>RAD(Rapid Application Development,快速应用开发)模型是软件开发过程中的一个重要模型,通过开发和测试同时进行的方式来缩短开发周期,以提高开发效率。因其形状像一个字母V,故称为V模型。</p><p>V模型是一个著名的、以测试为驱动的开发模型,该模型强调开发过程中测试贯穿始终,是瀑布模型的一个变体。V模型描述了<strong>质量保证活动和沟通、建模相关活动以及早期构键相关的活动之间的关系</strong>。随着软件团队工作沿着V模型左侧步骤向下推进,基本问题需求逐步细化,形成问题及解决方案的技术描述。一旦编码结束,团队沿着V模型右侧的步骤向上推进工作,其实际上是执行了一系列测试(质量保证活动),这些测试验证了团队沿着V模型左侧步骤向下推进过程中所生成的每个模型。V模型提供了一种<strong>将验证确认活动应用于早期软件工程工作中的方法</strong>。<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/vmodel.webp"></p><p>V 模型包含了底层测试和高层测试,清楚标识了开发和测试的各个阶段:<strong>自上而下求精,对每个阶段都分工明确,方便把控整体项目</strong>。但也因为它的自上而下,导致缺陷到测试阶段才被发现,甚至很难发现;在开发过程中,也很难把握用户的需求,使得 V 模型步骤反复执行,返工量大,灵活度低。这时可以在每个阶段都加入<strong>适量的迭代</strong>进行改良。</p><blockquote><p><strong>底层测试:</strong>检测源代码质量,如单元测试;<strong>高层测试:</strong>检验整个系统的测试。<br><strong>迭代:</strong>重复反馈过程的活动,每一次迭代的结果会作为下一次迭代的初始值。</p></blockquote><p>一般来讲:<strong>单元测试所对应的是详细设计环节</strong>,也就是说,单元测试的测试用例是和详细设计一起出现的,在研发人员做详细设计的时候,相应的测试人员也就把测试用例写了出来;<strong>集成测试对应概要设计</strong>,在做模块功能分析及模块接口,数据传输方法的时候,就把集成测试用例根据概要设计中模块功能及接口等实现方法编写出来,以备以后作集成测试的时候可以直接引用;而<strong>系统测试,就是根据需求分析而来</strong>,在系统分析人员作系统分析,编写需求说明书的时候测试人员就根据客户需求说明书,把最后能实现系统功能的各种测试用例写出来,为做最后系统测试作准备。验收测试与用户需求对应,是非设计流程。</p><p>V模式是一种传统软件开发模型,一般适用于<strong>一些传统信息系统应用的开发</strong>,而一些高性能高风险的系统、互联网软件,或一个系统难以被具体模块化的时候,就比较难做成V模式所需的各种构件,需要更强调迭代的开发模型或者敏捷开发模型。</p><p>参考:<a href="https://blog.csdn.net/weixin_39294633/article/details/81805893">软件测试模型——V模型 & W模型</a>、<a href="https://zhuanlan.zhihu.com/p/99355357">深究递归和迭代的区别、优缺点及实例对比</a>、<a href="https://www.cnblogs.com/zp0923/p/10686334.html">测试模型—V模型</a>、<a href="https://www.cnblogs.com/yilang/p/12252661.html">软件生存周期模型之V模型 </a></p><h3 id="W-模型"><a href="#W-模型" class="headerlink" title="W 模型"></a>W 模型</h3><p>W 模型也称之为双 V 模型,一个 V 是开发的生命同期,另一个 V 是测试的生命周期,W 模型与 V 模型有一个很大的不同,就是 ** W 模型是一个并行的模型,V 模型是一个串行的模型**,W 模型开始是从用户需求分析开始,而不是等到编码完成后。并且测试阶段的划分更清楚,而不仅仅是单元测试、集成测试、系统测试,还包括前期的测试计划、测试方案等内容,这更符合现在企业测试的流程。<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/wmodel.webp"></p><p>W 模型有利于尽早全面地发现问题。从用户需求分析开始测试工程师就参与到项目的测试中,<strong>当用户需求分析完成后,测试工程师就需要参与到需求的验证和确认活动中,并需要提供可测试性用户需求分析说明书,这样可以尽早地发现需求阶段的缺陷</strong>。同时,对需求的测试也有利于及时了解项目难度和测试风险,及早制定应对措施,这将显著减少总体测试时间,加快项目进度。但 W 模型也存在局限性,需求、设计、编码等活动被视为是串行的,同时,<strong>测试和开发活动也保持着一种线性的前后关系,上一阶段完全结束,才可正式开始下一阶段工作</strong>,这样就无法支持迭代的开发模型。对于当前软件开发复杂多变的情况,W 模型并不能解除测试管理面临的困惑。</p><ul><li>在<strong>需求分析阶段</strong>,测试人员需要与开发人员共同参与需求讨论,确保对需求的理解准确无误。同时,测试人员应从测试角度提出疑虑和建议,以便在后续测试过程中顺利地发现问题和缺陷。在需求确定后,测试人员需要根据需求分析结果制定测试计划和测试用例。</li><li>在<strong>设计阶段</strong>,测试人员需要关注设计合理性和可测试性,对设计中可能存在的问题提出反馈。此时,测试人员应参与到设计评审中,以确保设计符合需求和测试要求。</li><li>在<strong>编码阶段</strong>,测试人员应关注代码质量,通过单元测试、集成测试和系统测试等手段,发现并修复代码中的缺陷。同时,测试人员还应与开发人员保持密切沟通,确保缺0陷修复的及时性和准确性。</li></ul><p>在 W 模型中,开发人员和测试人员不再是独立的个体,而是紧密结合成一个团队。开发人员需要根据测试人员的需求,提供合适的接口和工具支持,以便测试人员能够更高效地进行测试。同时,开发人员还需要积极参与缺陷修复工作,确保问题能够及时解决。测试人员则需要根据开发进度,合理安排测试资源和时间,确保测试工作的有效性和及时性。在发现缺陷时,测试人员应快速反馈给开发人员进行修复,并跟进修复进度,确保问题得到及时解决。</p><p>通过这种方式,开发与测试在W模型中相互支持、相互影响。不仅提高了软件质量和可靠性,还提高了开发效率。</p><h3 id="H-模型"><a href="#H-模型" class="headerlink" title="H 模型"></a>H 模型</h3><p>H模型将测试活动完全独立出来,形成一个完整的流程,同时将测试准备和测试执行清晰表现出来:<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/hmodel.webp"></p><ul><li>测试准备:所有测试活动的准备判断是否到测试就绪点。</li><li>测试就绪点:测试准入准则,即是否可以开始执行测试的条件。</li><li>测试执行:具体的执行测试的程序。</li><li>其它流程:回归测试、冒烟测试、探索性测试。</li></ul><p>H 模型让软件测试<strong>完全独立</strong>贯穿整个生命周期与其它流程并发进行,使得其可以<strong>尽早准备尽早执行</strong>,灵活性很强。同时也揭示了除测试执行外,还<strong>应有其他工作要处理</strong>,并且能根据<strong>被测对象的不同</strong>而分层次、分阶段、分次序的执行,同时也是可以被迭代。</p><p>H 模型要定义清晰的规则和管理制度,否则测试过程将很难管理和控制(<strong>管理型要求高</strong>),也要求能够很好的定义每个迭代的规模,不能太大也不能太小(<strong>技能要求高</strong>)。并且测试很多时候,你并不知道测试准备到什么时候是合适的,就绪点在哪,就绪点标准是什么,对后续的测试执行启动带来很大的困难(<strong>测试就绪点分析困难</strong>)。当被分了一个很小的迭代时,因人员技能不足而无法完成,会使整个项目会受到很大的干扰(<strong>对整个项目组的人员要求非常高</strong>)。</p><h3 id="X-模型"><a href="#X-模型" class="headerlink" title="X 模型"></a>X 模型</h3><p>X 模型也是对 V 模型的改进,X 模型的左边描述的是针对单独程序片段所进行的相互分离的编码和测试,此后将进行频繁的交接,通过集成,最终成为可执行的程序,然后再对这些可执行程序进行测试。己通过集成测试的成品可以进行封装并提交给用户,也可以作为更大规模和范围内集成的一部分。多根并行的曲线表示变更可以在各个部分发生。<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/xmodel.webp"></p><p>X 模型定位了探索性测试,这是<strong>不进行事先计划</strong>的特殊类型的测试,这一方式往往能帮助有经验的测试人员在测试计划之外发现更多的软件错误。但这样可能对测试造成人力、物力和财力的浪费,对测试员的熟练程度要求比较高。</p><h3 id="前置模型"><a href="#前置模型" class="headerlink" title="前置模型"></a>前置模型</h3><p>开发和测试相结合:前置测试模型将开发和测试的生命周期整合在起,标识了项目生命周期从开始到结束之间的关键行为。并且标识了这些行为在项目用期中的价值所在。如果其中有些行为没有得到很好的执行,那么项目成功的问能性就会因此而有所降低。如果有业务需求,则系统开发过程将更有效率。我们认为在没有业务需求的情况下进行开发和测试是不可能的。而且,业务需求最好在设计和开发之前就被正确定义。<br><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/frontmodel.webp"></p><p>对每一个交付内容进行测试:每一个交付的开发结果都必须通过一定的方式进行测试。源程序代码并不是惟一需要测试的内容。图中的椭圆框表示了其他一些要测试的对象,包括可行性报告、业务需求说明,以及系统设计文档等。这同V模型中开发和测试的对应关系是一致的,并且在其基础上有所扩展,变得更为明确。</p><p>在设计阶段进行测试计划和测试设计:设计阶段是作测试计划和测试设计的最好时机。很多组织要么根本不作测试计划和测试设计,要么在即将开始执行测试之前才飞快地完成测试计划和测试设计。在这种情况下,测试只是验证了程序的正确性,而不是验证整个系统本该实现的东西。</p><p>测试和开发结合在一起:前置测试将测试执行和开发结合在起,并在开发阶段以编码一测试一编码一测试的方式来体现。也就是说,程序片段一旦编写完成,就会立即进行测试。一般情况下,先进行的测试是单元测试,因为开发人员认为通过测试来发现错误是最经济的方式。但也可参考X模型,即一一个程序片段也需要相关的集成测试,甚至有时还需要一些特殊测试。 对于一个特定的程序片段,其测试的顺序可以按照v模型的规定,但其中还会交织一些程序片段的开发,而不是按阶段完全地隔离。</p><p>让验收测试和技术测试保持相互独立:验收测试应该独立于技术测试,这样可以提供双重的保险,以保证设计及程序编码能够符合最终用户的需求。验收测试既可以在实施阶段的第一步来执行,也可以在开发阶段的最后一步执行。前置测试模型提倡验收测试和技术测试沿循两条不同的路线来进行,每条路线分别地验证系统是否能够如预期设想的那样进行正常工作。这样,当单独设计好的验收测试完成了系统的验证时,我们即可确信这是一个正确的系统。</p><h2 id="软件测试的分类"><a href="#软件测试的分类" class="headerlink" title="软件测试的分类"></a>软件测试的分类</h2><p><img src="https://jsd.onmicrosoft.cn/npm/hexoblogimages/softwaretest/testcategories.webp"></p><p>为了更有效地组织测试活动,我们可以<strong>根据不同的标准和目的将软件测试分为多个类别</strong>,比如<strong>按测试阶段划分、按测试类型划分和按测试方法划分</strong>等。下面将详细介绍这几种主要的软件测试的分类。</p><h3 id="按测试阶段划分"><a href="#按测试阶段划分" class="headerlink" title="按测试阶段划分"></a>按测试阶段划分</h3><p><strong>① 单元测试(Unit Testing):</strong>单元测试是测试软件中最小可测试单元的过程,通常是一个函数或方法。其目的是验证单元在隔离的环境中是否按照设计的预期工作,常用的工具包括 JUnit、PyTest 等。</p><p><strong>② 集成测试(Integration Testing):</strong>集成测试是将已通过单元测试的模块组合在一起进行测试,验证它们在集成时是否能够协同工作。这样可以及时发现模块之间的接口问题、数据传递问题以及其他由集成引起的错误。常用的集成测试方法包括自顶向下、自底向上、增量式集成等。</p><p><strong>③ 系统测试(System Testing):</strong>系统测试是指对整个软件系统进行全面测试,确保系统在各种情况下都能按照需求工作,包括功能测试、性能测试、安全测试等。验收测试,这在最终用户或客户的环境中进行,以确认系统是否符合用户需求,为软件发布做准备。</p><p><strong>④ 验收测试(Acceptance Testing):</strong>验收测试是在最终用户或客户的环境中对整个软件系统进行的测试,以确认系统是否符合用户需求,为软件发布做准备。</p><h3 id="按测试类型划分"><a href="#按测试类型划分" class="headerlink" title="按测试类型划分"></a>按测试类型划分</h3><p><strong>① 功能测试(Functional Testing):</strong>功能测试主要是验证软件系统的各个功能是否按照需求规格书中描述的要求正常工作。通常采用黑盒测试的方法,关注输入和输出。</p><p><strong>② 性能测试(Performance Testing):</strong>性能测试主要评估系统在不同负载和压力条件下的性能,包括响应时间、吞吐量等。一般可分为负载测试、压力测试和性能稳定性测试。</p><p><strong>③ 安全测试(Security Testing):</strong>安全测试主要评估软件系统对潜在威胁和攻击的抵御能力,包括身份验证、授权、数据保护等。</p><p><strong>④ 兼容性测试(Compatibility Testing):</strong>兼容性测试的目的是确保软件在不同环境、操作系统、浏览器等条件下能够正常运行。测试对象主要为不同操作系统、不同浏览器、不同设备等。</p><h3 id="按测试方法划分"><a href="#按测试方法划分" class="headerlink" title="按测试方法划分"></a>按测试方法划分</h3><p><strong>① 手动测试(Manual Testing):</strong>也是功能测试,测试中把被测的软件当成一个黑盒子,不关心盒子的内部结构是什么,只关心软件的输入数据和输出数据。</p><p><strong>② 自动化测试(Automated Testing):</strong>自动化测试主要通过使用自动化工具和脚本来执行测试用例,提高测试效率和覆盖范围。虽然初期投入较大,但适用于大规模和重复性测试。</p><h3 id="按代码可见度划分"><a href="#按代码可见度划分" class="headerlink" title="按代码可见度划分"></a>按代码可见度划分</h3><p><strong>① 黑盒测试(Black-box Testing):</strong>功能测试主要是验证软件系统的各个功能是否按照需求规格书中描述的要求正常工作。通常采用黑盒测试的方法,关注输入和输出。</p><p><strong>② 白盒测试(White-box Testing):</strong>又称结构测试、透明盒测试、逻辑驱动测试或基于代码的测试。白盒测试是指打开盒子,去研究里面的源代码和程序结果。白盒测试也是接口测试的一种。</p><p><strong>③ 灰盒测试(Gray-Box Testing):</strong>灰盒测试是介于白盒测试和黑盒测试之间的一种,灰盒测试多用于集成测试阶段,不仅关注输入、输出的正确性,同时也关注程序内部的情况。灰盒测试也是接口测试和功能测试的结合。</p><p>以上就是集中常见的软件测试的分类,通过深入了解这些分类的细节,团队能够更精确地选择合适的测试方法和阶段,确保全面有效地进行软件测试,提高软件质量。</p><blockquote><p>参考:<a href="https://ones.cn/blog/articles/the-classification-of-software-testing">软件测试的分类有哪些,如何进行软件测试?</a></p></blockquote><h2 id="测试流程"><a href="#测试流程" class="headerlink" title="测试流程"></a>测试流程</h2><p>软件测试的基本流程和步骤是<strong>确保软件质量</strong>的重要过程。它涉及验证软件的功能、性能、安全性和用户体验是否符合预期。</p><p>下面是软件测试的基本流程和步骤:</p><ol><li><p><strong>需求分析:</strong>了解软件的功能和性能需求。分析用户需求和系统需求,明确软件的预期目标和功能。</p></li><li><p><strong>测试计划:</strong>制定测试计划,确定测试目标、测试范围、测试资源和时间计划。考虑测试环境、测试工具和测试数据的需求。</p></li><li><p><strong>测试设计:</strong>设计测试用例,包括功能测试、性能测试、安全性测试和用户体验测试。测试用例应该覆盖各种功能和使用场景,以确保软件的全面测试。</p></li><li><p><strong>测试环境搭建:</strong>配置测试环境,包括硬件设备、操作系统、数据库和网络设置。确保测试环境与实际使用环境相似。</p></li><li><p><strong>测试执行:</strong>根据测试计划和测试设计执行测试用例。记录测试结果,包括测试通过和测试失败的情况。对测试失败的情况进行缺陷管理和跟踪。</p></li><li><p><strong>缺陷管理:</strong>将测试中发现的缺陷记录到缺陷管理系统中。包括缺陷的描述、重现步骤和优先级。跟踪缺陷的修复和验证过程。</p></li><li><p><strong>缺陷修复和验证:</strong>开发人员修复测试中发现的缺陷。测试人员验证修复后的软件是否符合预期。如果修复不完全或引入新的问题,则重新进行测试。</p></li><li><p><strong>测试报告:</strong>生成测试报告,总结测试结果和缺陷情况。报告应包括测试覆盖率、通过率和失败率。提供给项目组、开发人员和管理层参考。</p></li><li><p><strong>回归测试:</strong>在软件的修改版本中执行回归测试,确保修复缺陷不会引入新的问题。重复执行测试用例,验证软件的稳定性和功能性。</p></li><li><p><strong>最终验证:</strong>在软件发布前进行最终验证,确保软件符合预期目标和质量要求。验证软件的功能、性能、安全性和用户体验。</p></li><li><p><strong>发布和维护:</strong>在软件通过最终验证后,将其发布到生产环境中。跟踪用户反馈和问题报告,进行维护和更新。</p></li></ol><p>软件测试的基本流程和步骤是确保软件质量的关键过程。通过合理的测试计划和测试设计,可以发现和修复软件中的缺陷,提高软件的可靠性和稳定性。</p><blockquote><p>参考:<a href="https://www.kexintest.com/sys-nd/902.html">软件测试的基本流程和步骤是什么?</a></p></blockquote>]]></content>
<summary type="html"><h2 id="软件测试的概念"><a href="#软件测试的概念" class="headerlink" title="软件测试的概念"></a>软件测试的概念</h2><p><strong>软件的产生过程</strong>,如下图所示:<br><img src="https</summary>
<category term="软件测试" scheme="https://blhorizon.github.io/categories/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95/"/>
<category term="软件测试基础" scheme="https://blhorizon.github.io/tags/%E8%BD%AF%E4%BB%B6%E6%B5%8B%E8%AF%95%E5%9F%BA%E7%A1%80/"/>
</entry>
<entry>
<title>Excel学习笔记之操作表格</title>
<link href="https://blhorizon.github.io/posts/83221823.html"/>
<id>https://blhorizon.github.io/posts/83221823.html</id>
<published>2024-03-03T14:01:55.000Z</published>
<updated>2024-03-04T13:00:10.393Z</updated>
<content type="html"><![CDATA[<ol><li><p>在你浏览表格的数据时,常会在第n个列时不知道该列代表什么,这时可以在<strong>视图</strong>中冻结该窗口<br><img src="/../images/excel/dongjie.gif"></p></li><li><p>在需要比对第一条和第n条时,常规拖拽来比对会很麻烦,这时可以<strong>分割视窗</strong>来解决该问题,选择<strong>视图 > 拆分</strong><br><img src="/../images/excel/fenge.gif"></p></li><li><p>在<strong>开始 > 编辑</strong>的<strong>排序和筛选</strong>中和通过筛选来对单个或多个栏目进行排序<br><img src="/../images/excel/paixu.gif"></p></li><li><p>在<strong>插入 > 表格</strong>中创建表【最好<strong>无填充</strong>】,之后会生成一个<strong>表设计</strong>,在这里就可以对该表进行设计了,在<strong>页面布局</strong>中也可以对表的颜色进行更改<br><img src="/../images/excel/biao.gif"></p></li><li><p>跟筛选类似,可以选择特定栏目的指定规则。在<strong>开始 > 条件格式</strong>中使用<br><img src="/../images/excel/tiaojian.gif"></p></li><li><p>在一个Excel文件中可以创建<strong>多个工作表</strong>,并可以对这些工作表进行相应操作。其中按<strong>CTRL键</strong>可以在一个Excel中复制另一个工作表,也可以在工作表中鼠标右键选择<strong>移动和复制</strong>将工作表复制到另外一个Excel中</p></li><li><p>可以通过表创建一个图表。在<strong>插入 > 图表</strong>中可以进行创建并执行操作,当创建完成后在窗口生成一个图表并在操作栏生成一个<strong>图表格式</strong>。其中可以通过<strong>CTRL键</strong>来单独选择两栏来创建图表<br>【<strong>圆形图:描述资料间的相对比例</strong>、<strong>折线图:数值在一定时间的高低变化</strong>、<strong>散点图:比较两组数值之间的关系</strong>、<strong>雷达图:一般用于企业的财务分析</strong>】<br><img src="/../images/excel/tubiao.gif"></p></li><li><p><strong>数据透视表</strong>可以用来分析一组庞大的数据,对其进行筛选分析。此操作在<strong>插入</strong>操作选项中,这里需要注意的是一旦原数据发生改变,那么在数据透视表中是不会自动改变的,想要一键改变可以先将其变成表,再变成数据透视表<br><img src="/../images/excel/shuju.gif"></p></li></ol>]]></content>
<summary type="html"><ol>
<li><p>在你浏览表格的数据时,常会在第n个列时不知道该列代表什么,这时可以在<strong>视图</strong>中冻结该窗口<br><img src="/../images/excel/dongjie.gif"></p>
</li>
<li><p>在需要比对第一</summary>
<category term="学习笔记" scheme="https://blhorizon.github.io/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="Excel" scheme="https://blhorizon.github.io/tags/Excel/"/>
</entry>
<entry>
<title>Excel学习笔记之入门</title>
<link href="https://blhorizon.github.io/posts/74f74261.html"/>
<id>https://blhorizon.github.io/posts/74f74261.html</id>
<published>2024-03-03T10:18:07.000Z</published>
<updated>2024-03-03T13:58:06.921Z</updated>
<content type="html"><![CDATA[<p>Excel在界面上都是由多个<strong>储存格</strong>所组成,这些储存格可以用来<strong>输入资料</strong>或<strong>设定公式</strong>,此外储存格还有一个重要的特点:每个都想地图上的坐标点那样有特点的<strong>参照位置</strong>,其显示在界面的左上角</p><p>先试着建立一个简单的资料表格:</p><ol><li><p>先在A1的储存格中输入<strong>ABC公司食品订单</strong>,这时你会发现文字已经超出了储存格的<strong>边界</strong>,这也可以说明一个储存格的长度是<strong>有限制</strong>的<br><img src="/../images/excel/Snipaste_2024-03-03_18-15-39.png"></p></li><li><p>通过按<strong>Enter键</strong>可以将选取的方框【kuang】移动到下方的储存格,在该储存格输入:<strong>订单编号</strong>,想要将游标移动到右边的储存格,可以按<strong>Tab键</strong>。当然如果想向左移动,可以按<strong>Shift + Tab键</strong>;事实上,也可以使用键盘的方向键来控制选取框的移动<br><img src="/../images/excel/yidong.gif"></p></li><li><p>只需要拖一标题栏位的<strong>间隔线【黑色十字架】</strong>来调整单一栏宽高,同时在excel也有一个小技巧:通过按两次间隔线可以将栏宽调整到文字宽度<br><img src="/../images/excel/kuan.gif"></p></li><li><p>一般来说对于表格的大标题通常会放在整个表格的正中央,可以通过<strong>合并并居中</strong>来实现,同时Excel也提供了针对标题文件的一些美化<br><img src="/../images/excel/hebing.gif"></p></li><li><p>在这里,Excel提供了一些简单的设计标准,可以在<strong>样式</strong>中使用。将多个单位<strong>一次性调整为相同的宽高度</strong>,可以先将宽度或高度不一致的栏位先框选起来,再选择其中任意一个间隔线的位置进行拖拽<br><img src="/../images/excel/du.gif"></p></li><li><p>Excel会根据你所输入的<strong>一组数据</strong>来产生<strong>相应差距</strong>的数列;Excel可以辨别的时间格式:如<strong>2023/5/6【以斜线隔开年月日】</strong>,可以利用<strong>设置单元格格式</strong>来改变格式;这里也有个小技巧,通过 CTRL + ;【分号】 来显示当前年月日<br><img src="/../images/excel/fen.gif"></p></li><li><p>如果里面有些资料是带<strong>重复性</strong>质的,通过鼠标右键选择<strong>从下拉列表中选择</strong>来快速填写<br><img src="/../images/excel/Snipaste_2024-03-03_20-49-21.png"></p></li><li><p>通过公式 <strong>【=参照位置*参照位置】</strong> 来进行计算。当复制该公式计算出来的数据时,<strong>不会复制数字而是复制其公式</strong>,也可以用下拉来快速进行计算,同时在栏位还是选取的时候可以通过<strong>格式刷</strong>来快速复制该栏位的所有样式<br><img src="/../images/excel/geshi.gif"></p></li><li><p>Excel里的表格线是不能打印出来的,这时可以在<strong>页面布局 > 工作表选项</strong>中的<strong>设置</strong>选择网格线,但是这种打印出来的外边框是黑线,内边框是虚线,并且是固定的,所以需要在<strong>开始 > 画笔</strong>中对边框线进行描绘<br><img src="/../images/excel/xian.gif"></p></li><li><p>可以在<strong>页面布局 > 背景</strong>中给表格添加背景;在<strong>页面布局 > 工作表选项</strong>中<strong>取消勾选</strong>查看,可以将Excel的网格线给隐藏;选取栏框后鼠标右键选择<strong>插入</strong>可以新增新的栏框</p></li><li><p>在 <strong>开始 > 自动求和</strong> 中对指定的储存格进行相应栏目的计算<br><img src="/../images/excel/sum.gif"></p></li></ol>]]></content>
<summary type="html"><p>Excel在界面上都是由多个<strong>储存格</strong>所组成,这些储存格可以用来<strong>输入资料</strong>或<strong>设定公式</strong>,此外储存格还有一个重要的特点:每个都想地图上的坐标点那样有特点的<strong>参照位置</</summary>
<category term="学习笔记" scheme="https://blhorizon.github.io/categories/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"/>
<category term="Excel" scheme="https://blhorizon.github.io/tags/Excel/"/>
</entry>
<entry>
<title>待办清单页</title>
<link href="https://blhorizon.github.io/posts/95d8ff79.html"/>
<id>https://blhorizon.github.io/posts/95d8ff79.html</id>
<published>2023-10-24T17:21:38.000Z</published>
<updated>2024-05-02T18:36:44.313Z</updated>
<content type="html"><![CDATA[<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>最初是在 <strong>Leonus</strong> 的 <a href="https://blog.leonus.cn/2023/memeos.html">基于Memos实现说说和清单功能</a> 文章上了解到,该功能是需要依赖 <a href="https://memos.nesxc.com/">Memos</a> 来发布新的清单,而我这个 <strong>Memos</strong> 是 <a href="https://www.imcharon.com/">小N同学</a> 的纯公益代部署服务</p><p>在用了一段时间后,我发现我也没有经常发布这个清单,也就想可不可以将这个<strong>静态部署</strong>呢。在看到 <strong>铭心石刻</strong> 的 <a href="https://blog.kouseki.cn/posts/bc0.html">为你的博客添加待办清单页</a> 后就根据教程进行了部署,只不过发现在我<strong>小米10</strong>的手机上顶部图左右移动会很卡,遂觉得需要解决掉</p><p>最后在某一天逛自已博客的朋友圈时,发现 <strong>冰梦</strong> 发布了 <a href="https://hcer.top/posts/f316.html">待办清单页(本地整改版)</a> 这篇文章,也是对清单页的静态部署,既然这样就根据教程部署吧,最后发现<strong>手机卡顿</strong>问题解决了</p><h2 id="部署"><a href="#部署" class="headerlink" title="部署"></a>部署</h2><p>执行如下命令创建一个页面</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hexo new page todolist</span><br></pre></td></tr></table></figure><p>在 <strong>/themes/anzhiyu/layout/includes/page</strong> 下创建 <strong>todolist.pug</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><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">#todolist-box</span><br><span class="line"> .page-top-card(style='background-image: url(https://pic.imgdb.cn/item/64fc80bd661c6c8e5403c5d9.webp) top / cover no-repeat;')</span><br><span class="line"> .content-item-tips 待办</span><br><span class="line"> span.content-item-title 记录清单</span><br><span class="line"> .content-bottom</span><br><span class="line"> .tips 想要记录的事还有很多,想要做的事源源不断</span><br><span class="line"></span><br><span class="line"> #todolist-container</span><br><span class="line"> each i in site.data.todolist</span><br><span class="line"> #todolist-main</span><br><span class="line"> h3= i.class_name</span><br><span class="line"> .todolist-content</span><br><span class="line"> ul</span><br><span class="line"> each item in i.todo_list</span><br><span class="line"> li</span><br><span class="line"> if item.completed</span><br><span class="line"> i.fa-regular.fa-check-circle</span><br><span class="line"> else</span><br><span class="line"> i.fa-regular.fa-circle</span><br><span class="line"> if item.link</span><br><span class="line"> a(href=item.link)= item.content</span><br><span class="line"> else</span><br><span class="line"> span= item.content</span><br></pre></td></tr></table></figure><p>在 <strong>/themes/anzhiyu/layout</strong> 目录下找到 <strong>page.pug</strong> 文件,添加文件路径:</p><figure class="highlight diff"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="addition">+ when 'todolist'</span></span><br><span class="line"><span class="addition">+ include includes/page/todolist.pug</span></span><br></pre></td></tr></table></figure><p>在 <strong>/source/_data</strong> 新建 <strong>todolist.yml</strong> 文件,添加如下代码:</p><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></pre></td><td class="code"><pre><span class="line"><span class="bullet">-</span> <span class="attr">class_name:</span> <span class="string">我的博客</span></span><br><span class="line"> <span class="attr">todo_list:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">content:</span> <span class="string">冰刻无痕</span></span><br><span class="line"> <span class="attr">completed:</span> <span class="literal">false</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">content:</span> <span class="string">冰梦の博客</span></span><br><span class="line"> <span class="attr">link:</span> <span class="string">https://icemyst.github.io/</span></span><br><span class="line"> <span class="attr">completed:</span> <span class="literal">true</span></span><br><span class="line"> <span class="bullet">-</span> <span class="attr">content:</span> <span class="string">Github支线</span></span><br><span class="line"> <span class="attr">link:</span> <span class="string">https://icemyst.github.io/</span></span><br><span class="line"> <span class="attr">completed:</span> <span class="literal">false</span></span><br></pre></td></tr></table></figure><p>在 <strong>/source/css</strong> 新建 <strong>todolist.css</strong> 文件,添加如下代码:</p><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-pseudo">:root</span> {</span><br><span class="line"> <span class="attr">--todo-border</span>: <span class="number">1px</span> solid <span class="number">#f7a796</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-attr">[data-theme=dark]</span> {</span><br><span class="line"> <span class="attr">--todo-border</span>: <span class="number">1px</span> solid <span class="number">#51908b</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="selector-tag">body</span><span class="selector-attr">[data-type=<span class="string">"todolist"</span>]</span> <span class="selector-id">#page</span> {</span><br><span class="line"> <span class="attribute">border</span>: <span class="number">0</span>;</span><br><span class="line"> <span class="attribute">box-shadow</span>: none <span class="meta">!important</span>;</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">0</span> <span class="meta">!important</span>;</span><br><span class="line"> <span class="attribute">background</span>: <span class="number">0</span> <span class="number">0</span> <span class="meta">!important</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">body</span><span class="selector-attr">[data-type=<span class="string">"todolist"</span>]</span> <span class="selector-id">#page</span> <span class="selector-class">.page-title</span> {</span><br><span class="line"> <span class="attribute">display</span>: none;</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="selector-class">.page-top-card</span> {</span><br><span class="line"> <span class="comment">/* background-size: cover;</span></span><br><span class="line"><span class="comment"> background-position: center; */</span></span><br><span class="line"> <span class="attribute">height</span>: <span class="number">20.5rem</span>;</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">10px</span> <span class="number">2.7rem</span>;</span><br><span class="line"> <span class="attribute">border-radius</span>: <span class="number">20px</span>;</span><br><span class="line"> <span class="attribute">color</span>: <span class="number">#fff</span>;</span><br><span class="line"> <span class="attribute">position</span>: relative;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.page-top-card</span> <span class="selector-tag">span</span><span class="selector-class">.content-item-title</span> {</span><br><span class="line"> <span class="attribute">font-size</span>: <span class="number">2.3em</span>;</span><br><span class="line"> <span class="attribute">font-weight</span>: bold;</span><br><span class="line"> <span class="attribute">line-height</span>: <span class="number">1.2</span>;</span><br><span class="line"> <span class="attribute">font-family</span>: <span class="string">'Microsoft YaHei'</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-class">.page-top-card</span> <span class="selector-class">.content-bottom</span> {</span><br><span class="line"> <span class="attribute">display</span>: flex;</span><br><span class="line"> <span class="attribute">justify-content</span>: space-between;</span><br><span class="line"> <span class="attribute">align-items</span>: center;</span><br><span class="line"> <span class="attribute">position</span>: absolute;</span><br><span class="line"> <span class="attribute">width</span>: <span class="built_in">calc</span>(<span class="number">100%</span> - <span class="number">5.4rem</span>);</span><br><span class="line"> <span class="attribute">bottom</span>: <span class="number">1rem</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-attr">[data-theme=<span class="string">'dark'</span>]</span> <span class="selector-class">.page-top-card</span> {</span><br><span class="line"> <span class="attribute">opacity</span>: .<span class="number">92</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><br><span class="line"><span class="selector-id">#todolist-container</span> {</span><br><span class="line"> <span class="attribute">margin</span>: <span class="number">16px</span> <span class="number">0</span> <span class="number">10px</span>;</span><br><span class="line"> <span class="attribute">column-count</span>: <span class="number">2</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-id">#todolist-main</span> {</span><br><span class="line"> <span class="attribute">break-inside</span>: avoid-column;</span><br><span class="line"> <span class="attribute">background</span>: <span class="number">#fae4df</span>;</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">10px</span>;</span><br><span class="line"> <span class="attribute">border</span>: <span class="number">2px</span> dashed <span class="number">#f7a796</span>;</span><br><span class="line"> <span class="attribute">margin-bottom</span>: <span class="number">20px</span>;</span><br><span class="line"> <span class="attribute">border-radius</span>: <span class="number">12px</span>;</span><br><span class="line"> <span class="attribute">box-shadow</span>: <span class="number">0</span> <span class="number">0</span> <span class="number">5px</span> <span class="number">0</span> <span class="built_in">rgba</span>(<span class="number">0</span>, <span class="number">0</span>, <span class="number">0</span>, <span class="number">0.5</span>);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-attr">[data-theme=dark]</span> <span class="selector-id">#todolist-main</span> {</span><br><span class="line"> <span class="attribute">background</span>: <span class="number">#242424</span>;</span><br><span class="line"> <span class="attribute">border</span>: <span class="number">2px</span> dashed <span class="number">#51908b</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-id">#todolist-main</span> <span class="selector-tag">h3</span> {</span><br><span class="line"> <span class="attribute">margin</span>: <span class="number">0</span> <span class="meta">!important</span>;</span><br><span class="line"> <span class="attribute">border-bottom</span>: <span class="built_in">var</span>(--todo-border);</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-id">#todolist-main</span> <span class="selector-tag">ul</span> {</span><br><span class="line"> <span class="attribute">margin</span>: <span class="number">0</span>;</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">0</span>;</span><br><span class="line"> <span class="attribute">list-style</span>: none;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-id">#todolist-main</span> <span class="selector-tag">li</span> {</span><br><span class="line"> <span class="attribute">font-size</span>: <span class="number">18px</span>;</span><br><span class="line"> <span class="attribute">text-indent</span>: <span class="number">5px</span>;</span><br><span class="line"> <span class="attribute">border-bottom</span>: <span class="built_in">var</span>(--todo-border);</span><br><span class="line"> <span class="attribute">margin</span>: <span class="number">0</span> <span class="meta">!important</span>;</span><br><span class="line"> <span class="attribute">font-weight</span>: normal;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="selector-id">#todolist-main</span> <span class="selector-tag">i</span> {</span><br><span class="line"> <span class="attribute">padding</span>: <span class="number">0</span> <span class="number">5px</span>;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">@media</span> screen <span class="keyword">and</span> (<span class="attribute">max-width</span>: <span class="number">768px</span>) {</span><br><span class="line"> <span class="selector-id">#todolist-container</span> {</span><br><span class="line"> <span class="attribute">column-count</span>: <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line">}</span><br></pre></td></tr></table></figure><p>在主题配置文件 <strong>_config.anzhiyu.yml</strong> 添加如下代码:</p><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></pre></td><td class="code"><pre><span class="line"><span class="attr">inject:</span></span><br><span class="line"> <span class="attr">head:</span></span><br><span class="line"> <span class="comment"># 清单</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string"><link</span> <span class="string">rel="stylesheet"</span> <span class="string">href="/css/todolist.css"></span></span><br></pre></td></tr></table></figure><p>接下来执行<strong>hexo三连</strong>就可以成功访问 <a href="/todolist/">清单</a> 了</p>]]></content>
<summary type="html"><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>最初是在 <strong>Leonus</strong> 的 <a href="https://blog.leonus.cn/2023/me</summary>
<category term="Hexo博客" scheme="https://blhorizon.github.io/categories/Hexo%E5%8D%9A%E5%AE%A2/"/>
<category term="功能" scheme="https://blhorizon.github.io/tags/%E5%8A%9F%E8%83%BD/"/>
</entry>
</feed>