Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 388
lapots Avatar asked Sep 29 '12 12:09

lapots


People also ask

What is the difference between a foreign key and a primary key?

A primary key is used to ensure data in the specific column is unique. A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It uniquely identifies a record in the relational database table.

What is difference between keys and constraints in SQL Server?

Its is used to fetch or retrieve records/data-rows from data table according to the condition/requirement. Keys are also used to create relationship among different database tables or views. SQL constraints are used to specify rules for the data in a table.

What is foreign key and example?

In simpler words, a foreign key is a set of attributes that references a candidate key. For example, a table called TEAM may have an attribute, MEMBER_NAME, which is a foreign key referencing a candidate key, PERSON_NAME, in the PERSON table.


3 Answers

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.

like image 143
Lukas Eder Avatar answered Oct 11 '22 17:10

Lukas Eder


The first option is purely for naming the constraint.

From SQL FOREIGN KEY Constraint

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

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

like image 20
Adriaan Stander Avatar answered Oct 11 '22 17:10

Adriaan Stander


Apart from controlling the name, nothing really. SQL Server will supply a name if you omit it. FYI, you only need this syntax (SQL Fiddle):

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

Here's a fuller example.

like image 35
RichardTheKiwi Avatar answered Oct 11 '22 15:10

RichardTheKiwi