I have a problem with create table statement in MySQL.
The scenario is, I want to have a table P_CDP wchich stores some informations, and the table P_CDPFiles which stores the data of the uploaded files.
The column SelectedCDPFileID by default should be NULL unless the user selects some file. Then that column is filled by the file ID. But, I'm still getting the errno 150, why ? I think the FK names are set properly
CREATE TABLE `P_CDP` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`P_OrderID` int(11) NOT NULL DEFAULT '0',
`SelectedCDPFileID` int(11) NULL,
PRIMARY KEY (`ID`),
KEY `FK_P_CDP_P_Orders` (`P_OrderID`),
KEY `FK_P_CDP_P_CDPFiles` (`SelectedCDPFileID`),
CONSTRAINT `FK_P_CDP_P_Orders` FOREIGN KEY (`P_OrderID`) REFERENCES `P_Orders` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_P_CDP_P_CDPFiles` FOREIGN KEY (`SelectedCDPFileID`) REFERENCES `P_CDPFiles` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `P_CDPFiles` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FileID` int(11) NOT NULL DEFAULT '0',
`P_CDPID` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `FK_P_CDPFiles_File` (`FileID`),
KEY `FK_P_CDPFiles_P_CDP` (`P_CDPID`),
CONSTRAINT `FK_P_CDPFiles_File` FOREIGN KEY (`FileID`) REFERENCES `File` (`FileID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_P_CDPFiles_P_CDP` FOREIGN KEY (`P_CDPID`) REFERENCES `P_CDP` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
You're attempting to reference a table that does not yet exist.
If you must have references in both directions between these two tables (which generally indicates quite poor design), you will have to:
Create the first table without any reference to the as-yet-undefined table:
CREATE TABLE `P_CDP` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`P_OrderID` int(11) NOT NULL DEFAULT '0',
`SelectedCDPFileID` int(11) NULL,
PRIMARY KEY (`ID`),
KEY `FK_P_CDP_P_Orders` (`P_OrderID`),
KEY `FK_P_CDP_P_CDPFiles` (`SelectedCDPFileID`),
CONSTRAINT `FK_P_CDP_P_Orders` FOREIGN KEY (`P_OrderID`)
REFERENCES `P_Orders` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Create the second table, including its reference to the first table:
CREATE TABLE `P_CDPFiles` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FileID` int(11) NOT NULL DEFAULT '0',
`P_CDPID` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`),
KEY `FK_P_CDPFiles_File` (`FileID`),
KEY `FK_P_CDPFiles_P_CDP` (`P_CDPID`),
CONSTRAINT `FK_P_CDPFiles_File` FOREIGN KEY (`FileID`)
REFERENCES `File` (`FileID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_P_CDPFiles_P_CDP` FOREIGN KEY (`P_CDPID`)
REFERENCES `P_CDP` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Alter the first table to add the intended constraint:
ALTER TABLE `P_CDP`
ADD CONSTRAINT `FK_P_CDP_P_CDPFiles` FOREIGN KEY (`SelectedCDPFileID`)
REFERENCES `P_CDPFiles` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION;
Related
I am trying to add a new table to MySQL but I am getting the cannot add foreign key constraint error.
The SQL i am trying to execute is :
CREATE TABLE `local_news` (
`local_news_id` int(11) NOT NULL AUTO_INCREMENT,
`org_id` int(11) NOT NULL,
`org_contact_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL COMMENT 'title',
`message` text DEFAULT NULL,
`published` tinyint(1) NOT NULL DEFAULT 0 COMMENT '0-draft, 1-published',
`create_time` datetime DEFAULT NULL,
PRIMARY KEY (`local_news_id`),
KEY `FK_Reference_56` (`org_id`),
KEY `FK_Reference_57` (`org_contact_id`),
CONSTRAINT `FK_Reference_56` FOREIGN KEY (`org_id`) REFERENCES `organization` (`org_id`),
CONSTRAINT `FK_Reference_57` FOREIGN KEY (`org_contact_id`) REFERENCES `org_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='All local site content in the news tab';
My 'organization' table has 'org_id' as int(11) and set as its primary key.
My 'org_contacts' table has 'contact_id' as int(11) and set as its primary key.
The complete error I found (while using show engine innodb status) is:
------------------------ LATEST FOREIGN KEY ERROR
------------------------ 2017-02-15 22:28:27 0x2b64 Error in foreign key constraint of table giveaday/local_news:
FOREIGN KEY (`org_id`) REFERENCES `organization` (`org_id`),
CONSTRAINT `FK_Reference_57` FOREIGN KEY (`org_contact_id`) REFERENCES `org_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='All local site content in the news tab':
Cannot resolve table name close to:
(`org_id`),
CONSTRAINT `FK_Reference_57` FOREIGN KEY (`org_contact_id`) REFERENCES `org_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='All local site content in the news tab'
However the table "organization" does exist and is exactly called "organization", no typos made.
I'm building a table with a two part foreign key that references a two part key in another table. It isn't working, and I can't see why because the data types line up.
Can't create table 'tsugi.video_comments' (errno: 150)
Here's the code I'm trying to run:
CREATE TABLE `video_comments` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(11) NOT NULL DEFAULT '',
`link_id` int(11) NOT NULL,
`videoTime` int(11) NOT NULL,
`comment` text NOT NULL,
`parent` int(11) unsigned DEFAULT NULL,
`private` tinyint(1) DEFAULT NULL,
`replies` int(11) unsigned DEFAULT '0',
`reports` int(11) DEFAULT '0',
`user_id` int(11) NOT NULL,
`displayname` varchar(2048) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `video_ibfk_1` (`link_id`),
KEY `video_ibfk_2` (`user_id`),
CONSTRAINT `video_comments_ibfk_1` FOREIGN KEY (`parent`) REFERENCES `video_comments` (`id`) ON DELETE CASCADE,
CONSTRAINT `video_ibfk_1` FOREIGN KEY (`link_id`) REFERENCES `lti_link` (`link_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `video_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `lti_user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `video_key` FOREIGN KEY (`link_id`, `video_id`) REFERENCES `video_ids` (`link_id`, `video_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=285 DEFAULT CHARSET=utf8;
The command runs successfully if I delete the last constraint, so that's where the problem is, not in the other constraints.
Here's the table it's referencing:
CREATE TABLE `video_ids` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(11) NOT NULL DEFAULT '',
`link_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `video_key` (`video_id`,`id`),
KEY `link_id` (`link_id`),
KEY `id` (`id`,`video_id`),
CONSTRAINT `video_ids_ibfk_1` FOREIGN KEY (`link_id`) REFERENCES `t_lti_link` (`link_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
To make sure that the video_id and link_id fields are exactly the same, I copied them directly from the existing table's code to the code for creating the new table. I expected that to solve it, but it did not. Here's the error log:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
141114 22:04:09 Error in foreign key constraint of table tsugi/video_comments:
FOREIGN KEY (`link_id`, `video_id`) REFERENCES `video_ids` (`link_id`, `video_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=285 DEFAULT CHARSET=utf8:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Foreign key references need to be to unique or primary keys. So, you can fix this just by having a unique declaration in video_ids:
CREATE TABLE `video_ids` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`video_id` varchar(11) NOT NULL DEFAULT '',
`link_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `video_key` (`video_id`,`id`),
KEY `link_id` (`link_id`),
KEY `id` (`id`,`video_id`),
UNIQUE (link_id, video_id),
CONSTRAINT `video_ids_ibfk_1` FOREIGN KEY (`link_id`) REFERENCES `t_lti_link` (`link_id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Here is a SQL Fiddle with an example.
I have this table:
CREATE TABLE IF NOT EXISTS `produtos` (
`id` int(11) NOT NULL auto_increment,
`idcatprodutos` int(11) NOT NULL,
`idcategoria` int(11) NOT NULL,
`idmarca` int(11) NOT NULL,
`nome` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_produtos_2` (`idcatprodutos`),
KEY `FK_produtos_3` (`idmarca`),
KEY `FK_produtos_4` (`idcategoria`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=39 ;
and this table:
CREATE TABLE IF NOT EXISTS `sugestoes` (
`id` int(11) NOT NULL auto_increment,
`idproduto` int(11) NOT NULL,
`idsugestao1` int(11) NOT NULL,
`idsugestao2` int(11) NOT NULL,
`idsugestao3` int(11) NOT NULL,
`idsugestao4` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK_sugestoes_prod` (`idproduto`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED AUTO_INCREMENT=9 ;
I already have created a fk sugestoes.idproduto -> produtos.id working, but I want each of the other fields also refer to the produtos.id through new FK.
Run this command below that return MySQL Error : #1005 - Can't create table (errno: 150):
ALTER TABLE `infantile`.`sugestoes` ADD CONSTRAINT `FK_sugestoes_2` FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
REFERENCES `produtos` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
, ROW_FORMAT = FIXED;
Does anyone have any idea what's going on?
Try this,
it works:
ALTER TABLE `sugestoes`
ADD CONSTRAINT `FK_idproduto_produtos_1` FOREIGN KEY (`idproduto`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_2` FOREIGN KEY (`idsugestao1`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_3` FOREIGN KEY (`idsugestao2`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_4` FOREIGN KEY (`idsugestao3`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_5` FOREIGN KEY (`idsugestao4`) REFERENCES `produtos` (`id`)
UPDATE:
You can not specify
ON DELETE SET NULL
Because of this:
You have defined a SET NULL condition though some of the
columns are defined as NOT NULL
You can see exact error when you run
SHOW ENGINE INNODB STATUS;
Perhaps removing the ROW_FORMAT = FIXED:
ALTER TABLE `infantile`.`sugestoes`
ADD CONSTRAINT `FK_sugestoes_2`
FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
REFERENCES `produtos` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
;
On second thought, how do you expect the ON DELETE SET NULL to behave when your column is defined with NOT NULL ?
And third, does the table has any data in it? If some of the rows violate this FK constraint, then you can't create that FK.
All I need is to create 2 tabeles with next structure:
The SQL:
CREATE TABLE IF NOT EXISTS `ds_cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `module_news_cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` int(11) NOT NULL,
`cat_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_module_news_cats_module_news_cats` (`parent`),
KEY `fk_module_news_cats_ds_cats1` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
ALTER TABLE `module_news_cats`
ADD CONSTRAINT `fk_module_news_cats_ds_cats1` FOREIGN KEY (`cat_id`) REFERENCES `ds_cats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `fk_module_news_cats_module_news_cats` FOREIGN KEY (`parent`) REFERENCES `module_news_cats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION;
But when I try to insert first row to my table "module_news_cats", I recive next error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`empty`.`module_news_cats`, CONSTRAINT `fk_module_news_cats_module_news_cats` FOREIGN KEY (`parent`) REFERENCES `module_news_cats` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION)
Question:
How I can create table which will have an index with non-identifying relationship to the anther index in the same table? Some rows will have parents, and some not.
I think you just need to allow NULLs in module_news_cats.parent:
CREATE TABLE IF NOT EXISTS `module_news_cats` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent` int(11) NULL, -- Change this
`cat_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_module_news_cats_module_news_cats` (`parent`),
KEY `fk_module_news_cats_ds_cats1` (`cat_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and then if there isn't a parent, create the row with a NULL in parent.
Your 'parent' field cannot be empty (NULL) if you insert a record, which means that every record you insert should refer to a parent ID (which is impossible if there are no entries in your table yet).
If you make the 'parent' field in the module_news_cats table nullable:
ALTER TABLE `module_news_cats` CHANGE `parent` `parent` INT( 11 ) NULL DEFAULT NULL
you should be able to insert records that have no parent ID associated (just supply NULL instead of a value).
You could make the parent column in the module_news_cats table nullable.
Then for rows that have no parents populate the parent column with null.
I have three tables: categories, languages and categories_languages. Categories_languages is many to many table which links together categories and languages. I would like to update a foregin key value in table languages but it throws me error #1451 - Cannot delete or update a parent row: a foreign key constraint fails!
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) unsigned NOT NULL auto_increment,
`name` varchar(20) NOT NULL,
`modified` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `languages` (
`id` char(2) NOT NULL,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `categories_languages` (
`id` int(11) unsigned NOT NULL auto_increment,
`category_id` int(11) unsigned NOT NULL,
`language_id` char(2) NOT NULL,
`translation` varchar(20) NOT NULL,
PRIMARY KEY (`id`),
KEY `fk_category_id_language_id` (`category_id`,`language_id`),
KEY `fk_language_id` (`language_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
ALTER TABLE `categories_languages`
ADD CONSTRAINT `categories_languages_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `categories_languages_ibfk_2` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`) ON DELETE CASCADE;
The error is clear to me, but how can I update a key value in this case? I tried adding ON UPDATA CASCADE:
ALTER TABLE `categories_languages`
ADD CONSTRAINT `categories_languages_ibfk_1` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `categories_languages_ibfk_2` FOREIGN KEY (`language_id`) REFERENCES `languages` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
but that also fails with message: MySQL said: Documentation #1005 - Can't create table './db_dodo/#sql-c2f_80e6f.frm' (errno: 121)
You can temporarily suspend foreign key checking:
SET foreign_key_checks = 0;
UPDATE languages SET id='xyz' WHERE id='abc';
UPDATE categories_languages SET language_id='xyz' WHERE language_id='abc';
SET foreign_key_checks = 1;
EDIT: As for the foreign key problem: is the data stored on a local or a remote file system? errno 121 is EREMOTEIO (Remote I/O error). Perhaps there are permission problems on the target file system or it doesn't support the # character in filenames?
If you are looking for a temp solution you can also change the ON UPDATE action to CASCADE and modify your ids