I'd like to find out the correspondence between XML Schema totalDigits
/fractionDigits
and SQL numeric precision/scale.
1) Assume we have the following simple type:
<xs:simpleType name="DecimalNumber">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="17"/>
<xs:totalDigits value="18"/>
</xs:restriction>
</xs:simpleType>
How would I represent it in SQL? Let's assume HSQL dialect (it does not matter much). I'm interested in the most restrictive SQL type which could hold all the values from the value space of the XML Schema simple type.
Would it be numeric(18,17)
? Or numeric(35,17)
?
2) What if we have just totalDigits
?
<xs:simpleType name="DecimalNumber">
<xs:restriction base="xs:decimal">
<xs:totalDigits value="18"/>
</xs:restriction>
</xs:simpleType>
What would be an adequate representation in SQL?
3) Or just fractionDigits
?
<xs:simpleType name="DecimalNumber">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="17"/>
</xs:restriction>
</xs:simpleType>
xs:fractionDigits defines the maximum number of fractional digits (i.e., digits that are after the decimal point) of an xs:decimal datatype.
XSD Restrictions/Facets Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
xs:integer is the only datatype directly derived from xs:decimal. This integer datatype is a subset of xs:decimal, representing numbers which don't have any fractional digits in its lexical or value spaces.
Start with this in mind:
scale ≡ fractionDigits
precision ≡ totalDigits
In HSQL:
DECIMAL (35, 17) -- edit: fractionDigits is an upper bound; in your case from none to 17. Hence, the domain of values to be represented ranges from 18 digits on the left, to 17 on the right (of the decimal point). So you need a scale of 17, and (18 + 17) digits to be able to represent the max values: 18 digits with no decimal digits.
A naïve translation to DECIMAL (18) really means 18 digits without a decimal point since the scale is defaulted to 0 - not XSD's equivalent. To allow capturing all numbers, one would need to code DECIMAL (36, 18) which would exceed, in terms of domain values, what's representable by XSD's 18 total digits. You'll have to make a call here.
In SQL, I am not aware of a way to specify scale without precision. Since XSD doesn't provide an upper limit (it asks for a minimum of 18)... you may have to just choose a number, maybe as limited by your infrastructure (e.g. MS-SQL has max 38 precision), or don't bother at all (Apache's HsqlDB uses unlimited precision and scale).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With