简体   繁体   English

如何在SQL中引用多个外键到一个表

[英]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.

相关问题 如何将一个表中的多个外键引用到 ms sql 中的单个主键 - How can I reference multiple foreign key in one table to a single primary key in ms sql 外键SQL:如何将一个表中的两个属性引用到在另一个表中包含3列的复合主键? - Foreign Key SQL: How can I reference two attributes in one table to a composite primary key that contains 3 columns in the other table? SQL引用另一个表作为外键 - SQL reference another table as foreign key 从多个表引用一个外键 - Reference to one foreign key from multiple tables 在 SQL 中,如何将一个表的多个列中的多个外键 ID 连接到另一个表中的单个主键和唯一的 select 一个? - In SQL, how to join multiple foreign keys IDs in multiple columns of a table to a single primary key in another table and uniquely select one? 如何在SQL中将表1中的复合主键引用为表2作为外键? - How to reference the composited PRIMARY KEY in table1 to table 2 as foreign key in SQL? 如何在SQL Server 2014中的单个表中的外键内联接多个外键 - How to join a multiple foreign key within the foreign key in a single table in SQL Server 2014 SQL-具有3个所有者表的外键一个表 - SQL - foreign key one table with 3 owner tables 一个表用一个外键引用两个不同的表,可能吗? - One table reference to two different tables with one foreign key, possible? SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM