建立外键的前提: 本表的列必须与外键类型相同(外键必须是外表主键)。 外键作用: 使两张表形成关联,外键只能引用外表中的列的值! 指定主键关键字: foreign key(列名) 引用外键关键字: references <外键表名>(外键列名) 1.在要创建外键的那个字段后面加上 foreign key references 所要引用的表的名字(引用表的名字的字段),比如:2.另起一行UserId int not null foreign key references users(Id),
create table temp(
id int,
name char(20)
foreign key(id) references outTable(id)
)
说明:把id列 设为外键 参照外表outTable的id列 当外键的值删除 本表中对应的列筛除 当外键的值改变 本表中对应的列值改变。