Sql server 设置外键-transact-SQL
我在SQL Server 2014中分配外键时遇到以下tSQL问题。有人能帮我理解什么是不正确的吗?谢谢-JTSql server 设置外键-transact-SQL,sql-server,tsql,foreign-keys,sql-server-2014,create-table,Sql Server,Tsql,Foreign Keys,Sql Server 2014,Create Table,我在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_Hour
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)
);
外键上出现以下错误-
Msg 102,15级,状态1,第11行附近语法不正确
“fk_团队详细信息1”
请指教 您的语法错误。
以下是正确语法的示例:
约束fk_团队详细信息1外键(chr_员工等级)
参考dbo.tbl_Util_成本(chr_等级)
有关详细信息:更换
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)
);
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)
);
在添加外键时修改第二个表的创建
constraint fk_Team_Details1 foreign key
( chr_Employee_Grade) references dbo.tbl_Util_Cost (chr_Grade)
给你