foreign key constraint fails when drop table from database - mysql

I've created 3 tables using the query bellow. But when I try to drop the LOANACCOUNT table I receive an error:
Error:
Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails
Create tables queries:
CREATE TABLE LOANACCOUNT
(
ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
LOANACCOUNTTYPE VARCHAR(9) NOT NULL,
CREATIONDATE DATE NOT NULL,
CONSTRAINT LOAN_ACCOUNT_PK PRIMARY KEY (ID),
);
CREATE TABLE TRANSACTIONS
(
ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
ACCOUNTID INT UNSIGNED NOT NULL,
TRANSACTIONTYPE VARCHAR(12) NOT NULL,
CONSTRAINT TRANSACTION_PK PRIMARY KEY (ID),
FOREIGN KEY LOANACCOUNT_FK (ACCOUNTID) REFERENCES LOANACCOUNT (ID) ON DELETE CASCADE
);
CREATE TABLE INSTALLMENT
(
ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
ACCOUNTID INT UNSIGNED NOT NULL,
DUEDATE DATE NOT NULL,
CONSTRAINT INSTALLMENT_PK PRIMARY KEY (ID),
FOREIGN KEY LOANACCOUNT_FK (ACCOUNTID) REFERENCES LOANACCOUNT (ID) ON DELETE CASCADE
);
Drop table query:
DROP TABLE IF EXISTS LOANACCOUNT;
I know that there is something wrong with my foreign keys, but I don't know how to fix it.

As #Rigg Suggested need to drop other table before dropping LOANACCOUNT.
(i.e.) Parent table can't be drop unless there is no child linked.
For time being you can disable foreign key check and then drop those tables.
SET SESSION foreign_key_checks = 0;
DROP TABLE IF EXISTS LOANACCOUNT;
SET SESSION foreign_key_checks = 1;

Related

mysql 1215 cannot add foreign key constraint

