如何檢測 MySQL 資料庫結構是否已更改(不是內容)?


讓我們首先看一個示例並建立一個表——

mysql> create table DemoTable
(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(40),
   StudentAge int,
   StudentMarks int
);
Query OK, 0 rows affected (0.76 sec)

以下是瞭解資料庫結構的查詢——

mysql> show create table DemoTable;

將會產生以下輸出——

+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                    |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable     | CREATE TABLE `DemoTable` (`StudentId` int(11) NOT NULL AUTO_INCREMENT, `StudentName` archar(40) COLLATE utf8_unicode_ci DEFAULT NULL, `StudentAge` int(11) DEFAULT NULL, `StudentMarks` int(11) DEFAULT NULL, PRIMARY KEY (`StudentId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci                 |
+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.06 sec)

現在,讓我們更改資料庫結構——

mysql> alter table DemoTable add column StudentCountryName varchar(20);
Query OK, 0 rows affected (0.65 sec)
Records : 0 Duplicates : 0 Warnings : 0

現在,我們將再次檢查資料庫結構——

mysql> show create table DemoTable;

將會產生以下輸出。我們現在可以匹配結構中的更改——

+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table         | Create Table                                                                                                                                                                                            |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| DemoTable     | CREATE TABLE `DemoTable` ( `StudentId` int(11) NOT NULL AUTO_INCREMENT, `StudentName` varchar(40) COLLATE utf8_unicode_ci DEFAULT NULL, `StudentAge` int(11) DEFAULT NULL, `StudentMarks` int(11) DEFAULT NULL, `StudentCountryName` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`StudentId`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

更新於:06-Jul-2020

225 次觀看

開啟您的職業生涯

透過完成本課程,獲得認證

開始
Advertisement
© . All rights reserved.