[英]How to reference multiple foreign key to one table in SQL
I want to create a data warehouse and I decided to use some field in more than one table to increase the speed! 我想创建一个数据仓库,所以我决定在多个表中使用某些字段来提高速度!
But I have a problem: How can I reference more than one foreign key to one table in SQL? 但是我有一个问题: 如何在SQL中为一个表引用多个外键?
For example: if I have a table for calendar like this: 例如:如果我有一个这样的日历表:
calendar(c_id, year, month, day)
and another table for daily snapshots like this: 还有另一个用于每日快照的表,如下所示:
snapshot(id, c_id, year, ...)
How can I reference both 'c_id' and 'year' from snapshot to calendar table? 如何从快照到日历表同时引用“ c_id”和“ year” ?
Thank you. 谢谢。
Try something like this: 尝试这样的事情:
ALTER TABLE calendar ADD CONSTRAINT UQ_calendar_c_id_year UNIQUE (c_id, year)
ALTER TABLE snapshot ADD CONSTRAINT FK_snapshot_c_id_year
FOREIGN KEY (c_id, year) REFERENCES calendar (c_id, YEAR)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.