sql數(shù)據(jù)庫(kù)外鍵怎么寫 如何設(shè)置數(shù)據(jù)庫(kù)中的外鍵?
如何設(shè)置數(shù)據(jù)庫(kù)中的外鍵?如果有外鍵,應(yīng)該先添加主表數(shù)據(jù),然后再添加輔助表數(shù)據(jù)。例如:有以下兩個(gè)表class table:CLassID nameclass 1class 2student table:
如何設(shè)置數(shù)據(jù)庫(kù)中的外鍵?
如果有外鍵,應(yīng)該先添加主表數(shù)據(jù),然后再添加輔助表數(shù)據(jù)。
例如:有以下兩個(gè)表
class table:
CLassID name
class 1
class 2
student table:
Sid name CLassID
one three 1
two Li Si 1
three Wang Wu 2
其中student表中的CLassID是class表CLassID的外鍵。
現(xiàn)在需要將sid=4、name=Zhao Liu和classsid=3的一段數(shù)據(jù)添加到student表中,因此只能先將一段新數(shù)據(jù)添加到class表中。
Insert into class table values(3,“three class”)
然后添加到student table中:
Insert into student table values(4,“Zhao Liu”,3)
mysql怎么設(shè)置外鍵?
外鍵設(shè)計(jì)的初衷是保證數(shù)據(jù)庫(kù)端操作中邏輯相關(guān)表數(shù)據(jù)的一致性和完整性。
優(yōu)點(diǎn):
外鍵引用操作列表: