这篇文章给大家分享的是有关Mysq中l建立测试父表、子表及测试用例的示例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
创新互联公司是专业的新晃网站建设公司,新晃接单;提供成都做网站、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行新晃网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
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建立测试父表、子表及测试用例的示例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!