INFORMIX
Informix Guide to SQL: Syntax
Chapter 1: SQL Statements
Home Contents Index Master Index New Book

SQL Statements

CREATE TRIGGER

Use the CREATE TRIGGER statement to create a trigger on a table in the database. A trigger is a database object that automatically sets off a specified set of SQL statements when a specified event occurs.

Syntax

Usage

You must be either the owner of the table or have DBA status to create a trigger on a table.

You can use roles with triggers. Role-related statements (CREATE ROLE, DROP ROLE, and SET ROLE) and SET SESSION AUTHORIZATION statements can be triggered inside a trigger. Privileges that a user has acquired through enabling a role or through a SET SESSION AUTHORIZATION statement are not relinquished when a trigger is executed.

You can define a trigger with a stand-alone CREATE TRIGGER statement.

DB
You can define a trigger as part of a schema by placing the CREATE TRIGGER statement inside a CREATE SCHEMA statement.

You can create a trigger only on a table in the current database. You cannot create a trigger on a temporary table, a view, or a system catalog table.

You cannot create a trigger inside a stored procedure if the procedure is called inside a data manipulation statement. For example, you cannot create a trigger inside the stored procedure sp_items in the following INSERT statement:

See "Data Manipulation Statements" for a list of data manipulation statements.

ESQL
If you are embedding the CREATE TRIGGER statement in an ESQL/C program, you cannot use a host variable in the trigger specification.

You cannot use a stored procedure variable in a CREATE TRIGGER statement.

Trigger Event

The trigger event specifies the type of statement that activates a trigger. The trigger event can be an INSERT, DELETE, or UPDATE statement. Each trigger can have only one trigger event. The occurrence of the trigger event is the triggering statement.

For each table, you can define only one trigger that is activated by an INSERT statement and only one trigger that is activated by a DELETE statement. For each table, you can define multiple triggers that are activated by UPDATE statements. See "UPDATE Clause" for more information about multiple triggers on the same table.

You cannot define a DELETE trigger event on a table with a referential constraint that specifies ON DELETE CASCADE.

You are responsible for guaranteeing that the triggering statement returns the same result with and without the triggered actions. See "Action Clause" and "Triggered Action List" for more information on the behavior of triggered actions.

If Universal Server is the database server, a triggering statement from an external database server can activate the trigger. As shown in the following example, an insert trigger on newtab, managed by dbserver1, is activated by an INSERT statement from dbserver2. The trigger executes as if the insert originated on dbserver1.

Trigger Events with Cursors
If the triggering statement uses a cursor, the complete trigger is activated each time the statement executes. For example, if you declare a cursor for a triggering INSERT statement, each PUT statement executes the complete trigger. Similarly, if a triggering UPDATE or DELETE statement contains the clause WHERE CURRENT OF, each update or delete activates the complete trigger. This behavior is different from what occurs when a triggering statement does not use a cursor and updates multiple rows. In this case, the set of triggered actions executes only once. For more information on the execution of triggered actions, see "Action Clause".

Privileges on the Trigger Event
You must have the appropriate Insert, Delete, or Update privilege on the triggering table to execute the INSERT, DELETE, or UPDATE statement that is the trigger event. The triggering statement might still fail, however, if you do not have the privileges necessary to execute one of the SQL statements in the action clause. When the triggered actions are executed, the database server checks your privileges for each SQL statement in the trigger definition as if the statement were being executed independently of the trigger. For information on the privileges you need to execute a trigger, see "Privileges to Execute Triggered Actions".

Impact of Triggers

The INSERT, DELETE, and UPDATE statements that initiate triggers might appear to execute slowly because they activate additional SQL statements, and the user might not know that other actions are occurring.

The execution time for a triggering data manipulation statement depends on the complexity of the triggered action and whether it initiates other triggers. Obviously, the elapsed time for the triggering data manipulation statement increases as the number of cascading triggers increases. For more information on triggers that initiate other triggers, see "Cascading Triggers".

