More than Just Lines on a Map: Best Practices for U.S Bike Routes
Data Integrity Row Spanning Dependency
1. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
Row Spanning Dependency
2. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
● What is Data Integrity?
– Wikipedia: “Data integrity refers to maintaining
and assuring the accuracy and consistency of
data over its entire life-cycle...”
● Consistency?
● Accuracy?
3. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
● Model an attribute
– Integer, not null, positive value between 1 and
2000
– Datatype and domain definition:
…, A int NOT NULL constraint ('A in (1..2000)'),
● Record inserted with value 3 in this field.
– Is it valid?
– Is it accurate?
4. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
● What is Data Integrity?
– Wikipedia: “Data integrity refers to maintaining
and assuring the accuracy and consistency of
data over its entire life-cycle...”
● Consistency?
● Accuracy?
● What about normalization?
– Why do we normalize?
5. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
create table T1(
…
int F1 NULL,
int F2 NULL,
int F3 NULL,
int F4 NULL,
int F5 NULL,
…
);
F1 F2 F3 F4 F5
<null> <null> <null> <null> <null>
F1 F2 F3 F4 F5
15 43 <null> <null> <null>
F1 F2 F3 F4 F5
15 43 9 379 403
F1 F2 F3 F4 F5
15 43 <null> 379 403
6. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
● What is Data Integrity?
– Assuring the data remains non-anomalous over its
entire life-cycle...
– Where “non-anomalous” means:
● the data in each field is valid when taken individually
● the data does not lead to inconsistent results when
take as meaningful groups
● Goal of Data Integrity
– Don’t let bogus data in your DB ― ever!
7. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Data Integrity
● Tools
– Data types/domains
– Referential
– Constraints
– Triggers
– Normalization
8. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Row Spanning Dependency
● Dependency between rows
● Particularly complicated
– Initial integrity
– Maintaining integrity
● No built-in tool to help
● Pretty much completely avoidable
9. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Row Spanning Dependency
● When two fields have a functional
dependency on each other. But the
dependency of each field is determined by
the other field contained in a different but
related row.
● That is, F1 ↔ F2, but F1 of row R1 is
dependent on the value of F2 of row R2 and
F2 of row R1 is dependent on F1 of row R3.
10. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Characteristics
● Generally identified by begin_value/end_value
pairs of fields.
– end_value of one row matches begin_value of the
next row: defines a series.
● DML becomes more complicated
– INSERT accompanied by UPDATE
– UPDATE accompanied by UPDATE
– DELETE accompanied by UPDATE
18 31
31 9999
1 18
begin_x end_x
11. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Currency Conversion Table
● Model a currency conversion table
– Each currency has a conversion factor to convert
to/from USD.
– Each entry good for at least one whole day.
– Past entries must be maintained for historical
purposes.
● Each day’s conversion factor based on closing
price of previous day’s market.
12. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Currency Conversion Table
● Based on commonly used models:
create table CurrConv(
Currency varchar( 3 ) NOT NULL (pk), -- ISO 4217 code
From_Date date NOT NULL (pk),
To_Date date NOT NULL default '12319999',
Factor double NOT NULL );
Currency From_Date To_Date Factor
‘JPY’ 1/1/2014 1/3/2014 0.006666667
14. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Currency Conversion Table
select Factor
from CurrConv
where Currency = :Curr -- i.e. 'JPY'
and From_Date <= :AsOf -- i.e. '01/15/2014'
and To_Date > :AsOf
16. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Currency Conversion Table
select MAX(From_Date)
from CurrConv
where Currency = :Curr
and From_Date <= :AsOf;
select Factor
from CurrConv
where Currency = :Curr -- i.e. 'JPY'
and From_Date =(
select MAX(From_Date)
from CurrConv
where Currency = :Curr
and From_Date <= :AsOf);
18. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Conclusion
● Data Integrity Nightmare
– Cannot guarantee non-anomalous data
– Complicated DML
– Queries perform index scans
● Easily fixed
– Just drop the offending column
– Normal DML
– Queries perform index seeks
19. April 5, 2014 Desert Code Camp
Row Spanning Dependency
Questions?
● TommCarr@GMail.com
● Next DDC topic:
– Implementing Bi-Temporal Access to Data
(4:45 – Rm. IRN-128)