Why I cant create table directly using CREATE in wordpress

$sql = "CREATE TABLE 'my_custom_table'(
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
  name tinytext NOT NULL,
  text text NOT NULL,
  url VARCHAR(55) DEFAULT '' NOT NULL,
  UNIQUE KEY id (id)
);";

require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );

This is my code. It produce an error when I try to execute it. However, if I replace the my_custom_table string with a variable $table_name = 'my_custom_table'; This works. Why??

Related posts

Leave a Reply

1 comment

  1. The proper notation is:

    CREATE TABLE `my_custom_table`
    

    Single quotes define strings. Backticks escape table names.

    It’s possible you did this in your alternate version:

    CREATE TABLE my_custom_table
    

    The backticks are not required unless your table name contains a non-standard character (outside A-Z, 0-9 or _), or is a reserved word. It’s safe to use them in all circumstances, though, if you prefer to be consistent.