您可以藉助 INFORMATION_SCHEMA.COLUMNS 來實現此目的。語法如下 -
SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CHANGE `', COLUMN_NAME, '` `', LOWER(COLUMN_NAME), '` ', COLUMN_TYPE, ';') AS anyAliasName FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ‘yourDatabaseName’;
現在使用有兩個表格的資料庫。資料庫名稱如下「bothinnodbandmyisam」。這個資料庫有以下表格 -
#employee 表的描述是如下 -
mysql> desc employee;
以下是輸出。假設員工表中有下列不是小寫的 -
+--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | EmployeeId | int(11) | YES | | NULL | | | EmployeeName | varchar(30) | YES | | NULL | | +--------------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
學生表的說明如下。查詢如下 -
mysql> desc student;
以下是輸出。假設學生表中有以下列不是小寫的 -
+-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | StudentId | int(11) | YES | | NULL | | | StudentName | varchar(20) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
這是將所有表的列名更改為小寫的查詢。查詢如下 -
mysql> SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CHANGE `', COLUMN_NAME, '` `', -> LOWER(COLUMN_NAME), '` ', COLUMN_TYPE, ';') AS changeColumnNameToLower -> FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'bothinnodbandmyisam';
以下是顯示 ALTER TABLE 命令的輸出,其中顯示了更新的列名稱 -
+------------------------------------------------------------------------+ | changeColumnNameToLower | +------------------------------------------------------------------------+ | ALTER TABLE employee CHANGE `EmployeeId` `employeeid` int(11); | | ALTER TABLE employee CHANGE `EmployeeName` `employeename` varchar(30); | | ALTER TABLE student CHANGE `StudentId` `studentid` int(11); | | ALTER TABLE student CHANGE `StudentName` `studentname` varchar(20); | +------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
查看上面的範例輸出,所有列名稱都已變更為小寫。
以上是在 MySQL 中將所有表格和欄位重新命名為小寫?的詳細內容。更多資訊請關注PHP中文網其他相關文章!