Trigger Name

Element Purpose Restrictions Syntax

owner

The user name of the owner of the trigger

The specified name must be a valid user name.

Identifier, p. 1-966

When you create a trigger, the name of the trigger must be unique within a database.

ANSI
When you create a trigger, the owner.name combination (the combination of the owner name and trigger name) must be unique within a database.

For information about the relationship between the trigger owner's privileges and the privileges of other users, see "Privileges to Execute Triggered Actions".

UPDATE Clause

Element Purpose Restrictions Syntax

column name

The name of a column or columns that activate the trigger. The default is all the columns in the table on which you create the trigger.

The specified columns must belong to the table on which you create the trigger. If you define more than one update trigger on a table, the column lists of the triggering statements must be mutually exclusive.

Identifier, p. 1-966

If the trigger event is an UPDATE statement, the trigger executes when any column in the triggering column list is updated.

If the triggering UPDATE statement updates more than one of the triggering columns in a trigger, the trigger executes only once.

Defining Multiple Update Triggers

If you define more than one update trigger event on a table, the column lists of the triggers must be mutually exclusive. The following example shows that trig3 is illegal on the items table because its column list includes stock_num, which is a triggering column in trig1. Multiple update triggers on a table cannot include the same columns.

When an UPDATE Statement Activates Multiple Triggers

When an UPDATE statement updates multiple columns that have different triggers, the column numbers of the triggering columns determine the order of trigger execution. Execution begins with the smallest triggering column number and proceeds in order to the largest triggering column number. The following example shows that table taba has four columns (a, b, c, d):

Define trig1 as an update on columns a and c, and define trig2 as an update on columns b and d, as the following example shows:

The triggering statement is shown in the following example:

Then trig1 for columns a and c executes first, and trig2 for columns b and d executes next. In this case, the smallest column number in the two triggers is column 1 (a), and the next is column 2 (b).

Action Clause

The action clause defines the characteristics of triggered actions and specifies the time when these actions occur. You must define at least one triggered action, using the keywords BEFORE, FOR EACH ROW, or AFTER to indicate when the action occurs relative to the triggering statement. You can specify triggered actions for all three options on a single trigger, but you must order them in the following sequence: BEFORE, FOR EACH ROW, and AFTER. You cannot follow a FOR EACH ROW triggered action list with a BEFORE triggered action list. If the first triggered action list is FOR EACH ROW, an AFTER action list is the only option that can follow it. See "Action Clause Referencing" for more information on the action clause when a REFERENCING clause is present.

BEFORE Actions

The BEFORE triggered action or actions execute once before the triggering statement executes. If the triggering statement does not process any rows, the BEFORE triggered actions still execute because the database server does not yet know whether any row is affected.

FOR EACH ROW Actions

The FOR EACH ROW triggered action or actions execute once for each row that the triggering statement affects. The triggered SQL statement executes after the triggering statement processes each row.

If the triggering statement does not insert, delete, or update any rows, the FOR EACH ROW triggered actions do not execute.

AFTER Actions

An AFTER triggered action or actions execute once after the action of the triggering statement is complete. If the triggering statement does not process any rows, the AFTER triggered actions still execute.

Actions of Multiple Triggers

When an UPDATE statement activates multiple triggers, the triggered actions merge. Assume that taba has columns a, b, c, and d, as the following example shows:

Next, assume that you define trig1 on columns a and c, and trig2 on columns b and d. If both triggers have triggered actions that are executed BEFORE, FOR EACH ROW, and AFTER, the triggered actions are executed in the following sequence:

    1. BEFORE action list for trigger (a, c)

    2. BEFORE action list for trigger (b, d)

    3. FOR EACH ROW action list for trigger (a, c)

    4. FOR EACH ROW action list for trigger (b, d)

    5. AFTER action list for trigger (a, c)

    6. AFTER action list for trigger (b, d)

