mysql - 在 wordpress 中更改表 SQL 命令

标签 mysql database wordpress alter-table

由于我的插件的数据库发生了一些变化,我需要更改一个表以向其中添加一些列,但即使该函数正在运行,表也没有改变。这是我编写的函数

    $sql = "ALTER TABLE `wp_tnt_videos` ADD `date_created` INT NOT NULL DEFAULT '0' AFTER `video_order`, ADD `date_modified` INT NOT NULL DEFAULT '0' AFTER  `date_created`, ADD `user_id` INT NOT NULL DEFAULT '0' AFTER `date_modified`;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);

我读到这个:http://hungred.com/how-to/wordpress-dbdelta-function/ 并尝试仔细检查 sql 命令。我还在 mysql 中运行该 sql 命令并且它有效! 所以,请帮助我! :|

最佳答案

您的答案在法典中:

  • You must put each field on its own line in your SQL statement.
  • You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
  • You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
  • You must not use any apostrophes or backticks around field names.

http://codex.wordpress.org/Creating_Tables_with_Plugins#Adding_an_Upgrade_Function

首先,您在字段名称周围使用了反引号。该规则可能也适用于表名,但我不确定。

此外,您正在尝试使用 dbDelta,就好像您正在对数据库进行直接查询一样。它不是那样工作的。您需要使用 CREATE TABLE 为它提供完整的所需表结构,就像您每次都在创建新表一样......

The dbDelta function examines the current table structure, compares it to the desired table structure, and either adds or modifies the table as necessary, so it can be very handy for updates (see wp-admin/upgrade-schema.php for more examples of how to use dbDelta).

http://codex.wordpress.org/Creating_Tables_with_Plugins#Adding_an_Upgrade_Function

您无需向其提供 ALTER TABLE 命令或仅向其提供您想要更改的部分。它不是那种通用的数据库更改工具。而且,正如已经提到的,它非常挑剔。完全按照示例中的格式格式化您的 SQL,包括换行符。

我要补充一点,你需要非常仔细地观察这个函数。它悄无声息地失败了,似乎不知道该做什么,比如删除索引——至少我在这方面遇到了很多麻烦。我不确定它是否会四处移动列以插入新列或仅添加到末尾。

关于mysql - 在 wordpress 中更改表 SQL 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14432600/

相关文章:

mysql - 将行引用作为 MySQL 函数输入参数传递

css - WordPress - 减少主菜单和 slider 之间的垂直空间

php - 我如何在wordpress页面上打印'bloginfo('url')'

php - MySQL 使用 RegEx 更新/选择列

php - 伪随机字节的存储

mysql - 是否可以从 mysql 函数返回多个值?

iphone - 在哪里可以找到我的 iPhone 应用程序的核心数据持久存储?

database - Django 设计 : storing and retrieving text files

sql - 在 Oracle 11gR2 上使用 Union ALL 的大 View

php - 邮政编码下方的内容无效