MySQL 核心操作全解析(用戶 + SHOW+DML+DCL)

MySQL 核心操作全解析(用戶 + SHOW+DML+DCL)

基于你提供的實操筆記,我們將 MySQL 核心操作拆解為用戶管理、SHOW 查詢命令、DML 數據操作、TRUNCATE 與 DELETE 對比、DCL 權限控制五大模塊,梳理語法邏輯、補充避坑提示,幫你系統性掌握 “用戶 - 數據 - 權限” 的完整操作鏈路

一、MySQL 用戶管理(創建 / 刪除 / 登錄)

MySQL 用戶的核心標識是 ‘用戶名’@‘主機’USERNAME@HOST),其中HOST用于限制用戶的登錄來源,是避免未授權訪問的關鍵

1.1 用戶的核心組成:USERNAME@HOST

HOST字段決定 “用戶能從哪些主機連接 MySQL”,常見取值及含義:

HOST 取值含義示例
具體 IP 地址僅允許從該 IP 登錄127.0.0.1(本地回環)、192.168.100.10(遠程 IP)
%(通配符)允許從任意主機登錄(謹慎使用,存在安全風險)'syf'@'%'
_(通配符)匹配單個字符(如'192.168.100._'允許 192.168.100 網段所有 IP 登錄)'syf'@'192.168.100._'
localhost僅允許本地通過 “socket 文件” 登錄(區別于127.0.0.1的 TCP 連接)'syf'@'localhost'

1.2 用戶操作實戰(DDL)

