“外键"和“外键"之间的区别和“约束外键" [英] Differences between "foreign key" and "constraint foreign key"

查看:27
本文介绍了“外键"和“外键"之间的区别和“约束外键"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是,例如,我可以创建类似的表

I mean for example I can create table like

create table XTable
( 
  idt int not null primary key,
  value nvarchar(50),
  idq int,
  constraint fk_idq foreign key(idq) references YTable(idq)
)

我可以像这样创建它

create table XTable
(
  idt int not null primary key,
  value nvarchar(50),
  idq int,
  foreign key(idq) references YTable(idq)
)

我通常像第二个示例一样创建表格,但现在我对第一个示例感到好奇.有什么区别?

I usually create table like in the second example but now I'm curious about the first example. What is the difference?

推荐答案

第一个选项纯粹是为了命名约束.

The first option is purely for naming the constraint.

来自 SQL 外键约束

要允许命名 FOREIGN KEY 约束,并在多列上定义 FOREIGN KEY 约束,请使用以下 SQL 语法

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple columns, use the following SQL syntax

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)
)

另外,来自 CREATE TABLE (Transact-SQL) 可以看到 [ CONSTRAINT constraint_name ] 是可选的.

Also, from CREATE TABLE (Transact-SQL) one can see that [ CONSTRAINT constraint_name ] is optional.

这篇关于“外键"和“外键"之间的区别和“约束外键"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