简体   繁体   English

无法截断表mysql

[英]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. 现在我不是制作此表的人,它不是提供给我的,也不是SQL方面的任何专家,实际上不是初学者,但是我需要能够截断数据,因为它是用于测试应用程序的,我具有不再需要的模拟数据。

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. 在对应用程序中的数据进行一次删除时,我注意到的另一个大问题是自动递增的ID无法调整。 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? 即使删除了3个条目的整个表,即使我在表中只有一个条目之后也删除了整个表,IE也不会删除它,但仍显示4的ID,IE是否可以解决此问题?

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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