Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

sql - Differences between "foreign key" and "constraint foreign key"

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

and I can create it like this

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The first one assigns a user-defined name to the foreign key, the second one will assign a system-generated name to the foreign key.

User-defined foreign key names can be useful for subsequent statements like these:

ALTER TABLE XTable DROP    CONSTRAINT fk_idq;
ALTER TABLE XTable ENABLE  CONSTRAINT fk_idq;
ALTER TABLE XTable DISABLE CONSTRAINT fk_idq;

It's harder to alter constraints with system-generated names, as you have to discover those names first.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...