(1)創建用戶
  • 語法:CREATE USER '用戶名'@'主機' IDENTIFIED BY '密碼';

  • 示例(創建允許從任何主機登錄的用戶syf,密碼redhat):

    mysql> create user 'syf'@'192.168.100.%' identified by 'redhat';
    Query OK, 0 rows affected (0.00 sec)
    
    [root@syf2 ~]# mysql -usyf -h 192.168.100.10 -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MySQL connection id is 3
    Server version: 5.7.37 MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [(none)]> exit
    Bye
    
    2)用戶登錄(命令行)
    • 語法:mysql -u用戶名 -h主機IP -p-h需與用戶的HOST匹配,否則登錄失敗)
    • 示例(用syf登錄):
    [root@syf ~]# mysql -usyf -h192.168.100.10 -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 15
    Server version: 5.7.37 MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> 
    
    (3)刪除用戶
    • 語法:DROP USER '用戶名'@'主機';(必須指定完整的USERNAME@HOST,否則可能刪錯)
    • 示例:
    mysql> drop user 'syf'@'192.168.100.%';
    Query OK, 0 rows affected (0.00 sec)mysql> select User,Host from mysql.user;
    +---------------+-----------+
    | User          | Host      |
    +---------------+-----------+
    | mysql.session | localhost |
    | mysql.sys     | localhost |
    | root          | localhost |
    +---------------+-----------+
    3 rows in set (0.00 sec)
    

    二、SHOW 系列命令:MySQL 的 “查看工具集”

    SHOW 命令用于查詢 MySQL 的系統信息、庫表結構、配置參數等,是日常運維和開發的高頻工具。按用途分類如下:

    2.1 查看系統基礎信息

    命令用途關鍵說明
    SHOW CHARACTER SET;查看 MySQL 支持的所有字符集推薦用utf8mb4(支持中文、emoji,utf8實際是utf8mb3,不支持 emoji)
    SHOW ENGINES;查看當前 MySQL 支持的存儲引擎默認引擎是InnoDB(支持事務、外鍵),MyISAM不支持事務,僅用于歷史場景

    2.2 查看庫表相關信息

    命令用途示例結果說明
    SHOW DATABASES;查看所有數據庫包含系統庫(information_schema元數據、mysql權限庫、sys管理庫)和自定義庫(如sy
    SHOW TABLES;查看當前數據庫的所有表需先執行USE 數據庫名;(如USE sy;
    SHOW TABLES FROM 數據庫名;不切換數據庫,直接查看指定庫的表示例:SHOW TABLES FROM sy;(查看sy庫的表)
    DESC [數據庫名.]表名;(或DESCRIBE查看表結構(字段名、類型、約束等)字段Null列顯示YES表示允許 NULL,Key列顯示PRI表示主鍵
    SHOW CREATE TABLE 表名;查看表的完整創建語句(含引擎、字符集)可用于復制表結構(如遷移表時直接復用 SQL)
    SHOW TABLE STATUS LIKE '表名'\G查看表的詳細狀態(行數、創建時間等)\G表示按行顯示結果(避免字段過多換行混亂),Rows列顯示表中記錄數

    三、DML 操作:數據的 “增刪改查”(核心)

    DML(Data Manipulation Language)是針對表中數據的操作,核心是INSERT(增)、SELECT(查)、UPDATE(改)、DELETE(刪),必須在 “切換到數據庫 + 存在表” 的前提下執行

    1. 首先創建表結構(CREATE TABLE

    根據表中字段(idnameage)和數據特征,創建表的 SQL 語句如下:

    -- 創建 shenyi 表
    mysql> use sy;
    Database changed
    mysql> create table shenyi(-> id int primary key auto_increment,-> name varchar(50) not null,-> age int);
    Query OK, 0 rows affected (0.01 sec)
    
    字段說明:
    • id:整數類型,設為主鍵(PRIMARY KEY)且自增(AUTO_INCREMENT),確保每條記錄唯一,插入時無需手動指定。
    • name:字符串類型(VARCHAR(50)),NOT NULL 表示姓名不能為空。
    • age:整數類型(INT),未加 NOT NULL,允許存儲 NULL(如 lisi 的年齡)。

    2. 插入示例數據(INSERT INTO

    創建表后,插入查詢結果中的 7 條記錄:

    mysql> insert into shenyi (name,age) values-> ('tom',20),-> ('jerry',23),-> ('shenyi',25),-> ('sean',28),-> ('zhangshan',26),-> ('zhangshan',20),-> ('lisi',null);
    Query OK, 7 rows affected (0.01 sec)
    Records: 7  Duplicates: 0  Warnings: 0
    

    3. 驗證結果

    mysql> select * from shenyi;
    +----+-----------+------+
    | id | name      | age  |
    +----+-----------+------+
    |  1 | tom       |   20 |
    |  2 | jerry     |   23 |
    |  3 | shenyi    |   25 |
    |  4 | sean      |   28 |
    |  5 | zhangshan |   26 |
    |  6 | zhangshan |   20 |
    |  7 | lisi      | NULL |
    +----+-----------+------+
    7 rows in set (0.00 sec)
    

    只查詢name字段的所有值(不顯示idage):

    包含重復值zhangshan(出現 2 次),說明表中允許同名記錄,這也解釋了后續按name篩選時需要結合age的原因

    mysql> select name from shenyi;
    +-----------+
    | name      |
    +-----------+
    | tom       |
    | jerry     |
    | shenyi    |
    | sean      |
    | zhangshan |
    | zhangshan |
    | lisi      |
    +-----------+
    7 rows in set (0.00 sec)
    
    排序查詢(ORDER BY

    升序(ORDER BY age)

    age從小到大排列,NULL值(最小)排在最前,之后按20→23→25→26→28順序排列,與表中數據完全匹配

    mysql> select * from shenyi order by age;
    +----+-----------+------+
    | id | name      | age  |
    +----+-----------+------+
    |  7 | lisi      | NULL |
    |  1 | tom       |   20 |
    |  6 | zhangshan |   20 |
    |  2 | jerry     |   23 |
    |  3 | shenyi    |   25 |
    |  5 | zhangshan |   26 |
    |  4 | sean      |   28 |
    +----+-----------+------+
    7 rows in set (0.00 sec)
    

    降序(ORDER BY age DESC)

    age從大到小排列,最大的28排在最前,依次遞減,NULL值(最大)排在最后,邏輯與升序完全相反

    mysql> select * from shenyi order by age desc;
    +----+-----------+------+
    | id | name      | age  |
    +----+-----------+------+
    |  4 | sean      |   28 |
    |  5 | zhangshan |   26 |
    |  3 | shenyi    |   25 |
    |  2 | jerry     |   23 |
    |  1 | tom       |   20 |
    |  6 | zhangshan |   20 |
    |  7 | lisi      | NULL |
    +----+-----------+------+
    7 rows in set (0.01 sec)
    
    限制結果行數(LIMIT
    • LIMIT 2:取排序后前 2 條記錄(升序中是NULL和第一個20

      mysql> select * from shenyi order by age limit 2;
      +----+------+------+
      | id | name | age  |
      +----+------+------+
      |  7 | lisi | NULL |
      |  1 | tom  |   20 |
      +----+------+------+
      2 rows in set (0.00 sec)
      
    • LIMIT 1,2LIMIT 偏移量, 行數,偏移量從 0 開始。這里偏移 1(跳過第 1 條NULL),取后面 2 條,即兩個age=20的記錄(id=1 和 id=6)

      mysql> select * from shenyi order by age limit 1,2;
      +----+-----------+------+
      | id | name      | age  |
      +----+-----------+------+
      |  1 | tom       |   20 |
      |  6 | zhangshan |   20 |
      +----+-----------+------+
      2 rows in set (0.00 sec)
      
    4. 條件篩選(WHERE
    • age >= 25:篩選age不小于 25 的記錄,對應25、26、28三個值,共 3 條

      mysql> select * from shenyi where age >= 25;
      +----+-----------+------+
      | id | name      | age  |
      +----+-----------+------+
      |  3 | shenyi    |   25 |
      |  4 | sean      |   28 |
      |  5 | zhangshan |   26 |
      +----+-----------+------+
      3 rows in set (0.00 sec)
      

      age >= 25 AND name = ‘zhangshan’:多條件篩選,需同時滿足 “年齡≥25” 和 “姓名為 zhangshan”。表中zhangshan`有兩條記錄(id=5:26 歲;id=6:20 歲),僅 id=5 符合條件

      mysql> select * from shenyi where age >= 25 and name='zhangshan';
      +----+-----------+------+
      | id | name      | age  |
      +----+-----------+------+
      |  5 | zhangshan |   26 |
      +----+-----------+------+
      1 row in set (0.00 sec)
      

      age BETWEEN 23 and 28:等價于age >=23 AND age <=28,包含23、25、26、28四個值,共 4 條記錄

      mysql> select * from shenyi where age between 23 and 28;
      +----+-----------+------+
      | id | name      | age  |
      +----+-----------+------+
      |  2 | jerry     |   23 |
      |  3 | shenyi    |   25 |
      |  4 | sean      |   28 |
      |  5 | zhangshan |   26 |
      +----+-----------+------+
      4 rows in set (0.00 sec)
      

      age IS NOT NULL/age IS NULL:專門針對NULL值的篩選,分別返回 6 條非空記錄和 1 條空值記錄(id=7)

      mysql> select * from shenyi where age is not null;
      +----+-----------+------+
      | id | name      | age  |
      +----+-----------+------+
      |  1 | tom       |   20 |
      |  2 | jerry     |   23 |
      |  3 | shenyi    |   25 |
      |  4 | sean      |   28 |
      |  5 | zhangshan |   26 |
      |  6 | zhangshan |   20 |
      +----+-----------+------+
      6 rows in set (0.00 sec)mysql> select * from shenyi where age is null;
      +----+------+------+
      | id | name | age  |
      +----+------+------+
      |  7 | lisi | NULL |
      +----+------+------+
      1 row in set (0.00 sec)
      

    3.1 INSERT:插入數據

    • 語法:

      INSERT INTO 表名 (字段1, 字段2, ...) VALUES (值1, 值2, ...), (值1, 值2, ...);
      

      支持單條插入(1 個VALUES)和多條插入(多個VALUES用逗號分隔)

      示例(插入數據到sy.shenyi表):

      -- 單條插入
      INSERT INTO shenyi (id, name, age) VALUES (1, 'tom', 20);
      -- 多條插入(更高效,減少IO)
      INSERT INTO shenyi (id, name, age) 
      VALUES (2, 'jerry', 23), (3, 'shenyi', 25), (7, 'lisi', NULL);
      

      ?? 注意:字段順序需與VALUES順序一致;允許 NULL 的字段(如age)可直接寫NULL(無需加引號);非 NULL 字段(如idname)必須填寫值,否則報錯

    3.2 SELECT:查詢數據(最復雜也最常用)

    SELECT`是 DML 的核心,支持按條件過濾、排序、限制結果數量,語法:
    `SELECT 字段列表 FROM 表名 [WHERE 條件] [ORDER BY 字段 排序方式] [LIMIT 偏移量, 數量];
    
    (1)字段列表表示法
    表示符含義示例
    *查詢所有字段(開發中盡量避免,效率低)SELECT * FROM shenyi;
    字段1, 字段2僅查詢指定字段(推薦,減少數據傳輸)SELECT name, age FROM shenyi;
    字段 AS 別名給字段起別名(方便閱讀)SELECT name AS 用戶名, age AS 年齡 FROM shenyi;
    (2)WHERE 條件:過濾數據
    條件類型操作符 / 關鍵字示例(查詢shenyi表)
    比較運算>, <, >=, <=, =, !=SELECT * FROM shenyi WHERE age >= 25;(年齡≥25)
    范圍匹配BETWEEN 最小值 AND 最大值(閉區間)SELECT * FROM shenyi WHERE age BETWEEN 23 AND 28;(23≤age≤28)
    NULL 判斷IS NULL(空)、IS NOT NULL(非空)SELECT * FROM shenyi WHERE age IS NULL;(年齡未填寫)
    邏輯組合AND(且)、OR(或)、NOT(非)SELECT * FROM shenyi WHERE age >=25 AND name='zhangshan';(年齡≥25 且姓名是 zhangshan)
    模糊匹配LIKE(配合%/_SELECT * FROM shenyi WHERE name LIKE 'zhang%';(姓名以 zhang 開頭)
    (3)ORDER BY:排序
    • 默認升序(ASC),顯式指定DESC為降序;
    • NULL 值排序規則:升序時 NULL 排在最前,降序時 NULL 排在最后(如用戶示例中ORDER BY age DESClisi的 NULL 排在最后);
    • 示例:SELECT * FROM shenyi ORDER BY age DESC;(按年齡降序)。
    (4)LIMIT:限制結果數量
    • 語法 1:LIMIT N(取前 N 條記錄)—— 示例:LIMIT 2(取前 2 條);
    • 語法 2:LIMIT 偏移量, N(跳過前 “偏移量” 條,取 N 條)—— 示例:LIMIT 1, 2(跳過第 1 條,取第 2-3 條);
    • 用途:分頁查詢(如第 1 頁LIMIT 0,10,第 2 頁LIMIT 10,10)。

    3.3 UPDATE:修改數據

    • 語法:UPDATE 表名 SET 字段1=新值1, 字段2=新值2, ... [WHERE 條件];
    • 示例(將lisi的年齡改為 30):
    mysql> update shenyi set age = 30 where name = 'lisi';
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from shenyi;
    +----+-----------+------+
    | id | name      | age  |
    +----+-----------+------+
    |  1 | tom       |   20 |
    |  2 | jerry     |   23 |
    |  3 | shenyi    |   25 |
    |  4 | sean      |   28 |
    |  5 | zhangshan |   26 |
    |  6 | zhangshan |   20 |
    |  7 | lisi      |   30 |
    +----+-----------+------+
    7 rows in set (0.00 sec)
    

    ?? 致命風險:如果不加 WHERE 條件,會修改表中所有記錄(如UPDATE shenyi SET age=30;會把所有用戶的年齡改為 30),生產環境必須先寫SELECT驗證條件,再執行UPDATE

    3.4 DELETE:刪除數據

    • 語法:DELETE FROM 表名 [WHERE 條件];

    • 示例:

      -- 刪除單條記錄(id=7的記錄)
      mysql> delete from shenyi where id = 7;
      Query OK, 1 row affected (0.00 sec)
      mysql> select * from shenyi;
      +----+-----------+------+
      | id | name      | age  |
      +----+-----------+------+
      |  1 | tom       |   20 |
      |  2 | jerry     |   23 |
      |  3 | shenyi    |   25 |
      |  4 | sean      |   28 |
      |  5 | zhangshan |   26 |
      |  6 | zhangshan |   20 |
      +----+-----------+------+
      6 rows in set (0.00 sec)
      
    -- 刪除所有記錄(不加WHERE,謹慎!)
    mysql> delete from shenyi;
    Query OK, 6 rows affected (0.00 sec)mysql> select * from shenyi;
    Empty set (0.00 sec)
    

記錄刪除,表結構依然存在:

  mysql> desc shenyi;+-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          |+-------+-------------+------+-----+---------+----------------+| id    | int(11)     | NO   | PRI | NULL    | auto_increment || name  | varchar(50) | NO   |     | NULL    |                || age   | int(11)     | YES  |     | NULL    |                |+-------+-------------+------+-----+---------+----------------+3 rows in set (0.00 sec)

?? 注意:DELETE FROM 表名;會刪除表中所有數據,但保留表結構(字段、約束等),且刪除的記錄可通過事務回滾恢復(若開啟事務)

四、TRUNCATE vs DELETE:清空表數據的區別

用戶示例中提到了TRUNCATE,它與DELETE都能清空數據,但核心差異極大,生產環境需嚴格區分:

對比維度DELETETRUNCATE
操作對象逐行刪除記錄直接釋放表的數據頁(不逐行刪除)
事務支持支持(可通過ROLLBACK回滾恢復數據)不支持(刪除后無法恢復,屬于 DDL 操作)
執行速度慢(逐行記錄日志)快(僅釋放數據頁,日志量少)
自增 ID 重置不重置(刪除后新增記錄,自增 ID 繼續遞增)重置(刪除后新增記錄,自增 ID 從 1 開始)
外鍵約束限制可刪除有外鍵引用的表數據(需滿足外鍵規則)不能刪除有外鍵引用的表數據(直接報錯)
語法形式DML 操作(DELETE FROM 表名;DDL 操作(TRUNCATE TABLE 表名;,TABLE 可省略)

?? 選擇建議:

  • 需恢復數據 / 保留自增 ID:用DELETE FROM 表名 WHERE 條件;
  • 無需恢復數據 / 追求速度(如測試環境清空表):用TRUNCATE 表名;
  • 有外鍵關聯的表:只能用DELETE,不能用TRUNCATE

示例:

mysql> select * from shenyi;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  8 | tom       |   20 |
|  9 | jerry     |   23 |
| 10 | shenyi    |   25 |
| 11 | sean      |   28 |
| 12 | zhangshan |   26 |
| 13 | zhangshan |   20 |
| 14 | lisi      | NULL |
+----+-----------+------+
7 rows in set (0.00 sec)mysql> truncate shenyi;
Query OK, 0 rows affected (0.00 sec)mysql> select * from shenyi;
Empty set (0.00 sec)mysql> desc shenyi;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50) | NO   |     | NULL    |                |
| age   | int(11)     | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

五、DCL 操作:權限的 “授予與回收”

DCL(Data Control Language)用于管理用戶的操作權限,確保不同用戶只能訪問自己權限范圍內的庫表,核心是GRANT(授權)、REVOKE(回收權限)、FLUSH PRIVILEGES(刷新權限)

5.1 GRANT:授予權限

  • 語法:`GRANT 權限類型 ON 操作對象 TO ‘用戶名’@‘主機’ [IDENTIFIED BY ‘密碼’] [WITH GRANT OPTION]
    各部分含義:

    1. 權限類型ALL(所有權限)、SELECT(查詢)、INSERT(插入)、UPDATE(修改)、DELETE(刪除)等
    2. 操作對象:
      • *.*:所有數據庫的所有表(超級權限,僅給管理員)
      • 數據庫名.*:指定數據庫的所有表(如sy.*表示sy庫的所有表)
      • 數據庫名.表名:指定數據庫的指定表(如sy.chenyu
    3. IDENTIFIED BY '密碼':若用戶不存在,可直接創建用戶并授權(MySQL 5.7 + 支持,8.0 + 需先創建用戶再授權)
    4. WITH GRANT OPTION:允許被授權用戶將自己的權限轉授給其他用戶(不建議給普通用戶,存在權限擴散風險)
  • 示例:

    -- 授權shenyi從localhost登錄,擁有所有庫表的所有權限
    GRANT ALL ON *.* TO 'shenyi'@'localhost' IDENTIFIED BY 'redhat';
    -- 授權shenyi從192.168.100.10登錄,僅擁有cy庫所有表的所有權限
    GRANT ALL ON sy.* TO 'shenyi'@'192.168.100.10' IDENTIFIED BY 'redhat';
    -- 授權shenyi從任意主機登錄(%),擁有所有庫表的所有權限(生產環境禁用%)
    GRANT ALL ON *.* TO 'shenyi'@'%' IDENTIFIED BY 'redhat';
    

5.2 查看權限

  • 查看當前登錄用戶的權限:SHOW GRANTS;
  • 查看指定用戶的權限:SHOW GRANTS FOR '用戶名'@'主機';
    示例:SHOW GRANTS FOR 'shenyi'@'localhost';(查看chenyu在localhost的權限)

5.3 REVOKE:回收權限

  • 語法:REVOKE 權限類型 ON 操作對象 FROM '用戶名'@'主機';

  • 示例(回收shenyi在 192.168.100.10 的所有權限):

    REVOKE ALL ON *.* FROM 'shenyi'@'192.168.100.10';
    

5.4 FLUSH PRIVILEGES:刷新權限

  • 原理:MySQL 啟動時會將mysql庫的授權表(如userdb)加載到內存,GRANT/REVOKE通常會自動刷新內存,但特殊情況(如手動修改授權表數據)需手動刷新
  • 語法:FLUSH PRIVILEGES;
  • 場景:回收權限后若立即生效,可執行此命令(用戶示例中回收權限后執行了該命令)

1.搭建mysql服務略
2.創建一個以你名字為名的數據庫,并創建一張表student,該表包含三個字段(id,name,age),表結構如下:

mysql> create database shenyi;
Query OK, 1 row affected (0.00 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| shenyi             |
| sy                 |
| sys                |
+--------------------+
6 rows in set (0.01 sec)mysql> use shenyi;
Database changed
mysql> create table student(-> id int(11) primary key auto_increment,-> name varchar(100) not NULL,-> age tinyint(4));
Query OK, 0 rows affected (0.02 sec)

3.查看下該新建的表有無內容(用select語句)

mysql> select * from student;
Empty set (0.00 sec)mysql> desc student;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(100) | NO   |     | NULL    |                |
| age   | tinyint(4)   | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)

4.往新建的student表中插入數據(用insert語句),結果應如下所示:

mysql> insert into student(name,age) values-> ('tom',20),-> ('jerry',23),-> ('shenyi',25),-> ('sean',28),-> ('zhangshan',26),-> ('zhangshan',20),-> ('lisi',NULL),-> ('chenshuo',10),-> ('wangwu',3),-> ('qiuyi',15),-> ('qiuxiaotian',20);
Query OK, 11 rows affected (0.00 sec)
Records: 11  Duplicates: 0  Warnings: 0mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | shenyi      |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        | NULL |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

5.修改lisi的年齡為50

mysql> update student set age=50 where name='lisi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | shenyi      |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

6.以age字段降序排序

mysql> select * from student order by age desc;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  7 | lisi        |   50 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  3 | shenyi      |   25 |
|  2 | jerry       |   23 |
|  1 | tom         |   20 |
|  6 | zhangshan   |   20 |
| 11 | qiuxiaotian |   20 |
| 10 | qiuyi       |   15 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |    3 |
+----+-------------+------+
11 rows in set (0.00 sec)

7.查詢student表中年齡最小的3位同學跳過前2位

mysql> select * from student order by age limit 2,3;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
| 10 | qiuyi       |   15 |
|  1 | tom         |   20 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
3 rows in set (0.00 sec)

8.查詢student表中年齡最大的4位同學

mysql> select * from student order by age desc limit 4;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  7 | lisi      |   50 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
|  3 | shenyi    |   25 |
+----+-----------+------+
4 rows in set (0.00 sec)

9.查詢student表中名字叫zhangshan的記錄

mysql> select * from student where name='zhangshan';
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
|  6 | zhangshan |   20 |
+----+-----------+------+
2 rows in set (0.01 sec)

10.查詢student表中名字叫zhangshan且年齡大于20歲的記錄

mysql> select * from student where name='zhangshan' and age > 20;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  5 | zhangshan |   26 |
+----+-----------+------+
1 row in set (0.00 sec)

11.查詢student表中年齡在23到30之間的記錄

mysql> select * from student where age between 23 and 30;
+----+-----------+------+
| id | name      | age  |
+----+-----------+------+
|  2 | jerry     |   23 |
|  3 | shenyi    |   25 |
|  4 | sean      |   28 |
|  5 | zhangshan |   26 |
+----+-----------+------+
4 rows in set (0.00 sec)

12.修改wangwu的年齡為100

mysql> update student set age=100 where name='wangwu';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | shenyi      |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  6 | zhangshan   |   20 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
11 rows in set (0.00 sec)

13.刪除student中名字叫zhangshan且年齡小于等于20的記錄

mysql> delete from student where name='zhangshan' and age<=20;
Query OK, 1 row affected (0.01 sec)mysql> select * from student;
+----+-------------+------+
| id | name        | age  |
+----+-------------+------+
|  1 | tom         |   20 |
|  2 | jerry       |   23 |
|  3 | shenyi      |   25 |
|  4 | sean        |   28 |
|  5 | zhangshan   |   26 |
|  7 | lisi        |   50 |
|  8 | chenshuo    |   10 |
|  9 | wangwu      |  100 |
| 10 | qiuyi       |   15 |
| 11 | qiuxiaotian |   20 |
+----+-------------+------+
10 rows in set (0.00 sec)

14.創建表course表,要求如下

mysql> create table course(-> id int(3) primary key,-> course_name varchar(100));
Query OK, 0 rows affected (0.00 sec)mysql> desc course;
+-------------+--------------+------+-----+---------+-------+
| Field       | Type         | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+-------+
| id          | int(3)       | NO   | PRI | NULL    |       |
| course_name | varchar(100) | YES  |     | NULL    |       |
+-------------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

15、為course表插入數據

mysql> insert into course values-> (1,'Java'),-> (2,'MySQL'),-> (3,'Python'),-> (4,'Go'),-> (5,'C++');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0mysql> select *from course;
+----+-------------+
| id | course_name |
+----+-------------+
|  1 | Java        |
|  2 | MySQL       |
|  3 | Python      |
|  4 | Go          |
|  5 | C++         |
+----+-------------+
5 rows in set (0.00 sec)

16、創建student123表,要求如下:

mysql> create table student123(-> id int(3) primary key,-> name varchar(100),-> age int(3),-> sex varchar(10),-> height int(3),-> course_id int(3),-> constraint st123_courseid_course_id foreign key (course_id) references course(id));
Query OK, 0 rows affected (0.01 sec)

17、為student123表插入數據如下:

mysql> insert into student123 values-> (1,'Dany',25,'man',160,1),-> (2,'Green',23,'man',158,2),-> (3,'Henry',23,'woman',185,1),-> (4,'Jane',22,'man',162,3),-> (5,'Jim',24,'woman',175,2),-> (6,'John',21,'woman',172,4),-> (7,'Lily',22,'man',165,4),-> (8,'Susan',23,'man',170,5),-> (9,'Thomas',22,'woman',178,5),-> (10,'Tom',23,'woman',165,5);
Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0mysql> select * from student123;
+----+--------+------+-------+--------+-----------+
| id | name   | age  | sex   | height | course_id |
+----+--------+------+-------+--------+-----------+
|  1 | Dany   |   25 | man   |    160 |         1 |
|  2 | Green  |   23 | man   |    158 |         2 |
|  3 | Henry  |   23 | woman |    185 |         1 |
|  4 | Jane   |   22 | man   |    162 |         3 |
|  5 | Jim    |   24 | woman |    175 |         2 |
|  6 | John   |   21 | woman |    172 |         4 |
|  7 | Lily   |   22 | man   |    165 |         4 |
|  8 | Susan  |   23 | man   |    170 |         5 |
|  9 | Thomas |   22 | woman |    178 |         5 |
| 10 | Tom    |   23 | woman |    165 |         5 |
+----+--------+------+-------+--------+-----------+
10 rows in set (0.00 sec)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/pingmian/97895.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/97895.shtml
英文地址,請注明出處:http://en.pswp.cn/pingmian/97895.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

多語言編碼Agent解決方案(6)-部署和使用指南

部署和使用指南 本指南提供完整的部署和使用說明&#xff0c;幫助您設置后端服務并在VSCode、Eclipse和IntelliJ中使用相應的插件。這個解決方案基于vLLM提供AI編碼輔助&#xff0c;支持英語、中文和日文。 前提條件 操作系統&#xff1a;Linux、macOS或Windows&#xff08;推薦…

濾波器的三重境界:從信號處理到自動駕駛測試的基石

在自動駕駛的宏大敘事中&#xff0c;我們常常聚焦于人工智能、深度學習、高精地圖等"明星技術"。然而&#xff0c;在這些耀眼的光環背后&#xff0c;有一個低調卻至關重要的"幕后英雄"——濾波器。它不僅是信號處理的工具&#xff0c;更是連接物理世界與數…

Part4.第8章:神經網絡

第8章 激活函數 如果沒有激活函數&#xff0c;不論幾層的神經網絡都是一個線性回歸。激活函數的作用是引入非線性。

nextjs+shadcn+tailwindcss實現博客中的overview

最近在用nextjsshadcntailwindcss練手&#xff0c;實現一個博客。做到了overView這里&#xff0c;可實現如下效果1.首先要安裝tailwindcss&#xff0c;這個在創建項目的時候就安裝了。2.然后安裝shadcn,官網教程&#xff1a;3.代碼如下&#xff1a;import {Card,CardContent } …

Kotlin 高階語法解析

Kotlin 高級語法深度解析1. 協程&#xff08;Coroutines&#xff09;1.1 基礎概念1.掛起和恢復2.協程構建器 (Coroutine Builders)3.協程作用域4.調度器1.2 核心用法1.3 實戰示例2. 密封類&#xff08;Sealed Classes&#xff09;2.1 定義與特性2.2 模式匹配2.3 應用場景3. 內聯…

9 基于機器學習進行遙感影像參數反演-以隨機森林為例

目錄 1 讀取數據 2 數據預處理 3模型訓練 4模型預測 5精度分析 由于回歸任務的標簽數據獲取比較困難,我們這次用水體指數NDWI來模擬作為回歸任務的標簽,通過隨機森林來擬合回歸NDWI,其計算公式如下: NDWI = (band3 - band5) / (band3 + band5) 實際情況下需要回歸的數…

C++多線程編程:跨線程操作全解析

C中的"線程"通常指單個執行流&#xff08;如std::thread對象&#xff09;&#xff0c;而"多線程"指程序中同時存在多個這樣的執行流&#xff0c;并涉及它們的創建、管理和同步。實現跨線程操作的核心在于安全地處理共享數據和線程間通信。 以下是實現跨線程…

【腦電分析系列】第13篇:腦電源定位:從頭皮到大腦深處,EEG源定位的原理、算法與可視化

前言腦電信號&#xff08;Electroencephalography, EEG&#xff09;是一種非侵入性的神經成像技術&#xff0c;能夠實時捕捉大腦的電活動。然而&#xff0c;頭皮上記錄到的信號是腦源活動經過頭皮、顱骨等介質“模糊”后的投影。想要從這些頭皮EEG信號追溯到大腦深處的電活動&a…

MySQL知識筆記

DATE_ADD(date,INTERVAL expr type) date 參數是合法的日期表達式。expr 參數是您希望添加的時間間隔。多查官方手冊&#xff01;&#xff01;命令行啟動和停止sql服務net start 數據庫名&#xff1b; 這是啟動服務命令&#xff1b; 例如&#xff1a;net start Mysql56…

2025算法八股——深度學習——MHA MQA GQA

MHA、MQA、GQA 都是深度學習中注意力機制的相關概念&#xff0c;其中 MHA 是標準的多頭注意力機制&#xff0c;MQA 和 GQA 則是其優化變體&#xff0c;以下是它們的區別、優缺點介紹&#xff1a;區別MHA&#xff08;多頭注意力&#xff09;&#xff1a;是 Transformer 架構的核…

Vue3》》eslint Prettier husky

安裝必要的依賴 npm install -D eslint eslint/js vue/eslint-config-prettier prettier eslint-plugin-vue 初始化 ESLint 配置 npm init eslint/config// eslint.config.js // 針對 JavaScript 的 ESLint 配置和規則。保持 JavaScript 代碼的一致性和質量 import js from &qu…

Custom SRP - Point and Spot Lights

https://catlikecoding.com/unity/tutorials/custom-srp/point-and-spot-lights/Lights with Limited Influence1 Point Lights1.1 Other Light Data (Point )同方向光一樣,我們支持有限數量的 Other Light.盡管場景中可能有很多 Other Lights,可能有超過光源上限的光源時可見的…

hive數據倉庫的搭建

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄前言一、內嵌模式二、本地模式三、遠程模式前言 HIVE是基于HDFS的數據倉庫&#xff0c;要首先搭建好HADOOP的集群才可以正常使用HIVE&#xff0c;HADOOP集運搭建詳見…

域名SSL證書免費申請lcjmSSL

.-.lcjmSSL&#xff08;又名“來此加密”&#xff09;是一個提供免費SSL證書申請的一站式平臺。它支持單域名、多域名以及泛域名證書申請&#xff0c;且單張證書最高可覆蓋100個域名&#xff0c;讓您輕松實現全站HTTPS加密。為什么您的網站必須安裝SSL證書&#xff1f;數據加密…

“能量逆流泵”:一種基于電容陣與開關矩陣的超高效大功率降壓架構

摘要本文提出并驗證了一種面向大功率降壓應用的革命性電源架構——"能量逆流泵"&#xff08;Energy Inversion Pump, EIP&#xff09;。該架構摒棄了傳統Buck轉換器中的電感元件&#xff0c;通過高速開關矩陣控制的電容陣列&#xff0c;將高壓側能量以"分時、分…

打造精簡高效的 uni-app 網絡請求工具

在 uni-app 開發中&#xff0c;網絡請求是連接前端與后端的核心橋梁。一個設計良好的請求工具能夠顯著提升開發效率&#xff0c;減少重復代碼。本文將分享一個精簡版的 uni-app 網絡請求工具實現&#xff0c;它保留了核心功能同時保持了足夠的靈活性。設計思路一個優秀的網絡請…

【面試場景題】交易流水表高qps寫入會有鎖等待或死鎖問題嗎

文章目錄一、先明確交易流水表的核心特性二、InnoDB的鎖機制在流水表寫入場景的表現1. 行鎖&#xff08;Record Lock&#xff09;&#xff1a;基本不涉及2. 間隙鎖&#xff08;Gap Lock&#xff09;與Next-Key Lock&#xff1a;幾乎不觸發3. 表鎖&#xff1a;僅在極端場景出現三…

項目部署——LAMP、LNMP和LTMJ

前情提要問&#xff1a;如何通過nginx的反向代理&#xff0c;代理多臺虛擬主機&#xff08;一臺apache服務器上的虛擬主機&#xff09;&#xff1f;1.在nginx的配置文件中&#xff0c;將基于域名的訪問改為基于端口的訪問&#xff08;nginx.conf&#xff09;upstream daili{ser…

晨曦中,它已勞作:一臺有溫度的機器人如何重塑我們的潔凈日常

清晨六點&#xff0c;城市的輪廓在微光中逐漸清晰。某高端小區的路面上&#xff0c;一臺灰色機身、線條流暢的機器正在安靜地工作。它繞過停靠的車輛&#xff0c;精準地沿著路緣石前進&#xff0c;吸走落葉與塵土&#xff0c;遇到突然竄出的流浪貓時輕巧避讓&#xff0c;仿佛有…

【最新高級版】酷柚易汛生產管理系統v1.2.8 +uniapp全開源+文檔教程

酷柚易汛生產管理系統是基于FastAdminThinkPHPLayuiuniapp開發的生產管理系統&#xff0c;幫助企業數字化轉型&#xff0c;打造智能工廠&#xff0c;專業為生產企業量身開發的一套完整的生產管理系統。主要包含以下模塊&#xff1a;購貨模塊、生產模塊、倉庫模塊、資料模塊&…