How do I create a UNIQUE KEY when creating wpdb table
-
I adapted the SQL query for creating a table from the WordPress codex using the global $wpdb method. However when I try to add another unique ID for lastname the Database is not created.
This code works:
function jal_install() { global $wpdb; global $jal_db_version; $table_name = $wpdb->prefix . 'simons_table'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, firstname tinytext NOT NULL, lastname tinytext NOT NULL, description text NOT NULL, url varchar(55) DEFAULT '' NOT NULL, UNIQUE KEY id (id) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); add_option( 'jal_db_version', $jal_db_version ); }
This code does NOT work:
function jal_install() { global $wpdb; global $jal_db_version; $table_name = $wpdb->prefix . 'simons_table'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, firstname tinytext NOT NULL, lastname tinytext NOT NULL, description text NOT NULL, url varchar(55) DEFAULT '' NOT NULL, UNIQUE KEY id (id), UNIQUE KEY firstname (firstname) ) $charset_collate;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); add_option( 'jal_db_version', $jal_db_version ); }
When I add another UNIQUE ID for fistname, it just won’t create the database when I activate the plugin. I tried ‘firstname’ instead of firstname. I’ve tried multiple variations.
Viewing 9 replies - 1 through 9 (of 9 total)
Viewing 9 replies - 1 through 9 (of 9 total)
- The topic ‘How do I create a UNIQUE KEY when creating wpdb table’ is closed to new replies.