Differences between “foreign key” and “constraint foreign key”

前端 未结 3 730
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 18:13

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 foreig         


        
3条回答
  •  一整个雨季
    2020-12-05 18:30

    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.

提交回复
热议问题