table 外來鍵限制
1.Create Table tableName(
columName datatype not null Primary key,
id int not null Primary key,
gender nvarchar(50)
)
2.隨時使用某資料庫
Use DBname
Go
3.增加FOREIGN KEY 限制
Alter table 資料表名字 add constraints 外來鍵的表_外來鍵_FK
Foreign key (外來鍵名) references 要參考的主鍵表名(主鍵名)
4.如果要加之前外來鍵表不符合這個限制
需要先改之後才能加
例如說Person表有GenderId=99
這時候要加限制 但Gender表的id只有1,2
所以不符合
不能加 要把99改掉
5.外來鍵限制的意義
保持資料完整性
避免出現不符合常理的資料
留言
張貼留言