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
.
(ie) 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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.