這篇文章給大家分享的是有關Mysq中l(wèi)建立測試父表、子表及測試用例的示例的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯公司是專業(yè)的新晃網站建設公司,新晃接單;提供成都做網站、網站建設,網頁設計,網站設計,建網站,PHP網站建設等專業(yè)做網站服務;采用PHP框架,可快速的進行新晃網站開發(fā)網頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網站,專業(yè)的做網站團隊,希望更多企業(yè)前來合作!
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');
結果:執(zhí)行失敗
異常:[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');
結果:執(zhí)行成功
select * from Models; 1 a select * from Orders; 1 1 a
delete from Orders where id = 1;
結果:執(zhí)行成功
select * from Models; 1 a select * from Orders; 為空
delete from Models where ModelID = 1;
結果:執(zhí)行成功
select * from Models; 為空 select * from Orders; 為空
update Orders set ModelID = 3 where ID =1;
結果:執(zhí)行失敗
異常:[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;
結果:執(zhí)行失敗
異常:[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;
結果:執(zhí)行成功
select * from Orders; 1 1 b
update Models set Name = 'c' where ModelID =1;
結果:執(zhí)行成功
select * from Models; 1 c
感謝各位的閱讀!關于“Mysq中l(wèi)建立測試父表、子表及測試用例的示例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!