MySQL 在 PHP 中重命名列

发布于 2024-09-14 22:16:25 字数 180 浏览 5 评论 0原文

我需要使用 PHP 重命名 MySQL 表中的列。

这变得很困难,因为语法是 ALTER TABLE [table] CHANGE COLUMN [oldname] [newname] [definition]。该定义是必需的参数。

有没有办法获取定义并将其简单地反馈到 SQL 语句中?一些示例代码会很棒,谢谢!

I need to rename columns in my MySQL table using PHP.

This is made difficult because the syntax is ALTER TABLE [table] CHANGE COLUMN [oldname] [newname] [definition]. The definition is a required parameter.

Is there a way to grab the definition and simply feed this back into the SQL statement? Some sample code would be fantastic, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月染过的梦 2024-09-21 22:16:25

根据 http://codingforums.com/showthread.php?t=148936,您可能必须解析 SHOW CREATE TABLE 的结果才能获取当前定义,然后在 ALTER 语句中使用它。

mysql_fetch_field() 也可能有用。

According to http://codingforums.com/showthread.php?t=148936, you may have to parse the results of SHOW CREATE TABLE to get the current definition, then use that in the ALTER statement.

mysql_fetch_field() may be useful also.

七七 2024-09-21 22:16:25
  1. 您可以阅读information_schema

  2. 显示表状态 [{FROM | IN} db_name] [LIKE '模式' | WHERE 表达式]
  1. You can read information_schema.
  2. SHOW TABLE STATUS [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
述情 2024-09-21 22:16:25

发出 SHOW CREATE TABLE,读出描述感兴趣的列的行,识别列定义并构建您的 ALTER TABLE 语句。

Issue SHOW CREATE TABLE, read off the line describing the column that is of interest, identify the column definition and construct your ALTER TABLE statement.

吾性傲以野 2024-09-21 22:16:25

我的解决方案是这样的:

$table = "tableName";
$createTableSQL = $dbh->Execute('SHOW CREATE TABLE ' . $table);
$createTableSQL = $createTableSQL[0][1];

$mappingTable = "originalToDevMapping";

//get mapping
$sql = "SELECT origField, newField
        FROM " . $mappingTable;
$newColumns = $dbh->Execute($sql);

foreach ($newColumns as $newColumn) {
    if (strlen($newColumn['newField'])<1) {
        echo "***Removing*** " . $newColumn['origField'] . "<br><br>";

        $sql = "ALTER TABLE " . $table . " DROP COLUMN " . $newColumn['origField'];

        $dbh->Execute($sql);
        if (strlen($dbh->errorStr)>1) {
            echo "<br>************************<br>";
            echo "<br>ERROR:<br>";
            echo $dbh->errorStr;
            echo "<br>************************<br>";
        }

    } else {
        echo "Renaming " .  $newColumn['origField'] . " to " . $newColumn['newField'] . "<br><br>";
        $sql = "ALTER TABLE " . $table . " CHANGE COLUMN " . $newColumn['origField'] . " " . $newColumn['newField'];
        $fieldPos = strpos($createTableSQL,$newColumn['origField']);
        $definitionStart = $fieldPos + strlen($newColumn['origField']) + 2;
        $definitionEnd = strpos($createTableSQL,',',$definitionStart) - 1;
        $definition = substr($createTableSQL,$definitionStart,$definitionEnd-$definitionStart+1);

        //workaround - if enum type, comma is included.  
        if (strstr($definition,'enum')) {
            //look for comma after enum end bracket.
            $commaPos = strpos($createTableSQL, ',', strpos($createTableSQL,')',$definitionStart));
            $definition = substr($createTableSQL,$definitionStart,$commaPos-$definitionStart);
        }

        $dbh->Execute($sql . " " . $definition);
        if (strlen($dbh->errorStr)>1) {
            echo "<br>************************<br>";
            echo "ERROR:<br>";
            echo $dbh->errorStr;
            echo "<br>************************<br>";
        }


    }

}

My solution was this:

$table = "tableName";
$createTableSQL = $dbh->Execute('SHOW CREATE TABLE ' . $table);
$createTableSQL = $createTableSQL[0][1];

$mappingTable = "originalToDevMapping";

//get mapping
$sql = "SELECT origField, newField
        FROM " . $mappingTable;
$newColumns = $dbh->Execute($sql);

foreach ($newColumns as $newColumn) {
    if (strlen($newColumn['newField'])<1) {
        echo "***Removing*** " . $newColumn['origField'] . "<br><br>";

        $sql = "ALTER TABLE " . $table . " DROP COLUMN " . $newColumn['origField'];

        $dbh->Execute($sql);
        if (strlen($dbh->errorStr)>1) {
            echo "<br>************************<br>";
            echo "<br>ERROR:<br>";
            echo $dbh->errorStr;
            echo "<br>************************<br>";
        }

    } else {
        echo "Renaming " .  $newColumn['origField'] . " to " . $newColumn['newField'] . "<br><br>";
        $sql = "ALTER TABLE " . $table . " CHANGE COLUMN " . $newColumn['origField'] . " " . $newColumn['newField'];
        $fieldPos = strpos($createTableSQL,$newColumn['origField']);
        $definitionStart = $fieldPos + strlen($newColumn['origField']) + 2;
        $definitionEnd = strpos($createTableSQL,',',$definitionStart) - 1;
        $definition = substr($createTableSQL,$definitionStart,$definitionEnd-$definitionStart+1);

        //workaround - if enum type, comma is included.  
        if (strstr($definition,'enum')) {
            //look for comma after enum end bracket.
            $commaPos = strpos($createTableSQL, ',', strpos($createTableSQL,')',$definitionStart));
            $definition = substr($createTableSQL,$definitionStart,$commaPos-$definitionStart);
        }

        $dbh->Execute($sql . " " . $definition);
        if (strlen($dbh->errorStr)>1) {
            echo "<br>************************<br>";
            echo "ERROR:<br>";
            echo $dbh->errorStr;
            echo "<br>************************<br>";
        }


    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文