当前位置:网站首页>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_PerOrders
SQL 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
边栏推荐
- 6096. Success logarithm of spells and potions
- Evaluation of embedded rz/g2l processor core board and development board of Feiling
- Case introduction and problem analysis of microservice
- 【LeetCode】977-有序数组的平方
- Download blender on Alibaba cloud image station
- [leetcode] 167 - sum of two numbers II - enter an ordered array
- 6095. 强密码检验器 II
- Redux——详解
- 6092. Replace elements in the array
- 6.12 critical moment of Unified Process Platform
猜你喜欢
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
使用 percona 工具给 MySQL 表加字段中断后该如何操作
[salesforce] how to confirm your salesforce version?
Bing.com网站
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Evaluation of embedded rz/g2l processor core board and development board of Feiling
Build your own semantic segmentation platform deeplabv3+
LeetCode刷题——奇偶链表#328#Medium
彻底弄懂浏览器强缓存和协商缓存
二叉树前,中,后序遍历
随机推荐
03.golang初步使用
Leetcode skimming - remove duplicate letters 316 medium
LeetCode刷题——奇偶链表#328#Medium
College entrance examination admission score line climbing
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
提前批院校名称
/bin/ld: 找不到 -lcrypto
MD5 encryption
【LeetCode】486-预测赢家
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
【网络安全】网络资产收集
Loss function and positive and negative sample allocation: Yolo series
04. Some thoughts on enterprise application construction after entering cloud native
/bin/ld: 找不到 -lxslt
SQL stored procedure
【LeetCode】1020-飞地的数量
士官类学校名录
[leetcode] 1140 stone game II
[leetcode] 283 move zero
怎样从微信返回的json字符串中截取某个key的值?