먼저 예제를 보고 테이블을 생성해 보겠습니다 -
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)