当前位置:网站首页>SQL FOREIGN KEY
SQL FOREIGN KEY
2022-07-02 12:16:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
一个表中的FOREIGH KEY 指向另一个表中的PRIMARY KEY。
通过实例来解释外键。请看下面两个表:
注意:
·”Orders”表中的”P_Id”列指向”Persons”表中的”P_Id”列。
·”Persons”表中的”P_Id”列是”Persons”表中的PRIMARY KEY。
·”Orders”表中的”P_Id”列是”Orders”表中的FOREIGN KEY。
FOREIGN KEY 约束用于预防破坏表之间连接的行为。
FOREIGN KEY 约束也能防止非法数据插入外键列,因为它必须是它指向的那个表中的值之一。
CREATE TABLE时的SQL FOREIGN KEY 约束
在”Orders”表创建时在”P_Id”列上创建FOREIGN KEY 约束:
MySQL:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)SQL Server/ Oracle /MS Access:
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)如需命名FOREIGN KEY 约束,并定义多个列的FOREIGN KEY 约束,请使用下面的SQL语法:
MySQL /SQL Server /Oracle /MS Access:
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)ALTER TABLE 时的 SQLFOREIGN KEY 约束
当”Orders”表已被创建时,如需在”P_Id”列创建FOREIGN KEY 约束,请使用下面的SQL:
MySQL /SQL Server /Oracle /MS Access
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)如需命名FOREIGN KEY 约束,并定义多个列的FOREIGN KEY 约束,请使用下面的SQL语法:
MySQL /SQL Server /Oracle /MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)撤销FOREIGN KEY 约束 如需撤销FOREIGN KEY约束,请使用下面的SQL:
MySQL:
ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrdersSQL Server /Oracle /MS Access:
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders参考:
https://www.yuque.com/docs/share/79273585-3f48-4505-b43b-edb59ba84662
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147652.html原文链接:https://javaforall.cn
边栏推荐
- 【LeetCode】1020-飞地的数量
- 彻底弄懂浏览器强缓存和协商缓存
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- 03.golang初步使用
- 【LeetCode】977-有序数组的平方
- MD5 encryption
- 2278. Percentage of letters in string
- [leetcode] 344 reverse string
- Deux séquences ergodiques connues pour construire des arbres binaires
- 【LeetCode】200-岛屿数量
猜你喜欢

【LeetCode】1162-地图分析

Beijing rental data analysis

【Salesforce】如何确认你的Salesforce版本?

微信支付宝账户体系和支付接口业务流程

Bing. Com website

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)

Build your own semantic segmentation platform deeplabv3+
![[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)](/img/26/3f19d36c048e669c736e27384e0fa7.jpg)
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)

Oracle primary key auto increment

怎样从微信返回的json字符串中截取某个key的值?
随机推荐
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)
自定义异常
5. Practice: jctree implements the annotation processor at compile time
MD5加密
6092. 替换数组中的元素
Yolov5 code reproduction and server operation
[leetcode] 876 intermediate node of linked list
【LeetCode】1905-统计子岛屿
Folium, diagnosis and close contact trajectory above
Thoroughly understand browser strong cache and negotiation cache
【LeetCode】200-岛屿数量
Deux séquences ergodiques connues pour construire des arbres binaires
folium地图无法显示的问题,临时性解决方案如下
高考录取分数线爬取
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board
ssh/scp 使不提示 All activities are monitored and reported.
Redux - detailed explanation
2278. 字母在字符串中的百分比
【LeetCode】977-有序数组的平方
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)