当前位置:网站首页>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
边栏推荐
- Deux séquences ergodiques connues pour construire des arbres binaires
- folium,确诊和密接轨迹上图
- Real estate market trend outlook in 2022
- Redux - detailed explanation
- College entrance examination score line climbing
- How to choose a third-party software testing organization for automated acceptance testing of mobile applications
- 【LeetCode】344-反转字符串
- 6095. 强密码检验器 II
- [leetcode] 486 predict winners
- Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
猜你喜欢

How to choose a third-party software testing organization for automated acceptance testing of mobile applications

Oracle primary key auto increment
![[leetcode] 1905 statistics sub Island](/img/82/d2f7b829f5beb7f9f1eabe8d101ecb.png)
[leetcode] 1905 statistics sub Island
![[salesforce] how to confirm your salesforce version?](/img/ce/4c844b1b686397faa1b6aa3d57e034.png)
[salesforce] how to confirm your salesforce version?

LeetCode刷题——去除重复字母#316#Medium

03.golang初步使用

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

【LeetCode】1162-地图分析

NBA player analysis

Leetcode skimming -- count the number of numbers with different numbers 357 medium
随机推荐
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
Solve the problem of frequent interruption of mobaxterm remote connection
How to avoid 7 common problems in mobile and network availability testing
【LeetCode】977-有序数组的平方
睿智的目标检测23——Pytorch搭建SSD目标检测平台
[development environment] install Visual Studio Ultimate 2013 development environment (download software | install software | run software)
04. Some thoughts on enterprise application construction after entering cloud native
【LeetCode】695-岛屿的最大面积
Case introduction and problem analysis of microservice
Engineer evaluation | rk3568 development board hands-on test
Yolo format data set processing (XML to txt)
损失函数与正负样本分配:YOLO系列
03. Preliminary use of golang
[leetcode] 977 square of ordered array
Folium, diagnosis and close contact trajectory above
/bin/ld: 找不到 -lssl
ssh/scp 使不提示 All activities are monitored and reported.
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
College entrance examination admission score line climbing
【Leetcode】167-两数之和II -输入有序数组