当前位置:网站首页>SQL FOREIGN KEY
SQL FOREIGN KEY
2022-07-02 15:46:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
In a watch FOREIGH KEY Point to... In another table PRIMARY KEY.
Explain foreign keys through examples . Look at the following two tables :
Be careful :
·”Orders” In the table ”P_Id” The column points to ”Persons” In the table ”P_Id” Column .
·”Persons” In the table ”P_Id” The column is ”Persons” In the table PRIMARY KEY.
·”Orders” In the table ”P_Id” The column is ”Orders” In the table FOREIGN KEY.
FOREIGN KEY Constraints are used to prevent the behavior of breaking connections between tables .
FOREIGN KEY Constraints also prevent illegal data from being inserted into foreign key columns , Because it has to be one of the values in the table it points to .
CREATE TABLE At the time of the SQL FOREIGN KEY constraint
stay ”Orders” The table is created at ”P_Id” Column to create FOREIGN KEY constraint :
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)
)To name FOREIGN KEY constraint , And define multiple columns of FOREIGN KEY constraint , Please use the following SQL grammar :
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 At the time of the SQLFOREIGN KEY constraint
When ”Orders” When the table has been created , If you need to be in ”P_Id” Column creation FOREIGN KEY constraint , Please use the following SQL:
MySQL /SQL Server /Oracle /MS Access
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)To name FOREIGN KEY constraint , And define multiple columns of FOREIGN KEY constraint , Please use the following SQL grammar :
MySQL /SQL Server /Oracle /MS Access:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)revoke FOREIGN KEY constraint If you need to cancel FOREIGN KEY constraint , Please use the following SQL:
MySQL:
ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrdersSQL Server /Oracle /MS Access:
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrdersReference resources :
https://www.yuque.com/docs/share/79273585-3f48-4505-b43b-edb59ba84662
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/147652.html Link to the original text :https://javaforall.cn
边栏推荐
- Astra: could not open "2bc5/ [email protected] /6“: Failed to set USB interface
- 动态规划入门一,队列的bfs(70.121.279.200)
- List of sergeant schools
- 数字藏品系统开发(程序开发)丨数字藏品3D建模经济模式系统开发源码
- [leetcode] 977 square of ordered array
- beforeEach
- 【LeetCode】1140-石子游戏II
- 04. Some thoughts on enterprise application construction after entering cloud native
- /bin/ld: 找不到 -lgssapi_krb5
- [experience cloud] how to get the metadata of experience cloud in vscode
猜你喜欢

Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
![[experience cloud] how to get the metadata of experience cloud in vscode](/img/45/012c2265402ba1b44f4497f468bc61.png)
[experience cloud] how to get the metadata of experience cloud in vscode

Deux séquences ergodiques connues pour construire des arbres binaires

Party History Documentary theme public welfare digital cultural and creative products officially launched

Custom exception

Leetcode skimming - remove duplicate letters 316 medium

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium

已知兩種遍曆序列構造二叉樹

Basic knowledge of cryptography
随机推荐
【LeetCode】577-反转字符串中的单词 III
6090. 极大极小游戏
Bing. Com website
fastjson List转JSONArray以及JSONArray转List「建议收藏」
自定义异常
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
Redux - detailed explanation
folium地图无法显示的问题,临时性解决方案如下
【LeetCode】977-有序數組的平方
数组和链表的区别浅析
Bing.com网站
matlab中wavedec2,说说wavedec2函数[通俗易懂]
Experiment collection of University "Fundamentals of circuit analysis". Experiment 4 - Research on linear circuit characteristics
【LeetCode】1905-统计子岛屿
【LeetCode】695-岛屿的最大面积
密码学基础知识
【LeetCode】1254-统计封闭岛屿的数量
List of sergeant schools
Golang MD5 encryption and MD5 salt value encryption
(5) Flink's table API and SQL update mode and Kafka connector case