简体   繁体   中英

Unable to truncate table mysql

I have several tables in my schema that I'm having a problem truncating. My structure is:

country
->PK countryId
->name

city
->PK cityId
->FK countryId
->name

address
->PK addressId
->FK cityId

customer
->PK customerId
->FK addressId

appointment
->PK appointmentId
->FK customerId

Now I am not the one who made this table, it was supplied to me, nor am I any sort of expert in SQL, hardly a beginner actually, but I need to be able to truncate the data as it's for a test application and I have simulated data that I no longer need.

This is the error I am getting:

TRUNCATE TABLE country  Error Code: 1701. Cannot truncate a table referenced in a foreign key constraint (`U05FKQ`.`city`, CONSTRAINT `city_ibfk_1` FOREIGN KEY (`countryId`) REFERENCES `U05FKQ`.`country` (`countryId`))  0.547 sec

I have also attempted to run this:

TRUNCATE TABLE appointment;
TRUNCATE TABLE customer;
TRUNCATE TABLE address;
TRUNCATE TABLE city;
TRUNCATE TABLE country;

Which succeeds on the appointment truncation but fails on customer, and doesn't continue.

Another large issue I have noticed while doing single deletions on data from my application is that the auto incremented id's do not adjust. Nor does deleting an entire table full of data, IE if I delete a full table of 3 entries even though I only have a single entry in the table after addition it still shows me 4 for the ID, is there a fix for this?

I don't have access to the original DB creation script.

A very data unsafe solution is:

SET FOREIGN_KEY_CHECKS=0;

Then, set it back up with:

SET 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM