Examples

The following examples illustrate both the usefulness and the dangers of using nondeterministic computed columns and index keys. They are intended only for illustrative purposes.


Example 1

The table Renting in this example stores rental information on various properties. It contains these fields:

create table Renting
     (Cust_ID int, Cust_Name varchar(30),
     Formatted_Name compute format_name(Cust_Name),      Property_ID int,Property_Name compute 
     get_pname(Property_ID), start_date compute
     today_date()materialized, Rent_due compute
     rent_calculator(Property_ID, Cust_ID,      Start_Date))

Formatted_Name, Property_Name, Start_Date, and Rent_Due are defined as computed columns.


How the deterministic property affects virtual computed columns

Adaptive Server guarantees repeatable reads on deterministic virtual computed columns, even though, by definition, a virtual computed column is evaluated each time it is referenced. For example, this statement always returns the same result, if the data in the table does not change:

select Cust_ID, Property_ID from Renting
     where Formatted_Name ='RICHARD HUANG'

Adaptive Server does not guarantee repeatable reads on nondeterministic virtual computed columns. For example, in this query, the column Rent_Due returns different results on different days; the column has a serial time property, for which the value is a function of the amount of time that passes between rent payments:

select Cust_Name, Rent_Due from renting 
    where Cust_Name= 'RICHARD HUANG'

The nondeterministic property is useful here, but use it with caution. For instance, if you inadvertently defined Start_Date as a virtual computed column and entered the same query, you would rent all your properties for nothing: Start_Date is always evaluated to the current date, so in this query, the number of Rental_Days is always 0.

Likewise, if you mistakenly define the nondeterministic computed column Rent_Due as a preevaluated column, either by declaring it materialized or by using it as an index key, you would rent your properties for nothing. It is evaluated only once, when the record is inserted, and the number of rental days is 0. This value is returned each time the column is referenced.


How the deterministic property affects materialized computed columns

Adaptive Server guarantees repeatable reads on materialized computed columns, regardless of their deterministic property, because they are not reevaluated when you reference them in a query. Instead, Adaptive Server uses the preevaluated values.

Deterministic materialized computed columns always have the same values, no matter how often they are reevaluated.

Nondeterministic materialized computed columns must adhere to these rules:

In Example 1, Start_Date is a nondeterministic materialized computed column. Its results differ, depending on what day you insert the row. For instance, if the rental period begins on “02/05/04,” “02/05/04” is inserted into the column, and later references to Start_Date use this value. If you reference this value later, on 06/05/04, the query continues to return “02/05/04,” not “06/05/04,” as you would expect if the expression was evaluated every time you query the column.


Example 2

Using the table created in Example 1, Renting: If you create an index on the virtual computed column Property_Name, it becomes a materialized computed column. If you then inserted a new record:

Property_ID=10

This new record calls get_pname(10) from the table Property, executing this JDBC query:

select Property_Name from Property where Property_ID=10

The query returns “Rose Palace,” which is stored in the data row. This all works, unless someone changes the name of the property by issuing:

update Property set Property_Name ='Red Rose Palace'
     where Property_ID = 10

The query returns “Red Rose Palace,” so Adaptive Server stores “Red Rose Palace.” This update command on the table Property invalidates the stored value of Property_Name in the table Renting, which must also be updated to “Red Rose Palace.” Because Property_Name is defined on the column Property_ID in the table Renting, not on the column Property_Name in the table Property, it is not automatically updated. Future reference to Property_Name may produce incorrect results.

To avoid this situation, create a trigger on the table Property:

CREATE TRIGGER my_t ON Property FOR UPDATE AS
     IF UPDATE(Property_Name)
     BEGIN
     UPDATE Renting SET Renting.Property_ID=Property.Property_ID
          FROM Renting, Property
          WHERE Renting.Property_ID=Property.Property_ID
     END

When this trigger updates the column Property_Name in the table Property, it also updates the column Renting.Property_ID, the base column of Property_Name. This automatic update triggers Adaptive Server to reevaluate Property_Name and update the value stored in the data row. Each time Adaptive Server updates the column Property_Name in the table Property, the materialized computed column Property_Name in the table Renting is refreshed, and shows the correct value.