MySQL Alter Table Change all fields type using wild card?
I am creating a new table by copying an existing table structure:
CREATE TABLE IF NOT EXISTS `PeopleView` LIKE `People`
I then want to c开发者_如何学Pythonhange the data type of all fields using a wild card:
ALTER TABLE `PeopleView` CHANGE * * VARCHAR(3) NOT NULL
Any ideas how to do this?
I can do something like:
ALTER TABLE `PeopleView` CHANGE `FirstName` `FirstName` VARCHAR(3) NOT NULL
But I need a function I can run on many different tables where the field names are all different.
Purpose: Create another table that holds values that are used to decide which columns will be shown or hidden.
PHP Solution:
mysql_query("CREATE TABLE IF NOT EXISTS `PeopleView` LIKE `People`");
$cols = mysql_query("SHOW COLUMNS FROM `PeopleView`");
$sql = "ALTER TABLE `PeopleView` ";
while($row = mysql_fetch_assoc($cols)) {
$sql .= "MODIFY `". $row['Field'] ."` VARCHAR(3) NOT NULL, ";
}
$sql = substr($sql, 0, -2);
mysql_query($sql);
I don't like that I have to run 3 queries to the database, surely there is a better way?
I then want to change the data type of all fields using a wild card:
You cannot do so. The syntax will only allow you to change the field types by enumerating them and their new definition, one by one.
精彩评论