|
SQL | Paths and Views |
SELECT
.
A View is a virtual table which is created from one or more
records or tables and can then be referenced in the FROM
clause as if it was a real table.
Create paths and views with the CREATE PATH and CREATE VIEW commands. List paths and views with the SHOW command. Delete paths and views with the DROP command.
FROM
clause of a
SELECT
.
If there is more than one path
defined between two records or tables, SQL uses the earliest defined path.
When a SIR/XS
database is connected, paths are automatically created between
all records with matching keys.Use the SHOW PATHS command to see the currently defined paths in the order that SQL deals with them.
Paths are explicitly invoked in the FROM
clause of the
SELECT
statement by specifying the name of the first record
type or table and the path name as the second name. For example, if there is a
PATH called NEWPATH from RECX to RECY, invoke the path with:
SELECT RECXVARS RECYVARS FROM RECX NEWPATHA path defined between two records on a case structured database operates within the same case unless the
USING
clause is specified on
the path. The USING
clause joins records belonging to
different cases. The setting of CASE
mode does not affect the
operation of the path. Paths can be defined between the common information
record (CIR) and other records or CIRs by specifying the keyword CIR.
A table name may be specified and the rows on the table are used in exactly the
same way as records on the database. The WHERE
clause in the
path definition determines how the path operates and should normally reference
indexed columns. VIA
and USING
clauses are
not used if the TO
clause specifies a table.
SELECT
statement and the WHERE
clause within the view.
A view definition resides in a tabfile and its columns are defined by a
SELECT
statement on the CREATE VIEW
command.
This is the same as any other SELECT
except that the
DISTINCT, ORDER BY, FORMAT
and UNION
clauses
may not be used.
When a view is created, it is created on the tabfile in the same way as any other table. Views can be exported and imported on another machine. Security can be defined as for any other table. Permissions on views can be granted and revoked. (See Permissions).
Views can be used to SELECT ... FROM ...
as any other table.
DISPLAY
does not access views. Views are used in SQL only;
they cannot be used in other SIR/XS products such as VisualPQL or FORMS.
The underlying data in tables or records can be updated using the
VIEW
name on the update command with the following conditions:
FROM
clause in the
SELECT
statement of the CREATE VIEW
command
references more than one table or record, the view cannot be updated.SELECT
statement implies any aggregation
(GROUP BY
or aggregation functions), the view cannot be
updated.SELECT
statement contains a constant or
an expression, then:INSERT
is not allowed on that columnUPDATE
is not allowed on that columnDELETE
is allowedCREATE PATH path_name FROM [[database.]recordtype] | [[tabfile.] table] TO [[database.]recordtype] | [[tabfile.] table] [WHERE boolean] [VIA value list | USING value list ] FIRST LAST OUTER REVERSE CIR
CREATE PATH
names the path and specifies the two records
or tables that the path joins. Paths may use Views as the
FROM
and TO
references. The WHERE,
VIA,
and USING
clauses link records and rows
depending on the values of data items.
FROM
FROM
is required. It specifies the record,
table or view which is to be joined to the TO
record,
table or view.TO
TO
is required. It specifies the record,
table or view which is to be joined to the FROM
record,
table or view.WHERE
VIA
TO
records. If one or more of the key fields are omitted, all records with
the specified key fields are returned. The values can be an arithmetic
expression, a constant or a variable name.FROM
record are to be passed.
The asterisk can be used in combination with other values. The asterisk can
only appear as the first item. An asterisk cannot be specified when a table is
the FROM
reference.VIA
is
used. Do not specify the case id in the VIA
clause.USING
TO
records. USING
allows the joining of
records from different cases. The values can be constants, variables or
arithmetic expressions. Only variables in the FROM
record
type can appear on the USING
clause.FIRST
TO
clause.LAST
TO
clause.OUTER
TO
record
can be found to satisfy the path definition, then a dummy of all
undefined values is used to complete the join.REVERSE
REV
CREATE PATH MYPATH - FROM EMPLOYEE TO OCCUP - WHERE GENDER EQ 2 AND DIVISION EQ 12) To create a path called
CURRENT
that joins
EMPLOYEE
and OCCUP
records only for the
current position, specify
CREATE PATH CURRENT - FROM EMPLOYEE TO OCCUP VIA CURRPOSNote that
VIA
and USING
are not specified
together. Valid Specifications on the VIA, VIA *
, and
USING
clauses are:
FROM TO VIA VIA * USING ------- ------- --- ---- ----- table recname Yes No Yes table table No No No recname recname Yes Yes Yes recname table No No No
CREATE VIEW [tabfile.] viewname [(column list) ] AS SELECT variable list ... FROM [database.] rectype | [tabfile.]table , ... [GROUP BY variable list, ... [HAVING expression]] [OUTER [database.] rectype | [tabfile.]table ] [WHERE expression] [WITH CHECK OPTION ]
CREATE VIEW
creates a named view. The view is a
SELECT
from a number of records, tables or other views
with particular conditions.
viewname
column list
SELECT
statement in the CREATE VIEW
command.
The column list must reference the same number of columns as the
SELECT
statement does. Renaming columns is useful when the
SELECT
creates columns with awkward names such as expressions
or concatenations of table and column names.
AS SELECT
FROM
clause.
Specify expressions (constants, arithmetic expressions, functions) as in a
SELECT
command. If the view includes record(s) from a
case structured database, the view automatically includes the case id and common
variables.FROM
FROM
record or table must be separated by commas.
If the optional database and tabfile names are not specified, the current
defaults are used.GROUP BY
CLEAR CASE
before using the view.HAVING
GROUP BY
clause. Only groups which satisfy the specified
condition are included in the view.OUTER
OUTER
is not
specified, a row is not returned.WHERE
WHERE
clause can contain subqueries but
these may not reference another view.WITH CHECK OPTION
WHERE
clause
conditions. This means that the view can
only add or modify rows which are part of the view. If this is not specified,
rows can be added which the view could not retrieve.EMPLOYEES
with ID, NAME
and SALARY
for male employees only (including
ID
automatically because it is the case id):
CREATE VIEW MALES - AS SELECT NAME SALARY - FROM 1 WHERE GENDER EQ 1To create a view of
EMPLOYEES
who have had low review ratings:
CREATE VIEW LOWRATING - AS SELECT NAME SALARY POSITION DIVISION REVDATE RATING - FROM 1 2 3 - WHERE RATING LT 4To create a dynamic summary of people by education level:
CREATE VIEW EDSUMM - (LEVEL,NUMBER,WAGES,AVERAGE) - AS SELECT EDUC COUNT(SALARY) SUM(SALARY) - SUM(SALARY)/COUNT(SALARY) - FROM 1 - GROUP BY EDUC
RENAME VIEW [ tabfile_name. ] view_name TO view_nameRenames a view. If the tabfile is not specified, the default tabfile is used.