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!
You must put each field on its own line in your SQL statement.
You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
You must not use any apostrophes or backticks around field names.
Field types must be all lowercase
SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
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 );
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 );
Related
I'm trying to add a new table which has a relation with {wp_posts} table, This code is working properly in localhost and it passes all tests but it failed on server database and generates [Cannot add foreign key constraint].
The [post_id] field is exactly like {wp_posts->ID} field .. I don't know what I missed.
global $wpdb;
$charset_collate = $wpdb->collate;
$tbl_my_users = $wpdb->prefix . '_TABLE_NAME';
dbDelta( "CREATE TABLE IF NOT EXISTS {$tbl_my_users} (
user_id BIGINT(20) UNSIGNED NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
name VARCHAR(150) NOT NULL,
email TINYTEXT NULL,
INDEX (name),
PRIMARY KEY (user_id),
CONSTRAINT Constr_Unique_UserID UNIQUE( user_id ),
CONSTRAINT Constr_Unique_PostID UNIQUE( post_id ),
CONSTRAINT Constr_Unique_User UNIQUE( name ),
CONSTRAINT Constr_My_Users
FOREIGN KEY FK_My_Users (post_id) REFERENCES {$wpdb->posts} (ID) ON DELETE CASCADE ON UPDATE CASCADE
) COLLATE {$charset_collate}" );
old question but i face this problem now.
the issue is when we want to set relation we need to match the column type, collation and the engine.
wp_posts ID : bigint(20) UNSIGNED
your table post_id: bigint(20) UNSIGNED
this is match, but you have problem with ENGINE.
example : the old table is written as innodb but your MYSQL default engine is set as MYISAM. so you without define ENGINE type, you will create MYISAM table instead InnoDB which not support relationship/foreign key. Note: old mysql version using MYISAM as default engine, not InnoDB
In my problem:
wp_posts ID: bigint(20) UNSIGNED InnoDB
my table post_id: bigint(20) InnoDB
the engine and type is match, but i got the eror. Because i forgot to set attribute UNSIGNED. when i set the same attribute, all works perfectly.
Note: don't forget about collation when using string type like varchar, char, etc
i hope this will help others.
try this bewlo query
global $wpdb;
$charset_collate = $wpdb->collate;
$tbl_my_users = $wpdb->prefix . '_TABLE_NAME';
dbDelta("CREATE TABLE IF NOT EXISTS ".$tbl_my_users." (
user_id BIGINT(20) UNSIGNED NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
name VARCHAR(150) NOT NULL,
email TINYTEXT NULL,
INDEX (name),
PRIMARY KEY (user_id),
CONSTRAINT Constr_Unique_UserID UNIQUE( user_id ),
CONSTRAINT Constr_Unique_PostID UNIQUE( post_id ),
CONSTRAINT Constr_Unique_User UNIQUE( name ),
CONSTRAINT Constr_My_Users
FOREIGN KEY (`post_id`) REFERENCES ".$wpdb->prefix."posts (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) COLLATE SET utf8 COLLATE utf8_general_ci" );
Trying to create a pair of database tables within my table. Below is the code block that is being executed on the activation of my plugin. Wordpress reports that it's successful, however, when refreshing the database the employee table is not being created. The department table however is created successfully.
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
// create the database table.
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "plugin_department";
$table_name2 = $wpdb->prefix . "plugin_employee";
$sql = "CREATE TABLE $table_name (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
$sql2 = "CREATE TABLE $table_name2 (
id INT(11) NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
department_id INT(11) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT department_id
FOREIGN KEY (id)
REFERENCES plugin_department (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
) $charset_collate;";
dbDelta($sql);
dbDelta($sql2);
Ideas on how to resolve would be greatly appreciated.
Discovered the issue, using mysql workbench I had it generate the create table script for me, then was a simple copy and paste into the $sql2 variable. My assumption is that there must be a wonky character or a syntax error that is hard for my eyes to pick out.
$sql2 = "CREATE TABLE $table_name2 (
`id` INT NOT NULL AUTO_INCREMENT,
`first_name` VARCHAR(255) NOT NULL,
`last_name` VARCHAR(45) NOT NULL,
`department_id` INT NOT NULL,
PRIMARY KEY (`id`),
INDEX `wp_plugin_employee_department_idx` (`department_id` ASC),
CONSTRAINT `wp_plugin_employee_department`
FOREIGN KEY (`department_id`)
REFERENCES `plugin_intranet`.`wp_plugin_department` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)";
Create Table
global $wpdb;
if ($wpdb->get_var("SHOW TABLES LIKE '{$wpdb->prefix}tabloadi'") != $wpdb->prefix . 'tabloadi'){
$wpdb->query("CREATE TABLE {$wpdb->prefix}tabloadi(
id integer not null auto_increment,
alan1 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan2 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan3 TINYTEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
alan4tarih TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);");
}
SELECT
global $wpdb;
$tabloadi = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}tabloadi WHERE sart1=$deger1" );
foreach($tabloadi as $row)
{
echo $row->id;
echo $row->alan1;
echo $row->tarih;
}
Delete
global $wpdb;
$delete = $wpdb->delete($wpdb->prefix.'tabloadi',array('alan1'=>$alan1deger,'alan2'=>$alan2deger));
So I want to create a new table in my database and I can't seem to get it working. It's a wordpress plugin so I don't know if that's what could be messing it up.
if ($wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . DB_PROFILE_TABLE . '"') != $wpdb->prefix . DB_PROFILE_TABLE)
{
$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(100) NOT NULL,
sname VARCHAR(100),
city VARCHAR(100),
country VARCHAR(100)
CHARSET=utf8; ';
dbDelta($sql);
}
Beginner at php and mySql.
You are missing parenthesis...
Can you try in this way
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,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
These are the rules to create table using wordpress plugin
1) You must put each field on its own line in your SQL statement.
2) You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
3) You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
4) You must not use any apostrophes or backticks around field names.
Field types must be all lowercase.
5) SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
Refer http://codex.wordpress.org/Creating_Tables_with_Plugins
Looks like you are missing a closing parenthesis after country VARCHAR(100). You may also need to remove the semi-colon as well.
$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(100) NOT NULL,
sname VARCHAR(100),
city VARCHAR(100),
country VARCHAR(100))
CHARSET=utf8';
dbDelta($sql);
I am trying to create a Wordpress plugin. The code for setting up the database tables will be provided shortly. However the problem I'm facing is not not an error as much as a lack thereof. No matter what I do, dbdelta seems to do absolutely nothing - and wordpress will not generate any relevant error to at least give me a clue on what's going wrong.
The code is:
function create_tables()
{
global $wpdb;
$pl_nm = VSystem::$plugin_name;
$create_voters = "create table if not exists {$wpdb->prefix}{$pl_nm}_voters(
ID bigint(20) unsigned auto_increment,
user_id bigint(20) unsigned not null,
primary key (ID),
foreign key (user_id) references {$wpdb->prefix}users(ID)
);";
$create_links = "create table if not exists {$wpdb->prefix}{$pl_nm}_links(
ID bigint(20) unsigned auto_increment,
product bigint(20) unsigned,
name varchar(256) not null,
url varchar(512) not null,
description text,
posted datetime,
poster bigint(20) unsigned not null,
primary key (ID),
foreign key (poster) references {$wpdb->prefix}users(ID)
);";
$create_votes = "create table if not exists {$wpdb->prefix}{$pl_nm}_votes(
ID bigint(20) unsigned auto_increment,
link_id bigint(20) unsigned not null,
voter_id bigint(20) unsigned not null,
choice int not null,
primary key (ID),
foreign key (link_id) references {$wpdb->prefix}{$pl_nm}_links(ID),
foreign key (voter_id) references {$wpdb->prefix}users(ID)
);";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_voters );
dbDelta( $create_links );
dbDelta( $create_votes );
dbDelta("asdasdsadsad");
//wp_die("{$wpdb->prefix}{$pl_nm}");
add_option('db_version', VSystem::$defaults['db_version']);
}
function vsystem_install()
{
create_tables();
}
Now I know this function is being called at least because uncommenting the wp_die command acts as it is supposed to. Can I get any help?
dbDelta("asdasdsadsad");
Not even that seems to generate any kind of response. I might be able to do this on my own if someone could tell me how to get dbdelta to do anything.
global $wpdb;
$table_name = $wpdb->prefix . "product_order";
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
range VARCHAR(255),
category VARCHAR(255),
order mediumint(9),
relation mediumint(9),
UNIQUE KEY id (id)
);";
$wpdb->query($sql);
That doesn't seem to create a table... any reasons why? I tried dbdelta too.
For the sake of an answer, surround the column names in back ticks as range and order are reserved words in MySQL:
$sql = "CREATE TABLE $table_name (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`range` VARCHAR(255),
`category` VARCHAR(255),
`order` mediumint(9),
`relation` mediumint(9),
UNIQUE KEY id (id)
);";
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
As a side note, if you want to make sure you don't conflict with other tables should this schema be shared with other wordpress instances or become a multisite, consider using {$wpdb->prefix}$table_name instead of just $table_name to use the database prefix defined in wp-config.php.