Defining a view

Suppose you frequently need to list employees by department. The following query creates a list of all employees by department and state that you can run against iqdemo:

SELECT Departments.DepartmentID,
  Departments.DepartmentName, Employees.EmployeeID,
  Employees.GivenName,Employees.Surname,
  Employees.Street,Employees.City,
  Employees.State,Employees.Phone
FROM Departments, Employees
ORDER BY Employees.State

Running the query creates a results set that looks like this:

Employee ID

Last Name

First Name

City

State

Telephone

148

Julie

Jordan

Woodbridge

AZ

6175557835

191

Jeannette

Bertrand

Waterloo

AZ

5085558138

586

James

Coleman

Waterloo

AZ

5085554735

703

Jose

Martinez

Waterdown

AZ

6175557114

299

Rollin

Overbey

Kanata

CA

5105557255

949

Pamela

Savarino

Sheffield

CA

3105551857

1142

Alison

Clark

Kanata

CA

5105559437

278

Terry

Melkisetian

Sarnia

CO

6175555188

....

...

...

...

...

...

Creating a view

In Interactive SQL, you can use a CREATE VIEW statement to create and store a view. This example creates a view called emp_dept in iqdemo:

CREATE VIEW emp_dept AS
  SELECT Departments.DepartmentID,
  Departments.DepartmentName, Employees.EmployeeID,
  Employees.GivenName,Employees.Surname,
  Employees.Street,Employees.City,
  Employees.State,Employees.Phone
FROM Departments, Employees
ORDER BY Employees.State

In Sybase Central, you can use a wizard to create a view. To start the wizard, connect to iqdemo, then do one of the following:

When the wizard starts, follow the instructions on the screen.

Displaying view data

You can query a view just like a table:

SELECT *
FROM emp_dept

Remember that the information in a view is not stored separately in the database. Each time you refer to the view, SQL executes the associated SELECT statement to find the appropriate data. This means that if the sales data changes, information in the view will be automatically up to date.

To see the results in Sybase Central, right-click the View, choose View Data in Interactive SQL.

Changing a view

In Interactive SQL, you can use an ALTER VIEW statement to change a view definition with a modified version. The emp_dept view lists all employees by department. You can use the following command to change the view to display only those employees in the Sales department:

ALTER VIEW emp_dept AS  
  SELECT Departments.DepartmentID,
   Departments.DepartmentName, Employees.EmployeeID,
   Employees.GivenName,Employees.Surname,
   Employees.Street,Employees.City,
   Employees.State,Employees.Phone
FROM Departments, Employees
WHERE Departments.DepartmentID = 200
ORDER BY Employees.State

ALTER VIEW replaces the existing SELECT statement in the view with the SELECT you defined in the ALTER VIEW command. Existing permissions on the view are maintained.

To change a view in Sybase Central, right-click the View, and choose Edit in New Window.

Deleting views

To delete a view from the database in Interactive SQL, use the DROP statement:

DROP VIEW <view name>

To drop a view in Sybase Central, right-click the view you want to delete, and choose Delete from the shortcut menu.