Create wordpress tables with unique index

2014-02-27T04:33:00.001-08:00

<?php
global $YOURVARIABLEPREFIX_db_version;
$YOURVARIABLEPREFIX_db_version = "1.0";
// Update this value to execute the script.
function JX_modules_install()
{
    global $wpdb;
    global $JX_db_version;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    // You can simply place the code without any explinatiory comments
    // in your sql string
    $table_name = $wpdb->prefix . "_YOURPREFIX_YOURTABLENAME";
    $sql = "CREATE TABLE $table_name (
   id int(11) NOT NULL AUTO_INCREMENT,
   active int(11) NOT NULL,
   name tinytext NOT NULL,
   text text NOT NULL,
   UNIQUE KEY id (id)
   );";
    $res = dbDelta( $sql );
    echo "Update $table_name.<BR/>" . implode('<BR/> - ', $res) . '<BR/><BR/>';
    $table_name = $wpdb->prefix . "_YOURPREFIX_YOUROTHERTABLENAME";
    // Or you can place comments by using the mighty dot...
    $sql = "CREATE TABLE $table_name (
   id int(11) NOT NULL AUTO_INCREMENT,".// You can place comments
    "name varchar(255) NOT NULL,". // On why you are
    "something text NULL,". // Using certain talbes
    "content text NOT NULL,". // This way for future reference....
    "UNIQUE KEY id (id),". // ID as primary key, gotta love integers
    "CONSTRAINT UNIQUE INDEX STYLE_CLASS_NAME (name)".//try to create a nice table index for the names so no duplicates can be inserted.
    ");";
    $res = dbDelta( $sql );
    echo "Update $table_name.<BR/>" . implode('<BR/> - ', $res) . '<BR/><BR/>';
    update_option( "YOURVARIABLEPREFIX_db_version", $YOURVARIABLEPREFIX_db_version );
}
function JX_install_fmv_theme()
{
    global $YOURVARIABLEPREFIX_db_version;
    if(get_option('YOURVARIABLEPREFIX_db_version') != $YOURVARIABLEPREFIX_db_version)
    {
        echo '<div id="message" class="updated below-h2"><p>The database for the WHAT EVER YOUR PLUGIN/TEMPLATE NAME IS need to be updated from version ';
        echo get_option('YOURVARIABLEPREFIX_db_version');
        echo ' to version '. $YOURVARIABLEPREFIX_db_version .'<BR/><BR/>';
        echo 'Installing database updates & settings<BR/>';
        JX_modules_install();
        echo '<BR/>Update complete. </p></div>';
    }
}
// This is to make sure this gets executed.
// You can also attach this to a hook if you like.
JX_install_fmv_theme();
?>