MySQL複製表的命令?
您可以使用INSERT INTO SELECT語句來實現此目的。語法如下:
INSERT INTO yourDatabaseName.yourTableName(SELECT *FROM yourDatabaseName.yourTableName);
為了理解上述語法,讓我們在一個數據庫中建立一個表,在另一個數據庫中建立第二個表。
資料庫名稱為“bothinnodbandmyisam”。讓我們在同一資料庫中建立一個表。查詢如下:
mysql> create table Student_Information -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10), -> Age int -> ); Query OK, 0 rows affected (0.67 sec)
現在,您可以使用insert命令在表中插入一些記錄。查詢如下:
mysql> insert into Student_Information(Name,Age) values('Larry',30); Query OK, 1 row affected (0.20 sec) mysql> insert into Student_Information(Name,Age) values('Mike',26); Query OK, 1 row affected (0.19 sec) mysql> insert into Student_Information(Name,Age) values('Bob',26); Query OK, 1 row affected (0.12 sec) mysql> insert into Student_Information(Name,Age) values('Carol',24); Query OK, 1 row affected (0.15 sec)
現在,您可以使用select語句顯示錶中的所有記錄。查詢如下:
mysql> select *from Student_Information;
以下是輸出:
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Larry | 30 | | 2 | Mike | 26 | | 3 | Bob | 26 | | 4 | Carol | 24 | +----+-------+------+ 4 rows in set (0.00 sec)
這是第二個資料庫:
mysql> use sample; Database changed
現在在這個資料庫中只建立一個表。查詢如下:
mysql> create table Student_Table_sample -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentName varchar(20), -> StudentAge int , -> PRIMARY KEY(StudentId) -> ); Query OK, 0 rows affected (0.57 sec)
以下是複製表的命令。查詢如下:
mysql> insert into sample.Student_Table_sample(select *from bothinnodbandmyisam.Student_Information); Query OK, 4 rows affected (0.23 sec) Records: 4 Duplicates: 0 Warnings: 0
受影響的記錄有四條,這意味著表已成功複製。以下是顯示第二個表“Student_Table_sample”中所有記錄的查詢。
查詢如下:
mysql> select *from Student_Table_sample;
以下是顯示另一個數據庫中表中記錄的輸出:
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Larry | 30 | | 2 | Mike | 26 | | 3 | Bob | 26 | | 4 | Carol | 24 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
廣告