The database server treats the triggers as a single trigger, and the triggered action is the merged-action list. All the rules governing a triggered action apply to the merged list as one list, and no distinction is made between the two original triggers.

Guaranteeing Row-Order Independence

In a FOR EACH ROW triggered-action list, the result might depend on the order of the rows being processed. You can ensure that the result is independent of row order by following these suggestions:

The database server does not enforce rules to prevent these situations because doing so would restrict the set of tables from which a triggered action can select. Furthermore, the result of most triggered actions is independent of row order. Consequently, you are responsible for ensuring that the results of the triggered actions are independent of row order.

INSERT REFERENCING Clause

Element Purpose Restrictions Syntax

correlation name

A name that you assign to a new column value so that you can refer to it within the triggered action. The new column value in the triggering table is the value of the column after execution of the triggering statement.

The correlation name must be unique within the CREATE TRIGGER statement.

Identifier, p. 1-966

Once you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See "Action Clause Referencing".

To use the correlation name, precede the column name with the correlation name, followed by a period. For example, if the new correlation name is post, refer to the new value for the column fname as post.fname.

If the trigger event is an INSERT statement, using the old correlation name as a qualifier causes an error because no value exists before the row is inserted. For the rules that govern the use of correlation names, see "Using Correlation Names in Triggered Actions".

You can use the INSERT REFERENCING clause only if you define a FOR EACH ROW triggered action.

The following example illustrates the use of the INSERT REFERENCING clause. This example inserts a row into backup_table1 for every row that is inserted into table1. The values that are inserted into col1 and col2 of backup_table1 are an exact copy of the values that were just inserted into table1.

As the preceding example shows, the advantage of the INSERT REFERENCING clause is that it allows you to refer to the data values that the trigger event in your triggered action produces.

DELETE REFERENCING Clause

Element Purpose Restrictions Syntax

correlation name

A name that you assign to an old column value so that you can refer to it within the triggered action. The old column value in the triggering table is the value of the column before execution of the triggering statement.

The correlation name must be unique within the CREATE TRIGGER statement.

Identifier, p. 1-966

Once you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See "Action Clause Referencing".

Use the correlation name to refer to an old column value by preceding the column name with the correlation name and a period (.). For example, if the old correlation name is pre, refer to the old value for the column fname as pre.fname.

If the trigger event is a DELETE statement, using the new correlation name as a qualifier causes an error because the column has no value after the row is deleted. See "Using Correlation Names in Triggered Actions" for the rules governing the use of correlation names.

You can use the DELETE REFERENCING clause only if you define a FOR EACH ROW triggered action.

UPDATE REFERENCING Clause

Element Purpose Restrictions Syntax

correlation name

A name that you assign to an old or new column value so that you can refer to it within the triggered action. The old column value in the triggering table is the value of the column before execution of the triggering statement. The new column value in the triggering table is the value of the column after the statement executes.

You can specify a correlation name for an old column value only (OLD option), for a new column value only (NEW option), or for both the old and new column values. Each correlation name you specify must be unique within the CREATE TRIGGER statement.

Identifier, p. 1-966

After you assign a correlation name, you can use it only inside the FOR EACH ROW triggered action. See "Action Clause Referencing".

Use the correlation name to refer to an old or new column value by preceding the column name with the correlation name and a period (.). For example, if the new correlation name is post, you refer to the new value for the column fname as post.fname.

If the trigger event is an UPDATE statement, you can define both old and new correlation names to refer to column values before and after the triggering update. See "Using Correlation Names in Triggered Actions" for the rules that govern the use of correlation names.

You can use the UPDATE REFERENCING clause only if you define a FOR EACH ROW triggered action.

Action Clause Referencing

