0

I am attempting to have my plugin create a new table. I am fairly new to SQL. I have found two different method for creating tables:

Method I

$table_name = $wpdb->prefix . 'cool_table';
$sql = "CREATE TABLE $table_name (
    [command to set up columns go here]
    ) DEFAULT CHARSET=utf8;";

Method II

if ( ! empty($wpdb->charset) )
    $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if ( ! empty($wpdb->collate) )
    $charset_collate .= " COLLATE $wpdb->collate";

$table_name = $wpdb->prefix . 'cool_table';

$sql = "CREATE TABLE $table_name (
    [command to set up columns go here]
    ) ENGINE = InnoDB {$charset_collate};";

What is the benefit of setting the default charset vs. setting the ENGINE when creating a new table?

4
  • This is a MySQL question but the charset and the database engine are different animals. Your two blocks of code do different things.
    – s_ha_dum
    Commented Apr 13, 2013 at 2:39
  • What do you mean? Do these two pieses of code not both set up a SQL statement that can be used with the dbDelta() function in order to create a new table in the database?
    – William
    Commented Apr 13, 2013 at 4:45
  • But the second-- the ENGINE one-- changes the database engine to InnoDB for the table created. The first creates the table using the server default database engine, which used to be MYISAM but I heard a rumor that that was changing.
    – s_ha_dum
    Commented Apr 13, 2013 at 13:34
  • Yea my understanding is that the most recent version of MySQL is using InnoDB as the default. When I specify the engine here, does it change it for the entire database or just the table? Also, is there a reason for me to set the default charset when creating a table? What are the implications of this?
    – William
    Commented Apr 13, 2013 at 16:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.