明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

Mysq中l(wèi)創(chuàng)建測試父表、子表及測試用例歸納總結(jié)

[摘要]建立測試表查看版本信息select version(); 5.7.22創(chuàng)建父表drop table if exists Models; CREATE TABLE Models(ModelID SMA...

建立測試表

查看版本信息

select version();
5.7.22

創(chuàng)建父表

drop table if exists Models;
CREATE TABLE Models
  (
    ModelID SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
   Name VARCHAR(40) NOT NULL,
    PRIMARY KEY (ModelID)
);

創(chuàng)建子表

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
  );

測試

測試用例-無父表相應數(shù)據(jù),先插入子表

insert into Orders(Id,ModelID,Description) values (1,1,'a');

結(jié)果:執(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 的外鍵約束檢查

測試用例-先插入主表數(shù)據(jù),再插入子表數(shù)據(jù)

insert into Models(ModelID,Name) values (1,'a');
insert into Orders(Id,ModelID,Description) values (1,1,'a');

結(jié)果:執(zhí)行成功

select * from Models;
1    a

select * from Orders;
1    1    a

測試用例-父子表都有數(shù)據(jù),刪除子表數(shù)據(jù)

delete from Orders where id = 1;

結(jié)果:執(zhí)行成功

select * from Models;
1    a
select * from Orders;
為空

測試用例-父子表都有數(shù)據(jù),刪除父表書庫

delete from Models where ModelID = 1;

結(jié)果:執(zhí)行成功

select * from Models;
為空
select * from Orders;
為空

測試用例-父子表都有數(shù)據(jù),更新子表外鍵

update Orders set ModelID = 3 where ID =1;

結(jié)果:執(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 的外鍵約束檢查

測試用例-父子表都有數(shù)據(jù),更新父表主鍵

update Models set ModelID = 2 where ModelID =1;

結(jié)果:執(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 的外鍵約束檢查

測試用例-父子表都有數(shù)據(jù),更新子表非外鍵

update Orders set Description = 'b' where ID =1;

結(jié)果:執(zhí)行成功

select * from Orders;
1    1    b

測試用例-父子表都有數(shù)據(jù),更新父表非主鍵

update Models set Name = 'c' where ModelID =1;

結(jié)果:執(zhí)行成功

select * from Models;
1    c

相關文章:

mysql數(shù)據(jù)庫索引的建立以及性能測試

mysql 高性能壓力測試(總結(jié)了好久

相關視頻:

數(shù)據(jù)結(jié)構(gòu)探險之線性表篇

以上就是Mysq中l(wèi)建立測試父表、子表及測試用例歸納總結(jié)的詳細內(nèi)容,更多請關注php中文網(wǎng)其它相關文章!


學習教程快速掌握從入門到精通的SQL知識。