cala
cala

Reputation: 1421

Wordpress codex for creating tables with plugins?

So it says that I have to put a double space between PRIMARY KEY. Does this mean that I have to put a double space between PRIMARY and KEY. Also, what does it mean to put it between the definition of your PRIMARY KEY too? Thanks!


global $wpdb;

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $table_name (
  id mediumint(9) NOT NULL PRIMARY KEY 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)

) $charset_collate;";

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

Upvotes: 2

Views: 531

Answers (1)

ham-sandwich
ham-sandwich

Reputation: 4052

This is a good question, and no, no need for an extra space between PRIMARY KEY. The dbDelta() function can be precarious about the SQL statement served as a parameter so don't use PRIMARY KEY declaration as you would normally. The primary key is declared (PRIMARY KEY) and then the definition (id). You should add the primary key as follows:

global $wpdb;

$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,
  name tinytext NOT NULL,
  text text NOT NULL,
  url varchar(55) DEFAULT '' NOT NULL,
  PRIMARY KEY  (id)
) $charset_collate;";

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

Upvotes: 1

Related Questions