Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » SQL Anywhere Server - SQL Usage » Working with Database Objects » Working with views

Working with views Next Page

Creating views


When you browse data, a SELECT statement operates on one or more tables and produces a result set that is also a table. Just like a base table, a result set from a SELECT query has columns and rows. A view gives a name to a particular query, and holds the definition in the database system tables.

Suppose you frequently need to list the number of employees in each department. You can get this list with the following statement:

SELECT DepartmentID, COUNT(*)
FROM Employees
GROUP BY DepartmentID;

You can create a view containing the results of this statement using either Sybase Central or Interactive SQL.

When you create a view, the database server stores the view definition in the database; no data is stored for the view. Instead, the view definition is executed only when it is referenced, and only for the duration of time that the view is in use. This means that creating a view does not require storing duplicate data in the database.

To create a new view (Sybase Central)
  1. Connect to a database.

  2. Open the Views folder for that database.

  3. From the File menu, choose New > View.

    The Create View wizard appears.

  4. Follow the instructions in the wizard.

    The new view appears in the Views folder.

  5. When the wizard exits, you can edit the view definition in the SQL tab in the right pane. If you do so, you must save the changes. From the File menu, choose Save.

To create a new view (SQL)
  1. Connect to a database.

  2. Execute a CREATE VIEW statement.

  3. Example

    Create a view called DepartmentSize that contains the results of the SELECT statement given earlier in this section:

    CREATE VIEW DepartmentSize AS
       SELECT DepartmentID, COUNT(*)
       FROM Employees
       GROUP BY DepartmentID;

    For more information, see CREATE VIEW statement.