Working with database views

A database view gives a different (and usually limited) perspective of the data in one or more tables. Although you see existing database views listed in the Objects view, a database view does not physically exist in the database as a table does. Each time you select a database view and use the view’s data, DataWindow Designer executes a SQL SELECT statement to retrieve the data and creates the database view.

For more information about using database views, see your DBMS documentation.

Using database views in DataWindow Designer

You can define and manipulate database views in DataWindow Designer. Typically you use database views for the following reasons:

In DataWindow Designer, you can create single- or multiple-table database views. You can also use a database view when you define data to create a new database view.

You define, open, and manipulate database views in the SQL Select painter. For more information about the SQL Select painter, see “Selecting a data source”.

NoteUpdating database views Some database views are logically updatable and others are not. Some DBMSs do not allow any updating of views. For the rules your DBMS follows, see your DBMS documentation.

StepsTo open a database view:

  1. In the Objects view, expand the list of Views for your database.

  2. Highlight the view you want to open and select Add To Layout from the pop-up menu, or drag the view’s icon to the Object Layout view.

StepsTo create a database view:

  1. Select New View from the Object>Insert or pop-up menu.

    The Select Tables dialog box displays, listing all tables and views that you can access in the database.

  2. Select the tables and views from which you will create the view by doing one of the following:

    Representations of the selected tables and views display in the SQL Select painter:

  3. Select the columns to include in the view and include computed columns as needed.

  4. Join the tables if there is more than one table in the view.

    For information, see “Joining tables”.

  5. Specify criteria to limit rows retrieved (Where tab), group retrieved rows (Group tab), and limit the retrieved groups (Having tab), if appropriate.

    For information, see the section on using the SQL Select painter in “Selecting a data source”.

  6. When you have completed the view, click the OK button.

  7. Name the view.

    Include view or some other identifier in the view’s name so that you will be able to distinguish it from a table in the Select Tables dialog box.

  8. Click the Create button.

    DataWindow Designer generates a CREATE VIEW statement and submits it to the DBMS. The view definition is created in the database. You return to the Database painter workspace with the new view displayed in the workspace.

Displaying a database view’s SQL statement

You can display the SQL statement that defines a database view. How you do it depends on whether you are creating a new view in SQL Select painter or want to look at the definition of an existing view.

StepsTo display the SQL statement from SQL Select painter:

  1. Select the Syntax tab in SQL Select painter.

    DataWindow Designer displays the SQL it is generating. The display is updated each time you change the view.

StepsTo display the SQL statement from the Database painter:

  1. Highlight the name of the database view in the Objects view and select Properties from the pop-up menu, or drag the view’s icon to the Object Details view.

    The completed SELECT statement used to create the database view displays in the Definition field on the General page:

    The sample shows the general tab listing Owner as d b a, and View as emp dep v w. It also displays the Definition field with the completed select statements used to create the database view.

    NoteView dialog box is read-only You cannot alter the view definition in the Object Details view. To alter a view, drop it and create another view.

Joining tables

If the database view contains more than one table, you should join the tables on their common columns. When the SQL Select painter is first opened for a database view containing more than one table, DataWindow Designer makes its best guess as to the join columns, as follows:

StepsTo join tables:

  1. Click the Join button.

  2. Click the columns on which you want to join the tables.

    In the following screen, the Employee and Department tables are joined on the dept_id column:

    The sample screen shows the Employee and Department tables and lists their columns. The tables are joined on the dept_id column.
  3. To create a join other than the equality join, click the join representation in the workspace.

    The Join dialog box displays:

    The sample shows the Join dialog box, titled Join. At the top, it states Join rows in employee and department where:. Beneath this prompt is a list of join operators. Highlighted is the operator = employee dot dept _ i d = department dot dept _ i d.
  4. Select the join operator you want from the Join dialog box.

    If your DBMS supports outer joins, outer join options also display in the Join dialog box. For example, in the preceding dialog box (which uses the Employee and Department tables), you can choose to include rows from the Employee table where there are no matching departments, or rows from the Department table where there are no matching employees.

    For more about outer joins, see “Using ANSI outer joins”.

Dropping a database view

Dropping a database view removes its definition from the database.

StepsTo drop a view:

  1. In the Objects view, select the database view you want to drop.

  2. Select Drop View from the pop-up menu.

    DataWindow Designer prompts you to confirm the drop, then generates a DROP VIEW statement and submits it to the DBMS.

Exporting view syntax

You can export the syntax for a view to the log. This feature is useful when you want to create a backup definition of the view before you alter it or when you want to create the same view in another DBMS.

StepsTo export the syntax of an existing view to a log:

  1. Select the view in the painter workspace.

  2. Select Export Syntax from the Object menu or the pop-up menu.

    For more information about the log, see “Logging your work”.