【问题标题】:Setting up a Foreign Key - transact SQL设置外键 - 处理 SQL
【发布时间】:2016-01-28 12:25:53
【问题描述】:

我在分配外键时遇到了 SQL Server 2014 中的以下 tSQL 问题。有人可以帮助我理解什么是不正确的吗?谢谢-JT

    create table dbo.tbl_Util_Cost
    (
    chr_Grade   nchar(10)    not null Primary Key,
    pct_Target  decimal(18, 2)  not null,
    mon_Cost_Per_Hour   money   not null,
    dec_Daily_Hours decimal(18, 1)  not null
    );

    create table dbo.tbl_Team_Details
   (
     num_Personal_Number    numeric(18, 0)  not null,
     chr_Name   nchar(30)    not null,
     chr_Employee_Grade nchar(10)    not null ,
     dt_Start_Date date not null,
     dt_End_Date date   not null,
     foreign key fk_Team_Details1  ( chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)   
    );

我在外键上收到以下错误 -

消息 102,级别 15,状态 1,第 11 行附近的语法不正确 'fk_Team_Details1'

请指教!

【问题讨论】:

    标签: sql-server tsql foreign-keys sql-server-2014 create-table


    【解决方案1】:

    您的语法错误。 这是正确语法的示例:

    CONSTRAINT fk_Team_Details1 外键 (chr_Employee_Grade) 参考 dbo.tbl_Util_Cost (chr_Grade)

    更多:Create Foreign Key Relationships

    【讨论】:

      【解决方案2】:

      在添加外键时修改第二个表的创建

        constraint fk_Team_Details1  foreign key  
       ( chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)   
      

      【讨论】:

        【解决方案3】:

        替换

        foreign key fk_Team_Details1  
        ( chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)
        

         constraint fk_Team_Details1  
         foreign key (chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)
        

        完整脚本:

        create table dbo.tbl_Util_Cost
        (
        chr_Grade   nchar(10)    not null Primary Key,
        pct_Target  decimal(18, 2)  not null,
        mon_Cost_Per_Hour   money   not null,
        dec_Daily_Hours decimal(18, 1)  not null
        );
        
        create table dbo.tbl_Team_Details
        (
         num_Personal_Number    numeric(18, 0)  not null,
         chr_Name   nchar(30)    not null,
         chr_Employee_Grade nchar(10)    not null ,
         dt_Start_Date date not null,
         dt_End_Date date   not null,
         constraint fk_Team_Details1  foreign key ( chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)   
        );
        

        【讨论】:

          【解决方案4】:

          给你

          create table dbo.tbl_Util_Cost
          (
          chr_Grade   nchar(10)    not null Primary Key,
          pct_Target  decimal(18, 2)  not null,
          mon_Cost_Per_Hour   money   not null,
          dec_Daily_Hours decimal(18, 1)  not null
          );
          
          create table dbo.tbl_Team_Details
          (
           num_Personal_Number    numeric(18, 0)  not null,
           chr_Name   nchar(30)    not null,
           chr_Employee_Grade nchar(10)    not null ,
           dt_Start_Date date not null,
           dt_End_Date date   not null,
           constraint fk_Team_Details1  foreign key ( chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)   
          );
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-06
            • 2016-09-03
            相关资源
            最近更新 更多