[英]MySQL how to make a column of child table point to the parent table?
我剛剛啟動MySQL數據庫,但對外鍵可以做什么感到困惑。
假設我有一個這個的父表(老師),而taxno是主鍵;
taxno Tfname Tlname Tgender Tquali Thours
1111 TOH JIM M HONOURS 120
和我的孩子表(teacsub);
taxno Tfname Tlname Tgender Tquali Thours Subtitle Subtype
例如,如果我對teacsub的輸入是
taxno Tfname Tlname Tgender Tquali Thours Subtitle Subtype
1111 dddd dddd F weqwe 100 HISTORY 3
它會提示我輸入的teacsub是錯誤的嗎?
我已經為Taxno創建了外鍵,但僅適用於Taxno。
關系數據庫的優點在於,使用正確構造的表,您無需像以前一樣重復信息。
如果您的teacher
表包含諸如姓名,性別等信息,則無需在其他任何地方重復此信息。
例如,如果您的teacsub
表只是teacsub(taxno, subtitle, subtype)
,那么您可以通過將兩個表與如下查詢結合在一起來獲取“完整”信息:
select *
from teacher
inner join teacsub
on teacher.taxno = teacsub.taxno
這會很高興地結束,並從兩個共享相同taxno
表中拉出所有行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.