mysql建表测试_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 );
测试
mysql下载add produce测试⽤例-⽆⽗表相应数据,先插⼊⼦表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 aselect * from Orders;1 1 a
测试⽤例-⽗⼦表都有数据,删除⼦表数据delete from Orders where id = 1;
结果:执⾏成功select * from Models;1 aselect * 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
相关⽂章:
mysql数据库索引的建⽴以及性能测试mysql ⾼性能压⼒测试(总结了好久)相关视频:
数据结构探险之线性表篇
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论