Using Caché SQL
Caché SQL Basics
[Back] [Next]
   
Server:docs1
Instance:LATEST
User:UnknownUser
 
-
Go to:
Search:    

This chapter provides an overview of the features of Caché SQL, especially those that are not covered by the SQL standard or are related to the Caché Unified Data Architecture. It assumes prior knowledge of SQL and is not designed to serve as an introduction to SQL concepts or syntax.

This chapter discusses the following topics:
Tables
Within Caché SQL, data is presented within tables. Each table is defined to contain a number of columns. A table may contain zero or more rows of data values. The following terms are roughly equivalent:
Data Terms Relational Database Terms Caché Terms
database schema package
table persistent class
field column property
record row  
For further details, refer to Introduction to the Default SQL Projection in the “Introduction to Persistent Objects” chapter of Using Caché Objects.
There are two basic types of tables: base tables (which contain data and are usually referred to simply as tables) and views (which present a logical view based on one or more tables).
To find out more on how to define tables, see the chapter Defining Tables.”
To find out more on how to define views, see the chapter Defining Views.”
In order to make queries against tables more efficient, you can define indices on tables. See the chapter Defining and Building Indices.” in the Caché SQL Optimization Guide.
In order to enforce referential integrity you can define foreign keys and triggers on tables. See the chapters Defining Foreign Keys and Defining Triggers.”
Schemas
SQL schemas provides a means of grouping sets of related tables, views, stored procedures, and cached queries. Schemas are defined within a specific namespace. The use of schemas helps prevent naming collisions at the table level, because a table name must only be unique within its schema. SQL schemas correspond to packages in Caché objects; schema-to-package mapping is further described in SQL to Class Name Transformations.
An application can specify tables in multiple schemas.
You can specify an SQL name as qualified or unqualified. A qualified name specifies the schema: schema.name. An unqualified name does not specify the schema: name. If you do not specify the schema, Caché supplies the schema using some combination of the following techniques:
To view all the existing schemas within a namespace:
  1. From the Management Portal select System Explorer, then SQL ([Home] > [SQL]). Select a namespace with the Switch option at the top of the page; this displays the list of available namespaces. After selecting a namespace, select the Schema drop-down list on the left side of the screen. This displays a list of the schemas in the current namespace with boolean flags indicating whether there are any tables or any views associated with each schema.
  2. Select a schema from this list; it appears in the Schema box. Just above it is a drop-down list that allows you to select Tables, System Tables, Views, Procedures, or All of these that belong to the schema. After setting this option, open the corresponding folder to view the member items. If there are no member items, opening the folder displays a blank page. (If you have not selected the corresponding per-schema option, or All, opening the folder displays the member items for the entire namespace.)
  3. Select any of the listed member items to display the Catalog Details for the item. Among the details you can select are the cached queries for that item.
Queries
Within Caché SQL, you view and modify data within tables by means of queries. Roughly speaking, queries come in two flavors: those that retrieve data (SELECT statements), and those that modify data (INSERT, UPDATE, and DELETE statements).
You can use SQL queries in a number of ways:
SELECT queries are described in the Querying the Database chapter of this guide.
Queries are part of Caché objects or Caché ObjectScript routines.
Privileges
Caché SQL provides a way to limit access to tables, views, and so on via privileges. You can define a set of users and roles and grant various privileges (read, write, and so on) to them. See the chapter Users, Roles, and Privileges.”
Data Display Options
Caché SQL uses a SelectMode option to specify how data is to be displayed or stored. The available options are Logical, Display, and ODBC. By default, data is stored internally in Logical mode. Every data type class can define transformations between internal Logical format and Display format or ODBC format by using the LogicalToDisplay(), LogicalToODBC(), DisplayToLogical(), and ODBCToLogical() methods. When SQL SelectMode is Display, the LogicalToDisplay transformation is applied, and returned values are formatted for display. The default SQL SelectMode is Logical; thus by default returned values are displayed in their storage format.
SelectMode affects the format that in which query result set data is displayed, SelectMode also affects the format in which data values should be supplied, for example in the WHERE clause. Caché applies the appropriate transformation method based on the storage mode and the specified SelectMode. A mismatch between a supplied data value and the SelectMode can result in an error or in erroneous results. For example, if DOB is a date stored in $HOROLOG Logical format, and a WHERE clause specifies WHERE DOB > 2000–01–01 (ODBC format), SelectMode = ODBC returns the intended results. SelectMode = Display generates SQLCODE -146 Unable to convert date input to a valid logical date value. SelectMode = Logical attempts to parse 2000–01–01 as a Logical date value, and returns zero rows.
For most data types, the three SelectMode modes return the same results. The following data types are affected by the SelectMode option:
The SQL SelectMode may be specified as follows:
Data Collation
Collation specifies how values are ordered and compared, and is part of both Caché SQL and Caché objects.
You can specify a collation type as part of field/property definition. Unless otherwise specified, a string field/property defaults to the namespace default collation. By default, the namespace default collation for strings is SQLUPPER. SQLUPPER collation transforms strings into uppercase for the purposes of sorting and comparing. Thus, unless otherwise specified, string ordering and comparison is not case-sensitive.
You can specify a collation type as part of index definition, or use the collation type of the indexed field.
An SQL query can override the defined field/property collation type by applying a collation function to a field name. The ORDER BY clause specifies the result set sequence for a query; if a specified string field is defined as SQLUPPER, query results order is not case-sensitive.
For further details refer to the Collation chapter of Using Caché SQL.
Executing SQL
Caché supports numerous ways to write and execute SQL code. These include:
You can use Caché objects (classes and methods) to: