Error #1064 near "REFERENCES" while adding a foreign key - mysql

I keep getting error 1064 while trying to create a foreign key:
ALTER TABLE `zajimavost`
ADD CONSTRAINT FK_zaj_bod
FOREIGN KEY def_bod
REFERENCES `bod`(`gid`)
ON UPDATE CASCADE ON DELETE CASCADE;
It complains:
#1064 - 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 'REFERENCES `bod`(`gid`)
ON UPDATE CASCADE ON DELETE CASCADE' at line 4
I tried it with or without the backticks, the problem is always the same.
My tables:
CREATE TABLE `zajimavost` (
`zaj_id` int(11) NOT NULL,
`lok_id` int(11) NOT NULL,
`nazev` varchar(255) COLLATE utf8_czech_ci NOT NULL,
`kategorie` int(11) NOT NULL DEFAULT '1',
`datace_od` int(11) NOT NULL DEFAULT '31',
`datace_do` int(11) NOT NULL DEFAULT '60',
`def_bod` int(11) DEFAULT NULL,
PRIMARY KEY (`zaj_id`),
KEY `ck_zaj_lok` (`lok_id`),
KEY `zaj_id` (`zaj_id`),
KEY `FK_zaj_kat` (`kategorie`),
KEY `FK_zaj_bod` (`def_bod`),
CONSTRAINT `FK_zaj_kat` FOREIGN KEY (`kategorie`) REFERENCES `kategorie` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_zaj_lok` FOREIGN KEY (`lok_id`) REFERENCES `lokalita` (`lok_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
CREATE TABLE `bod` (
`gid` int(11) NOT NULL AUTO_INCREMENT,
`geom` point DEFAULT NULL,
`radius` int(4) NOT NULL DEFAULT '50',
PRIMARY KEY (`gid`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci
There are data in the tables.
So how to solve it?

You need to use () in child table column name while adding foreign key, Try this one
ALTER TABLE zajimavost
ADD CONSTRAINT FK_zaj_bod
FOREIGN KEY (def_bod) REFERENCES `bod`(`gid`)
ON UPDATE CASCADE ON DELETE CASCADE;

Related

MySQL throwing duplicate entry on update

I'm trying to update a non-key field on my table, but I keep getting duplicate primary key errors. For what I searched, it's an actual MySQL bug, but I'd like to know if there is a way around it, since so far I canĀ“t update my table at all.
CREATE TABLE `AREA_FOLDER_PERMISSION` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`FOLDER_ID` int(11) NOT NULL,
`COMPANY_AREA_ID` int(11) NOT NULL,
`CAN_READ` bit(1) NOT NULL,
`CAN_WRITE` bit(1) NOT NULL,
`CAN_PRINT` bit(1) NOT NULL,
`CAN_APPROVE` bit(1) NOT NULL,
`CAN_REVIEW` bit(1) NOT NULL,
`CAN_CREATE` bit(1) NOT NULL,
PRIMARY KEY (`ID`),
UNIQUE KEY `id_UNIQUE` (`ID`),
KEY `FOLDER_ID_idx` (`FOLDER_ID`),
KEY `COMPANY_AREA_ID_idx` (`COMPANY_AREA_ID`),
CONSTRAINT `COMPANY_AREA_ID` FOREIGN KEY (`COMPANY_AREA_ID`) REFERENCES `COMPANY_AREA` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK2g92hsfhocnfsdr57fp7065mv` FOREIGN KEY (`COMPANY_AREA_ID`) REFERENCES `COMPANY_AREA` (`ID`),
CONSTRAINT `FKhisnf7elm60on0mjytiteio2a` FOREIGN KEY (`FOLDER_ID`) REFERENCES `FOLDER` (`ID`),
CONSTRAINT `FOLDER_ID` FOREIGN KEY (`FOLDER_ID`) REFERENCES `FOLDER` (`ID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1124828 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
And the error is:
Operation failed: There was an error while applying the SQL script to the database.
Executing:
UPDATE `sgidb`.`AREA_FOLDER_PERMISSION` SET `CAN_PRINT` = '1' WHERE (`ID` = '791063');
ERROR 1062: 1062: Duplicate entry '791063' for key 'id_UNIQUE'
SQL Statement:
UPDATE `sgidb`.`AREA_FOLDER_PERMISSION` SET `CAN_PRINT` = '1' WHERE (`ID` = '791063')

Mysql foreign key

When trying to create a table and alter it with the below query:
CREATE TABLE `sms_codes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`code` varchar(6) NOT NULL,
`status` int(1) NOT NULL DEFAULT '0',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
)
ALTER TABLE `sms_codes`
ADD CONSTRAINT `sms_codes_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
I got the error:
#1064 - 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 'ALTER TABLE `sms_codes` ADD CONSTRAINT `sms_codes_ibfk_1` FOREIGN KEY (`user_' at line 11
Each statement need to separate by the delimiter ;. So add the ; in end of the CREATE TABLE in the same way how you added ; in end of the ALTER TABLE statement:
In this line:
KEY `user_id` (`user_id`)
); <-- here

Error 1215: Cannot add foreign key constraint SQL Statement

Not sure why I am still encountering the issue "Error 1215" wherein they have the same data type and parent table is in primary key.
child table:
CREATE TABLE `customer_notice_type` (
`CUSTOMER_NOTICE_TYPE_ID` int(11) NOT NULL AUTO_INCREMENT,
`CUSTOMER_ID` int(11) NOT NULL,
`CUSTOMER_NOTICE_TYPE_NAME` varchar(50) NOT NULL,
`SYSTEM_NOTICE_TYPE_ID` int(11) NOT NULL,
`STATUS` char(1) NOT NULL,
`CREATED_BY` varchar(50) NOT NULL,
`CREATED_DATE` datetime NOT NULL,
`MODIFIED_BY` varchar(50) DEFAULT NULL,
`MODIFIED_DATE` datetime DEFAULT NULL,
PRIMARY KEY (`CUSTOMER_NOTICE_TYPE_ID`),
KEY `fk_customer_id_customer_notice_type_idx` (`CUSTOMER_ID`),
CONSTRAINT `fk_customer_id_customer_notice_type` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer` (`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=494 DEFAULT CHARSET=latin1;
parent table:
CREATE TABLE `system_notice_type` (
`SYSTEM_NOTICE_TYPE_ID` int(11) NOT NULL,
`SYSTEM_NOTICE_TYPE_NAME` varchar(45) NOT NULL,
`LINE_OF_BUSINESS_ID` int(11) NOT NULL,
`STATUS` char(1) NOT NULL,
PRIMARY KEY (`SYSTEM_NOTICE_TYPE_ID`)
) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
SQL script to create Foreign Key:
ALTER TABLE `fexpress`.`customer_notice_type`
ADD CONSTRAINT `fk_system_notice_type_customer_notice_type`
FOREIGN KEY (`SYSTEM_NOTICE_TYPE_ID`)
REFERENCES `fexpress`.`system_notice_type` (`SYSTEM_NOTICE_TYPE_ID`)
ON DELETE CASCADE ON UPDATE CASCADE;
You have two potential problems. First, the alter table statement references fexpress. This may or may not be the correct schema for the table. So, that is one potential problem.
The second real problem is the constraint defined in the child table:
CONSTRAINT `fk_customer_id_customer_notice_type` FOREIGN KEY (`CUSTOMER_ID`) REFERENCES `customer`(`CUSTOMER_ID`) ON DELETE CASCADE ON UPDATE CASCADE
The parent table is not yet defined, so it generates an error.
Removing this row and adjusting the schema name results in working code, as in this SQL Fiddle.

MySQL Error : #1005 - Can't create table (errno: 150) When I try create more than 1 FK

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.

How to update foreign key value in mysql database

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