Database Design   «Prev  Next»

Lesson 9 Calculated fields
Objective Explain why calculated fields require user views.

Calculated Fields required for User Views

Question: What are calculated fields within the context of user views in SQL?
Calculated fields, in the context of user views in SQL, refer to the creation of new data fields within a view by applying specific mathematical, logical, or text manipulation operations on existing fields. These fields are not physically stored in the database but are instead computed on-the-fly during query execution. Calculated fields are a powerful tool for data transformation, aggregation, and the derivation of new insights.
A user view, or simply a view, is a virtual table generated by a predefined SQL query, which combines data from one or more underlying tables. Views provide a level of abstraction, allowing users to work with data without the need to understand the underlying table structure or relationships. They can also help enhance security by limiting access to specific data columns. To create a clculated field within a user view, you can use standard SQL expressions and functions to define the new field. Here's a brief overview of different types of operations that can be used for creating calculated fields:
  1. Arithmetic operations: Perform calculations such as addition, subtraction, multiplication, and division on numeric data fields. For example, calculating the total cost of an item by multiplying the unit price by quantity.
  2. Text manipulation: Concatenate, extract, or transform text data using string functions. For example, combining first and last names to create a full name field.
  3. Date and time operations: Perform calculations on date and time fields, such as determining the difference between dates, adding or subtracting time intervals, or extracting specific date components.
  4. Conditional expressions: Use logical operators and conditional functions (e.g., CASE, COALESCE, NULLIF) to create new fields based on specific conditions or to replace missing values with default values.
  5. Aggregation functions: Apply functions such as SUM, COUNT, AVG, MIN, or MAX to summarize or aggregate data based on specific grouping criteria.

Here is an example of creating a user view with calculated fields in SQL:
CREATE VIEW Employee_Salary_Summary AS
    CONCAT(FirstName, ' ', LastName) AS FullName,
    (Salary * 0.10) AS Tax,
    (Salary - (Salary * 0.10)) AS NetSalary

In this example, a user view named Employee_Salary_Summary is created, which includes a calculated field for the full name by concatenating first and last names, as well as tax and net salary fields derived from the original salary field using arithmetic operations.

The tables actually stored in a database (called base tables) cannot have any type of calculations built into their fields. The reason for this is simple: every field in a base table uniquely and independently stores data about the subject of its table. If calculation were permitted in base tables, then the data stored in some fields would depend for their values on data stored in other fields.
User views, however, store their data in virtual tables[1] that consist of fields loaded into computer memory from base tables. Virtual tables themselves are not stored in the database; rather, the definition of the view is stored and given a name. Users call up that name, and the view is created (from base tables) on the fly. When a user closes the view, it disappears from memory, only to be recreated the next time its name is invoked. Users call up that name, and the view is created (from base tables) on the fly. When a user closes the view, it “disappears” from memory, only to be recreated the next time its name is invoked.

User view allows Calculations

A user view (like a database application) allows calculations to be performed on the data contained in its fields. That is because these field calculations within virtual tables have no effect on the data stored in the base tables. One significant reason to create user views, then, is because, through calculations, views can deliver extremely useful information. The next lesson describes the documents created during Requirements Analysis.

Calculated Fields - Exercise

Before moving on to the next lesson, click the Exercise link below to check your understanding of calculated fields.
Calculated Fields - Exercise
[1]virtual table: A table stored in the computer’s memory.