无法删除或更新父行:外键约束失败错误

Cannot delete or update a parent row: a foreign key constraint fails Error

我正在尝试使用以下命令删除 table 个应用程序用户:

drop table appusers;

我收到以下错误:

Cannot delete or update a parent row: a foreign key constraint fails

这是我 table 的方案。

CREATE TABLE appUsers (
  uid INT PRIMARY KEY AUTO_INCREMENT,
  fullName VARCHAR(80) NOT NULL,
  email VARCHAR(100) NOT NULL,
  password VARCHAR(100) NOT NULL,
  Gender VARCHAR(7) NOT NULL,  /*["Male", "Female"]*/
  Country VARCHAR(150) NOT NULL,
  Bdate date NOT NULL, 
  Status VARCHAR(25) NOT NULL, /*["Single", "In Relationship", "Merried", "Divorced", "Widow"]*/  /*check married!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  Religion VARCHAR(25) NOT NULL, /*["Jewish", "Christian", "Muslim", "Atheist", "Other"]*/
  ReligionStrength INT(1) NOT NULL, /*0-5*/
  PoliticalView VARCHAR(25) NOT NULL, /*["Left Wing","Center","Right Wing"]*/
  Occupation VARCHAR(25) NOT NULL, /*["Unemployed","White Collar","Blue Collar","Student", "Independent"]*/
  Volunteering VARCHAR(25) NOT NULL,  /*["Yes", "No"]*/
  Donating VARCHAR(25) NOT NULL,  /*["Yes", "No"]*/
  Economy VARCHAR(25) NOT NULL, /*["Poor","Middle Class","Rich"]*/
  EducationalYears INT(2) NOT NULL
);

我有以下 table

CREATE TABLE Accelerometer(
  id INT PRIMARY KEY AUTO_INCREMENT,
  uid INT NOT NULL,
  sampleTime timestamp(2) NOT NULL,
  data VARCHAR(100) NOT NULL,
  FOREIGN KEY (uid) REFERENCES appUsers(uid) ON DELETE CASCADE 
);  

据我所知,如果我删除 table appusers,Accelerometer table 也应该被删除, 我在这里缺少什么?

ON DELETE CASCADE仅适用于数据,不适用于元数据。

=>

 Alter TABLE Accelerometer drop FOREIGN KEY (uid);

那么只有

drop table appusers;

我意识到这已经过时了一段时间并且已经选择了一个答案,但是允许外键为 NULL 然后选择 ON DELETE SET NULL.[=15= 的替代方案怎么样? ]

以下可能适合您:

ALTER TABLE 'Accelerometer' 
DROP FOREIGN KEY 'uid_fk';

ALTER TABLE 'Accelerometer'
ADD CONSTRAINT 'uid_fk' FOREIGN KEY ('uid') REFERENCES 'appUsers' ('uid') ON 
UPDATE CASCADE ON DELETE SET NULL;`

我个人建议同时使用 ON UPDATE CASCADEON DELETE SET NULL 以避免不必要的并发症,但是您的设置可能会决定采用不同的方法。

希望这对您有所帮助。