2016-03-23 18 views
0

SportId = 1;MySQL DELETEが機能しない

上記を実行すると、このエラーが発生します。私を助けてください;

DogSportはuse.Errorである:(、dogsportorders CONSTRAINT orders_ibfk_1 FOREIGN KEY(EqIdequipmentEqId)を参照する。)外部キー制約が失敗します= 1 SportIdは、親行を削除または更新できないスポーツ。DELETE FROM

/* Creating Sport table */ 
CREATE TABLE Sport(
SportId INT NOT NULL AUTO_INCREMENT, 
SportName VARCHAR(20) NOT NULL, 
Description VARCHAR(200) NOT NULL, 
PRIMARY KEY(SportId) 
); 

/* Inserting values for Sport table */ 
INSERT INTO Sport(SportName,Description) 
VALUES('Agility','This is a fun dog sport which a handler directs a dog through a course of obstacles. Body motion, speed, hand signals are used by the handler to direct the dog.'), 
('Flyball','Energetic and exciting sport which is also called drag racing for dogs'), 
('Disc Dog','This sport is also known as Frisbee dog. The sport involves the bond between the owner and dog, by letting them to work together'), 
('Dog Diving','This sport involves loving run, jumping, and swimming. This is a very popular sport nowadays'), 
('Cani-Cross','This is a sport event which running off-road with dogs. It is an useful sport which keeps the owner and the dog fit by a physical workout and directional commands.'); 

/* Creating UserSport table */ 
CREATE TABLE UserSport(
UserId INT NOT NULL, 
SportId INT NOT NULL, 
PRIMARY KEY(UserId,SportId), 
FOREIGN KEY(UserId) REFERENCES User(UserId) ON DELETE CASCADE, 
FOREIGN KEY(SportId) REFERENCES Sport(SportId) ON DELETE CASCADE 
); 

/* Inserting values for UserSport table */ 
INSERT INTO UserSport(UserId,SportId) 
VALUES(1,2), 
(1,4), 
(1,3), 
(1,5); 

/* Creating Equipment table */ 
CREATE TABLE Equipment(
EqId INT NOT NULL AUTO_INCREMENT, 
EqName VARCHAR(40) NOT NULL, 
Price DECIMAL(9,2) NOT NULL, 
SportId INT NOT NULL, 
PRIMARY KEY(EqId), 
FOREIGN KEY(SportId) REFERENCES Sport(SportId) ON DELETE CASCADE 
); 

/* Inserting values for Equipment table */ 
INSERT INTO Equipment(EqName,Price,SportId) 
VALUES('Wing Jump',6700,1), 
('Tire',4500,1), 
('Open Tunnel',9000,1), 
('Chute',3000,1), 
('Boomerang Flyball Box',6300,2), 
('Training Box',4200,2), 
('Flyball Jumps',7800,2), 
('Can Am Flyball Box',3100,2), 
('Flyball Harness',1800,2), 
('Balance Disc',2000,3), 
('Training Punches',1000,3), 
('Retrieve Water Ring',4500,4), 
('Dive Jacket',3000,4), 
('Aqua Floating Toy',1500,4), 
('Cani-Cross Harness',4000,5), 
('PawZ Dog Boots',1000,5), 
('Custom Shoulder Harness',3000,5); 


/* Creating User table */ 
CREATE TABLE User(
UserId INT NOT NULL AUTO_INCREMENT, 
Title VARCHAR(5) NOT NULL, 
FName VARCHAR(20) NOT NULL, 
LName VARCHAR(20) NOT NULL, 
Email VARCHAR(30) NOT NULL UNIQUE, 
Phone VARCHAR(15), 
Address VARCHAR(30) NOT NULL, 
Password VARCHAR(20) NOT NULL UNIQUE, 
PRIMARY KEY(UserId) 
); 

/* Inserting values for User table */ 
INSERT INTO User(Title,FName,LName,Email,Phone,Address,Password) 
VALUES('Mr.','Raveen','Chandra','[email protected]','0778547079','Ambalangoda','123'), 
('Miss','Bisma','Ishani','[email protected]','0785234123','Colombo','456'), 
('Dr','Shantha','Kumara','[email protected]','0912374392','Ambalangoda','789'), 
('Ms.','Madusha','Perera','[email protected]','0782361490','Galle','3235'); 
+0

機器の子テーブルであるテーブルがありません.... –

答えて

0

少なくとも1つの機器の子テーブルが存在する必要があります。

まず、機器のあなたのすべての子テーブルを提供するコマンドの下に実行卓上

select * from information_schema.KEY_COLUMN_USAGE where TABLE_SCHEMA ='your_db' and REFERENCED_TABLE_NAME ='equipment'; 

注:お使いのデータベース名と変更your_db。 「削除カスケードに何の行動を」

その後のいずれかにその子テーブルから行を削除していないか、また、「削除カスケード」がなければならない代わりに

はきれいway-

ではありませんも短い方法があります

あなたは外部キーチェックを無効にして削除することができますが、私はそれを提案しません。

set foreign_key_checks=0; 
DELETE FROM Sport WHERE SportId=1; 
set foreign_key_checks=1; 
2

まず、この表にFKを持っている他のテーブルの上に削除し、だけにして、それから、削除:

DELETE FROM Equipment WHERE SportId=1; 
DELETE FROM UserSport WHERE SportId=1; 
DELETE FROM Sport WHERE SportId=1; 
+0

サー、あなたは正しいです。しかし、私はDELETE CASCADEを使用しました。なぜそれは働かないのですか? – cveee