next up previous contents
Next: 實機操作練習題 Up: Query 紀錄詢問 Previous: Update 變更紀錄   Contents   DYWANG_HOME

Delete 刪除紀錄

  1. 先列出資料表中的紀錄。
    mariadb root@localhost:dyw> select * from tbl;                                  
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | c++     | peter  | 2020-01-28      |
    | 2  | MariaDB | dywang | 2020-01-01      |
    | 3  | Linux   | linux  | 2020-01-11      |
    +----+---------+--------+-----------------+
    
    3 rows in set
    Time: 0.032s
    
  2. 如果 DELETE 命令不配合 WHERE 條件使用,會刪除資料表中的所有資料。只刪除 id=2 的紀錄。
    mariadb root@localhost:dyw> delete from tbl where id=3;                         
    You're about to run a destructive command.
    Do you want to proceed? (y/n): y
    Your call!
    Query OK, 1 row affected
    Time: 0.008s
    
  3. 再查詢資料表 tbl,只剩下兩筆紀錄。
    mariadb root@localhost:dyw> select * from tbl;                                  
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | c++     | peter  | 2020-01-28      |
    | 2  | MariaDB | dywang | 2020-01-01      |
    +----+---------+--------+-----------------+
    
    2 rows in set
    Time: 0.030s
    



De-Yu Wang 2020-04-07