請看下面的SQL:MYSQL外鍵執法需要
CREATE DATABASE TEST;
USE TEST;
CREATE TABLE PARENT (
PARENT_NAME varchar(255) PRIMARY KEY,
PARENT_DESC varchar(255));
CREATE TABLE CHILD (
PARENT_NAME varchar(255),
CHILD_NAME varchar(255),
CHILD_DESC varchar(255),
PRIMARY KEY (PARENT_NAME, CHILD_NAME),
FOREIGN KEY (PARENT_NAME) REFERENCES PARENT(PARENT_NAME)
ON UPDATE CASCADE
ON DELETE CASCADE);
INSERT INTO PARENT VALUES("PARENT A","Some Parent");
INSERT INTO CHILD VALUES("PARENT A","CHILD A","Some Child");
INSERT INTO CHILD VALUES("PARENT A","CHILD B","Some Child");
INSERT INTO CHILD VALUES("PARENT A","CHILD C","Some Child");
INSERT INTO CHILD VALUES("PARENT B","CHILD D","Non-present Parent");
DELETE FROM PARENT WHERE PARENT_NAME = "PARENT A";
SELECT * FROM CHILD;
此外,
mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+---------------------+
| Variable_name | Value |
+-------------------------+---------------------+
| protocol_version | 10 |
| version | 5.1.41-3ubuntu12.10 |
| version_comment | (Ubuntu) |
| version_compile_machine | i486 |
| version_compile_os | debian-linux-gnu |
+-------------------------+---------------------+
5 rows in set (0.00 sec)
兩件事情正在發生,我認爲會有所不同。
*首先是MySQL允許我將值插入CHILD
中,該值與PARENT
中的任何內容都不匹配。我認爲外鍵約束會阻止這種情況發生。
*第二個是,當PARENT A
從PARENT
中刪除時,MySQL不會刪除的PARENT A
記錄。我認爲級聯聲明會使這種情況發生。
我覺得我失去了一些明顯的東西。有什麼建議麼?
沒錯。而且速度超快。非常感謝! – TeejMonster 2011-05-30 17:55:57