I used following query to create a table, but it returns error about foreign key:
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
supervisor char(30) NOT NULL,
customer char(30) NOT NULL,
order_id int(40) NOT NULL,
status int(5) NOT NULL,
amount int(40) DEFAULT 0,
FOREIGN KEY (order_id) REFERENCES wp_post(ID),
) $charset_collate;";
Error:
20-Sep-2015 12:12:36] خطای You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''ID') ON DELETE CASCADE
) DEFAULT CHARACTER SET utf8' at line 8 در پایگاهداده وردپرس برای دستور CREATE TABLE wp_Arvand_Marketing (
id mediumint(9) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
supervisor char(30) NOT NULL,
customer char(30) NOT NULL,
order_id int(40) NOT NULL,
status int(5) NOT NULL,
amount int(40) DEFAULT 0,
FOREIGN KEY (order_id) REFERENCES post('ID') ON DELETE CASCADE
) DEFAULT CHARACTER SET utf8 ساخته شده توسط activate_plugin, do_action('activate_woocommerce-arvandkala/safircod.php'), call_user_func_array, adv_activate_plugins, dbDelta
You meant to use backtique around the ID column name and not single quote; which is essentially making it a string literal rather a column name.
Your foreign key line
REFERENCES post('ID') ON DELETE CASCADE
It should be
REFERENCES post(`ID`) ON DELETE CASCADE
Related
I have created tables in MYSQL as follows
Author Table
CREATE TABLE `author` (
`AuthorId` varchar(25) NOT NULL,
`AuthorName` varchar(50) NOT NULL,
PRIMARY KEY (`AuthorId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Books Table
CREATE TABLE `books` (
`DocId` int(11) NOT NULL,
`ISBN` varchar(13) NOT NULL,
PRIMARY KEY (`DocId`),
KEY `DocId` (`DocId`),
CONSTRAINT `books_ibfk_1` FOREIGN KEY (`DocId`) REFERENCES `document`
(`DocId`)
ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Document Table
CREATE TABLE `document` (
`DocId` int(11) NOT NULL,
`Title` varchar(50) NOT NULL,
`PublishDate` date DEFAULT NULL,
`AuthorId` varchar(10) DEFAULT NULL,
`DocType` varchar(10) DEFAULT NULL,
PRIMARY KEY (`DocId`),
KEY `AuthorId` (`AuthorId`),
CONSTRAINT `document_ibfk_3` FOREIGN KEY (`AuthorId`) REFERENCES `author`
(`AuthorId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I made a server for my PHP website on XAMPP and am using mysql but when I try inserting the values in Document table I get
error while inserting the recordsCannot add or update a child row: a foreign
key constraint fails (`librarydb`.`document`, CONSTRAINT `document_ibfk_3`
FOREIGN KEY (`AuthorId`) REFERENCES `author` (`AuthorId`))
How do I resolve this issue?
Your linked fields author.AuthorId and document.AuthorId are not defined the same way.
From the docs:
Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same.
So make them both varchar(10) NOT NULL - or whatever you need.
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" );
i'm trying to make an sql create table but I fail to make a foreign key.
My code :
Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'sondage`(
`id` int(6) NOT NULL AUTO_INCREMENT,
`question` varchar(60) NOT NULL,
PRIMARY KEY(`id`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8')
|| !Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'sondage_reponse`(
`id` int(6) NOT NULL AUTO_INCREMENT,
`champ1` int(4) NOT NULL,
`champ2` int(4) NOT NULL,
`champ3` int(4) NOT NULL,
PRIMARY KEY(`id`)) ENGINE='._MYSQL_ENGINE_.' default CHARSET=utf8')
|| !Db::getInstance()->execute('
INSERT INTO '._DB_PREFIX_.'sondage_reponse (champ1,champ2,champ3)
VALUES (0,0,0)')
I want to make id from "sondage" foreign key in "sondage_reponse".I've tryed :
ALTER TABLE ps_sondage_reponse
ADD FOREIGN KEY fk_id_sdg(id)
REFERENCES sondage(id)')
But Prestashop can't add the foreign key.
Thx
i'd say to try this :
ALTER TABLE ps_sondage_reponse
ADD CONSTRAINT FK_id_sdg
FOREIGN KEY (id) REFERENCES sondage(id)
the berita_ukm table
CREATE TABLE `berita_ukm` (
`id_berita` int(11) NOT NULL AUTO_INCREMENT,
`id_admin` int(11) DEFAULT NULL,
`judul_berita` varchar(45) DEFAULT NULL,
`content` varchar(225) DEFAULT NULL,
`tanggal` date DEFAULT NULL,
PRIMARY KEY (`id_berita`),
FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`)
)
admin table
CREATE TABLE `berita_ukm` (
`id_admin` int(11) NOT NULL AUTO_INCREMENT,
`name` int(11) DEFAULT NULL,
PRIMARY KEY (`id_admin`),
)
and i found error like this
Error Number: 1452
Cannot add or update a child row: a foreign key constraint fails
(`tugas_akhir`.`berita_ukm`, CONSTRAINT `berita_ukm_ibfk_1`
FOREIGN KEY (`id_admin`) REFERENCES `admin` (`id_admin`))
INSERT INTO `berita_ukm`
(`id_berita`, `tanggal`, `judul_berita`, `content`)
VALUES ('34', '3/25/2014', 'putri', 'nfdn')
please help me what to do. thank you
I guess you misplaced the col names in your insert("id_berita" in place of "id_admin" ), since col "id_berita" is auto increment, you don't need to provide value for that col during insert.
INSERT INTO `berita_ukm`
(`id_admin`, `tanggal`, `judul_berita`, `content`)
VALUES ('34', '3/25/2014', 'putri', 'nfdn')
I have a script which takes a database and generate export like query for the database as a backup. I need this script for a purpose. My php code is:
mysql_query("SHOW CREATE TABLE `myTable`");
there are 17 tables in the database. I am running a for loop for all 17 tables. I get the create table query with foreign key which I do not want. It returns like-
CREATE TABLE `customers` (
`person_id` int(10) NOT NULL,
`account_number` varchar(255) DEFAULT NULL,
`taxable` int(1) NOT NULL DEFAULT '1',
`deleted` int(1) NOT NULL DEFAULT '0',
UNIQUE KEY `account_number` (`account_number`),
KEY `person_id` (`person_id`),
CONSTRAINT `customers_ibfk_1` FOREIGN KEY (`person_id`) REFERENCES `people` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
because of the foreign key, it gets an error while running queries for creating tables. is there any way to get the sql without foreign key? like-
CREATE TABLE IF NOT EXISTS `customers` (
`person_id` int(10) NOT NULL,
`account_number` varchar(255) DEFAULT NULL,
`taxable` int(1) NOT NULL DEFAULT '1',
`deleted` int(1) NOT NULL DEFAULT '0',
UNIQUE KEY `account_number` (`account_number`),
KEY `person_id` (`person_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Disable foreign keys during the creation of your tables.
SET FOREIGN_KEY_CHECKS=0
CREATE TABLE a
CREATE TABLE b
...
Enable keys again
SET FOREIGN_KEY_CHECKS=1
You could disable foreign keys checks as suggested elsewhere. If you really want to remove the constraints from the CREATE TABLE statements the following code could be used, it assumes that $createSql will contain the CREATE TABLE SQL statement:
$createSql = preg_replace('/CONSTRAINT[^\n]+/im', '', $createSql);
$createSql = preg_replace('/,\s+\)/im', "\n)", $createSql);