您可以借助 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”。該數據庫有以下表格 –
- employeestudent
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 中將所有表和列重命名為小寫?的詳細內容,更多請關注www.92cms.cn其它相關文章!