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, PowerBuilder 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 PowerBuilder

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

In PowerBuilder, 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 View painter, which is similar to 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. Click the Create View button, or select View or 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:

    • Click the name of each table or view you want to open in the list displayed in the Select Tables dialog box, then click the Open button to open them. The Select Tables dialog box closes.

    • Double-click the name of each table or view you want to open. Each object is opened immediately. Then click the Cancel button to close the Select Tables dialog box.

    Representations of the selected tables and views display in the View painter workspace:

    The sample shows the View painter workspace with the department and employee tables opened and joined at dept _ i d . Beneath them, the Syntax tab displays the sequel that defines the view.
  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”. The View painter and the SQL Select painter are similar.

  6. When you have completed the view, click the Return 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.

    PowerBuilder 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 the View painter or want to look at the definition of an existing view.

StepsTo display the SQL statement from the View painter:

  1. Select the Syntax tab in the View painter.

    PowerBuilder 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.

Joining tables

If the database view contains more than one table, you should join the tables on their common columns. When the View painter is first opened for a database view containing more than one table, PowerBuilder 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:

    An example of tables and their joins are shown
  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, which lists existing join operators. In the dialog box there are also OK, Cancel, Delete, and Help buttons.
  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. Click the Drop Object button or select Drop View from the pop-up menu.

    PowerBuilder 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”.