2014-02-06 64 views
0

我創建MySQL中的兩個表,子表沒有被刪除

  1. 客戶
  2. 房子表houseID是在我的客戶表的外鍵。

    Create customer table(
    id int not null primary key auto_increment, 
    name varchar not null, 
    houseId int not null, 
    telephoneNo, int not null, 
    CONSTRAINT FOREIGN KEY (houseId) REFERENCES house(id) ON DELETE CASCADE); 
    
    
    
    CREATE house table(id int not null primary key auto_increment, 
            houseNo int not null, 
            address varchar not null); 
    

然而,當我刪除用戶與特定houseId,在內部表中的行沒有被刪除,雖然我在客戶表放在刪除級聯。任何想法爲什麼?

回答

0

您的外鍵位於錯誤的表格中。你建立的方式是,如果你刪除了一棟房子,相應的修剪者將被級聯。

你會想在客戶表中放置一個customerId外鍵,並且從側面有ON DELETE CASCADE外鍵觸發。

ON DELETE正在詢問外鍵:如果我所引用的外鍵被刪除(級聯,設置爲null,什麼也不做)? 不是該行被刪除時該怎麼做。

+0

那麼我是否需要將customerID作爲外鍵放在內部表中? – Sne

+0

啊,它現在正在工作!謝謝! – Sne

+0

歡迎來到堆棧溢出。請記住將問題標記爲已回答:) – Moak