﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by fediori  / Enforcing surrogate key uniqueness in slwoly changing dimension / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Wed, 22 May 2013 14:59:38 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>By the way, this dimension is Type 6 ..http://en.wikipedia.org/wiki/Slowly_changing_dimension#Pure_Type_6_Implementation</description><pubDate>Thu, 19 Jan 2012 06:00:34 GMT</pubDate><dc:creator>Federico Iori</dc:creator></item><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>Yes, you are right, this is not the best for disk space and performance, but recalculation of fact tables is much easier and often not necessary if there are some errors in loading dimensions  . If a fact table contains the natural key to the dimension, this fact table will never require recalculation if dimension has been bad loaded. I just replace the natural key ( varchar) with an int . I ensure that this hybrid approach can work fine . By the way, my article was also written to show that, by using indexed views, is possible to implement sofisticated data integrity checks at DBMS level[quote][b]h_d_t (1/5/2012)[/b][hr]you now have a composite PK, which isn't ideal, and joining to facts will always require 2 fields in the join clause. the PK is now bigint + datetime, which is 8+8 bytes, double the amount of space than just a PK on the bigint alone.you are building 'intelligence' into your PK - this should never be. your PK should be 'dumb', with no business value / logic. i suggest you drop this SK field, use your PK as autoincrement and make it the SK, which joins to your FK in your fact table.[/quote]</description><pubDate>Mon, 09 Jan 2012 02:24:56 GMT</pubDate><dc:creator>Federico Iori</dc:creator></item><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>you now have a composite PK, which isn't ideal, and joining to facts will always require 2 fields in the join clause. the PK is now bigint + datetime, which is 8+8 bytes, double the amount of space than just a PK on the bigint alone.you are building 'intelligence' into your PK - this should never be. your PK should be 'dumb', with no business value / logic. i suggest you drop this SK field, use your PK as autoincrement and make it the SK, which joins to your FK in your fact table.</description><pubDate>Thu, 05 Jan 2012 19:55:41 GMT</pubDate><dc:creator>h_d_t</dc:creator></item><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>Benefit is a more loosely coupling between dimension and fact table .It mays save up from need of recalculating the fact table if something go wrong, because the fact table contains only the reference to the entity contained in the dimension instead of the reference to the entity at a specific time . Very often Sql Server is IO bound, so for the DBMS to perform a join on 2 columns instead of one is not problem since there is plenty of CPU</description><pubDate>Thu, 05 Jan 2012 00:39:25 GMT</pubDate><dc:creator>Federico Iori</dc:creator></item><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>fediori,This is interesting...But what are the benefits of this approach comparing to the one when surrogate key is IDENTITY?</description><pubDate>Wed, 04 Jan 2012 09:59:57 GMT</pubDate><dc:creator>Alexander Suprun</dc:creator></item><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>Alexander , you are right : in my script is missing the Primary Key definition for the dimension .The primary key will be on Surrogate_Key, Date_From . This is not orthodox implementation of SCD Type 2 : the reason-to-be for Surrogate_Key is not to be and identity,but to replace the alphanumeric Natural_Key with a numeric sequence to perform more efficient joins and to take less space in fact tables.Fact tables should contain Surrogate Key and Fact_Date to be joined with the dimension  Example :  [code="sql"]  select * from fact_table a left join dim_scd_prova b on a.Surrogate_Key = b.Surrogate_Key  and a.Event_Date between b.Date_From and isnull(b.date_to, '2100-01-01')  [/code]This method , although not orthodox, works fine, also because in general all fact tables contains a date.Not using the Surrogate_Key would require to perform less efficient joins on the Natural_Key, that is in general alphanumericThis method requires too that for each Natural_Key there is only one Surrogate_Key, and my solution guarantees itat the database level .Your proposed solution [code="sql"] CREATE UNIQUE INDEX [IndexName] ON [TableName](Natural_Key) WHERE Date_To IS NULL [/code] is clever, but does not examine the whole table, but only the current version, and in some cases is not enough</description><pubDate>Wed, 04 Jan 2012 02:43:35 GMT</pubDate><dc:creator>Federico Iori</dc:creator></item><item><title>RE: Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>Maybe I didn't get something but I have not heard anything about the rule: "for each unique value of the natural key there should be only one value of the surrogate key". I always thought that SCD works differently. So my questions are:1. Where is a Primary Key on the table? This is a fundamental requirement for ANY relational table.2. How are you going to join a Fact table to this dimension table if there is no unique constraint?3. Isn't it a best practise to create surrogate key as autoincremental IDENTITY field? So it can be a Primary Key as well and in this case why do we need any special unique constraints on an indexed view?For DW with SCD Type 2 I found useful this index to check Natural Key uniqueness:[code="sql"]CREATE UNIQUE INDEX [IndexName] ON [TableName](Natural_Key) WHERE Date_To IS NULL[/code]</description><pubDate>Tue, 03 Jan 2012 20:49:02 GMT</pubDate><dc:creator>Alexander Suprun</dc:creator></item><item><title>Enforcing surrogate key uniqueness in slwoly changing dimension</title><link>http://www.sqlservercentral.com/Forums/Topic1219727-2632-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/DWH/86860/"&gt;Enforcing surrogate key uniqueness in slwoly changing dimension&lt;/A&gt;[/B]</description><pubDate>Fri, 09 Dec 2011 15:30:54 GMT</pubDate><dc:creator>Federico Iori</dc:creator></item></channel></rss>