这是我发生的事情的一个例子:
CREATE TABLE Parent (id BIGINT NOT NULL,
PRIMARY KEY (id)) ENGINE=InnoDB;
CREATE TABLE Child (id BIGINT NOT NULL,
parentid BIGINT NOT NULL,
PRIMARY KEY (id),
KEY (parentid),
CONSTRAINT fk_parent FOREIGN KEY (parentid) REFERENCES Parent (id) ON DELETE CASCADE) ENGINE=InnoDB;
CREATE TABLE Uncle (id BIGINT NOT NULL,
parentid BIGINT NOT NULL,
childid BIGINT NOT NULL,
PRIMARY KEY (id),
KEY (parentid),
KEY (childid),
CONSTRAINT fk_parent_u FOREIGN KEY (parentid) REFERENCES Parent (id) ON DELETE CASCADE,
CONSTRAINT fk_child FOREIGN KEY (childid) REFERENCES Child …Run Code Online (Sandbox Code Playgroud) CREATE TABLE `categories` (
`idcategories` INT NOT NULL AUTO_INCREMENT ,
`idparent` INT NULL ,
`description` VARCHAR(45) NULL ,
PRIMARY KEY (`idcategories`) );
ALTER TABLE `categories`
ADD CONSTRAINT `FK_idparent`
FOREIGN KEY (`idparent` )
REFERENCES `ilmercatinodelpulcino`.`categories` (`idcategories` )
ON DELETE CASCADE
ON UPDATE CASCADE
, ADD INDEX `FK_idparent` (`idparent` ASC) ;
INSERT INTO `categories` (`idcategories`, `description`)
VALUES (1, 'cat1');
INSERT INTO `categories` (`idcategories`, `idparent`, `description`)
VALUES (2, 1, 'cat1_child');
Run Code Online (Sandbox Code Playgroud)
因此,此表表示一个类别,具有ID和自我指向的父ID.我插入了一个类别cat1和一个子类别cat1_child,其父ID为cat1.
现在,我希望能够将cat1的idcategory从1更改为10,因为我在更新CASCADE上设置了外键,我希望cat1_child的idparent也将设置为10.但当我这样做时:
UPDATE `categories` SET `idcategories`=10 WHERE `idcategories`='1';
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
错误1451:无法删除或更新父行:外键约束失败(
categories,CONSTRAINTFK_idparent …
我有代码1451的mysql错误.
无法删除或更新父行:外键约束失败(
online_store_admin.osa_admin_logs,CONSTRAINTfk_admins_logsFOREIGN KEY(aid)REFERENCESosa_admins(aid))
这里是sql语句:
drop table if exists osa_admins; create table if not exists osa_admins( aid int unsigned not null auto_increment, uid varchar(50) not null, pass char(41) not null, erp_id int unsigned not null, last_login int unsigned not null, is_block tinyint unsigned not null, menus varchar(50) not null, is_login tinyint unsigned not null, ip_login char(15) not null, constraint idx_osa_admins primary key using btree(aid) ); insert into osa_admins value …
亲爱的,我在MySQL中遇到问题:我无法DELETE FROM users where user_id ='1';在MySQL CLI中执行.所以我试着在phpMyAdmin中:使用GUI删除一行,我得到了这个:
SQL查询:
DELETE FROM `health_portal`.`users` WHERE `users`.`user_id` =1
Run Code Online (Sandbox Code Playgroud)
MySQL说:文档
Cannot delete or update a parent row: a foreign key constraint fails (`health_portal`.`users`, CONSTRAINT `users_ibfk_2` FOREIGN KEY (`doctor_id`) REFERENCES `users` (`user_id`))
Run Code Online (Sandbox Code Playgroud)
我在Mysql网站上查找了这个错误并得到了:错误:1451 SQLSTATE:23000(ER_ROW_IS_REFERENCED_2)
消息:无法删除或更新父行:外键约束失败(%s)
我不知道这里有什么问题,希望有人能给我一个提醒.
很多thx!