I have a SQL script that yields me the error:
DROP TABLE IF EXISTS test_db.users
;
CREATE TABLE users
(
id SERIAL,
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS test_db.comments
;
CREATE TABLE comments
(
id SERIAL,
content varchar(255) NOT NULL,
userId BIGINT(20) NOT NULL,
CONSTRAINT fk_comments_has_user
FOREIGN KEY (userId)
REFERENCES test_db.users(id)
ON DELETE CASCADE,
PRIMARY KEY (id)
);
ERROR 1215 (HY000): Cannot add foreign key constraint
This error is not so specific, and I can't really seem to pinpoint the error by reading other posts regarding similar error.
The datatypes need to be the same bigint is not the same as serial.
Try this
drop table if exists comments;
DROP TABLE IF EXISTS temp;
CREATE TABLE temp
(
id bigint auto_increment,
username VARCHAR(20) NOT NULL,
password VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
DROP TABLE IF EXISTS comments
;
CREATE TABLE comments
(
id bigint auto_increment,
content varchar(255) NOT NULL,
userId bigint NOT NULL,
CONSTRAINT fk_comments_has_user
FOREIGN KEY (userId)
REFERENCES temp(id)
ON DELETE CASCADE,
PRIMARY KEY (id)
);

I can't add new row , a foreign key constraint fails in Mysql

i have created two tables User and Caisse with the following code :
CREATE TABLE IF NOT EXISTS `GGC`.`User` (
`IdUser` INT NOT NULL,
`LibUser` VARCHAR(50) NULL,
PRIMARY KEY (`IdUser`))
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `GGC`.`Caisse` (
`IdCais` INT NOT NULL AUTO_INCREMENT,
`LibCais` VARCHAR(50) NULL,
`RefCais` VARCHAR(50) NULL,
`MontantCais` FLOAT NULL,
`IdUser` INT NOT NULL,
PRIMARY KEY (`IdCais`),
INDEX `fk_Caisse_User1_idx` (`IdUser` ASC),
CONSTRAINT `fk_Caisse_User1`
FOREIGN KEY (`IdUser`)
REFERENCES `GGC`.`User` (`IdUser`))
ENGINE = InnoDB;
but the probleme when i try to add now row i keep getting this error message:
ERROR : #1452 - Cannot add or update a child row: a foreign key constraint fails (GGC.caisse, CONSTRAINT fk_Caisse_User1 FOREIGN KEY (IdUser) REFERENCES User (IdUser))
NOTE : Although i have some data in table user
add new row in table Caisse
the problem is solved , i change the type of table to InnoDB , and it work .

Confusing cannot add foreign key constraint error

Ok, maybe it's late and I'm being stupid, but I can't seem to figure out why I'm getting a Cannot add Foreign Key Constraint error for the following query
DROP TABLE IF EXISTS People_Lordships;
CREATE TABLE People_Lordships
(
Id INT PRIMARY KEY AUTO_INCREMENT,
PersonId INT NOT NULL,
LordshipId INT NOT NULL,
AssumedDate Date,
AbdicatedDate Date
);
DROP TABLE IF EXISTS People_Lordships_Current;
CREATE TABLE People_Lordships_Current
(
Id INT PRIMARY KEY AUTO_INCREMENT,
People_LordshipsId INT NOT NULL,
LordShipId INT NOT NULL,
CONSTRAINT Fk_People_Lordships_Current_People_LordshipsId_LordshipId
FOREIGN KEY (`LordshipId`,`People_LordshipsId`)
REFERENCES People_Lordships (`LordshipId`,`Id`)
ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT UQ_People_Lordships_Current_LordshipId
UNIQUE KEY (`LordshipId`)
);
And yes, it is a database about noble titles... it's a long story
There is no column LordshipId in table People_Lordships.
Your foreign key definition attempts to reference a column that doesn't exist.
REFERENCES People_Lordships (`LordshipId`,`Id`)
^^^^^^^^^^^^
Figured this one out.
It turns out MySQL cannot add a foreign key constraint against a column that is not the first column in an index.
The following will work
DROP TABLE IF EXISTS People_Lordships;
CREATE TABLE People_Lordships
(
Id INT PRIMARY KEY AUTO_INCREMENT,
PersonId INT NOT NULL,
LordshipId INT NOT NULL,
AssumedDate Date,
AbdicatedDate Date,
INDEX Idx_LordshipId (LordshipId)
);
DROP TABLE IF EXISTS People_Lordships_Current;
CREATE TABLE People_Lordships_Current
(
Id INT PRIMARY KEY AUTO_INCREMENT,
People_LordshipsId INT NOT NULL,
LordShipId INT NOT NULL,
CONSTRAINT Fk_People_Lordships_Current_People_LordshipsId_LordshipId
FOREIGN KEY (`LordshipId`,`People_LordshipsId`)
REFERENCES People_Lordships (`LordshipId`,`Id`)
ON DELETE RESTRICT ON UPDATE CASCADE,
CONSTRAINT UQ_People_Lordships_Current_LordshipId
UNIQUE KEY (`LordshipId`)
);

Foreign key constraint fails on drop table even if "ON DELETE SET NULL" is set

I have a table
create table if not exists Emp(
PId int not null primary key auto_increment,
DId int,
Name varchar(30) not null,
LastName varchar(30) not null)
and
create table if not exists Wages(
ZId int not null primary key auto_increment,
PId int,
amount int not null,
FOREIGN KEY (PId) REFERENCES Emp(PId) ON DELETE SET NULL ON UPDATE SET NULL)
and when i try to drop table Emp i got: "Cannot delete or update a parent row: a foreign key constraint fails".
What is wrong with this code? Why ON DELETE does not seem to work properly?

Can't figure out what's wrong with foreign key constraint statement

This is the error message from show engine innodb status; when I try to create the table:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110628 16:56:07 Error in foreign key constraint of table test/menu_items:
foreign key(id_menu)
references menus(id)
on update cascade
on delete cascade,
foreign key(id_item)
references items(id)
on update cascade
on delete cascade,
primary key(id_menu, id_item)
) engine=InnoDB:
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.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
These are the relevant SQL statements:
create table if not exists menus (
id mediumint unsigned not null auto_increment,
id_restaurant mediumint unsigned not null,
name varchar(50) not null,
description varchar(255) default null,
foreign key(id_restaurant)
references restaurants(id)
on update cascade
on delete cascade,
primary key(id)
) engine=InnoDB;
create table if not exists items (
id mediumint unsigned not null auto_increment,
id_restaurant mediumint unsigned not null,
name varchar(50),
description text,
type enum('appetizer','salad','soup','entree','dessert','drink','other'),
price decimal(4,2),
foreign key(id_restaurant)
references restaurants(id)
on update cascade
on delete cascade,
primary key(id)
) engine=InnoDB;
create table if not exists order_items (
id_order bigint unsigned not null,
id_item mediumint unsigned not null,
item_name varchar(50),
item_description text,
item_price decimal(4,2),
notes varchar(1024),
quantity smallint unsigned,
foreign key(id_order)
references orders(id)
on update cascade
on delete cascade,
foreign key(id_item)
references items(id)
on update cascade
on delete cascade,
primary key(id_order, id_item)
) engine=InnoDB;
Removing menu_items.id_menu and corresponding foreign key / primary key allows the SQL statements to be parsed properly.
Why can't I make a foreign key reference to menus(id) from menu_items?
You've got a mismatch of datatypes:
create table order_items (
id_order bigint unsigned not null, -- BIGINT
create table items (
id mediumint unsigned not null auto_increment, -- MEDIUMINT
but order_items.id_order references items.id, so they should be the same.
Try changing order_items id_order to medium int:
order_items (
id_order mediumint unsigned not null,