您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Mysq中l建立測試父表、子表及測試用例的示例的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
select version(); 5.7.22
drop table if exists Models; CREATE TABLE Models ( ModelID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, Name VARCHAR(40) NOT NULL, PRIMARY KEY (ModelID) );
drop table if exists Orders; CREATE TABLE Orders ( ID SMALLINT UNSIGNED NOT NULL PRIMARY KEY, ModelID SMALLINT UNSIGNED NOT NULL, Description VARCHAR(40), FOREIGN KEY (ModelID) REFERENCES Models (ModelID) ON DELETE cascade );
insert into Orders(Id,ModelID,Description) values (1,1,'a');
結果:執行失敗
異常:[2018-07-31 11:08:01] 23000 Cannot add or update a child row: a foreign key constraint fails (bov
.Orders
, CONSTRAINT Orders_ibfk_1
FOREIGN KEY (ModelID
) REFERENCES Models
(ModelID
) ON DELETE CASCADE)
原因:通不過on delete cascade 的外鍵約束檢查
insert into Models(ModelID,Name) values (1,'a'); insert into Orders(Id,ModelID,Description) values (1,1,'a');
結果:執行成功
select * from Models; 1 a select * from Orders; 1 1 a
delete from Orders where id = 1;
結果:執行成功
select * from Models; 1 a select * from Orders; 為空
delete from Models where ModelID = 1;
結果:執行成功
select * from Models; 為空 select * from Orders; 為空
update Orders set ModelID = 3 where ID =1;
結果:執行失敗
異常:[2018-07-31 12:33:02] 23000 Cannot add or update a child row: a foreign key constraint fails (bov
.Orders
, CONSTRAINT Orders_ibfk_1
FOREIGN KEY (ModelID
) REFERENCES Models
(ModelID
) ON DELETE CASCADE)
原因:通不過on delete cascade 的外鍵約束檢查
update Models set ModelID = 2 where ModelID =1;
結果:執行失敗
異常:[2018-07-31 12:34:24] 23000 Cannot delete or update a parent row: a foreign key constraint fails (bov
.Orders
, CONSTRAINT Orders_ibfk_1
FOREIGN KEY (ModelID
) REFERENCES Models
(ModelID
) ON DELETE CASCADE)
原因:通不過on delete cascade 的外鍵約束檢查
update Orders set Description = 'b' where ID =1;
結果:執行成功
select * from Orders; 1 1 b
update Models set Name = 'c' where ModelID =1;
結果:執行成功
select * from Models; 1 c
感謝各位的閱讀!關于“Mysq中l建立測試父表、子表及測試用例的示例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。