Microsoft SQL Server 2008 Bible

All numeric data could generally be divided into two categories: exact numbers and approximate numbers.

Exact numbers

Exact numbers can either be whole integers (numeric primary keys, quantities, such as number of items ordered, age) or have decimal points (prices, weights, percentages). Numbers can be positive and negative and have precision and scale. Precision determines the maximum total number of decimal digits that can be stored (both to the left and to the right of the decimal point). Scale specifies the maximum number of decimals allowed. Exact numeric data types are summarized in Table 3-3.

Table 3-3: Exact Numeric Data Types

SQL99

Oracle 9i

DB2 UDB 8.1

MS SQLSERVER 2000

INT[EGER]

NUMBER(38)

INT[EGER]

INT[EGER]

  

BIGINT

BIGINT

SMALLINT

SMALLINT

SMALLINT

SMALLINT

 

NUMBER(38)

 

TINYINT

NUMERIC[(p[,s])] OR DEC[IMAL][(p[,s])]

NUMERIC[(p[,s])]

NUMERIC[(p[,s])]

NUMERIC[(p[,s])]

 

DEC[IMAL] [(p[,s])]

DEC[IMAL] [(p[,s])]

DEC[IMAL] [(p[,s])]

 

NUMBER[(p[,s])

 

MONEY

   

SMALLMONEY

SQL99

SQL99 specifies the following data types for exact numbers: INTEGER, SMALLINT, NUMERIC, DECIMAL (as well as some synonyms found in Table 3-3).

Oracle 9i

Oracle has one data type, NUMBER, to represent all numeric data and numerous synonyms for it to comply with SQL99 (see Table 3-3). INTEGER and SMALLINT will translate into NUMBER(38); NUMERIC and DECIMAL will be substituted with NUMBER. The NUMBER data type stores zero, positive, and negative fixed and floating-point numbers with magnitudes between 1.0 * 10–130 and 9.9...9 * 10125 with 38 digits of precision. The space is allocated dynamically, so Oracle claims having one numeric data type for all numeric data won't hurt performance.

DB2 UDB 8.1

DB2 has four data types for exact numbers: INTEGER, SMALLINT, BIGINT, and DOUBLE.

MS SQL Server 2000

MS SQL Server has more numeric data types for exact numeric data than Oracle and DB2. In addition to INT, BIGINT, SMALLINT, and TINYINT it also offers MONEY and SMALLMONEY.

Literals for exact numbers

Literals for exact numbers are represented by string of numbers optionally preceded by plus or minus signs with an optional decimal part for NUMERIC and DECIMAL data types separated by a dot (.):

123 -33.45 +334.488

Oracle optionally allows enclosing literals in single quotes:

'123' '-677.34'

Note 

MS SQL Server has literal formats for MONEY and SMALLMONEY data types represented as strings of numbers with an optional decimal point optionally prefixed with a currency symbol:

$12 $542023.14

Selecting Correct Data Types

The incorrect use of data types is quite typical for inexperienced database developers and can result in serious problems.

For example, defining a money-related field as a FLOAT or NUMERIC(12,1) causes rounding errors. (Accountants are just going to hate you!) Insufficient precision for a primary key column (say, ORDHDR_ID_N NUMBER(5) in an ORDER_HEADER table) will work for a while, but after inserting a certain number of records (99,999 in our case), you will not be able to insert new rows anymore — the next value for the primary key (100,000) won't fit NUMBER(5) precision.

The last example is easily fixable — the precision of a numeric column can easily be adjusted (more details in the next chapter). That is one of the benefits of a relational database over the old legacy systems. But still, it might take some time to figure out what causes the problem and fix it, and if your database is, for example, a large 24/7 order management system, your users are not going to be happy with the delay.

Approximate numbers

Approximate numbers are numbers that cannot be represented with absolute precision (or don't have a precise value). Approximate numeric data types are summarized in Table 3-4.

Table 3-4: Approximate Numeric Data Types

SQL99

Oracle 9i

DB2 UDB 8.1

MS SQL SERVER 2000

FLOAT[(p)]

FLOAT[(p)] NUMBER

FLOAT[(p)]

FLOAT[(p)]

REAL

REAL NUMBER

REAL

REAL

DOUBLE PRECISION

DOUBLE PRECISION NUMBER

DOUBLE [PRECISION]

DOUBLE PRECISION

Note 

A classic example is number p, which is usually approximated to 3.14. The number was known in ancient Babylon and Egypt some 4,500 years ago and has been a matter of interest for mathematicians from Archimedes to modern scientists. As of today, 206,158,430,208 (3 * 236) decimal digits of p have been calculated. It would take approximately forty million pages, or fifty thousand volumes to store it in written form!

SQL99

SQL99 specifies the following data types for approximate numbers: FLOAT, REAL, and DOUBLE PRECISION.

Oracle 9i

As we already know, Oracle has one numeric data type, NUMBER, for both exact and approximate numbers. Another supported data type is FLOAT, which is mostly used to represent binary precision. The maximum decimal precision for FLOAT is 38; maximum binary precision is 126.

Note 

In addition to positive precision, Oracle allows negative precision as well. For example, if you have a column specified as NUMBER(10, –2), all inserted values will be implicitly rounded to the second significant digit. For example, 6,345,454,454.673 will be stored as 6,345,454,500

DB2 UDB 8.1

DB2 has REAL single-precision data type as well as DOUBLE double-precision data type for approximate numbers. FLOAT is a synonym to DOUBLE.

MS SQL Server 2000

MS SQL Server has one data type for floating-point numbers — FLOAT. It also has a number of synonyms for SQL99 compliance (Table 3-4).

FLOAT data type can hold the same range of real numbers as DOUBLE in DB2. The actual storage size can be either four or eight bytes.

Literals for approximate numbers

In addition to literals for exact numbers you can specify a real number as two numbers separated by upper- or lowercase character E (scientific notation). Both numbers may include plus or minus; the first number may also include a decimal point:

+1.23E2 -3.345e1 -3.44488E+002

The value of the constant is the product of the first number and the power of 10 specified by the second number.

Категории