JonnySmith JonnySmith - 5 months ago 86
MySQL Question

#1217 - Cannot delete or update a parent row: a foreign key constraint fails in PHPMyAdmin

I'm trying to make a database in

PHPMyAdmin
and I'm getting the error
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
after I've already created the table. The code that I have is

drop table if exists tbl;

create table tbl(
name varchar(20) primary key,
pword char(30) not null,
mail varchar(50) not null
);


And I've tried it in
MySQL Workbench
and that's not giving me any errors at all when it runs

EDIT

My other table that references the
tbl
table is

create table tbl2(
tbl2_id int not null auto_increment,
name varchar(20),
primary key (tbl2_id),
foreign key (name) references tbl(name)
);

Answer

The error means that there is a foreign key from another table that refers to a row of your tbl table. You can't delete a table if another table has a reference to it, it would mean that one of your table has a foreign key that refers to a table which doesn't exist anymore.

This is what foreign key constraint fails mean

Comments