Support » Fixing WordPress » Confusion in how to upgrade database in a custom plugin

  • I’m confused by the method for upgrading a database table in a custom plugin as described here:

    Why do they suggest using “CREATE TABLE” for upgrading the database associated with your plugin if you already have a table? Won’t that cause MYSQL errors because the table already exists? Shouldn’t I use “ALTER TABLE” to make the changes instead? I see the same “CREATE TABLE” on other WordPress tutorials, but no real explanation. Every non-wordpress example I’ve found of upgrading a DB table tells you to use “ALTER TABLE.”

    I assume I’m misunderstanding how the upgrade process works in WordPress and I’m really confused.

    Thanks for any clarification.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think the answer is that the plugin code uses the dbDelta function which takes care of the difference. From the article:

    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).

    Aha!!! Thanks. That sheds some light on it. Not sure how I missed that sentence (I read that article a few times). Certainly allows for some lazy update scripting, but I guess it makes a lot of sense to prevent folks from running raw SQL statements on the database from their plugin.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Confusion in how to upgrade database in a custom plugin’ is closed to new replies.