If the CREATE TRIGGER statement contains an INSERT REFERENCING clause, a DELETE REFERENCING clause, or an UPDATE REFERENCING clause, you must include a FOR EACH ROW triggered-action list in the action clause. You can also include BEFORE and AFTER triggered-action lists, but they are optional. See "Action Clause" for information on the BEFORE, FOR EACH ROW, and AFTER triggered-action lists.

Triggered Action List

The triggered action consists of an optional WHEN condition and the action statements. Objects that are referenced in the triggered action, that is, tables, columns, and stored procedures, must exist when the CREATE TRIGGER statement is executed. This rule applies only to objects that are referenced directly in the trigger definition.

Warning: When you specify a date expression in the WHEN condition or in an action statement, make sure to specify 4 digits instead of 2 digits for the year. When you specify a 4-digit year, the DBCENTURY environment variable has no effect on how the database server interprets the date expression. When you specify a 2-digit year, the DBCENTURY environment variable can affect how the database server interprets the date expression, so the triggered action might produce unpredictable results. See the "Informix Guide to SQL: Syntax" for more information on the DBCENTURY environment variable.

WHEN Condition

The WHEN condition lets you make the triggered action dependent on the outcome of a test. When you include a WHEN condition in a triggered action, if the triggered action evaluates to true, the actions in the triggered action list execute in the order in which they appear. If the WHEN condition evaluates to false or unknown, the actions in the triggered action list are not executed. If the triggered action is in a FOR EACH ROW section, its search condition is evaluated for each row.

For example, the triggered action in the following trigger executes only if the condition in the WHEN clause is true:

A routine that executes inside the WHEN condition carries the same restrictions as a routine that is called in a data manipulation statement. See the Extending INFORMIX-Universal Server: User-Defined Routines manual for more information about a routine that is called within a data manipulation statement.

Action Statements

The triggered-action statements can be INSERT, DELETE, UPDATE, or EXECUTE PROCEDURE statements. If a triggered-action list contains multiple statements, these statements execute in the order in which they appear in the list.

Achieving a Consistent Result
To guarantee that the triggering statement returns the same result with and without the triggered actions, make sure that the triggered actions in the BEFORE and FOR EACH ROW sections do not modify any table referenced in the following clauses:

Using Keywords
If you use the INSERT, DELETE, UPDATE, or EXECUTE keywords as an identifier in any of the following clauses inside a triggered action list, you must qualify them by the owner name, the table name, or both:

You get a syntax error if these keywords are not qualified when you use these clauses inside a triggered action.

If you use the keyword as a column name, it must be qualified by the table name-for example, table.update. If both the table name and the column name are keywords, they must be qualified by the owner name-for example, owner.insert.update. If the owner name, table name, and column name are all keywords, the owner name must be in quotes-for example, 'delete'.insert.update. The only exception is when these keywords are the first table or column name in the list, and you do not have to qualify them. For example, delete in the following statement does not need to be qualified because it is the first column listed in the INTO clause:

The following statements show examples in which you must qualify the column name or the table name:

FROM clause of a SELECT statement

INTO clause of an EXECUTE PROCEDURE statement

GROUP BY clause of a SELECT statement

SET clause of an UPDATE statement

Using Correlation Names in Triggered Actions

The following rules apply when you use correlation names in triggered actions:

When to Use Correlation Names

In an SQL statement in a FOR EACH ROW triggered action, you must qualify all references to columns in the triggering table with either the old or new correlation name, unless the statement is valid independent of the triggered action.

In other words, if a column name inside a FOR EACH ROW triggered action list is not qualified by a correlation name, even if it is qualified by the triggering table name, it is interpreted as if the statement is independent of the triggered action. No special effort is made to search the definition of the triggering table for the nonqualified column name.

For example, assume that the following DELETE statement is a triggered action inside the FOR EACH ROW section of a trigger:

For the statement to be valid, both col_c and col_c2 must be columns from tab1. If col_c2 is intended to be a correlation reference to a column in the triggering table, it must be qualified by either the old or the new correlation name. If col_c2 is not a column in tab1 and is not qualified by either the old or new correlation name, you get an error.

