How to detect if a MySQL database structure changed (not content)?



Let us first see an example and create a table −

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)

Following is the query to know the database structure −

mysql> show create table DemoTable;

This will produce the following output −

+---------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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)

Now, let us change the database structure −

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

We will now check the database structure once again −

mysql> show create table DemoTable;

This will produce the following output. We can now match the changes in the structure −

+---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 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)
Updated on: 2020-07-06T11:39:59+05:30

308 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements