sql - 在列级 FOR​​EIGN KEY 约束中指定了多个键

标签 sql sql-server

我正在为我的学校做一个数据库项目,我偶然发现了这个我似乎无法解决的奇怪问题。这是我尝试创建表时的代码:

Create table Offering(
StartDate       DATE            NOT NULL    ,       
PRIMARY KEY(StartDate) 
);

Create table OfferPerWeek(
StartDate       DATE            NOT NULL    ,
StartTime       TIME(0)         NOT NULL    ,
[Day]           VARCHAR(10)     NOT NULL
PRIMARY KEY (StartDate,[Day],StartTime)
FOREIGN KEY (StartDate) 
REFERENCES Offering(StartDate)
);

--The table that have the issues 
Create table OfferPerWeek_Venue(  
[Day]           VARCHAR(10)     NOT NULL    ,
StartDate       DATE            NOT NULL    , 
StartTime       TIME(0)         NOT NULL    
PRIMARY KEY (StartDate, [Day], StartTime)
FOREIGN KEY (StartDate, [Day], StartTime) 
REFERENCES OfferPerWeek (StartDate, [Day] , StartTime)
);

错误信息是这样说的:

More than one key specified in column level FOREIGN KEY constraint

我真的被这个问题弄糊涂了,我试图创建多个外键来引用父表的那么多主键。

希望能解决这个问题,谢谢

最佳答案

您缺少逗号。尝试:

Create table OfferPerWeek_Venue(  
[Day]           VARCHAR(10)     NOT NULL    ,
StartDate       DATE            NOT NULL    , 
StartTime       TIME(0)         NOT NULL , <-- comma there  
PRIMARY KEY (StartDate, [Day], StartTime), <-- comma there
FOREIGN KEY (StartDate, [Day], StartTime) 
REFERENCES OfferPerWeek (StartDate, [Day] , StartTime)
);

您可以尝试以下方法,命名外键:

Create table OfferPerWeek_Venue(  
[Day]           VARCHAR(10)     NOT NULL    ,
StartDate       DATE            NOT NULL    , 
StartTime       TIME(0)         NOT NULL ,  
CONSTRAINT [PK_GoodName] PRIMARY KEY (StartDate, [Day], StartTime), 
CONSTRAINT [FK_GoodName] FOREIGN KEY (StartDate, [Day], StartTime) 
REFERENCES OfferPerWeek (StartDate, [Day] , StartTime)
);

关于sql - 在列级 FOR​​EIGN KEY 约束中指定了多个键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48683042/

相关文章:

sql - #在SQL中的意义是什么?

sql - 从表中的列中删除字符串的某些部分

mysql - 按另一列分组的列的平均值

php - 从 2 个没有任何相似行的表中选择 *

sql-server - SQL Server 如何将日期与字符串文字进行比较?

c# - 在 SQL Server 2017 中部署 CLR UDF : procedure

sql-server - Azure SQL 数据库 DTU 指标

php - 使用特定搜索条件将内容插入 MYSQL 表列

python - 为什么在微型 df 上使用 fast_executemany 会出现内存错误?

php - 文件/图像复制