When a column is not qualified by a correlation name, and the statement is valid independent of the triggered action, the column name refers to the current value in the database. In the triggered action for trigger t1 in the following example, mgr in the WHERE clause of the correlated subquery is an unqualified column from the triggering table. In this case, mgr refers to the current column value in empsal because the INSERT statement is valid independent of the triggered action.

In a triggered action, an unqualified column name from the triggering table refers to the current column value, but only when the triggered statement is valid independent of the triggered action.

Qualified Versus Unqualified Value

The following table summarizes the value retrieved when you use the column name qualified by the old correlation name and the column name qualified by the new correlation name.

Trigger Event old.col new.col

INSERT

no value (error)

inserted value

UPDATE

(column updated)

original value

current value (N)

UPDATE

(column not updated)

original value

current value (U)

DELETE

original value

no value (error)

Refer to the following key when you read the table.

Term Meaning
original value

is the value before the triggering statement.

current value

is the value after the triggering statement.

(N)

cannot be changed by triggered action.

(U)

can be updated by triggered statements; value may be different from original value because of preceding triggered actions.

Outside a FOR EACH ROW triggered-action list, you cannot qualify a column from the triggering table with either the old correlation name or the new correlation name; it always refers to the current value in the database.

Action on the Triggering Table
You cannot reference the triggering table in any triggered SQL statement, with the following exceptions:

    For example, assume that the following UPDATE statement, which updates columns a and b of tab1, is the triggering statement:

    Now consider the triggered actions in the following example. The first UPDATE statement is a valid triggered action, but the second one is not because it updates column b again.

This rule, which states that a triggered SQL statement cannot reference the triggering table, with the two noted exceptions, applies recursively to all cascading triggers, which are considered part of the initial trigger. This situation means that a cascading trigger cannot update any columns in the triggering table that were updated by the original triggering statement, including any nontriggering columns affected by that statement. For example, assume the following UPDATE statement is the triggering statement:

Then in the cascading triggers shown in the following example, trig2 fails at runtime because it references column b, which is updated by the triggering UPDATE statement. See "Cascading Triggers" for more information about cascading triggers.

Rules for Procedures

The following rules apply to a procedure that is used as a triggered action:

When you use a procedure as a triggered action, the objects that it references are not checked until the procedure is executed.

Privileges to Execute Triggered Actions

If you are not the trigger owner, but the trigger owner's privileges include the WITH GRANT OPTION privilege, you inherit the owner's privileges as well as the WITH GRANT OPTION privilege for each triggered SQL statement. You have these privileges in addition to your privileges.

If the triggered action is an SPL or external routine, you must have the Execute privilege on the routine or the owner of the trigger must have the Execute privilege and the WITH GRANT OPTION privilege.

While executing the routine, you do not carry the privileges of the trigger owner; instead you receive the privileges granted with the routine, as follows:

    1. Privileges for a DBA routine

    When the routine is registered with the CREATE DBA keywords and you are granted the Execute privilege on the routine, the database server automatically grants you temporary DBA privileges while the routine executes. These DBA privileges are available only when you are executing the routine.

    2. Privileges for a routine without DBA restrictions

    If the routine owner has the WITH GRANT OPTION right for the necessary privileges on the underlying objects, you inherit these privilege when you are granted the Execute privilege. In this case, all the nonqualified objects that the Routine references are qualified by the name of the Routine owner.

For more information on privileges on routines, see Chapter 14 in the Informix Guide to SQL: Tutorial.

Creating a Triggered Action That Anyone Can Use
To create a trigger that is executable by anyone who has the privileges to execute the triggering statement, you can ask the DBA to create a DBA-privileged procedure and grant you the Execute privilege with the WITH GRANT OPTION right. You then use the DBA-privileged procedure as the triggered action. Anyone can execute the triggered action because the DBA-privileged procedure carries the WITH GRANT OPTION right. When you activate the procedure, the database server applies privilege-checking rules for a DBA. For more information about privileges on stored procedures, see Chapter 14 of the Informix Guide to SQL: Tutorial.

