当前位置:网站首页>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_PerOrders
SQL Server /Oracle /MS Access:
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders
Reference 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
边栏推荐
- 高考录取分数线爬取
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- /bin/ld: 找不到 -lxslt
- 2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
- 【LeetCode】1162-地图分析
- [leetcode] 877 stone game
- 2278. Percentage of letters in string
- folium,确诊和密接轨迹上图
- 【LeetCode】1254-统计封闭岛屿的数量
- 夏季高考文化成绩一分一段表
猜你喜欢
《大学“电路分析基础”课程实验合集.实验四》丨线性电路特性的研究
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
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
03. Preliminary use of golang
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
Leetcode skimming - remove duplicate letters 316 medium
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
动态规划入门二(5.647.62)
随机推荐
MD5加密
(Wanzi essence knowledge summary) basic knowledge of shell script programming
Bing.com網站
6090. Minimax games
For the problem that Folium map cannot be displayed, the temporary solution is as follows
/bin/ld: 找不到 -lssl
Party History Documentary theme public welfare digital cultural and creative products officially launched
[leetcode] 876 intermediate node of linked list
【LeetCode】189-轮转数组
目标检测—利用labelimg制作自己的深度学习目标检测数据集
Leetcode question brushing - parity linked list 328 medium
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
将点云坐标转换成世界坐标的demo
(4) Flink's table API and SQL table schema
制作p12证书[通俗易懂]
XPT2046 四线电阻式触摸屏
Leetcode skimming - remove duplicate letters 316 medium
[leetcode] 977 square of ordered array
数组和链表的区别浅析
【LeetCode】486-预测赢家