您好,登錄后才能下訂單哦!
對于只備份單張表的備份與還原,通過SELECT命令,可以更快速的達到備份和恢復的目的;
以及通過此方法把一個數據庫中表的數據,導入至另一數據庫的表中去。
備份格式:SELECT * INTO OUTFILE '/PATH/TO/somefile.txt'
FROM table_name [WHERE CLAUSE];
#備份table_name表中的[或者備份滿足WHERE語句的數據]數據至服務器上保存。
注釋:table_name:需要備份的表 WHERE:滿足的條件,可選項。
/PATH/TO:服務器上的路徑目錄,且此目錄必須是執行SELECT語句的用戶有寫的權限,
否則無法備份。
還原格式:LOAD DATA INFILE '/PATH/TO/somefile.txt' INTO TABLE table_name;
注釋:table_name:需要還原的表的名稱,此表必須先在數據庫中存在。
/PATH/TO:備份所存放的路徑
mysql> CREATE TABLE tutor LIKE tutors; #仿照tutors表的框架創建一個空表tutor
實例:備份一個數據庫表的數據,導入至另一個數據庫表的數據;
mysql> USE jiaowu;
Database changed
mysql> SELECT * FROM tutors; #查詢表tutors的信息
+-----+------------------+--------+-----+
| TID | Tname | Gender | Age |
+-----+------------------+--------+-----+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | HuangRong | F | 46 |
| 4 | HuYidao | M | 65 |
| 5 | XiaoLongnv | F | 28 |
| 6 | HuFei | M | 45 |
| 7 | GuoXiang | F | 32 |
+-----+------------------+--------+-----+
7 rows in set (0.00 sec)
mysql> SELECT * INTO OUTFILE '/tmp/tutor.txt' FROM tutors;
Query OK, 7 rows affected (0.01 sec)
mysql> CREATE TABLE tutor LIKE tutors;
Query OK, 0 rows affected (0.03 sec)
mysql> DESC tutor;
+---------+---------------------------+------+-----+---------+------------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------------+------+-----+---------+------------------+
| TID | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| Tname | varchar(50) | NO | | NULL | |
| Gender | enum('F','M') | YES | | M | |
| Age | tinyint(3) unsigned | YES | | NULL | |
+---------+---------------------------+------+-----+---------+------------------+
4 rows in set (0.01 sec)
mysql> DESC tutors ;
+---------+---------------------------+------+-----+---------+------------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------------+------+-----+---------+------------------+
| TID | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| Tname | varchar(50) | NO | | NULL | |
| Gender | enum('F','M') | YES | | M | |
| Age | tinyint(3) unsigned | YES | | NULL | |
+---------+---------------------------+------+-----+---------+------------------+
4 rows in set (0.01 sec)
mysql> DROP TABLE tuors;
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM tutor; #查詢表tutor的信息
Empty set (0.04 sec) (暫時無數據)
mysql> LOAD DATA INFILE '/tmp/tutor.txt' INTO TABLE tutor;
Query OK, 7 rows affected (0.04 sec)
Records: 7 Deleted: 0 Skipped: 0 Warnings: 0
mysql> SELECT * FROM tutor; #查詢表tutor的信息
+-----+------------------+--------+-----+
| TID | Tname | Gender | Age |
+-----+------------------+--------+-----+
| 1 | HongQigong | M | 93 |
| 2 | HuangYaoshi | M | 63 |
| 3 | HuangRong | F | 46 |
| 4 | HuYidao | M | 65 |
| 5 | XiaoLongnv | F | 28 |
| 6 | HuFei | M | 45 |
| 7 | GuoXiang | F | 32 |
+-----+------------------+--------+-----+
至此通過SELECT備份還原操作完成。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。