共計 6215 個字符,預計需要花費 16 分鐘才能閱讀完成。
本篇內容介紹了“MySQL 怎么復制表”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
1.MySQL 中的外鍵
1) 只能用于 InnoDB 表。
2) 父表可以在另一個數(shù)據(jù)庫中。
3) 父表中被參照的字段只要有索引就行,可以存在重復的值。
4) 在“foreign key (colname) references tabname (colname)”后面加上 on delete cascade,可以實現(xiàn)級聯(lián)刪除。
注:除了 CASCADE,delete 后面還可以跟著 SET NULL,RESTRICT,NO ACTION 等關鍵字。另外,還可以使用 on update 子句實現(xiàn)級聯(lián)更新,且兩者可以同時使用,這里不再贅述。
[@more@]
2. 表類型
MyISAM 和 InnoDB 是 MySQL 中最重要的表類型。一個 MyISAM 表在文件系統(tǒng)中有三個對應的文件(tabname.frm, tabname.MYD, tabname.MYI),而一個 InnoDB 表則只有一個對應的文件(tabname.frm)。
1)MyISAM
MyISAM 延伸了基本的 ISAM 類型,它可以在不同的操作系統(tǒng)和平臺之間進行移植,支持大表文件(大于 4GB),允許對 BLOB 和 TEXT 列進行索引,可以對表和表索引進行壓縮(這個特性對于 BLOB 和 TEXT 字段很實用)。它還可以約束 VARCHAR 字段的長度,或者自動根據(jù)數(shù)據(jù)的情況進行動態(tài)調節(jié),支持使用鍵前綴和使用完整的鍵搜索記錄。
MySQL 啟動時會自動檢查 MyISAM 表以防止崩潰,甚至可以在出現(xiàn)錯誤時進行修復。表數(shù)據(jù)文件和表索引文件可以存儲在不同的位置,甚至不同的文件系統(tǒng)中。即使進行大量的插入、更新和刪除操作的表,智能防碎片邏輯也能保證其高性能的協(xié)作性。
2)InnoDB
InnoDB 是 MySQL 5.1 中的默認表類型,它完全兼容 ACID,又擁有可以與 MyISAM 媲美的性能,完全支持 MySQL 的事務處理并且不會降低速度或性能,它也可以在不同的操作系統(tǒng)和體系結構之間移植。InnoDB 提供行級和表級鎖定,也支持無鎖定讀操作(類似 Oracle)和多版本。另外,它還提供對外鍵、提交、回顧和前滾的操作的支持。
InnoDB 支持在需要時自動在內存中創(chuàng)建散列索引來提高性能,還使用緩沖來提高可靠性和數(shù)據(jù)庫操作的速度。異步輸入 / 輸出和一系列的讀緩沖提高了數(shù)據(jù)檢索的速度,“伙伴算法”和 Oracle 類型的表空間可以進行文件的優(yōu)化和內存的管理。
此外,還有 ISAM(主要是兼容舊版本)、HEAP(在內存中創(chuàng)建的臨時表)、BerkleyDB、MERGE(多個 MyISAM 表組合成一個單獨的表)等類型。
3. 其他表修飾符
除了 TYPE 外,還可以指定許多其他屬性來控制創(chuàng)建表的操作。
屬性 描述
AUTO_INCREMENT 第一次向 AUTO_INCREMENT 字段插入的值
CHECKSUM 是否儲存表校驗和(布爾值)
COMMENT 表的描述性注釋
MAX_ROWS 表中存儲的最大行數(shù)
MIN_ROWS 表中存儲的最小行數(shù)
PACK_KEYS 是否壓縮表索引(布爾值)
UNION 映射到一個單獨的 MERGE 表的表
DATA DIRECTORY 表數(shù)據(jù)文件的位置
INDEX DIRECTORY 表索引文件的位置
4. 復制表
MySQL 中可以結合 CREATE TABLE 和 SELECT 語句來復制表。例如:
mysql create table test_crttb
– (
– id tinyint(3),
– name varchar(15),
– primary key (id)
–
Query OK, 0 rows affected (0.10 sec)
mysql insert into test_crttb values (1, Adam
Query OK, 1 row affected (0.04 sec)
mysql insert into test_crttb values (2, Bob
Query OK, 1 row affected (0.03 sec)
mysql insert into test_crttb values (3, Clark
Query OK, 1 row affected (0.03 sec)
mysql select * from test_crttb;
+—-+——-+
| id | name |
+—-+——-+
| 1 | Adam |
| 2 | Bob |
| 3 | Clark |
+—-+——-+
3 rows in set (0.00 sec)
mysql create table test_crttb2 select * from test_crttb;
Query OK, 3 rows affected (0.52 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql select * from test_crttb;
+—-+——-+
| id | name |
+—-+——-+
| 1 | Adam |
| 2 | Bob |
| 3 | Clark |
+—-+——-+
3 rows in set (0.00 sec)
注意,select 緊跟在“create table …”后面,前面沒有 as,這一點和 Oracle 不同。
可以添加一個不成立的 where 條件,來創(chuàng)建一個結構相同的空表:
mysql desc test_crttb;
+——-+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| id | tinyint(3) | NO | PRI | 0 | |
| name | varchar(15) | YES | | NULL | |
+——-+————-+——+—–+———+——-+
2 rows in set (0.02 sec)
mysql desc test_crttb3;
+——-+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| id | tinyint(3) | NO | | 0 | |
| name | varchar(15) | YES | | NULL | |
+——-+————-+——+—–+———+——-+
2 rows in set (0.03 sec)
但是,這種方法只能復制表本身,表上的鍵需要手動創(chuàng)建。要在復制表的同時復制表上的鍵,可以使用下面的方法:
mysql create table test_crttb4 like test_crttb;
Query OK, 0 rows affected (0.13 sec)
mysql show create table test_crttb;
+————+——————————————————————
——————————————————————————–
——————+
| Table | Create Table
|
+————+——————————————————————
——————————————————————————–
——————+
| test_crttb | CREATE TABLE `test_crttb` (
`id` tinyint(3) NOT NULL DEFAULT 0 ,
`name` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+————+——————————————————————
——————————————————————————–
——————+
1 row in set (0.00 sec)
mysql show create table test_crttb4;
+————-+—————————————————————–
——————————————————————————–
——————–+
| Table | Create Table
|
+————-+—————————————————————–
——————————————————————————–
——————–+
| test_crttb4 | CREATE TABLE `test_crttb4` (
`id` tinyint(3) NOT NULL DEFAULT 0 ,
`name` varchar(15) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+————-+—————————————————————–
——————————————————————————–
——————–+
1 row in set (0.00 sec)
這樣,就創(chuàng)建一個結構相同的空表,并帶有原表上的鍵。這時可以向其中插入原表中的記錄:
mysql insert into test_crttb4 select * from test_crttb;
Query OK, 3 rows affected (0.24 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql select * from test_crttb4;
+—-+——-+
| id | name |
+—-+——-+
| 1 | Adam |
| 2 | Bob |
| 3 | Clark |
+—-+——-+
3 rows in set (0.00 sec)
還可以創(chuàng)建一個既包含原表中的列,又包含新的列的“混合表”:
mysql create table test_crttb5
– (
– tel smallint(15)
– )
– select * from test_crttb;
Query OK, 3 rows affected (0.16 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql select * from test_crttb5;
+——+—-+——-+
| tel | id | name |
+——+—-+——-+
| NULL | 1 | Adam |
| NULL | 2 | Bob |
| NULL | 3 | Clark |
+——+—-+——-+
3 rows in set (0.00 sec)
使用 alter table 語句的 first 和 after 子句可以調整字段的位置:
mysql alter table test_crttb5 modify id tinyint(3) first;
Query OK, 3 rows affected (0.65 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql desc test_crttb5;
+——-+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————–+——+—–+———+——-+
| id | tinyint(3) | YES | | NULL | |
| tel | smallint(15) | YES | | NULL | |
| name | varchar(15) | YES | | NULL | |
+——-+————–+——+—–+———+——-+
3 rows in set (0.01 sec)
mysql alter table test_crttb5 modify tel smallint(15) after name;
Query OK, 3 rows affected (0.56 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql desc test_crttb5;
+——-+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——-+————–+——+—–+———+——-+
| id | tinyint(3) | YES | | NULL | |
| name | varchar(15) | YES | | NULL | |
| tel | smallint(15) | YES | | NULL | |
+——-+————–+——+—–+———+——-+
3 rows in set (0.01 sec)
“MySQL 怎么復制表”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質量的實用文章!