還原資料表

  1. 使用上一章用 mysqldump 指令匯出的資料表檔案 dump.txt,查詢 dump.txt 所屬的資料庫為 dyw,資料表名稱為 animals。
    [root@kvm3 ~]# grep Database dump.txt
    -- Host: localhost    Database: dyw
    [root@kvm3 ~]# grep Table dump.txt
    -- Table structure for table `animals`
    
  2. 將檔案 dump.txt 的紀錄匯入到資料庫 dyw。
    [root@kvm3 ~]# mariadb -uroot -p123qwe dyw < dump.txt
    
  3. 查看資料庫 dyw 的資料表 animals 還原成 4 筆紀錄。
    [root@kvm3 ~]# mariadb -uroot -p123qwe -e "select * from dyw.animals;"
    +----+---------+------+
    | id | name    | foot |
    +----+---------+------+
    |  1 | dog     |    4 |
    |  2 | cat     |    4 |
    |  3 | bird    |    2 |
    |  4 | chicken |    2 |
    +----+---------+------+
    
  4. 資料表 animals 的欄位 id 也設定回為 primary key 且 auto_increment。
    [root@kvm3 ~]# mariadb -uroot -p123qwe -e "desc dyw.animals;"
    +-------+---------------------+------+-----+---------+----------------+
    | Field | Type                | Null | Key | Default | Extra          |
    +-------+---------------------+------+-----+---------+----------------+
    | id    | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |
    | name  | varchar(20)         | NO   |     | NULL    |                |
    | foot  | tinyint(3) unsigned | NO   |     | NULL    |                |
    +-------+---------------------+------+-----+---------+----------------+