我們?cè)诓僮饕粡埍碇埃绻抡`刪除數(shù)據(jù)芦瘾,可以進(jìn)行事先備份闷盔。
一:復(fù)制表結(jié)構(gòu)
方法1:
MySQL [test]> create table csvmy like mycsv; //僅僅復(fù)制表結(jié)構(gòu)
Query OK, 0 rows affected (0.03 sec)
MySQL [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| csvmy |
| mycsv |
+----------------+
方法2:
MySQL [test]> create table csv select * from mycsv limit 0;
Query OK, 0 rows affected (0.03 sec) //僅僅復(fù)制表結(jié)構(gòu)
Records: 0 Duplicates: 0 Warnings: 0
MySQL [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| csv |
| csvmy |
| mycsv |
+----------------+
3 rows in set (0.01 sec)
方法3:
MySQL [test]> show create table mycsv\G;
*************************** 1. row ***************************
Table: mycsv
Create Table: CREATE TABLE `mycsv` (
`id` int(11) NOT NULL,
`c1` varchar(10) NOT NULL,
`c2` char(20) NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)
獲得創(chuàng)建表的sql語句,然后復(fù)制語句旅急,修改表名逢勾。
二,復(fù)制表結(jié)構(gòu)藐吮,以及表數(shù)據(jù)
方法1:
MySQL [test]> create table a select * from mycsv;
Query OK, 3 rows affected (0.04 sec)
Records: 3 Duplicates: 0 Warnings: 0
MySQL [test]> select * from a;
+----+-----+-----+
| id | c1 | c2 |
+----+-----+-----+
| 1 | aaa | bbb |
| 2 | ccc | ddd |
| 3 | eee | fff |
+----+-----+-----+
3 rows in set (0.01 sec)
方法2:
mysql> create table d select user_name,user_pass from users where id=1;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
方法3:
先創(chuàng)建一個(gè)空表溺拱, INSERT INTO 新表 SELECT * FROM 舊表 ,或者
INSERT INTO 新表(字段1,字段2,…….) SELECT 字段1,字段2,…… FROM 舊表