Cascading Triggers

The database server allows triggers to cascade, meaning that the triggered actions of one trigger can activate another trigger. The maximum number of triggers in a cascading sequence is 61; the initial trigger plus a maximum of 60 cascading triggers. When the number of cascading triggers in a series exceeds the maximum, the database server returns error number -748, as the following example shows:

The following example illustrates a series of cascading triggers that enforce referential integrity on the manufact, stock, and items tables in the stores7 database. When a manufacturer is deleted from the manufact table, the first trigger, del_manu, deletes all the items from that manufacturer from the stock table. Each delete in the stock table activates a second trigger, del_items, that deletes all the items from that manufacturer from the items table. Finally, each delete in the items table triggers the stored procedure log_order, which creates a record of any orders in the orders table that can no longer be filled.

When you are not using logging, referential integrity constraints on both the manufact and stock tables would prohibit the triggers in this example from executing. When you use INFORMIX-Universal Server with logging, however, the triggers execute successfully because constraint checking is deferred until all the triggered actions are complete, including the actions of cascading triggers. See "Constraint Checking" for more information about how constraints are handled when triggers execute.

The database server prevents loops of cascading triggers by not allowing you to modify the triggering table in any cascading triggered action, except an UPDATE statement, which does not modify any column that the triggering UPDATE statement updated.

Constraint Checking

When you use logging, INFORMIX-Universal Server defers constraint checking on the triggering statement until after the statements in the triggered-action list execute. Universal Server effectively executes a SET statement (SET CONSTRAINTS ALL DEFERRED) before it executes the triggering statement. After the triggered action is completed, it effectively executes another SET statement (SET CONSTRAINTS constr_name IMMEDIATE) to check the constraints that were deferred. This action allows you to write triggers so that the triggered action can resolve any constraint violations that the triggering statement creates. For more information, see the SET statement on page 1-647.

Consider the following example, in which the table child has constraint r1, which references the table parent. You define trigger trig1 and activate it with an INSERT statement. In the triggered action, trig1 checks to see if parent has a row with the value of the current cola in child; if not, it inserts it.

When you insert a row into a table that is the child table in a referential constraint, the row might not exist in the parent table. The database server does not immediately return this error on a triggering statement. Instead, it allows the triggered action to resolve the constraint violation by inserting the corresponding row into the parent table. As the previous example shows, you can check within the triggered action to see whether the parent row exists, and if so, bypass the insert.

For a database without logging, Universal Server does not defer constraint checking on the triggering statement. In this case, it immediately returns an error if the triggering statement violates a constraint.

Universal Server does not allow the SET statement in a triggered action. Universal Server checks this restriction when you activate a trigger because the statement could occur inside a stored procedure.

Preventing Triggers from Overriding Each Other

When you activate multiple triggers with an UPDATE statement, a trigger can possibly override the changes that an earlier trigger made. If you do not want the triggered actions to interact, you can split the UPDATE statement into multiple UPDATE statements, each of which updates an individual column. As another alternative, you can create a single update trigger for all columns that require a triggered action. Then, inside the triggered action, you can test for the column being updated and apply the actions in the desired order. This approach, however, is different than having the database server apply the actions of individual triggers, and it has the following disadvantages:

Client/Server Environment

In an Universal Server database, the statements inside the triggered action can affect tables in external databases. The following example shows an update trigger on dbserver1, which triggers an update to items on dbserver2:

If a statement from an external database server initiates the trigger, however, and the triggered action affects tables in an external database, the triggered actions fail. For example, the following combination of triggered action and triggering statement results in an error when the triggering statement executes:

Logging and Recovery

