当前位置:网站首页>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
边栏推荐
- I made an istio workshop. This is the first introduction
- Wechat Alipay account system and payment interface business process
- Leetcode skimming -- sum of two integers 371 medium
- /bin/ld: 找不到 -lxslt
- Loss function and positive and negative sample allocation: Yolo series
- 6090. 极大极小游戏
- 密码学基础知识
- Build your own semantic segmentation platform deeplabv3+
- 6096. 咒语和药水的成功对数
- [leetcode] 977 - carré du tableau ordonné
猜你喜欢

动态规划入门二(5.647.62)

Leetcode skimming -- incremental ternary subsequence 334 medium

Beijing rental data analysis

Semantic segmentation learning notes (1)

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

Basic knowledge of cryptography
![[leetcode] 1162 map analysis](/img/9a/d04bde0417d4d5232950a4e260eb91.png)
[leetcode] 1162 map analysis
![[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)](/img/cf/38e4035c3b318814672f21c8a42618.jpg)
[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)

动态规划入门一,队列的bfs(70.121.279.200)

【LeetCode】1254-统计封闭岛屿的数量
随机推荐
[network security] network asset collection
Force deduction solution summarizes the lucky numbers in 1380 matrix
floyed「建议收藏」
PTA ladder game exercise set l2-001 inter city emergency rescue
Pytoch saves tensor to Mat file
[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)
密码学基础知识
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
[salesforce] how to confirm your salesforce version?
Bing.com網站
SQL stored procedure
PTA 天梯赛习题集 L2-001 城市间紧急救援
03.golang初步使用
【LeetCode】876-链表的中间结点
【LeetCode】877-石子游戏
2278. 字母在字符串中的百分比
Semantic segmentation learning notes (1)
MySQL -- Index Optimization -- order by
【LeetCode】486-预测赢家
03. Preliminary use of golang