当前位置:网站首页>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
边栏推荐
猜你喜欢

Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)

Thoroughly understand browser strong cache and negotiation cache

Ant group's large-scale map computing system tugraph passed the national evaluation

Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC

Soul torture, what is AQS???

Aiko ai Frontier promotion (7.2)
![[leetcode] 1905 statistics sub Island](/img/82/d2f7b829f5beb7f9f1eabe8d101ecb.png)
[leetcode] 1905 statistics sub Island

Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals

动态规划入门二(5.647.62)

Bing. Com website
随机推荐
beforeEach
6090. Minimax games
【LeetCode】1162-地图分析
[leetcode] 1140 stone game II
士官类学校名录
蚂蚁集团大规模图计算系统TuGraph通过国家级评测
Bing.com网站
Experiment collection of University "Fundamentals of circuit analysis". Experiment 6 - observation and measurement of typical signals
How to intercept the value of a key from the JSON string returned by wechat?
[leetcode] 876 intermediate node of linked list
(Wanzi essence knowledge summary) basic knowledge of shell script programming
[2. Basics of Delphi grammar] 3 Object Pascal constants and variables
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
[leetcode] 283 move zero
Comparison between rstan Bayesian regression model and standard linear regression model of R language MCMC
【LeetCode】877-石子游戏
6090. 极大极小游戏
怎样从微信返回的json字符串中截取某个key的值?
数组和链表的区别浅析
提前批院校名称