| Lesson 8 | Calculations and Domain Types |
| Objective | List domain types that support calculations. |
An important advantage of using non-text data domains for columns in a relational database is that the RDBMS can perform calculations directly on the stored values. Text domains - CHAR and VARCHAR - store data as character strings. The RDBMS treats these values as text and cannot apply arithmetic or temporal operations to them. Non-text domains tell the RDBMS what kind of value a column holds, which enables it to apply the appropriate operations.
The four domain types that enable calculations on column contents are:
A practical example: to calculate the cost of an order where a customer has a 15% discount coupon, store the retail price in a DECIMAL column and the discount rate in a DECIMAL column, then multiply the retail price by 0.85 (1 - 0.15) to produce the discounted cost. This calculation is only possible because the columns holding price and discount data use numeric domains rather than text.
Numeric domain types are designed to store numerical values and support the full range of arithmetic operations: addition, subtraction, multiplication, division, modulus, and integer division. Three categories of numeric domain types are available in SQL-based RDBMS platforms:
Date and time domain types allow storage and manipulation of temporal values. Most SQL-based RDBMS platforms support arithmetic operations on date and time columns: adding or subtracting intervals, calculating the difference between two dates, and extracting specific components such as day, month, or year.
Date arithmetic has practical value in business applications. If an order was placed on 03/07/00 and shipped on 03/21/00, subtracting the placement date from the shipment date produces a difference of 14 days. A 14-day gap between order and shipment could indicate that the item was out of stock and required a special order from the producer - information that is immediately useful for inventory management and customer service.
Boolean domain types store logical true or false values, typically represented internally as 1 (true) or 0 (false). Boolean columns are not primarily used for arithmetic calculations, but they participate in logical operations - AND, OR, NOT - that are essential for filtering and conditional evaluation in queries. A WHERE clause that combines multiple conditions uses Boolean logic to determine which rows satisfy all the specified criteria.
When you create a calculation in an RDBMS, you use arithmetic operators to tell the system which mathematical operation to perform. The following table lists the standard operators and the operations they represent:
| Symbol | Operation | Example |
| + | Addition | 5 + 2 = 7 |
| - | Subtraction | 5 - 2 = 3 |
| * | Multiplication | 5 * 2 = 10 |
| / | Division | 5 / 2 = 2.5 |
| Mod | Modulo division (remainder division) | 5 Mod 2 = 1 (2 goes into 5 twice, with a remainder of 1) |
| \ | Integer division (remainder discarded) | 5 \ 2 = 2 |
Eight operations were originally defined for relational databases by E.F. Codd as part of the relational algebra. These operations form the theoretical foundation of modern database query languages including SQL. Understanding them clarifies why relational databases are structured the way they are and how query languages achieve their expressive power.
SELECT drink FROM fridge WHERE drink = 'juice' returns only the rows where the
drink column contains 'juice'. Selection filters rows without changing the column structure.
The ability to perform calculations on stored data makes relational databases useful for a wide range of analytical and operational applications:
When a calculation involves columns of different numeric types, the result's data type determines how the value is stored and what precision is retained. Pay careful attention to the data types involved in any calculation that mixes INT and DECIMAL values.
For example, if you subtract a DECIMAL value of 5.25 from an INT value of 35, the result can be handled two ways:
For financial and scientific applications, always use DECIMAL types for columns involved in calculations where precision matters. Storing prices, rates, and quantities as INT to save storage space and then performing arithmetic on them risks precision loss that compounds over many calculations. Always consult your RDBMS documentation for the rules governing cross-domain arithmetic on your specific platform.
Every column in a base table must independently and uniquely store information about the table's subject. A column's value should represent a raw attribute of the entity - data that was observed, entered, or imported, not derived from other columns in the same table.
Calculated fields violate this principle. A LineCost column that is computed by
multiplying Quantity by SellingPrice and then applying a Discount
does not independently store information - it derives its value entirely from other columns. Including
such a column in a base table creates redundancy: the same information is now represented twice (as the
source columns and as the calculated result), and any update to a source column can leave the calculated
column out of sync.
The rule is: never include calculated fields in a base table. Instead, place calculated expressions in user views[1]. A view can define a calculated column as a SELECT expression derived from base table columns, and every query against the view will compute the value from the current source data. User views also serve the secondary purpose of specifying which users are permitted access to specific data in the database. Views are examined in detail later in this course.
The next lesson describes null values[2].
Before moving on to the next lesson, click the Exercise link below to reinforce your understanding
of calculated domains:
Calculation Domain - Exercise