Referring to the Codex on "Creating Tables with Plugins", I can't find any reference on specifying the table storage engine during creation.
Consider the below function to create the table initially:
public function xy_plugin_install() {
global $wpdb;
global $xy_db_version;
$table_name = $wpdb->prefix . 'xy_foobar';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
email varchar(55) DEFAULT '' NOT NULL,
postcode varchar(55) DEFAULT '' NOT NULL,
message text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'xy_db_version', $xy_db_version );
}
With the above, the table is set to MyISAM
by default.
As a preference, the table should be set to InnoDB
if the MySQL ver is greater than or equal to 5.6
, as it has crash recovery built into the Storage Engine's initialization, amongst other advantages.
- Would this be possible to explicitly specify during the initial creation?
- And is there anyway to include a conditional check to determine if MySQL
ver is greater or equal to ver
5.6
?
Referring to the Codex on "Creating Tables with Plugins", I can't find any reference on specifying the table storage engine during creation.
Consider the below function to create the table initially:
public function xy_plugin_install() {
global $wpdb;
global $xy_db_version;
$table_name = $wpdb->prefix . 'xy_foobar';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
email varchar(55) DEFAULT '' NOT NULL,
postcode varchar(55) DEFAULT '' NOT NULL,
message text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'xy_db_version', $xy_db_version );
}
With the above, the table is set to MyISAM
by default.
As a preference, the table should be set to InnoDB
if the MySQL ver is greater than or equal to 5.6
, as it has crash recovery built into the Storage Engine's initialization, amongst other advantages.
- Would this be possible to explicitly specify during the initial creation?
- And is there anyway to include a conditional check to determine if MySQL
ver is greater or equal to ver
5.6
?
1 Answer
Reset to default 1It is possible with MySQL, and it doesn't seem to create any problem with wordpress's dbDelta()
Here is the line you need to change
$charset_collate = $wpdb->get_charset_collate() . ' engine = innoDB';
You simply have to add engine = innoDB
at the end of the SQL query and the correct engine will be used by MySQL
$charset_collate engine = InnoDB;
– Tofandel Commented Jul 29, 2019 at 9:58