You can create triggers for databases, with and without logging. However, when the database does not have logging, you cannot roll back when the triggering statement fails. In this case, you are responsible for maintaining data integrity in the database.

In INFORMIX-Universal Server, if the trigger fails and the database has transactions, all triggered actions and the triggering statement are rolled back because the triggered actions are an extension of the triggering statement. The rest of the transaction, however, is not rolled back.

The row action of the triggering statement occurs before the triggered actions in the FOR EACH ROW section. If the triggered action fails for a database without logging, the application must restore the row that was changed by the triggering statement to its previous value.

When you use a stored procedure as a triggered action, if you terminate the procedure in an exception-handling section, any actions that modify data inside that section are rolled back along with the triggering statement. In the following partial example, when the exception handler traps an error, it inserts a row into the table logtab:

When the RAISE EXCEPTION statement returns the error, however, the database server rolls back this insert because it is part of the triggered actions. If the procedure is executed outside a triggered action, the insert is not rolled back.

The stored procedure that implements a triggered action cannot contain any BEGIN WORK, COMMIT WORK, or ROLLBACK WORK statements. If the database has logging, you must either begin an explicit transaction before the triggering statement, or the statement itself must be an implicit transaction. In any case, another transaction-related statement cannot appear inside the stored procedure.

You can use triggers to enforce referential actions that the database server does not currently support. For any database without logging, you are responsible for maintaining data integrity when the triggering statement fails.

Trigger Object Modes

The Trigger Object Modes option allows you to create a trigger in either the enabled or disabled object mode.

You can create triggers in the following object modes.

Object Mode Effect

disabled

When a trigger is created in disabled mode, the database server does not execute the triggered action when the trigger event (an insert, delete, or update operation) takes place. In effect, the database server ignores the trigger even though its catalog information is maintained.

enabled

When a trigger is created in enabled mode, the database server executes the triggered action when the trigger event (an insert, delete, or update operation) takes place.

Specifying Object Modes for Triggers

You must observe the following rules when you specify the object mode for a trigger in the CREATE TRIGGER statement:

References

See the DROP TRIGGER, CREATE PROCEDURE, and EXECUTE PROCEDURE statements in this manual.

In the Informix Guide to SQL: Tutorial, see Chapter 14 for information about stored procedures.

CREATE VIEW

Use the CREATE VIEW statement to create a new view that is based upon existing tables and views in the database.

Syntax

Element Purpose Restrictions Syntax

row type name

The name of a named row type that you use to specify the type of a typed view

You must have USAGE privileges on the named row type or be its owner or the DBA. The named row type must exist before you can assign it to a view.

Data Type, p. 1-859

column name

The name of a column in the view being created

See "Naming View Columns".

Identifier, p. 1-966

Usage

You can create typed or untyped views. If you omit the OF TYPE clause, the rows in the view are considered to be untyped and default to an unnamed row type.

Typed views, like typed tables, are based on a named row type. Each column in the view corresponds to a field in the named row type.

You can use a view in any SQL statement where you can use a table, except the following.

ALTER FRAGMENT

DROP TABLE

ALTER INDEX

DROP TRIGGER

ALTER TABLE

LOCK TABLE

CREATE INDEX

RECOVER TABLE

CREATE TABLE

RENAME TABLE

CREATE TRIGGER

UNLOCK TABLE

DROP INDEX

The view behaves like a table that is called view name. It consists of the set of rows and columns that the SELECT statement returns each time the SELECT statement is executed by using the view. The view reflects changes to the underlying tables with one exception. If a SELECT * clause defines the view, the view has only the columns in the underlying tables at the time the view is created. New columns that are subsequently added to the underlying tables with the ALTER TABLE statement do not appear in the view.

The view name must be unique; that is, a view name cannot have the same name as another database object, such as a table, synonym, or temporary table.

The view inherits the data types of the columns from the tables from which they come. Data types of virtual columns are determined from the nature of the expression.

