WordPress.org

Ready to get started?Download WordPress

Forums

Confusion in how to upgrade database in a custom plugin (3 posts)

  1. Matthew Fries
    Member
    Posted 2 years ago #

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

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

    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.

  2. vtxyzzy
    Member
    Posted 2 years ago #

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

  3. Matthew Fries
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic