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

Categories

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

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

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...