To create a view, you must have the Select privilege on all columns from which the view is derived.

The SELECT statement is stored in the sysviews system catalog table. When you subsequently refer to a view in another statement, the database server performs the defining SELECT statement while it executes the new statement.

You cannot create a view on a temporary table.

DB
If you create a view outside the CREATE SCHEMA statement, you receive warnings if you use the -ansi flag or set DBANSIWARN.

Subset of a SELECT Allowed in CREATE VIEW

The SELECT statement has the form that is described on page 1-596, but in CREATE VIEW, it cannot have an ORDER BY clause, INTO TEMP clause, or UNION operator. Do not use display labels in the select list; display labels are interpreted as column names.

Naming View Columns

The number of columns that you specify in the column name parameter must match the number of columns returned by the SELECT statement that defines the view.

If you do not specify a list of columns, the view inherits the column names of the underlying tables. In the following example, the view herostock has the same column names as the ones in the SELECT statement:

If the SELECT statement returns an expression, the corresponding column in the view is called a virtual column. You must provide a name for virtual columns. You must also provide a column name in cases where the selected columns have duplicate column names when the table prefixes are stripped. For example, when both orders.order_num and items.order_num appear in the SELECT statement, you must provide two separate column names to label them in the CREATE VIEW statement, as the following example shows:

If you must provide names for some of the columns in a view, then you must provide names for all the columns; that is, the column list must contain an entry for every column that appears in the view.

Using a View in the SELECT Statement

You can define a view in terms of other views, but you must abide by the restrictions on creating views that are listed in Chapter 11 of the Informix Guide to SQL: Tutorial. See that manual for further information.

WITH CHECK OPTION Keywords

The WITH CHECK OPTION keywords instruct the database server to ensure that all modifications that are made through the view to the underlying tables satisfy the definition of the view.

The following example creates a view that is named palo_alto, which uses all the information in the customer table for customers in the city of Palo Alto. The database server checks any modifications made to the customer table through palo_alto because the WITH CHECK OPTION is specified.

What do the WITH CHECK OPTION keywords really check and prevent? It is possible to insert into a view a row that does not satisfy the conditions of the view (that is, a row that is not visible through the view). It is also possible to update a row of a view so that it no longer satisfies the conditions of the view. For example, if the view was created without the WITH CHECK OPTION keywords, you could insert a row through the view where the city is Los Altos, or you could update a row through the view by changing the city from Palo Alto to Los Altos.

To prevent such inserts and updates, you can add the WITH CHECK OPTION keywords when you create the view. These keywords ask the database server to test every inserted or updated row to ensure that it meets the conditions that are set by the WHERE clause of the view. The database server rejects the operation with an error if the row does not meet the conditions.

However, even if the view was created with the WITH CHECK OPTION keywords, you can perform inserts and updates through the view to change columns that are not part of the view definition. A column is not part of the view definition if it does not appear in the WHERE clause of the SELECT statement that defines the view.

Updating Through Views

If a view is built on a single table, the view is updatable if the SELECT statement that defined it did not contain any of the following items:

In an updatable view, you can update the values in the underlying table by inserting values into the view.

Important: You cannot update or insert rows in a remote table through views with check options.

Examples

The following statement creates a view that is based on the person table. When you create a view without an OF TYPE clause, the view is referred to as an untyped view.

The following statement creates a typed view that is based on the table person. To create a typed view, you must include an OF TYPE clause. When you create a typed view, the named row type that you specify immediately after the OF TYPE keywords must already exist.

For more information about how to create and use typed views, see Chapter 11 of the Informix Guide to SQL: Tutorial.

References

See the CREATE TABLE, DROP VIEW, GRANT, SELECT, and SET SESSION AUTHORIZATION statements in this manual.

In the Informix Guide to SQL: Tutorial, see the discussions of views and security in Chapter 11. Also, see the discussion of named row types in Chapter 10.




Informix Guide to SQL: Syntax, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.