Mysq中l建立測試父表、子表及測試案例歸納總結

來源:互聯網
上載者:User

建立測試表

查看版本資訊

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    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 高效能壓力測試(總結了好久)

相關視頻:

資料結構探險之線性表篇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.