next up previous contents
Next: Delete 刪除紀錄 Up: Query 紀錄詢問 Previous: Select 查詢資料表   Contents   DYWANG_HOME

Update 變更紀錄

  1. 先列出資料表中的紀錄。
    mariadb root@localhost:dyw> select * from tbl;                                  
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | PHP     | david  | 2020-01-28      |
    | 2  | MariaDB | dywang | 2020-01-01      |
    | 3  | Linux   | linux  | 2020-01-11      |
    +----+---------+--------+-----------------+
    
    3 rows in set
    Time: 0.031s
    
  2. 變更紀錄 id=1 的 title 為 python。
    mariadb root@localhost:dyw> update tbl set title='python' where id=1;           
    Query OK, 1 row affected
    Time: 0.006s
    
  3. 再列出資料表中的紀錄,紀錄 id=1 的 title 已更新為 python。
    mariadb root@localhost:dyw> select * from tbl;                                  
    +----+---------+--------+-----------------+
    | id | title   | author | submission_date |
    +----+---------+--------+-----------------+
    | 1  | python  | david  | 2020-01-28      |
    | 2  | MariaDB | dywang | 2020-01-01      |
    | 3  | Linux   | linux  | 2020-01-11      |
    +----+---------+--------+-----------------+
    
    3 rows in set
    Time: 0.029s
    
  4. 一次更新一筆紀錄的 2 個欄位。
    mariadb root@localhost:dyw> update tbl set title='c++',author='peter' where id=1
                                ;                                                   
    Query OK, 1 row affected
    Time: 0.008s
    
  5. 再列出資料表中的紀錄,紀錄 id=1 的 title 及 author 2 個欄位都已更新。
    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.030s
    



De-Yu Wang 2020-04-07