如何用SQL语句添加外键
1) 对于创建表的时候添加外键
CREATE TABLE score1(
score DOUBLE,
sid INT,
CONSTRAINT fk_stu_score1_sid FOREIGN KEY(sid) REFERENCES stu(sid)
);
2)对于已经存在表了再添加外键
CREATE TABLE score1(
score DOUBLE,
sid INT
);
ALTER TABLE score1 ADD CONSTRAINT fk_sid FOREIGN KEY(sid) REFERENCES stu(sid)
注:CONSTRAINT后面的名字是自定义的
如果想要定义多个外键,语法为: FOREIGN KEY(外键…) REFERENCES stu(sid…)。如果该方法不行,那么就一个一个外键老老实实的添加
相关文章
- 外键不兼容问题( in foreign key constraint are incompatible)
- Navicat For MySQL--外键建立与cannot add foreign key constraint分析
- Navicat cannot add foreign key constraint或者添加外键后不显示
- mysql外键设置问题 Failed to add the foreign key constraint.
- MySQL如何设置外键约束cannot add foreign key constraint设置外键约束失败原因
- MySql导入.sql文件时外键报错,a foreign key constraint fails,Cannot add foreign key constraint
- mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决
- MySQL添加外键时报错 ERROR 1215 (HY000): Cannot add foreign key constraint
- mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决
- Constraint6:更新外键约束(Foreign Key Constraint)的引用列