简体   繁体   English

三向参照完整性 - SQL Server 2008

[英]Three-way Referential Integrity - SQL Server 2008

I am building a database using SQL Server 2008 to store prices of securities that are traded on multiple markets. 我正在使用SQL Server 2008构建一个数据库来存储在多个市场上交易的证券的价格。

For a given market, all the securities have the same holiday calendar. 对于特定市场,所有证券都具有相同的假日日历。 However, the holiday calendars are different from market to market. 但是,假日日历因市场而异。

I want the following four tables: Market , GoodBusinessDay , Security , and SecurityPriceHistory , and I want to enforce that SecurityPriceHistory does not have rows for business days when the market on which a security was traded was closed. 我想要以下四个表: MarketGoodBusinessDaySecuritySecurityPriceHistory ,并且我想强制执行SecurityPriceHistory在交易证券交易市场关闭时的工作日没有行。

The fields in the tables are as follows: 表中的字段如下:

Market: MarketID (PK), MarketName 市场:MarketID(PK),MarketName

GoodBusinessDay: MarketID (FK), SettlementDate (the pair is the PK) GoodBusinessDay:MarketID(FK),SettlementDate(该对是PK)

Security: SecurityID (PK), MarketID (FK), SecurityName 安全性:SecurityID(PK),MarketID(FK),SecurityName

SecurityPriceHistory: This is the question - my preference is SecurityID, SettlementDate, SecurityPrice SecurityPriceHistory:这是问题 - 我的偏好是SecurityID,SettlementDate,SecurityPrice

How can I define the tables this way and guarantee that for every row in SecurityPriceHistory , there is a corresponding row in GoodBusinessDay ? 如何以这种方式定义表并保证对于SecurityPriceHistory中的每一行, GoodBusinessDay都有相应的行?

If I added a column for MarketID to SecurityPriceHistory . 如果我将MarketID列添加到SecurityPriceHistory I could see how I could do this with two foreign keys (one pointing back to Security and one pointing to GoodBusinessDay ), but that doesn't seem like the right way to do it. 我可以看到我如何使用两个外键(一个指向安全和一个指向GoodBusinessDay ),但这似乎不是正确的方法。

This model should do. 这个模型应该做。 The relationship between Market and BusinessDay is identifying , that is, a businessday does not exist outside the context of the market to which it belongs. Market和BusinessDay之间的关系是识别 ,即商业日不属于它所属的市场环境。

Similarly, the relationship between BusinessDay and SecurityPriceHistory is identifying, as it the relationship between Security and SecurityPriceHistory. 同样,BusinessDay和SecurityPriceHistory之间的关系正在识别Security和SecurityPriceHistory之间的关系。

This means that the primary key of SecurityPriceHistory is composite: security_id,market_id and settlement_date. 这意味着SecurityPriceHistory的主键是composite:security_id,market_id和settlement_date。

拿1

This will enforce the constraint that each security may be have no more than one row in SecurityPriceHistory for a given market/business day. 这将强制执行约束,即对于给定的市场/营业日,每个安全性在SecurityPriceHistory中可能不超过一行。 It does, however, allow for the same security to trade in multiple markets, despite the security's relationship to a particular market: to restrict that, the relationship between Market and Security needs to be identifying, thus: 然而,尽管安全性与特定市场的关系,它确实允许在多个市场中进行相同的证券交易:为了限制这一点,市场和证券之间的关系需要识别,因此:

拿2

SecurityPriceHistory could have a FK to GoodBusinessDay and NO FK to Market . SecurityPriceHistory可能有一个FK到GoodBusinessDay和没有FK到Market You could figure out the market by joining to the GoodBusinessDay table. 您可以通过加入GoodBusinessDay表来弄清楚市场。 I don't really like this option, but it's a possibility. 我真的不喜欢这个选项,但这是一种可能性。

You could also use a trigger to check and make sure that there's a proper GoodBusinessDay record on insert/update, otherwise reject the transaction. 您还可以使用触发器检查并确保插入/更新时存在正确的GoodBusinessDay记录,否则拒绝该事务。

I think you're going to need the marketID field in your securityPriceHistory table, assuming the same securityID could be sold in different markets on the same goodBusinessDay. 我认为你需要securityPriceHistory表中的marketID字段,假设同一goodBusinessDay可以在不同的市场上销售相同的securityID。

Setting it up the way you're thinking is fine. 按照你想的方式设置它很好。 You have a parent, two related children, then a grandchild that has relationships with both children. 你有一个父母,两个相关的孩子,然后是一个与两个孩子有关系的孙子。


Even if a security can only be sold on one market, I'd still include the marketID in the securityPriceHistory table, with two compound FKs, MarketDay and MarketSecurity. 即使证券只能在一个市场上出售,我仍然会在securityPriceHistory表中包含marketID,其中包含两个复合FK,MarketDay和MarketSecurity。 Clearer that way, IMO. 这样清楚,IMO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM