博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
怎么给一张表添加外键(四种方法)
阅读量:5745 次
发布时间:2019-06-18

本文共 739 字,大约阅读时间需要 2 分钟。

添加外键约束名字一定不能重复

如何添加外键
方法一:直接在属性值后面添加

create table score(cscore int(11),st_id int(50) references student(id),cs_id int(30) references classes(id),primary key(st_id,cs_id));

 

方法二:

create table score(cscore int(11),st_id int(50),cs_id int(30),primary key(st_id,cs_id),FOREIGN KEY (st_id) REFERENCES student(id),FOREIGN KEY (cs_id) REFERENCES classes(id));

 

方法三:添加约束

create table score(cscore int(11),st_id int(50),cs_id int(30),primary key(st_id,cs_id),CONSTRAINT `FK_ID_ST` FOREIGN KEY (st_id) REFERENCES student(id),CONSTRAINT `FK_ID_CS` FOREIGN KEY (cs_id) REFERENCES classes(id));

 

方法四:在表的定义外进行添加

alter table 表名 add constraint FK_ID foreign key(你的外键字段名) REFERENCES 外表表名(对应的表的主键字段名);

 

转载于:https://www.cnblogs.com/qisong178878915/p/4435488.html

你可能感兴趣的文章
华大基因BGI Online的云计算实践
查看>>
深入理解自定义Annotation,实现ButterKnif小原理
查看>>
排序高级之交换排序_冒泡排序
查看>>
Cocos2d-x3.2 Ease加速度
查看>>
[EntLib]关于SR.Strings的使用办法[加了下载地址]
查看>>
中小型网站架构分析及优化
查看>>
写shell的事情
查看>>
负载均衡之Haproxy配置详解(及httpd配置)
查看>>
linux虚拟机拷贝之后联网出错
查看>>
Linux文件系统探索
查看>>
标准与扩展ACL 、 命名ACL 、 总结和答疑
查看>>
查找恶意的TOR中继节点
查看>>
MAVEN 属性定义与使用
查看>>
hadoop2.7.2 HA搭建
查看>>
shell高级视频答学生while循环问题
查看>>
无法SSH到Ubuntu
查看>>
使用@media实现IE hack的方法
查看>>
《11招玩转网络安全》之第一招:Docker For Docker
查看>>
hive_0.11中文用户手册
查看>>
hiveserver2修改线程数
查看>>