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?
The first option is purely for naming the constraint.
From SQL FOREIGN KEY Constraint
To allow naming of a
FOREIGN KEYconstraint, and for defining aFOREIGN KEYconstraint on multiple columns, use the following SQL syntaxAlso, from CREATE TABLE (Transact-SQL) one can see that
[ CONSTRAINT constraint_name ]is optional.