Transbase SQL Reference Manual
Transbase SQL Reference Manual
While every precaution has been taken in the preparation of this document, the
publisher assumes no responsibility for errors or omissions, or for damages resulting
from the use of the information contained herein.
2022-11-29
Table of Contents
Introduction
1. General Concepts
1.1. Syntax Notation
1.2. Separators
1.3. Keywords
1.4. Identifiers
1.4.1. User Schemas
1.4.2. Names and Identifiers
1.5. Data Types
1.5.1. Type Compatibility
1.5.2. Type Exceptions and Overflow
1.5.3. CASTing Types from and to CHAR
1.6. Literals
1.6.1. Directory/File Literal
1.6.2. IntegerLiteral
1.6.3. NumericLiteral
1.6.4. RealLiteral
1.6.5. StringLiteral
1.6.6. BinaryLiteral
1.6.7. BitsLiteral
1.6.8. BoolLiteral
1.6.9. DATETIME Literal
1.6.10. TIMESPAN Literal
2. Data Definition Language
2.1. Dataspaces
2.1.1. CreateDataspaceStatement
2.1.2. AlterDataspaceStatement
2.2. Users
2.2.1. GrantUserclassStatement
2.2.2. RevokeUserclassStatement
2.2.3. AlterPasswordStatement
2.2.4. GrantPrivilegeStatement
2.2.5. RevokePrivilegeStatement
2.3. Domains
2.3.1. CreateDomainStatement
2.3.2. AlterDomainStatement
2.3.3. DropDomainStatement
2.4. Sequences
2.4.1. CreateSequenceStatement
2.4.2. DropSequenceStatement
2.5. CreateTableStatement
2.5.1. Defaults
2.5.2. AUTO_INCREMENT Fields
2.5.3. TableConstraintDefinition FieldConstraintDefinition
2.5.4. PrimaryKey
2.5.5. CheckConstraint
2.5.6. ForeignKey
2.6. AlterTableStatement
2.6.1. AlterTableConstraint
2.6.2. AlterTableChangeField
2.6.3. AlterTableRenameField
2.6.4. AlterTableFields
2.6.5. AlterTableRename
2.6.6. AlterTableMove
2.6.7. AlterTableInmemory
2.7. DropTableStatement
2.8. CreateIndexStatement
2.8.1. StandardIndexStatement
2.8.2. HyperCubeIndexStatement
2.8.3. FulltextIndexStatement
2.8.4. BitmapIndexStatement
2.9. DropIndexStatement
2.10. Triggers
2.10.1. CreateTriggerStatement
2.10.2. DropTriggerStatement
2.11. Views
2.11.1. CreateViewStatement
2.11.2. DropViewStatement
3. Data Manipulation Language
3.1. FieldReference
3.2. User
3.3. Expression
3.4. Primary, CAST Operator
3.5. SimplePrimary
3.5.1. MathematicalStdFunction
3.5.2. SetFunction
3.5.3. WindowFunction
3.5.4. StringFunction
3.5.5. TocharFunction
3.5.6. ResultcountExpression
3.5.7. SequenceExpression
3.5.8. ConditionalExpression
3.5.9. TimeExpression
3.5.10. SizeExpression
3.5.11. LobExpression
3.5.12. ODBC_FunctionCall
3.5.13. UserDefinedFunctionCall
3.5.14. LastInsertIdFunc
3.5.15. LastUpdateFunc
3.5.16. CrowdStatusFunc
3.5.17. ReplicationStatusFunc
3.5.18. SearchCondition
3.5.19. HierarchicalCondition
3.6. Predicate
3.6.1. ComparisonPredicate
3.6.2. ValueCompPredicate
3.6.3. SetCompPredicate
3.6.4. InPredicate
3.6.5. BetweenPredicate
3.6.6. LikePredicate
3.6.7. MatchesPredicate, Regular Pattern Matcher
3.6.8. ExistsPredicate
3.6.9. QuantifiedPredicate
3.6.10. NullPredicate
3.6.11. FulltextPredicate
3.7. Null Values
3.8. SelectExpression (QueryBlock)
3.9. TableExpression, SubTableExpression
3.10. TableReference, SubTableReference
3.11. FlatFileReference - direct processing of text files
3.12. CrowdExpression
3.12.1. Crowd Queries
3.12.2. Crowd Errors
3.13. TableFunction
3.14. JoinedTable (Survey)
3.14.1. INNER JOIN with ON/USING Clause
3.14.2. JoinedTable with NATURAL
3.14.3. JoinedTable with OUTER JOIN
3.15. Scope of TableReferences and CorrelationNames
3.16. SelectStatement
3.17. WithClause
3.18. InsertStatement
3.18.1. Insertion with Fieldlist and DEFAULT Values
3.18.2. Insertion on AUTO_INCREMENT Fields
3.18.3. Insertion on Views
3.18.4. Handling of Key Collisions
3.18.5. Insertion with ReturningClause
3.19. DeleteStatement
3.20. UpdateStatement
3.21. UpdateFromStatement
3.22. MergeStatement
3.23. General Rule for Updates
3.24. Rules of Resolution
3.24.1. Resolution of Fields
3.24.2. Resolution of SetFunctions
4. Load and Unload Statements
5. Alter Session statements
5.1. Sort Buffer Size
5.2. Multithreading Mode
5.3. Integer Division Mode
5.4. Lock Mode
5.5. Evaluation Plans
5.6. Schema Default
5.7. SQL Dialect
6. Lock Statements
6.1. LockStatement
6.2. UnlockStatement
7. The Data Types Datetime and Timespan
7.1. Principles of Datetime
7.1.1. RangeSpec
7.1.2. SQL Compatible Subtypes
7.1.3. DatetimeLiteral
7.1.4. Valid Datetime Values
7.1.5. Creating a Table with Datetimes
7.1.6. The CURRENTDATE/SYSDATE Operator
7.1.7. Casting Datetimes
7.1.8. TRUNC Function
7.1.9. Comparison and Ordering of Datetimes
7.2. Principles of Timespan and Interval
7.2.1. Transbase Notation for Type TIMESPAN
7.2.2. INTERVAL Notation for TIMESPAN
7.2.3. Ranges of TIMESPAN Components
7.2.4. TimespanLiteral
7.2.5. Sign of Timespans
7.2.6. Creating a Table containing Timespans
7.2.7. Casting Timespans
7.2.8. Comparison and Ordering of Timespans
7.2.9. Scalar Operations on Timespan
7.2.10. Addition and Substraction of Timespans
7.3. Mixed Operations
7.3.1. Datetime + Timespan, Datetime - Timespan
7.3.2. Datetime - Datetime
7.4. The DAY Operator
7.5. The WEEKDAY Operator
7.6. The WEEK Operator
7.7. The ISOWEEK Operator
7.8. The QUARTER Operator
7.9. Selector Operators on Datetimes and Timespans
7.10. Constructor Operator for Datetimes and Timespans
8. The Datatypes BITS(p) and BITS(*)
8.1. Purpose of Bits Vectors
8.2. Creation of Tables with type BITS
8.3. Compatibility of BINCHAR and BITS
8.4. BITS and BINCHAR Literals
8.5. Spool Format for BINCHAR and BITS
8.6. Operations for Type BITS
8.6.1. Bitcomplement Operator BITNOT
8.6.2. Binary Operators BITAND , BITOR
8.6.3. Comparison Operators
8.6.4. Dynamic Construction of BITS with MAKEBIT
8.6.5. Counting Bits with COUNTBIT
8.6.6. Searching Bits with FINDBIT
8.6.7. Subranges and Single Bits with SUBRANGE
8.7. Transformation between Bits and Integer Sets
8.7.1. Compression into Bits with the SUM function
8.7.2. Expanding BITS into Record Sets with UNGROUP
9. LOB (Large Object) datatypes
9.1. The Data Type BLOB (Binary Large Object)
9.1.1. Inherent Properties of BLOBs
9.1.2. BLOBs and the Data Definition Language
9.1.3. BLOBs and the Data Manipulation Language
9.2. The Data Type CLOB (Character Large Object)
9.2.1. Inherent Properties of CLOBs
9.2.2. CLOBs and the Data Definition Language
9.2.3. CLOBs and the Data Manipulation Language
10. Fulltext Indexes
10.1. FulltextIndexStatement
10.1.1. WORDLIST and STOPWORDS
10.1.2. CHARMAP
10.1.3. DELIMITERS
10.1.4. WITH SOUNDEX
10.2. Implicit Tables of a Fulltext Index
10.3. FulltextPredicate
10.4. Examples and Restrictions
10.4.1. Examples for Fulltext Predicates
10.4.2. Restrictions for Fulltext Predicates
10.4.3. Phonetic Search in Fulltext Indexes
10.5. Performance Considerations
10.5.1. Search Performance
10.5.2. Scratch Area for Index Creation
10.5.3. Record Deletion
11. Data Import/Export
11.1. SpoolStatement
11.1.1. The DSV Spooler
11.1.2. The XML Spooler
11.1.3. The JSON Spooler
11.2. External data sources
11.2.1. Remote Database Access
11.2.2. JDBCReader
11.2.3. OraReader
11.2.4. FILE Tables
A. The Data Dictionary
A.1. The sysdatabase(s) Table
A.2. The syssession Table
A.3. The sysuser Table
A.4. The systable Table
A.5. The syscolumn Table
A.6. The sysindex Table
A.7. The sysview Table
A.8. The sysviewdep Table
A.9. The sysblob Table
A.10. The systablepriv Table
A.11. The syscolumnpriv Table
A.12. The sysdomain Table
A.13. The sysconstraint Table
A.14. The sysrefconstraint Table
A.15. The sysdataspace Table
A.16. The sysdatafile Table
A.17. The loadinfo Table
A.18. The syskeyword Table
B. Sample Database
C. Precedence of Operators
List of Figures
List of Tables
Introduction
TB/SQL is the data retrieval, manipulation and definition language for the relational
data base system Transbase. TB/SQL is an SQL implementation compatible to the
ISO/DIS standard 9075 with additionally functional extensions which make the SQL
language more powerful and easier to use.
This manual is intended for users who already have a basic knowledge of SQL. Heavy
use of examples is made in order to clarify syntactic or semantic questions. All
examples refer to a Sample Database outlined in the appendix of this manual.
1. General Concepts
1.1. Syntax Notation
The vertical line | separates alternatives.
Braces { } group several items together, e.g. to form complex alternatives. They
are functionally equivalent to the standard braces () as used in arithmetic
expressions.
An ellipsis ... indicates that the preceding item may be repeated arbitrarily often.
All non-terminal symbols start with an uppercase letter followed by lowercase letters.
All terminal symbols are represented by themselves without syntactic markups. All
terminal symbols consisting of uppercase letters only denote keywords. These are
supposed to be case-insensitive and thus be used in SQL in any possible mix of
uppercase and lowercase letters.
[1] [ ::= [
[2] ] ::= ]
1.2. Separators
In a TB/SQL-statement, keywords, identifiers and literals must be separated from
each other by at least one separator. As in many programming languages, possible
separators in TB/SQL are the space character (blank), the tab character and the
newline character.
1.3. Keywords
TB/SQL keywords are listed in the catalog table syskeyword of each database.
Names that can be interpreted as keywords need to be denoted as delimited
identifiers.
1.4. Identifiers
Syntax:
[3] Identifier ::= StandardIdentifier |
DelimitedIdentifier
[4] StandardIdentifier ::= LetterDigit...
[5] LetterDigit ::= <Letters a-z, A-Z, digits 0-9,
underscore>
[6]DelimitedIdentifier ::= "Character..." | [Character...]
[7] Character ::= <each printable character except
double quote>
Explanation:
Identifiers serve to embed the references to database objects (users, tables, views
etc.) into SQL statements. The names of these objects may be composed of more
than one parts. In this case the identifiers specifying the name parts are combined
with some syntactic glue (see e.g. TableIdentifier).
Each identifier unambiguously denotes a certain name, but the mapping from the
identifier to the name may depend on the case-insensitive setting of the database.
It denotes a name
Example 1.1. Valid Identifiers
suppliers
Suppno
xyz_abc
q1
q23p
"5xy"
"select"
Example 1.2. Invalid Identifiers
5xy
select
SeLecT
x:y
?x
"as"df"
1.4.1. User Schemas
A user schema is a container for a set of database objects.
Domains
Sequences
Tables
Constraints
Indices
Triggers
Views
Each database object belongs to exactly one schema and can be identified by the
schema name and the objects name. The object's name needs to be unique only
within the schema and the object category it belongs to.
Constraints, indices, and triggers belong to the same schema as the table they
belong to.
If no schema is given, the default schema of the database is used (either PUBLIC
(default) or USER ) with PUBLIC configured as fallback. The keyword USER
represents the name of the current user.
The default schema of the database is set at database creation time and cannot be
changed afterwards. By one of the ALTER SESSION statements, the default schema
can be changed temporarily for a connection.
Whether a user is allowed to access a specific object only depends on the privileges
of the user on the appropriate object.
A table can be moved from one schema to another by the ALTER TABLE ... RENAME
TO ... statement. All related indices, triggers and constraints are also moved to the
new schema.
1.5. Data Types
A DataType specifies the type of a field or the target type for a type conversion.
Syntax:
[20]DataType ::= TINYINT | SMALLINT | INTEGER |
BIGINT |
NUMERIC [(Precision [,Scale])] |
DECIMAL [(Precision [,Scale])] |
FLOAT | DOUBLE | REAL |
VARCHAR [(Precision)] | CHAR
[(Precision)] |
VARCHAR(*) | CHAR(*) | STRING |
BINCHAR [(Precision)] | BINCHAR (*) |
BITS (Precision) | BITS (*) |
BITS2 (Precision) | BITS2 (*) |
BOOL |
DATETIME Range | DATE | TIME |
TIMESTAMP |
TIMESPAN Range |
INTERVAL StartIx2 [ TO EndIx2 ] |
BLOB | CLOB
[21] Precision ::= IntegerLiteral
[22] Scale ::= IntegerLiteral
[23] Range ::= [ RangeIx1 [ : RangeIx1 ] ]
[1] [ ::= [
[2] ] ::= ]
[24]RangeIx1 ::= YY | MO | DD | HH | MI | SS | MS
[25] StartIx2 ::= RangeIx2 [ Precision ]
[26] EndIx2 ::= RangeIx2 [ Precision ]
[27]RangeIx2 ::= YEAR | MONTH | DAY | HOUR | MINUTE |
SECOND
Explanation:
Each field of a table has a data type which is defined at creation time of the table.
Constant values (Literals) also have a data type which is derived by the syntax and
the value of the Literal.
Please note that the total record size may additionally restrict these values in
length when they are to be stored in the database. (see also Constants and
Sizes in the Transbase System Guide).
Please note that in the internal representation ASCII characters take exactly
one byte, non-ASCII characters may take up to 6 bytes depending on their
internal encoding which is based on the UTF8 format.
BINCHAR, BINCHAR(p), BINCHAR(*) are called binary types.
BITS(p), BITS2(p), BITS(*), BITS2(*) are fixed sized or variable sized bit
sequences, resp. The maximum value of p (number of bits) is 31968 for both
variants.
BOOL is called the logical type. Its values are TRUE and FALSE. The ordering of
logical values is: FALSE is less than TRUE.
BLOB is the type for binary large objects. The type is described in a separate
chapter within this manual.
The CLOB (character large object) datatype is used for character data.
The following table provides a short summary of data types and ranges.
Table 1.1. Transbase Datatypes and Ranges
DATE DATETIME[YY:DD]
Datatype Description Range
TIME DATETIME[HH:MS]
TIMESTAMP DATETIME[YY:MS]
1.5.1. Type Compatibility
Whenever values serve as operands for an operation, their types must be
compatible. The compatibility rules are as follows:
The compatibilities of time types among each other and with other types is
described in The Data Types Datetime and Timespan.
Arithmetic data types are ordered according to the following type hierarchy:
FLOAT
NUMERIC
BIGINT
INTEGER
SMALLINT
If values of different arithmetic types are involved in an operation, they are implicitly
converted to the highest type among them before the operation is performed.
Upward conversion within arithmetic types never causes loss of significant digits, but
note that values converted to FLOAT or DOUBLE are not always exactly
representable.
Character data types are ordered according to the following type hierarchy:
If two values of type VARCHAR or CHAR with different length are compared, then the
shorter string is padded with the space character ' ' up to the length of the longer
string.
If two character types are processed in operations UNION, INTERSECTION and DIFF
then the following rules apply for determinig the result type: One participating
CHAR(*) yields CHAR(*). 2 input types of CHAR or VARCHAR with precisions p1 and
p2 yield output precision p = maximum(p1,p2). If at least one of them is VARCHAR -
then VARCHAR(p) is the result type else CHAR(p).
For operations on type BITS see The Datatypes BITS(p) and BITS(*).
In case (1) - arithmetic computation - the type of the result value is requested to be
the same as that of the input operands.
The expression
1000000 * 2
is legal, because the input type is INTEGER and the result is still in the range of
INTEGER.
The expression
1000000 * 1000000
leads to a type exception because the result is no more in the range of INTEGER.
To avoid this, it would be sufficent to cast one (or both) input operands to a higher
ranged type e.g. NUMERIC(30,0) or BIGINT
1000000.0 * 1000000
In case (2) - Insertion or Update of records - the requested types are those of the
corresponding fields of the target table.
With a table T with a field f of type TINYINT, the following statement would cause a
type exception:
In case (3) - explicit casting - the requested type is the explicitly specified type in
the CAST-operator.
The expressions
are legal (the latter example pads the string with 5 blanks at the end).
The expressions
are illegal, since they cannot be converted into the requested types because of
overflow.
Additionally, with the exception of type BLOB, there is the possibility to convert
values of each type to the type CHAR(*) and (VAR)CHAR(p). This is done by the
CAST operator.
1.5.3.1. CASTing to CHAR
The main usage of casting to CHAR is to make string operations (e.g. the LIKE
operator or the string concatenation operator '||') available to other types.
As a further example, assume that fields 'partno1' and 'partno2' of type INTEGER are
in table 'parts'. The following query constructs a composed partno of type CHAR(*)
with a '/' in the middle
Note that an error occurs if the source value is not a valid literal of the target type.
1.6. Literals
Literals are denote constant values of a certain type.
Syntax:
[28] Literal ::= IntegerLiteral | NumericLiteral |
RealLiteral |
StringLiteral |
BinaryLiteral | BitsLiteral | BoolLiteral |
DatetimeLiteral | TimespanLiteral |
DirectoryLiteral | FileLiteral
Explanation:
All Literals are defined in the following paragraphs. The data types DATETIME and
TIMESPAN are explained in detail in Datetime and Timespan .
1.6.1. Directory/File Literal
A FileLiteral denotes a filename and a directory literal denotes a directory name.
It may be a simple name or include a relative or absolute path. The exact syntax of a
path may depend on the platform.
Syntax:
5 -- INTEGER
33000 -- INTEGER
-33000 -- INTEGER
1234567890123 -- BIGINT
12345678901234567890123 -- NUMERIC(23,0)
1.6.2. IntegerLiteral
An IntegerLiteral is the representation of a constant number without fractional part.
Syntax:
Each IntegerLiteral has a data type which is either INTEGER, BIGINT or NUMERIC
with scale 0. The data type is derived by the value of the IntegerLiteral: if the value
is inside the range of INTEGER then the type is INTEGER. If the INTEGER range is
not sufficient and the value is inside the range of BIGINT then the type is BIGINT
else the type is NUMERIC(p,0) where p is the number of digits of the literal.
5 -- INTEGER
33000 -- INTEGER
-33000 -- INTEGER
1234567890123 -- BIGINT
12345678901234567890123 -- NUMERIC(23,0)
1.6.3. NumericLiteral
A NumericLiteral is the representation of a constant number with fractional part.
Syntax:
1.6.4. RealLiteral
A RealLiteral is the representation of a constant number with mantissa and
exponent.
Syntax:
Each RealLiteral has a data type which is FLOAT or DOUBLE. The data type is derived
by the value of the RealLiteral (see table datatypes ).
5.13e10 -- FLOAT
5.13e+10 -- FLOAT
0.31415e1 -- FLOAT
314.15E-2 -- FLOAT
314e-2 -- FLOAT
- 314e-2 -- FLOAT
1.2e52 -- DOUBLE
Note that no separators are allowed within RealLiteral, but are allowed between an
eventual unary minus and a RealLiteral. The next example shows incorrect
RealLiterals:
3.14 e4 -- illegal
3.98E -4 -- illegal
3.98e- 4 -- illegal
1.6.5. StringLiteral
A StringLiteral is the representation of a constant string.
Syntax:
Explanation:
The data type of a StringLiteral is CHAR(p) where p is the size of the UTF-8 coded
equivalent.
'xyz' -- CHAR(3)
'string with a single quote '' inside' -- CHAR(35)
'single quote ''' -- CHAR(14)
'' -- CHAR(0)
0u006D00fc006E -- CHAR(4)
0ufeff -- CHAR(3)
0uFC -- illegal
0u00FC -- CHAR(2)
1.6.6. BinaryLiteral
[37]BinaryLiteral ::= 0x[{0..9a..f}{0..9a..f}]...
0xA0B1C2 -- BINCHAR(3)
0xA0B -- illegal
1.6.7. BitsLiteral
[38]BitsLiteral ::= 0b[ 0 | 1 ]...
Syntax:
1.6.9. DATETIME Literal
A DatetimeLiteral is the representation of a constant DATETIME value.
Syntax:
Syntax:
2.1. Dataspaces
2.1.1. CreateDataspaceStatement
Serves to create a user dataspace in the database.
Syntax:
[49]CreateDataspaceStatement ::= CREATE DATASPACE
DataspaceIdentifier
[ DATAFILE IN DirectoryLiteral ]
[ SIZE SizeSpec ]
[ AUTOEXTEND SizeSpec ]
[ MAXSIZE SizeSpec ]
[50] SizeSpec ::= IntegerLiteral { MB | GB }
Initially the dataspace consists of one file with the specified size which is created in
the specified directory which must exist. If no directory is specified, the file is
created in the "disks" directory inside the database home directory.
The creation of each table optionally may be specified with a user dataspace name.
All pages of the table as well as the pages of its BLOB container and of all secondary
are allocated in the files belonging to this dataspace.
If a dataspace runs out of space, another file may be added with the ALTER
DATASPACE statement. If the AUTOEXTEND option has been specified, a full
dataspace is automatically extended by another file with the specified size.
With the MAXSIZE specification the size of the dataspace is limited to the specified
size, irrespective whether AUTOEXTEND has been specified or not.
At creation time of a database exactly one dataspace with name dspace0 exists. All
system catalogue tables are stored in dspace0. Furthermore, all tables without
explicit DATASPACE clause are also stored there.
Creation of a dataspace is always committed even if the corresponding transaction is
aborted.
2.1.2. AlterDataspaceStatement
Serves to alter a dataspace.
Syntax:
Explanation:
For a MAXSIZE property of the dataspace, there is the restriction that a new
MAXSIZE can only be set if there was already a MAXSIZE property, and the'new
value'must not be smaller than the existing. The OFF specification drops a currently
existing MAXSIZE limit.
Caution
Alter of a dataspace is always committed even if the
corresponding transaction is aborted.
2.2. Users
2.2.1. GrantUserclassStatement
Serves to install a new user or to raise the userclass of an installed user.
Syntax:
[57]GrantUserclassStatement ::= GRANT Userclass TO
UserIdentifier
[58] Userclass ::= ACCESS | RESOURCE | DBA
Explanation: If the specified user is not yet installed then the statement installs the
user and sets its userclass to the specified one.
If the specified user is already installed then the statement raises its userclass to the
specified one. In this case the specified userclass must be of higher level than the
user's current userclass.
The userclass of a user defines the permission to login to the database and to create
objects (tables, views, indexes). The special userclass DBA serves to grant the
privileges of a superuser who has all rights.
Table 2.1. User Classes
2.2.2. RevokeUserclassStatement
Serves to lower the userclass of an installed user or to disable a user from login.
Syntax:
Explanation: The specified user must be an installed user with a userclass which is
not lower than the specified one. The statement lowers the userclass of the user to
the level immediately below the specified userclass.
In particular, when userclass ACCESS is revoked the user cannot login to the
database anymore.
If CASCADE is specified, all tables and domains owned by the user are also
deleted (domain information used by other tables is expanded like in DROP
DOMAIN .. CASCADE).
If CASCADE is not specified, all tables and domains owned by the user remain
existent and their ownership is transferred to tbadmin. All database objects in
the user's schema are transferred to the schema public.
2.2.3. AlterPasswordStatement
Serves to install or change a password.
Syntax:
Explanation: Oldpassword must match the password of the current user. The
password is changed to Newpassword.
2.2.4. GrantPrivilegeStatement
Serves to transfer privileges to other users.
Syntax:
[63]GrantPrivilegeStatement ::= GRANT Privileges ON
TableIdentifier TO UserList
[WITH GRANT OPTION]
[64] Privileges ::= ALL [ PRIVILEGES ] |
Privilege [, Privilege]
[65] Privilege ::= SELECT | INSERT | DELETE |
UPDATE [ (FieldList) ]
[66] UserList ::= UserIdentifier [, UserIdentifier]
[9] UserIdentifier ::= Identifier | PUBLIC | USER
Explanation: The specified privileges on the table are granted to the specified user.
If the WITH GRANT OPTION is specified, the privileges are grantable, i.e. the users
get the right to further grant the privileges, otherwise not.
The variant ALL is equivalent to a PrivilegeList where all four Privileges are specified
and where all FieldNames of the table are specified in the UPDATE- privilege.
If PUBLIC is specified then the privileges are granted to all users (new users also
inherit these privileges).
Note
A privilege can be granted both to a specific user and to PUBLIC
at the same time. To effectively remove the privilege from the
user, both grantings must be revoked.
Tip
UpdatePrivileges can be granted on field level whereas SELECT-
privileges cannot. To achieve that effect, however, it is sufficient
to create an appropriate view and to grant SELECT-privilege on
it.
Privileges: The current user must have userclass DBA or must have all specified
privileges with the right to grant them.
2.2.5. RevokePrivilegeStatement
Serves to revoke privileges which have been granted to other users.
Syntax:
Explanation: If the current user is owner of the table, then the specified privileges
are removed from the user such that none of the privileges are left for the user.
If the current user is not owner of the table, then the privilege instances granted by
the current user are removed from the specified users. If some identical privileges
had been additionally granted by other users, they remain in effect (see Example).
If a SELECT privilege is revoked then all views which depend on the specified table
and are not owned by the current user are dropped. If an INSERT or UPDATE or
DELETE privilege is revoked then all views which depend on the specified table and
are not owned by the current user and are updatable are dropped.
Note
It is not an error to REVOKE privileges from a user which had
not been granted to the user. This case is simply treated as an
operation with no effect. This enables an error-free REVOKING
for the user without keeping track of the granting history.
User jim:
User mary:
User anne:
2.3. Domains
2.3.1. CreateDomainStatement
Serves to create a domain in the database. A domain is a named type, optionally
with default value and integrity constraints.
Syntax:
If a DEFAULT specification is given then the created domain has the specified value
as its default value else the domain has no default value. See the
CreateTableStatement for the default mechanism of fields.
The default expression must not contain any subqueries or field references.
If DomainConstraints are specified then all values of the specified domains are
subject to the specified search conditions, e.g. if a record is inserted or updated in a
table and a field of the table is defined on a domain, then the field value is checked
against all domain constraints specified on the domain.
The search condition in DomainConstraint must not contain any subqueries or field
references. The keyword VALUE is used to describe domain values in the search
conditions.
For the check to be performed, the formal variable VALUE in the search condition is
consistently replaced by the field value. The integrity condition is violated, if and only
if the expression NOT (SearchCondition) evaluates to TRUE.
Note
The definition of check constraints is such that NULL values pass
the check in most simple cases. For all examples above, a NULL
value yields the result "unknown" for the search condition, thus
the negation NOT(..) also yields unknown (and thus the
constraint is not violated ).
Example:
Catalog Tables: For each domain, at least one entry into the table sysdomain is
made. This entry also contains a DomainConstraint if specified. For each further
specified DomainConstraint, one additional entry is made.
2.3.2. AlterDomainStatement
Serves to alter a domain in the database, i.e. to set or drop a default, to add or
remove Check Constraints.
Syntax:
Explanation: Note that no field values in the database are changed by any of these
statements.
SET DEFAULT sets the default of the domain to the specified value.
ADD DomainConstraint adds the specified domain constraint to the domain. All
table fields based on the domain are checked whether they fulfill the new
constraint and the statement is rejected if there are any violations against the
new constraint.
2.3.3. DropDomainStatement
Serves to remove a domain from the database.
Syntax:
Explanation: The statement removes the specified domain from the database.
If CASCADE is specified, the domain is removed also in the cases where the
RESTRICT variant would fail. For all table fields based on the domain, the domain
constraints (if any) are integrated as table constraints into the table definitions. The
domain default (if any) is integrated as field default unless the field has been
specified with an explicit DEFAULT value at table definition time.
Note
The semantics of a DROP … CASCADE is such that the integrity
constraints defined via the domain (if any) effectively are not
lost.
2.4. Sequences
2.4.1. CreateSequenceStatement
Creates a sequence.
Syntax:
For a sequence S, there are 2 operations available namely S.nextval and S.currval.
The first S.nextval operation delivers the value specified in StartSpec or 1 as default.
Each S.nextval increases the value of S by the value specified in IncrSpec or 1 as
default. If a MaxSpec has been given and the nextval operation would generate a
value beyond the maximal value then either an error is generated or (if CYCLE has
been specified) the startvalue again is delivered as next value. S.nextval also is
permitted as default specification for a field of a table.
The S.currval operation is only allowed if there has been a S.nextval operation within
the same transaction and again delivers the last value delivered by S.nextval.
S.currval does not increase the current value of S.
Privileges: To create a sequence, the current user must have userclass DBA or
RESOURCE.
For nextval the user must have UPDATE privilege on the sequence.
For currval the user must have SELECT privilege on the sequence.
2.4.2. DropSequenceStatement
Drops a sequence.
Syntax:
[78]DropSequenceStatement ::= DROP SEQUENCE
SequenceIdentifier
DROP SEQUENCE S
2.5. CreateTableStatement
Serves to create a table in the database.
Syntax:
[79]CreateTableStatement ::= StdTableStatement |
FlatTableStatement |
FileTableStatement
[80] StdTableStatement ::= CREATE TABLE [IF NOT EXISTS]
TableIdentifier
[ IkSpec ] [ DataspaceSpec ] [
InmemorySpec ]
( TableElem [ , TableElem ] ... )
[ KeySpec ]
[81] FlatTableStatement ::= CREATE FLAT [FtSizeSpec]
TABLE [IF NOT EXISTS]
TableIdentifier
[ IkSpec ]
( TableElem [ , TableElem ]... )
[82] FtSizeSpec ::= ( IntegerLiteral [ KB | MB ] )
[83] FileTableStatement ::= CREATE
FILE ( FileLiteral [CodePageSpec]
[NullSpec] [DelimSpec] )
TABLE [IF NOT EXISTS]
TableIdentifier
( FieldDefinition [ , FieldDefinition
]... )
[84] CodePageSpec ::= CODEPAGE [IS] CodePage
[ [ WITH | WITHOUT ] PROLOGUE ]
]
[85] CodePage ::= UTF8 | UCS | UCS2 | UCS4 |
UCS2LE | UCS2BE | UCS4LE |
UCS4BE
[86] IkSpec ::= { WITH | WITHOUT } IKACCESS
[87] DataspaceSpec ::= DATASPACE DataspaceIdentifier
[88] InmemorySpec ::= INMEMORY [ PRELOAD | NONE ]
[89] TableElem ::= FieldDefinition |
TableConstraintDefinition
[90] FieldDefinition ::= FieldIdentifier DataTypeSpec
[ DefaultClause |
AUTO_INCREMENT ]
[ FieldConstraintDefinition ]...
[91] DataTypeSpec ::= DataType | DomainIdentifier
[92] DefaultClause ::= DEFAULT Expression
[93] KeySpec ::= StdKeySpec | HCKeySpec
[94] StdKeySpec ::= KEY IS FieldList
[95] HCKeySpec ::= HCKEY [ NOT UNIQUE ] IS FieldList
[96] PrimaryKeySpec ::= StdPrimaryKeySpec |
HCPrimaryKeySpec
[97] StdPrimaryKeySpec ::= PRIMARY KEY ( FieldList )
[98] HCPrimaryKeySpec ::= PRIMARY HCKEY [ NOT UNIQUE ] (
FieldList )
[99] FieldList ::= FieldIdentifier [ , FieldIdentifier ]...
The FlatTableStatement creates a table without primary key and without clustering.
In contrast to standard tables, data is stored in input order. This allow faster data
insertion as data is always appended. Via its SizeSpec the table can be restricted to
occupy no more than a certain maximum of space. If this maximum is exceeded, the
oldest data will automatically be replaced. Thus Flat Tables are ideally suited for data
staging during bulk load processes, as temporary storage and for logging facilities.
An error is returned if a table with the same name already exists unless the IF NOT
EXISTS option is specified. In the latter case no action is performed and no error is
returned.
The IkSpec adjusts whether to create a table with or without internal key (IK) access
path. IKs are used as row identifier, e.g. for referencing records in the base table
after accessing secondary indexes. This IK access path requires additional space of 6
to 8 bytes per record. Alternatively Transbase can use the primary key access path.
In this case the base table's primary key is stored in all index records for referencing
the base table. Depending on how extensive the primary key is, Transbase will
automatically decide at table creation time whether to create a table WITH or
WITHOUT IKACCESS . This guarantees optimal space efficiency. If the primary key
occupies no more that the IK, then the table is created WITHOUT IKACCESS. Else an
IK access path is added by default.
The InmemorySpec allows to specify that a table resides in the main memory cache
of the database and will never be swapped out. Specification of NONE is the default
(can be used in the AlterTableStatement to reset Inmemory property of the table). If
the PRELOAD option is chosen, the table is loaded completely at boottime, else it
appears in the cache one by one on a database page basis as it is accessed (the
accessed pages then remaining in the cache). The InmemorySpec refers to the table
itself and all its secondary indexes, but not on LOB objects possibly belonging to the
table. The Inmemory property of a table can be changed with the
AlterTableStatement.
Secondary indexes can also be created on Flat Tables. As these tables do not have a
primary key, secondary indexes are only possible on Flat Tables WITH IKACCESS.
Typically such secondary indexes are added once the load process is complete, so
load performance is not compromised by secondary index maintenance.
Each FieldDefinition specifies a field of the table. The ordering of fields is relevant for
the *-notation in SELECT * FROM ....
If the key specification is omitted, the combination of all fields (except BLOB and
CLOB fields)implicitly is the key. No column of type BLOB or CLOB is allowed to be
part of an explicitly specified key.
Unless a NOT UNIQUE specification is given, insert and update operations which
produce records with the same values on all key fields are rejected.
The "KEY IS .." specification creates a table with a compound B-tree index.
The "HCKEY IS .." specification creates a table with a HyperCube index. Key fields of
a HyperCube table are restricted to exact arithmetic types (BIGINT, INTEGER,
SMALLINT, TINYINT, NUMERIC). If NOT UNIQUE is specified, then also duplicates on
the key combination are allowed. NOT UNIQUE, however, is restricted to HyperCube
tables. On each HyperCube key field a NOT NULL constraint and a CheckConstraint
must exist.
Note
If there exists one field (or one or more field combinations)
which is known to be unique in the table, it is strongly
recommended to explicitly specify it as key of the table. One
advantage is that uniqueness is guaranteed; another advantage
is much better performance in update operations (which
normally do not change key values).
The current user becomes owner of the table and gets SELECT-privilege, INSERT-
privilege, DELETE-privilege on the table and UPDATE-privilege on all fields of the
table. All privileges are grantable.
2.5.1. Defaults
Each field has a (explicitly specified or implicit) default value which is taken as input
value if a field value is not explicitly specified in an INSERT statement (or by an
INSERT via a view). If a DEFAULT clause is specified with an expression evaluating to
d, then d is the (explicitly specified) default value for that field, otherwise, if the field
is based on a domain with explicit default value d, then d is the default value,
otherwise NULL is the default value.
In the DEFAULT Expression, neither field references nor subqueries are allowed.
2.5.2. AUTO_INCREMENT Fields
An AUTO_INCREMENT field serves to generate unique key values. At most one
AUTO_INCREMENT field is allowed in a table. Its data type must be one of TINYINT,
SMALLINT, INTEGER, BIGINT.
In table T, Id is the only key field. For each INSERT statement which does not assign
a value for Id but uses a fieldlist (Prop1,Prop2), a unique value for the field Id
automatically is generated. Value generation starts with the value 1. See below how
the assigned value can be transferred to the application.
then the next generated value would be 11. Automatic value generation always takes
the maximum value so far plus one. At definition time of the table,a start value for
the automatic numbering may be specified:
Note that an AUTO_INCREMENT field may overflow which results in an error. So the
data type should be chosen appropriately.
2.5.3. TableConstraintDefinition
FieldConstraintDefinition
Overview syntax for specification of integrity constraints in a CreateTableStatement.
Syntax:
[100]TableConstraintDefinition ::= [ CONSTRAINT
ConstraintIdentifier ]
TableConstraint
[101] FieldConstraintDefinition ::= [ CONSTRAINT
ConstraintIdentifier ]
FieldConstraint
[102] TableConstraint ::= PrimaryKeySpec |
CheckConstraint | ForeignKey
[103] CheckConstraint ::= CHECK (SearchCondition )
[104] ForeignKey ::= FOREIGN KEY (FieldList )
ReferencesDef
[105] ReferencesDef ::= REFERENCES TableIdentifier [ (
FieldList ) ] [ ON DELETE Action
] [ ON UPDATE Action ]
[106] Action ::= NO ACTION | CASCADE | SET
DEFAULT | SET NULL
[107] FieldConstraint ::= NOT NULL | PRIMARY KEY |
CheckConstraint | ReferencesDef
Note
All constraints are effectively checked after execution of each
SQL query.
2.5.4. PrimaryKey
Specify the main key for a table.
Syntax:
If no PrimaryKey is specified, all fields except BLOB or CLOB fields form the primary
key in the order of specification.
The SQL-2 formulation PRIMARY KEY (f1, f2, ..,fn) is equivalent to the
alternative (Transbase proprietary) formulation KEY IS f1,f2,..,fn (see below for
an example). The SQL-2 formulation PRIMARY HCKEY [NOT UNIQUE](f1, f2, ..,fn)
is equivalent to the alternative (Transbase proprietary) formulation HCKEY [NOT
UNIQUE] IS f1,f2,..,fn
For the semantics of the key specification see CreateTableStatement . See also the
Performance Guide for more details.
The following two examples are equivalent. The first is the official SQL-2 notation
supported by Transbase, the second is an alternative notation also supported by
Transbase (note that the formulations exclude each other):
The following two examples show alternative formulations of primary key via a
TableConstraint and a FieldConstraint - this is possible if and only if one single field
constitutes the primary key:
CREATE TABLE suppliers
( suppno INTEGER,
name CHAR(*),
address CHAR(*),
PRIMARY KEY(suppno)
)
2.5.5. CheckConstraint
Specify a CheckConstraint for a table.
Syntax:
In detail, for all records of the table which are inserted or updated an error is
reported if the condition NOT (SearchCondition) evaluates to TRUE.
In the first example, only one field is involved. Therefore it can also be formulated
using the syntactic variation FieldConstraint:
Note that in a FieldConstraint, there is no comma between the field definition and
the constraint definition.
Catalog Tables:
One entry into the table sysconstraint is made for each check constraint.
Null values:
The definition of integrity violation is such that NULL values pass the test in most
cases. In the example above, the constraint "price100" is not violated by a record
with a NULL value on price, because the SearchCondition evaluates to unknown
(thus the negation NOT(..) also evaluates to unknown but not to TRUE).
To make NULL values fail the test, one must explicitly formulate the
CheckConstraint like: CHECK (price IS NOT NULL AND ...).
To specify that the value must not be null the shorthand notation NOT NULL can
be used in the field definition, but then it is not part of the specified constraint:
CREATE TABLE quotations
( suppno INTEGER,
partno INTEGER,
price NUMERIC(9,2) NOT NULL
CONSTRAINT price100 CHECK (price < 100),
delivery_time INTEGER
)
2.5.6. ForeignKey
Specify a Referential Constraint between 2 tables.
Syntax:
With respect to the constraint, the table containing the fields of the foreign key is
called the referencing table, the table which is mentioned after REFERENCES is called
the referenced table. Analogously, the fields in the FOREIGN KEY clause and the
(explicit or implicit) fields in the REFERENCES clause are called referencing fields and
referenced fields, resp. The referencing and referenced fields must have same
number and identical types.
If no field name list is specified in the REFERENCES clause, then the primary key
combination of the referenced table constitutes the referenced fields. The referenced
fields either must constitute the primary key or must have a UNIQUE INDEX.
For each record in the referencing table whose referencing fields do not have any
NULL value, there must be one record in the referenced table with identical field
values on the corresponding referenced fields.
Let RG and RD the referencing table and the referenced table, resp., i.e. RG
references RD.
If CASCADE is specified:
A deletion of record t in RD triggers the deletion of all matching records in the RG
(thus maintaining the referential constraint). An update of a referenced field in RD
triggers the corresponding update of all referencing fields in RG to the same value
(thus maintaining the referential constraint).
In this (single field reference) example, also the syntactic shorthand variant of
FieldConstraint can be used as shown below:
CREATE TABLE quotations
( suppno INTEGER
CONSTRAINT quotrefsupp
REFERENCES suppliers(suppno)
ON DELETE SET NULL,
partno INTEGER
CONSTRAINT quotrefpart
REFERENCES inventory(partno)
ON DELETE CASCADE,
price NUMERIC(9,2),
delivery_time INTEGER
)
Catalog Tables:
For a referential constraint with a n-ary field combination, n records are inserted
into sysrefconstraint.
Performance:
DELETE and UPDATE operations on referenced tables which require the
referential check on the referencing table are slow if the referencing table does not
have a secondary index (or the primary key) on the referencing fields.
INSERTs and UPDATEs on referencing fields requiring the referential check on the
referenced table are fast because by definition there is a index on the referenced
fields.
Note
Like all constraints, referential constraints are effectively
checked after execution of each SQL query. In general, it is
therefore not possible to insert records into tables in arbitrary
order if there exists a referential constraint between them.
2.6. AlterTableStatement
Serves to alter fields of a table and to add or remove table constraints.
Syntax:
2.6.1. AlterTableConstraint
Serves to add or remove a table constraint.
Syntax:
Explanation:
In the TableConstraintDefinition all except the redefinition of PRIMARY KEY is allowed
on this position.
The ADDition of a table constraint is rejected if the values in the database do not
fulfill the constraint.
2.6.2. AlterTableChangeField
Serves to alter a the default specifiation of a field in a table.
Syntax:
Explanation:
2.6.3. AlterTableRenameField
Serves to rename a field of a table.
Syntax:
changes the name of a field of a table. The RENAME operation fails in case of a name
conflict.
2.6.4. AlterTableFields
Serves to add, modify or drop one or more fields.
Syntax:
[114]AlterTableFields ::= ALTER TABLE TableIdentifier
AlterTableElem [ , AlterTableElem ] ...
[115] AlterTableElem ::= ADD FieldDefinition [ PositionClause ]
|
MODIFY FieldDefinition [
PositionClause ] |
DROP FieldIdentifier
[116] PositionClause ::= [ FIRST | AFTER FieldIdentifier ]
[90] FieldDefinition ::= FieldIdentifier DataTypeSpec
[ DefaultClause | AUTO_INCREMENT ]
[ FieldConstraintDefinition ]...
Explanation:
ADD FieldDefinition PositionClause adds a new field and initializes it with the
explicitly specified or implicit default value. When no PositionClause is given the
field is placed as the last field of the table. Otherwise the field will be inserted at
the specified position.
2.6.5. AlterTableRename
Serves to rename a table.
Syntax:
changes the name of a table. The RENAME operation fails in case of a name conflict.
2.6.6. AlterTableMove
Serves to reorganize a table.
Syntax:
[118] AlterTableMove ::= ALTER TABLE TableIdentifier MOVE
[ TO ] MoveTarget
[119] MoveTarget ::= BLOCK BlockNo | DATASPACE
DataspaceIdentifier
[120] BlockNo ::= IntegerLiteral
[8] DataspaceIdentifier ::= Identifier
All indices of the table, the lobcontainer (if the table contains one or more lob fields)
and the table itself are moved.
2.6.7. AlterTableInmemory
Serves to change the inmemory property of a table together with its indexes
Syntax:
2.7. DropTableStatement
Serves to drop a table in the database.
Syntax:
Explanation: The specified table, all indexes and all triggers defined on that table
are dropped. All views which are directly or transitively based on the specified table
are also dropped. Error is returned if the table does not exis unless the IF EXISTS
option is specified.
Privileges: The current user must have userclass DBA or must be owner of the
table.
2.8. CreateIndexStatement
Serves to create an index, fulltext index or a bitmap index on a table.
Syntax:
[123]CreateIndexStatement ::= StandardIndexStatement |
HyperCubeIndexStatement |
FulltextIndexStatement |
BitmapIndexStatement
2.8.1. StandardIndexStatement
Serves to create a standard index on a table.
Syntax:
Explanation: An index with the specified name is created on the specified fields or
expressions resp. of the specified table.
In most cases, indexes are built on one or several basic fields of a table. This means
that the Expressions are simple field names.
Indexes have no effect on query results except for possibly different performance
(depending on the query type) and possibly different sort orders of query results (if
no ORDER BY-clause is specified in the query).
Note
It is unwise to create a standard B-tree index on the highest
weighted key fields because in Transbase an unnamed (multi
field) index exists on the key fields anyway.
Privileges: The current user must have userclass DBA or must have userclass
RESOURCE and be owner of the table.
Although the (t1,t2) combination is not declared to be key in T, you might want to
specify a constraint that the value pairs are unique.
There are 2 different ways to achieve this. One way is to specify the UNIQUE clause
for Tx at creation time. The alternative is to use the KEY IS clause.
The KEY IS clause is useful if one more field (t3) should be part of the index but the
uniqueness property should remain as sharp as before and not be weakened to the
triple.
CREATE INDEX Tx on T(t1,t2,t3) KEY IS t1,t2
This construction is also useful for example for efficiently supporting the following
kind of query:
The index key on (t1,t2) supports the efficient processing of the search condition,
and the integration of field t3 into the index directly supports the delivery of t3
without accessing the base table T.
2.8.2. HyperCubeIndexStatement
Serves to create a Hypercube index on a table.
Syntax:
Privileges: The current user must have userclass DBA or must have userclass
RESOURCE and be owner of the table.
The index supports the evaluation of the interval restriction on both fields which are
not efficiently supported by a standard B-Tree index.
2.8.3. FulltextIndexStatement
Serves to create a fulltext index on a VARCHAR, CHAR, BLOB or CLOB field of a
table.
Syntax:
Explanation: All explanations are given in the separate chapter on Fulltext Indexes
.
2.8.4. BitmapIndexStatement
Serves to create a bitmap index on a BOOL, TINYINT, SMALLINT or INTEGER field of a
table.
Syntax:
[135]BitmapIndexStatement ::= CREATE BITMAP INDEX
IndexIdentifier
ON TableIdentifier
(FieldIdentifier)
Explanation: A bitmap index with the specified name is created on the specified
field of the specified table.
Bitmap indexes are preferably used for non-selective columns having few different
values (e.g. classifications). Bitmap indexes are innately very space efficient. With
their additional compression in average they occupy less than one bit per index row.
A bitmap index can be created on any base table (B-Tree or Flat) having a single
INTEGER field as primary key or an IKACCESS path.
There must not be an index with the same name on any table in the database. There
must not be an index on the same field of the table.
2.9. DropIndexStatement
Serves to drop an index.
Syntax:
Privileges: The current user must have userclass DBA or must be owner of the table
on which the index has been created.
2.10. Triggers
2.10.1. CreateTriggerStatement
Serves to create a trigger on a table.
Syntax:
[137]CreateTriggerStatement ::= CREATE TRIGGER
TriggerIdentifier
TriggerActionTime TriggerEvent
ON TableIdentifier
[ REFERENCING OldNewAliasList ]
TriggeredAction
[138] TriggerActionTime ::= BEFORE | AFTER
[139] TriggerEvent ::= INSERT | DELETE | UPDATE [ OF
FieldList ]
[99] FieldList ::= FieldIdentifier [ , FieldIdentifier
]...
[140] OldNewAliasList ::= OldNewAlias [ OldNewAlias ]
[141] OldNewAlias ::= OLD [ ROW ] [ AS ]
CorrelationIdentifier |
NEW [ ROW ] [ AS ]
CorrelationIdentifier
[142] TriggeredAction ::= [ FOR EACH { ROW | STATEMENT
}]
[ WHEN ( SearchCondition ) ]
TriggerSQLStatement
[143] TriggerSQLStatement ::= DMLorCallStatement |
BEGIN ATOMIC
DMLorCallStatement
[; DMLorCallStatement ]... END
[144] DMLorCallStatement ::= InsertStatement |
UpdateStatement |
DeleteStatement |
CallStatement
Explanation:
For a row trigger, the specified actions may refer to the actually processed record
values. The syntax NEW.fieldname is the value of fieldname of the inserted record or
the (possibly changed) value of fieldname for an record being updated. The syntax
OLD.fieldname is the value of fieldname of a deleted record or the original field value
for an record being updated.
When a trigger is fired it runs under the privileges of the creator of the trigger.
If more than one trigger qualifies at the same TriggerActionTime, the order of
execution is defined by ascending creation date.
UPDATEs and DELETEs on a table T which has triggers and also is the target of a
referential constraint require special consideration. Referential actions (called
reference triggers here) may occur if the reference constraint is specified with
CASCADE, SET NULL or SET DEFAULT.
(1) Before-StatementTriggers
(2) Before-Row Triggers
(3) Reference Triggers
(4) After-Row Triggers
(5) After-Statement Triggers
Note that the firing of a trigger may cause the firing of subsequent triggers. It is
recommended to use triggers moderately to keep the complexity of nested actions
small. In particular, it is strongly discouraged to construct a set of triggers which lead
to the effect that a modification of a table T fires a trigger which transitively fires
another trigger which also tries to modify table T. This may cause endless loops or
nonpredictable effects.
Privileges: The current user must have userclass DBA or RESOURCE and becomes
the owner of the trigger. Additionally, the current user must have SELECT privilege
on the table on which the trigger is created.
2.10.2. DropTriggerStatement
Serves to drop a trigger.
Syntax:
Explanation: The specified trigger is dropped. Note that the trigger also is dropped
if the table is dropped on which the trigger is defined.
Privileges: The current user must have userclass DBA or must be owner of the
trigger.
2.11. Views
2.11.1. CreateViewStatement
Serves to create a view in the database.
Syntax:
[146]CreateViewStatement ::= CREATE [OR REPLACE] VIEW
ViewIdentifier
[ ( FieldList ) ]
AS SelectStatement [ WITH
CHECK OPTION ]
[99] FieldList ::= FieldIdentifier [ , FieldIdentifier
]...
The rows in a view are not stored in the database, but only the view definition.
Queries on views are simply evaluated as if the view definition were incorporated
into the query.
A view can be used in any SelectStatement like a table. Especially, existing views can
be used for the definition of a view.
The current user becomes owner of the view. If the view is not updatable, the user
only gets a non-grantable SELECT-privilege on the view, otherwise the user gets the
same view privileges on the view as those on the (one and only) table or view which
occurs in the defining SelectStatement.
A view may also contain one ore more RemoteTableNames. When evaluating remote
views, the privileges of the view owner apply for accessing remote tables. However,
the current user must have at least ACCESS privilege for the remote database. If an
updatable remote view is is specified as the target of an UPDATE or DELETE
operation, all subqueries (if any) must specify tables residing on the same database.
However, if the target table is local, any tables (remote or local) may be specified in
subqueries.
Privileges: The current user must have userclass DBA or RESOURCE and must have
the privileges for the defining SelectStatement.
A non-updatable view:
CREATE VIEW totalprice (supplier, part, total)
AS
SELECT name, description, price * qonorder
FROM suppliers, quotations, inventory
WHERE suppliers.suppno = quotations.suppno
AND inventory.partno = quotations.partno
AND qonorder > 0
2.11.2. DropViewStatement
Serves to drop a view in the database.
Syntax:
All views which are directly or transitively based on the specified view are also
dropped. Error is returned if the view does not exist unless the IF EXISTS option is
specified.
Privileges: The current user must have userclass DBA or must be owner of the
view.
3.1. FieldReference
The construct FieldReference is used to refer to a specific field of a specific table.
Syntax:
Explanation:
The following examples show the usage of Field in a SelectStatement. The last
example explains the use of CorrelationName in QualifiedField.
SELECT suppno FROM suppliers
3.2. User
The keyword USER serves to refer to the name of the current user.
3.3. Expression
An Expression is the most general construct to calculate non-boolean values.
Syntax:
[150]Expression ::= [Unary] Primary [ Binary [Unary]
Primary ]...
[151] Unary ::= + | - | BITNOT
[152] Binary ::= + | - | * | / | BITAND | BITOR |
StrConcat
[153] StrConcat ::= ||
Explanation: For Primaries of arithmetic types all operators are legal and have the
usual arithmetic meaning. Additionally, the binary '+' is also defined for character
types and then has the meaning of text concatenation.
Some of these operators are also defined for The Data Types Datetime and
Timespan.
The operator precedences for arithmetic types are as usual: Unary operators bind
strongest. BITAND / BITOR bind stronger than '*' and '/' which in turn bind stronger
than binary '+' and '-'.
The operator || denotes concatenation of string values and is an alternative for + for
strings, see example below.
Note
Computation of an Expression may lead to a type exception if
the result value exceeds the range of the corresponding type.
See Type Exceptions and Overflow. See also Null Values .
- 5.0
-5.0
price * -1.02
'TB/SQL' + ' ' + 'Language'
'Dear' + title + name
'Dear' || title || name
+ 1.1 * (5 + 6 * 7)
In all but the last example, the constituting Primaries are Fields or Literals. In the
last example, the second Primary is itself an Expression in parentheses.
For the operands BITOR and BITAND see The TB/SQL Datatypes BITS(p) and
BITS(*).
Syntax:
Explanation: The functional notation CAST(…) is the official SQL standard syntax,
the postfix notation is the traditional Transbase syntax.
Caution
CASTing produces a type exception when the value exceeds the
range of the target type.
3.5. SimplePrimary
A SimplePrimary is the building unit for CastPrimaries or Expressions.
Syntax:
[156]SimplePrimary ::= Literal | User |
FieldReference |
Parameter |
( Expression ) |
( SubTableExpression ) |
MathematicalStdFunction |
SetFunction |
WindowFunction |
ConditionalExpression |
TimeExpression |
SizeExpression |
LobExpression |
StringFunction |
SignFunction |
ResultcountExpression |
SequenceExpression |
ODBC_FunctionCall |
FunctionCall |
LastInsertIdFunc |
LastUpdateFunc |
CrowdStatusFunc |
ReplicationStatusFunc
[157] Parameter ::= # IntegerLiteral ( DataType ) |
Colon StandardIdentifier |
Questionmark
[158] Colon ::= :
[159] Questionmark ::= ?
Explanation:
Parameter is the means to specify a parameter for a stored query in an application
program. The notations without data type specification can be used wherever the
type of the parameter can be deduced from its context (e.g. Field = ?). This can be
enforced by a CAST operation on the parameter.
price
0.5
(price + 0.5)
(SELECT suppno FROM suppliers WHERE name = 'TAS')
3.5.1. MathematicalStdFunction
A set of predefined mathematical functions
Syntax:
[160]MathematicalStdFunction ::= TrigonometricFunction |
PiFunction | ExpLogFunction |
PowLogFunction | SqrtFunction |
FloorFunction | CeilFunction |
SignFunction |
WidthBucketFunction
[161] TrigonometricFunction ::= { SIN | COS | TAN | SINH |
COSH | TANH | ASIN | ACOS |
ATAN } ( Expression )
[162] PiFunction ::= PI()
[163] ExpLogFunction ::= { EXP | LOG10 | LN }
( Expression )
[164] PowLogFunction ::= { POWER | LOG } ( Expression ,
Expression )
[165] SqrtFunction ::= SQRT ( Expression )
[166] FloorFunction ::= FLOOR ( Expression )
[167] CeilFunction ::= CEIL ( Expression )
[168] SignFunction ::= SIGN ( Expression )
[169] WidthBucketFunction ::= WIDTH_BUCKET ( Expression ,
Expression , Expression ,
Expression )
Explanation:
SIN(x), COS(x), TAN(x) compute the sine, cosine and tangent of x, resp.
SINH(x), COSH(x), TANH(x) compute the hyberbolic sine, hyberbolic cosine and
hyberbolic tangent of x, resp.
ASIN(x), ACOS(x), ATAN(x) compute the inverse of sine, cosine and tangent of x,
resp.
LN(x), LOG10(x) compute the value of the natural logarithm and the base 10
logaritm of x, resp.
FLOOR(x) computes the largest integer value less or equal to x. CEIL(x) computes
the smallest integer value greater or equal x. The result type corresponds to the
argument type, in case of input type NUMERIC(p,s) the result type has scale 0.
Function SIGN computes the sign of a numerical expression. It returns -1 for negaive
values, +1 for positive values, else 0.
3.5.2. SetFunction
A SetFunction computes one value from a set of input values or input records.
Syntax:
Explanation:
For all other forms of a SetFunction, the input is the set of values which results from
application of the Expression to the input records. If a DistinctFunction is specified,
all duplicate values are removed before the SetFunction is applied. Functions COUNT,
SUM, AVG, MAX, MIN compute the cardinality, the sum, the average, the maximum
and the minimum value of the input value set, resp.
Functions COUNT, MIN and MAX are applicable to all data types.
Functions AVG and SUM are applicable to arithmetic types and to TIMESPAN.
The result type of COUNT is BIGINT. The result type of AVG on arithmetic types is
DOUBLE. For all other cases the result type is the same as the type of the input
values. Of course, the CAST operator can be used to force explicit type adaptation.
SetFunctions except COUNT ignore null values in their input. If the input set is
empty, COUNT delivers 0, all others deliver the null value.
Note
Function SUM and AVG may lead to a type exception if the sum
of the input values exceeds the range of the result type. See
Type Exceptions and Overflow.
The functions with suffix _POP and _SAMP calculate variance and standard deviation
of an input set X1, .. ,XN. They convert arithmetic input to DOUBLE and deliver
DOUBLE.
Let XDS be SUM(Xi - XM) POW 2 , i.e. the sum of squared distances between Xi and
XM (over all Xi).
Function VAR_POP delivers XDS / N .
Functions VAR and VAR_SAMP deliver XDS / (N-1) . If N is less than 2, the function
returns NULL.
Examples:
How many parts are delivered by those suppliers who deliver more than 2
parts?
3.5.3. WindowFunction
While SetFunctions aggregate a set of input rows into one result row, a
WindowFunction calculates one result row for every input row. Here the aggregates
are calculated over a set of rows in the vicinity of the current input row.
Syntax:
[173] WindowFunction ::= WindowAggregate ( ExpressionList
)
OVER ([PartitionClause]
[OrderByClause] [WindowClause])
[174] WindowAggregate ::= { SUM | AVG | COUNT | MIN | MAX
| RANK | DENSE_RANK | VAR_POP
| VAR_SAMP | VAR | STDDEV_POP |
STDDEV_SAMP | STDDEV }
[175] PartitionClause ::= PARTITION BY { ( ExpressionList ) |
ExpressionList }
[176] OrderByClause ::= ORDER BY { ( ExpressionList ) |
ExpressionList }
[177] WindowClause ::= { ROWS | RANGE }
{ PrecedingClause |
BETWEEN LowerboundClause AND
UpperboundClause }
[178] PrecedingClause ::= UNBOUNDED PRECEDING |
ValueExpression PRECEDING |
CURRENT ROW
[179]LowerboundClause ::= UNBOUNDED PRECEDING |
ValueExpression { PRECEDING |
FOLLOWING } |
CURRENT ROW
[180]UpperboundClause ::= UNBOUNDED FOLLOWING |
ValueExpression { PRECEDING |
FOLLOWING } |
CURRENT ROW
[181] ValueExpression ::= <a logical or physical offset>
Explanation: WindowFunction are useful for calculating rankings and running totals
or moving averages. They are typically used in the SELECT clause of a
SelectExpression. They operate on a query result set, i.e. after FROM, WHERE,
GROUP BY and HAVING clauses are evaluated. First this result is partitioned
according to the PartitionClause. Then each partition is processed row by row, so
every row will become the current row once. The aggregate for the current row is
calculated OVER a set of rows (window) in this partition, as defined by the
WindowClause.
ROWS specifies that the windows is defined between absolute boundary offsets. If
ROWS is specified, there are no restrictions to the following OrderByClause and it is
completely optional. Windows boundaries refer to row positions relative to the
current row.
If the limits of a ROWS window are BETWEEN CURRENT ROW AND 5 FOLLOWING,
then the current row and the five following rows are within the window. Therefore
this ROWS window contains at most 6 rows.
RANGE specifies that the window is defined between relative boundary offsets. If
RANGE is specified with a ValueExpression boundary, the OrderByClause is
mandatory and must contain exactly one expression. These ValueExpression
windows boundaries refer to the one field used in the OrderByClause.
If the limits of a RANGE window are BETWEEN CURRENT ROW AND 5 FOLLOWING,
then the window contains all rows whose sort field is
(1) equal or larger than the sort expression of the current row and
(2) equal or smaller than the sort expression of the current row + 5.
Therefore this RANGE window can contain any number of rows.
Defaults:
If the PartitionClause is missing, the defaults is PARTITION BY NULL, i.e. no
partitioning is applied.
3.5.4. StringFunction
StringFunctions accept character expressions (strings) as input and compute integers
or strings. NULL is returned when one of the operands is NULL.
Syntax:
[182]StringFunction ::= PositionFunction |
InstrFunction |
LengthFunction |
UpperFunction |
LowerFunction |
TrimFunction |
SubstringFunction |
ReplaceFunction |
ReplicateFunction |
TocharFunction
The concatenation of strings is denoted using the infix operators '+' or '||' (see
Expression ).
3.5.4.1. PositionFunction
The POSITION function searches a string inside another string and computes the
position of its first occurrence if any.
Syntax:
Explanation: Source may be of type CLOB, VARCHAR, CHAR. Search must be CHAR
or VARCHAR. Resulttype is INTEGER.
3.5.4.2. InstrFunction
The INSTR function searches a string inside another string. It provides a superset of
the functionality of POSITION.
Syntax:
In general, the function searches the string "Search" in "Source" starting the search
on the s-th character of "Source" (for s >= 1). If o > 1 then the o-th occurrence of
"Search" is searched.
If s <= -1 then the search is made backwards starting with the |s|-th character
counted relative to the end of "Source".
3.5.4.3. LengthFunction
The CHARACTER_LENGTH function computes the length of a CHAR, VARCHAR or
CLOB value in characters.
Syntax:
[188]LengthFunction ::= CHARACTER_LENGTH ( Source )
[339] Source ::= VALUES ( ValueList ) |
TABLE ( ( ValueList ) [, (ValueList) ]...
)|
TableExpression |
DEFAULT VALUES
Explanation: Note that the function delivers number of characters, not number of
bytes. For counting bytes use the operator SIZE.
3.5.4.4. UpperFunction, LowerFunction
The UPPER and LOWER function maps uppercase letters to lowercase letters and vice
versa. Syntax:
Syntax:
3.5.4.5. TrimFunction
The TRIM function eliminates in a string leading and/or trailing characters belonging
to a specifiable character set.
Syntax:
FROM must be specified if and only if at least one of Trimset or Trimspec is specified.
If Trimspec is not specified, BOTH is implicit.
If Trimset is not specified, a string consisting of one ' ' (blank) is implicit.
3.5.4.6. SubstringFunction
The SUBSTRING function extracts a substring from a string value.
Syntax:
[194]SubstringFunction ::= SUBSTRING ( Source FROM
Startpos [ FOR Length ] ) |
SUBSTR ( Source, Startpos [,
Length ] )
[339] Source ::= VALUES ( ValueList ) |
TABLE ( ( ValueList ) [, (ValueList)
]... ) |
TableExpression |
DEFAULT VALUES
[238] Startpos ::= Expression
[239] Length ::= Expression
Explanation: Source must be CHAR, VARCHAR or CLOB. Startpos and Length must
be arithmetic. Resulttype is same as Sourcetype.
The function constructs a string which results from Source by extracting Length
letters beginning with the one on position Startpos. If Length is not specified or is
larger than the length of the substring starting at Startpos, the complete substring
starting at Startpos constitutes the result.
If Startpos is less equal zero then Length (if specified) is set to Length + Startpos
and Startpos is set to 1 .
If Startpos is larger than the length of Source, the result is the empty string.
3.5.4.7. ReplaceFunction
The REPLACE function replaces substrings or characters in a string.
Syntax:
The function constructs from Source a result string by substituting certain substrings
in Source.
If Subsspec is not defined or defined as WORDS, then all occurrences of Subs1 are
replaced by Subs2 (after substitution, the inserted string Subs2 is not further
checked for substitution).
If Subsspec is defined as CHARS, then Subs1 and Subs2 must have same length and
each character in Source which is equal to the i-th character in Subs1 for some i is
replaced by the i-th character of Subs2.
3.5.4.8. ReplicateFunction
The REPLICATE function replicates a string a specified number of times.
Syntax:
The function constructs a result string by concatenating Source t times where t is the
value of Times. Error occurs if t is less than zero.
Syntax:
Note that it is possible to build a phonetic secondary index onto a field of a table
(see CREATE INDEX statement ).
3.5.5. TocharFunction
The Tocharfunction is a shorthand notation for a CAST operator to STRING.
Syntax:
3.5.6. ResultcountExpression
Numbers result records of a select query.
Syntax:
The usage of RESULTCOUNT is also allowed in more than one block (for example in a
UNION of blocks) and also in nested blocks, but the effect then is not deterministic.
If a query contains several parallel blocks combined with UNION or similar set
operators, then a numbering of the result records is achieved by surrounding it with:
SELECT RESULTCOUNT, * FROM ( <original query> ) .
3.5.7. SequenceExpression
Performs a nextval or currval operation on a sequence.
Syntax:
Syntax:
3.5.8.1. IfExpression
The IfExpression is the simplest ConditionalExpression. It computes one of 2 values
depending on one condition.
Syntax:
3.5.8.2. CaseExpression
The CaseExpression is the most general ConditionalExpression. It comes in the
variants simple CASE and searched CASE.
Syntax:
[207] CaseExpression ::= SearchedCaseExpression |
SimpleCaseExpression
[208]SearchedCaseExpression ::= CASE
SearchedWhenClause [
SearchedWhenClause ]...
[ ELSE Expression ]
END
[209] SearchedWhenClause ::= WHEN SearchCondition THEN
Expression
[210] SimpleCaseExpression ::= CASE CaseOperand
SimpleWhenClause [
SimpleWhenClause ]...
[ ELSE Expression ]
END
[211] SimpleWhenClause ::= WHEN WhenOperand THEN Result
[212] CaseOperand ::= Result
[213] Result ::= Expression
[214] WhenOperand ::= Expression
Explanation:
For both variants, all Result expressions in the THEN clauses as well as the Result of
the ELSE clause (if existent) must be type compatible. The result type of the
CaseExpression is the highest level type of all participating result expressions.
For the SimpleCaseExpression, the types of the CaseOperand and all WhenOperands
must be type compatible.
UPDATE quotations
SET price = price * CASE
WHEN price > 25 THEN 1.5
WHEN price > 19.5 THEN 1.4
WHEN price > 5 THEN 1.3
WHEN price > 1 THEN 1.2
ELSE 1.1
END
Syntax:
3.5.8.4. CoalesceExpression, NvlExpression,
NullifExpression
COALESCE and NVL are shorthand notations for a CASE or IF which maps an
Expression from the NULL value to a defined value. The NULLIF is a shorthand
notation for a CASE or IF which maps an expression from a defined value to the
NULL value.
Syntax:
Explanation:
All involved expressions must be of compatible types. The result type is the highest
level type of the result expressions.
COALESCE delivers the first expression which does not evaluate to NULL if there
exists such an expression otherwise NULL. Thus it is equivalent to an expression of
the form:
CASE
WHEN x1 IS NOT NULL THEN x1
WHEN x2 IS NOT NULL THEN x2
...
ELSE NULL
END
Note that with COALESCE, each involved expression is denoted only once in contrast
to an equivalent CASE or IF construction. Therefore, in general, the COALESCE runs
faster.
3.5.9. TimeExpression
A TimeExpression is an expression which is based on value of type DATETIME or
TIMESPAN
Syntax:
[224]TimeExpression ::= [ Selector OF ] { Constructor |
TimePrimary }
[225] Selector ::= DAY | WEEKDAY | WEEK | ISOWEEK |
QUARTER | YY | MO | DD | HH | MI |
SS | MS
[226] Constructor ::= CONSTRUCT Timetype (
ConstituentList )
[227] Timetype ::= { DATETIME | TIMESPAN } [
RangeSpec ]
[228] ConstituentList ::= Constituent [, Constituent ]...
[229] Constituent ::= Expression | SubTableExpression
[230] TimePrimary ::= DatetimeLiteral |
TimespanLiteral |
FieldReference |
Parameter |
CURRENTDATE | SYSDATE |
( Expression ) |
( SubTableExpression ) |
SetFunction |
TruncFunction |
ConditionalExpression
[231] TruncFunction ::= TRUNC ( Expression )
Explanation: For all semantics see The Data Types Datetime and Timespan.
Note that a selector as well as a constructor binds more strongly than a CAST
operator (see also Precedence of Operators ).
DATETIME[YY:MS](1989-6-8 12:30:21.032)
CURRENTDATE
HH OF CURRENTDATE
WEEKDAY OF CURRENTDATE
CONSTRUCT TIMESPAN(:year,:month,:day)
3.5.10. SizeExpression
The SIZE [ OF ] Operator computes the size (length) of a CHAR, CLOB, BINCHAR or
BLOB Expression in bytes.
Syntax:
Explanation: The resulting type of the argument of the SIZE operator must be
CHAR(*), (VAR)CHAR(p), BINCHAR(*), BINCHAR(p), BITS(*), BITS(p), BLOB or
CLOB. The resulting type of the operator is INTEGER.
If the argument of the operator is the NULL value, then the operator delivers NULL.
Otherwise the operator delivers a value that denotes the size (in bytes, for BITS in
bits) of its argument. If the argument is CHAR(*) or (VAR)CHAR(p) then the trailing
\0 is not counted. If the argument is BINCHAR(*) or BINCHAR(p) then the length
field is not counted. If the argument is BLOB then the number of bytes that the BLOB
object occupies is delivered. If the argument is CLOB then the number of bytes (not
characters) that the CLOB object occupies is delivered.
Note also the strong binding of the SIZE operator (see Precedence of Operators ).
3.5.11. LobExpression
A LobExpression delivers a LOB value or a part of a LOB value.
Syntax:
The field must be of type BLOB, Lwb and Upb must be of type TINYINT, SMALLINT,
INTEGER or BIGINT. The resulting value of Lwb and Upb must not be less or equal 0.
If SUBRANGE is not specified, then the resulting value is the BLOB object of the
denoted FieldReference. If one of FieldReference, Lwb or Upb is the NULL value then
the resulting value is also the NULL value. Otherwise the BLOB object restricted to
the indicated range is delivered.
The smallest valid Lwb is 1. If Upb is greater than (SIZE OF Field) then it is
equivalent to (SIZE OF Field).
If the value of Upb is less than the value of Lwb then a BLOB object of length 0 is
delivered.
3.5.11.2. ClobExpression
The field must be of type CLOB, Lwb, Upb,Startpos and Length must be of type
TINYINT, SMALLINT, INTEGER or BIGINT.
If SUBRANGE, SUBSTRING or SUBSTR is not specified, then the resulting value is the
CLOB object of the denoted FieldReference.
The SUBSTRING or SUBSTR function extracts Length characters beginning with the
one on position Startpos and the SUBRANGE function extracts (Upb-Lwb) bytes
beginning at position Lwb. The smallest valid Lwb and Startpos is 1. If the value of
Upb is less than the value of Lwb then a CLOB object of length 0 is delivered.
3.5.12. ODBC_FunctionCall
Syntax:
3.5.13. UserDefinedFunctionCall
Syntax:
3.5.14. LastInsertIdFunc
Syntax:
3.5.15. LastUpdateFunc
The LastUpdFunc delivers date and time of the most recently committed update
operation performed on the current database. This point in time may be delivered in
local time (default) or in UTC.
Syntax:
[246] LastUpdateFunc ::= LAST_UPDATE( [ LastUpdOptions [
SECOND ] ] )
[ @ ConnectionIdentifier ]
[247] LastUpdOptions ::= UTC | LOCALTIME
[316]ConnectionIdentifier ::= Identifier < containing a
ConnectionString >
[319] ConnectionString ::= [ssl:]//[ Host ]/<Dbname>[?
OptionList] |
file://DirectoryLiteral[?OptionList] |
<Dbname>[@Host]
Explanation:
Without any parameter or with the option LOCALTIME, the time of the last update is
delivered as a value of type DATETIME[YY:MS] inside the local timezone.
For each of the 2 variants, the optional parameter SECOND delivers the time value
as Epoch value in type Bigint (seconds elapsed since (1970-1-1 00:00:00) UTC ).
This format discards the millisecond value.
3.5.16. CrowdStatusFunc
The CrowdStatusFunc delivers the current status of the crowd service.
Syntax:
Explanation:
INACTIVE means that there is no active connection between the database and the
crowd.
SELECT CROWD_STATUS()
3.5.17. ReplicationStatusFunc
The ReplicationStatusFunc delivers the current status of a replication update process.
Syntax:
Explanation:
INACTIVE means that there is no active connection between the slave and the
master.
SELECT REPLICATION_STATUS()
3.5.18. SearchCondition
SearchConditions form the WHERE-clause and the HAVING-clause of a
SelectExpression and return a boolean value for each record and group, resp. They
also appear in ConditionalExpressions to choose one of two Expressions.
Syntax:
Explanation: The precedence of operators is: 'NOT' before 'AND' before 'OR' (see
Precedence of Operators ). Additional parentheses may be used as usually to
override precedence rules (see Predicate ).
3.5.19. HierarchicalCondition
In addition to the SearchCondition in the WHERE clause hierarchical data can be
queried using HierarchicalConditions.
Syntax:
[252] HierarchicalCondition ::= [START WITH SearchCondition]
CONNECT BY [NOCYCLE]
ConnectByCondition
[253] ConnectByCondition ::= <Predicate using
HierarchicalExpression>
[254]HierarchicalExpression ::= LEVEL |
CONNECT_BY_ISCYCLE |
CONNECT_BY_ISLEAF |
PRIOR Expression |
CONNECT_BY_ROOT Expression |
SYS_CONNECT_BY_PATH
(Expression, StringLiteral)
Explanation:
START WITH defines the set of root rows for a hierarchical query. It is formulated as
an SearchCondition without HierarchicalExpressions. If this optional clause is omitted
every row the set defined by the FROM clause is considered as root row.
PRIOR and CONNECT_BY_ROOT are unary operators that indicate that Expression
refers to FieldReferences from the prior or root row.
SYS_CONNECT_BY_PATH is a built-in function that calculates the path from the root
row to the current row by concatenating the results of Expression for every visited
predecessor, separating each with StringLiteral.
id parent_id
1 2
2 1
3 2
4 2
5 6
6 5
7 7
SELECT id, parent_id, LEVEL "level",
CONNECT_BY_ISLEAF isleaf, CONNECT_BY_ISCYCLE iscyle,
SYS_CONNECT_BY_PATH(id,'/') path
FROM hierarchy
WHERE level < 4
START WITH id = 1
CONNECT BY NOCYCLE parent_id = PRIOR id
At each point in time of the depth-first search, there exists a "current row". At the
beginning, one of the root rows satisfying the START WITH condition is chosen as
"current row". In this example it is the row with id 1. To find the next row, the
ConnectByCondition is evaluated whereby the PRIOR expressions are those which
refer to the current row (thus representing defined and constant value for the actual
search) and the remaining expressions are treated like in a standard search. In the
example, given the row id 1 as the current row, the search condition "parent_id =
PRIOR id" effectively is "parent_id = 1" as PRIOR id evaluates to 1 in the current
row. The first result record of this search then becomes the new "current row" and
the algorithm proceeds depth-first down the hierarchy until no more successors are
found. If a search on one level delivers more than one result record, the remaining
records successively become the "current row" after the recursive searches have
finished.
The result indicates that a cycle begins in row 2 since it leads back to the root row.
This is also why the NOCYCLE option is required. Rows 3 and 4 are leaf rows.
SELECT id, parent_id, level "level",
SYS_CONNECT_BY_PATH(id,'/') path
FROM hierarchy
START WITH id > 4
CONNECT BY NOCYCLE parent_id = PRIOR id
Here query rows 5, 6, and 7 satisfy the START WITH condition. The query result is
equivalent to a union of three queries with each of these rows successively acting as
root rows.
3.6. Predicate
Predicates are the building units for SearchConditions.
Syntax:
Syntax:
3.6.2. ValueCompPredicate
A ValueCompPredicate compares two values.
Syntax:
If two character sequences (strings) with different length are compared, then the
shorter string is padded with the space character ' ' up to the length of the longer
string.
suppno < 54
q.price >
(SELECT AVG (price)
FROM quotations
WHERE partno = q.partno)
(SELECT MAX(price) - MIN(price)
FROM quotations
WHERE partno = q.partno)
>
(SELECT AVG (price)
FROM quotations
WHERE partno = q.partno) * 0.5
The last example would be a suitable SearchCondition to find out partnos from
records q in quotation with a big variance in price offerings.
3.6.3. SetCompPredicate
A SetCompPredicate compares two sets of records.
Syntax:
q1 <> q2
NOT q1=q2
Note
Duplicate records in any of s1 or s2 do not contribute to the
result, i.e. sets are treated in the mathematical sense in all set
comparison operators.
List all supplier numbers who deliver (at least) the same parts and price offerings as
supplier 54
Syntax:
SELECT *
FROM suppliers
WHERE suppno IN (54,61,64)
List suppliers who deliver at least one part for the same price as supplier 57
3.6.5. BetweenPredicate
The BetweenPredicate tests a value against an interval of two values. Each of the
two interval boundaries can be specified as inclusive or exclusive.
Syntax:
[266]BetweenPredicate ::= Expression [NOT]
BETWEEN Expression [
BetweenQualifier ]
AND Expression [ BetweenQualifier
]
[267] BetweenQualifier ::= INCLUSIVE | EXCLUSIVE
3.6.6. LikePredicate
The LikePredicate tests a string value against a pattern.
Syntax:
Note that all Expressions including the Pattern need not be constants but may also
be calculated at runtime.
The result of Pattern is interpreted as a search pattern for strings where two special
characters have the meaning of wild cards:
If EscapeChar is specified (let its value be c) then all occurrences of wild card
characters in Pattern which are preceeded by a c are not interpreted as wild card
characters but as characters in their original meaning and the EscapeChar c is not
interpreted as part of the pattern in these occurences.
If Sensspec is not specified or is specified with SENSITIVE then the search pattern is
interpreted case sensitive, otherwise the search is performed case insensitive.
The insensitive character comparison depends on the Locale setting of the database.
Note
If no wildcard is used in the pattern, e.g. description LIKE
'xyz' then this expression is not equivalent to description =
'xyz' because the string comparison ignores trailing blanks
whereas the LIKE operator is sensitive with respect to trailing
blanks.
Syntax:
In all following examples, the patterns and values are written in StringLiteral
notation (i.e. with surrounding single quotes).
Alternatives:
The | sign separates alternatives in a pattern. For example, the pattern abc|yz is
matched by abc as well as by yz. The implicit character concatenation binds stronger
than the alternative sign, so to match either abcz or abyz one has to specify the
pattern ab(c|y)z (of course also abcz|abyz would work). Note also that character
classes are nothing else but a shorthand notation for otherwise possibly lengthy
alternatives, so ab[cy]z is equivalent to ab(c|y)z, too.
Repetition factors:
When an asterisk * occurs in a pattern, then zero or arbitray many occurrences of
the preceding pattern element must occur in the value to match. For example, the
pattern abc* is matched by ab, abc, abcc etc. All repetition factor operands bind
most strongly, so the pattern (abc)* must be specified to match abc, abcabc, etc.
The '+' sign means one or more occurrences of the preceding pattern element, so x+
is identical to xx*. The '?' sign means zero or one occurrences. At least n but
maximal m occurrences of a pattern element x can be specified by the notation
x{n,m} where n and m must be integer constants. For example ag{1,3}z is matched
by agz, aggz, agggz.
Precedence of operands:
Three levels of precedence are given, namely the repetition factors which bind
stronger than concatenation which binds stronger than the alternative. To overrule
the precedence of operators, round precedence brackets can be used as shown in
the above examples.
If Sensspec is not specified or is specified with SENSITIVE then the search pattern is
interpreted case sensitive, otherwise the search is performed case insensitive. For
example, the expression s MATCHES INSENSITIVE 'ab.*z' is equivalent to s
MATCHES SENSITIVE '(a|A)(b|B).*(z|Z)'
Note that in case of INSENSITIVE, the ranges in character classes are somewhat
restricted, i.e. if one of the characters is a lowercase (uppercase) character then the
other must also be a lowercase (uppercase) character. For example, the ranges [b-
G] or [B-g] are erroneous.
Note
The MatchesPredicate is more powerful than the LikePredicate
which, however, is supported for compatibility. A pattern in a
LikePredicate can be transformed to a regular patterns by
substituting each non-escaped % by .* and each non-escaped
_ by . .
3.6.8. ExistsPredicate
The ExistsPredicate tests the result of a SubTableExpression on emptyness.
Syntax:
3.6.9. QuantifiedPredicate
A QuantifiedPredicate compares one value against a set of values.
Syntax:
3.6.10. NullPredicate
A Null-Predicate checks the result of an Expression against the null value.
Syntax:
Expression IS NULL
Expression = NULL
3.6.11. FulltextPredicate
On fulltext-indexed fields search expressions of type FulltextPredicate can be issued.
Syntax:
[278] FulltextPredicate ::= FieldIdentifier CONTAINS [
SOUNDEX ] ( FulltextTerm )
[279] FulltextTerm ::= FulltextFactor [ OR FulltextFactor ]
[280] FulltextFactor ::= FulltextPhrase [ Andnot
FulltextPhrase ]
[281] Andnot ::= AND | NOT
[282] FulltextPhrase ::= ( FulltextTerm ) |
Atom [ [ DistSpec ] Atom ]...
[283] Atom ::= SingleValueAtom | MultiValueAtom
[284]SingleValueAtom ::= StringLiteral | Parameter |
FtExpression
[285] MultiValueAtom ::= ANY ( TableExpression )
[286] DistSpec ::= [ [ MinBetween , ] MaxBetween ]
[287] MinBetween ::= <Expression of type Integer>
[288] MaxBetween ::= <Expression of type Integer>
[289] FtExpression ::= <Expression without FieldReference
to same block>
[1] [ ::= [
[2] ] ::= ]
[157] Parameter ::= # IntegerLiteral ( DataType ) |
Colon StandardIdentifier |
Questionmark
[34] StringLiteral ::= CharacterLiteral | UnicodeLiteral |
USER
A special constant NULL is provided to test a result of Expressions against the null
value inside a SearchCondition.
price = NULL
price <> NULL
The first expression delivers TRUE if the field price is null-valued, it delivers FALSE if
the field price has a known value.
The evaluation rules for boolean operators are given in tables not , or , and .
Table 3.1. NOT operator
Table 3.2. OR operator
OR TRUE FALSE UNKNOWN
Table 3.3. AND operator
Assume that a field named price is null-valued for a specific record, the following
SearchConditions effectively evaluate as follows:
price < 10.0 -- FALSE
price >= 10.0 -- FALSE
price = null -- TRUE
In the ORDER BY clause, all NULL values are sorted before any other value.
3.8. SelectExpression (QueryBlock)
A SelectExpression derives records from tables in the database. QueryBlock is a
synonym for SelectExpression.
Syntax:
[290]SelectExpression ::= SelectClause /* 6 */
[ FromClause /* 1 */
[ WhereClause ] /* 2 */
[ UngroupClause ] /* 3 */
[ GroupClause ] /* 4 */
[ HavingClause ] /* 5 */
[ FirstClause ] ]
[291] SelectClause ::= SELECT [ ALL | DISTINCT ]
SelectList
[292] FromClause ::= FROM TableReference [,
TableReference ]...
[293] WhereClause ::= [ WHERE SearchCondition ]
[ HierarchicalCondition ]
[294] UngroupClause ::= UNGROUP BY FieldReference
[295] GroupClause ::= [ GROUP BY Expression [,
Expression ]... ]
[296] HavingClause ::= [ HAVING SearchCondition ]
[297] SelectList ::= SelectElem [, SelectElem ]...
[298] SelectElem ::= Expression [ AS FieldIdentifier ] |
[CorrelationIdentifier .] *
[299] FirstClause ::= FIRST ( CountSpec [ SortSpec ] )
[300] CountSpec ::= [ StartNum TO ] EndNum
[301] StartNum ::= IntegerLiteral
[302] EndNum ::= IntegerLiteral
[333] SortSpec ::= ORDER BY ALL |
ORDER BY SortElem [, SortElem ]...
All specified Expressions in the GROUP-BY-clause must have a resolution in the given
QueryBlock (see Rules of Resolution ).
Each TableReference exports a set of field names (see TableReference). These names
can be used in the defining QueryBlock to reference fields of the result records of the
FROM clause.
The QueryBlock also exports a set of field names: the i-th field of the block exports
name ''f'' if it is a field reference with fieldname ''f'' or ''q.f'' or if it is specified with
an ''AS f'' clause, otherwise the i-th field is ''unnnamed''.
1. The Cartesian product of the results from the TableReferences in the FROM-
clause is constructed.
6. If DISTINCT is specified, duplicate records are removed from the result of (5).
By default or if ALL is specified, duplicate records are not removed.
Updatability:
A SelectExpression is updatable if all following conditions hold:
Note
The UngroupClause is a very special operator only used in
conjunction with the type BITS(*). It is explained in The
Datatypes BITS(p) and BITS(*).
To introduce a field name cnt for the unnnamed field COUNT(*) this is specified as
a FieldIdentifier.
Which suppliers deliver part 221 (all suppliers and part information delivered):
3.9. TableExpression, SubTableExpression
A TableExpression and SubTableExpression construct UNIONs, INTERSECTions and
set DIFFerences from the result sets of TableReferences.
SubTableExpression is a slightly restricted form of TableExpression (is made up of
SubTableReferences instead of TableReferences).
Syntax:
Explanation: UNION computes the set theoretical union of both input sets. If ALL is
specified then duplicate records are retained otherwise they are removed. DIFF and
INTERSECT compute the set theoretical difference and intersection, resp. Duplicate
records are removed.
Note that according to the grammar rules, INTERSECT binds stronger than UNION
and DIFF. Associativity is from left to right (see also Precedence of Operators ).
The expression
A setop CORRESPONDING BY (C1, …, Cn) B
The expression
A setop CORRESPONDING B
is equivalent to
where C1, …, Cn are the fields with common names in A and B in the order of A.
If A and B have no fields in common, an error is returned.
The result types of DIFF are those of the left operand. With UNION and INTERSECT,
the type adaption rules (see Data Types and Type Compatibility ) are applied to
determine the result types.
DIFF preserves the naming of its left operand, i.e. if the i-th field of the left operand
of DIFF is named 'xyz' (or is unnamed), then the i-th field of the result of DIFF is
also named 'xyz' (is unnamed, resp.).
The i-th result field of a UNION or INTERSECT with operands A and B is unnamed if
the i-th field of either A or B is unnamed or if their names differ, otherwise it is
named and has the common input name as name.
Updatability:
A TableExpression is updatable if no UNION, INTERSECT, DIFF is specified and if the
underlying TableReference is updatable.
CorrelationNames:
A TableExpression exports no CorrelationName if one of the set operators UNION,
INTERSECT, DIFF is specified, otherwise it exports the CorrelationName of the
constituting TableReference.
3.10. TableReference, SubTableReference
A TableReference or SubTableReference is the constituent of the FROM clause and of
UNION / INTERSECTION / DIFF expressions (TableExpressions).
Syntax:
[311] TableReference ::= [ TABLE ] TableSpec [ [AS] Alias ] |
FUNCTION TableFunction |
SelectExpression |
( TableExpression ) [ [AS] Alias ] |
JoinedTable |
FULLTEXT <SpecialFulltextTable> |
FlatFileReference |
CrowdExpression
[312] SubTableReference ::= SelectExpression | (
SubTableExpression )
[313] TableSpec ::= LocalTableSpec | RemoteTableSpec
[314] LocalTableSpec ::= TableIdentifier
[315] RemoteTableSpec ::= TableIdentifier @
ConnectionIdentifier ]
[316]ConnectionIdentifier ::= Identifier < containing a
ConnectionString >
[317] Alias ::= [ CorrelationIdentifier ] [ ( FieldList
)]
[99] FieldList ::= FieldIdentifier [ , FieldIdentifier ]...
[318] TableFunction ::= FunctionIdentifier ( ExpressionList )
[263] ExpressionList ::= Expression [, Expression)]...
Note
TableExpression constitutes the top query block and subqueries
in FROM clause, SubTableExpression constitutes subqueries in
SELECT, WHERE, HAVING clause.
Tip
If a TableExpression TE is needed on a place where only a
SubTableExpression is allowed, then the equivalent expression (
SELECT * FROM (TE)) can be used. As can be seen from the
grammar, this is a SubTableReference and thus also a
SubTableExpression.
If Alias is specified with a FieldList, the exported field names are defined by the
specified FieldIdentifiers. The number of FieldNames specified must be equal to the
arity of TableName or TableExpression, resp.
If no FieldList is specified, the exported field names are derived from the underlying
syntactic construct (e.g. TableSpec exports the field names of the fields of the
specified table).
Updatability:
A TableReference is updatable if one of the following holds:
CorrelationNames:
see TableReferences, CorrelationNames and Scopes .
RemoteTableName:
Host names are always treated case insensitive.
Important
The identifiers in the remote TableIdentifiers, ViewIdentifiers or
Identifiers of other user-defined database objects are mapped to
names according to the local case-sensitivity settings.
Examples:
In the following example, an alias is used for each of the TableReferences, the first
consisting of a CorrelationName only, the second with a field list.
The following example needs an Alias for its subquery to reference its result field - it
works with or without a CorrelationName. Both solutions are shown:
3.11. FlatFileReference - direct
processing of text files
A FlatFileReference enables to use a flat text file as a base table in a SELECT
statement. The text file must be in standard SPOOL format, i.e. the field values must
be separated by tabs.
Syntax:
Explanation:
Note that the name of the file holding the data must be enclosed in single quotes.
Each data field in the file must be specified by an arbitrary name and by its
datatype. The syntax of the data values in the file must correspond to that of the
SPOOL statement. The tabulator character is used to separate data values.
Example:
Given a file Persons in directory /usr/x/data which stores some lines each
consisting of two character strings and a number, the file may be processed like this:
SELECT * FROM
( '/usr/x/data/Persons') (firstname CHAR(*), secondname CHAR(*), info NUMBER)
WHERE info > 100
ORDER BY secondname
3.12. CrowdExpression
A CrowdExpression is used to distribute a subquery to a set of connected databases.
The fields of the retrieved result records may be treated like any other basetable
fields.
Transbase Crowd Queries describes the concept and common use cases of crowd
queries.
Syntax:
[325] CrowdExpression ::= CROWD ( CrowdIdentifier [,
CrowdIdentifier]... )
[ RETURN AFTER ] IntegerLiteral
SECONDS
[ OR IntegerLiteral MEMBERS ]
( CrowdSubquery )
CorrelationIdentifier (
CrowdFieldspecList ) |
LastCrowdErrors
[326] CrowdIdentifier ::= Identifier
[327] CrowdSubquery ::= SelectExpression
[328]CrowdFieldspecList ::= Fieldspec [, Fieldspec]...
[324] Fieldspec ::= FieldIdentifier DataType
3.12.1. Crowd Queries
Example:
The actual date and time of all databases, which are connected to crowd
"my_crowd", is collected by the following query:
Explanation:
The first result record will be delivered after the time window has expired or, if
specified, after the desired number of crowd members have returned a complete
result. If the time window expires without a member delivering a complete result, an
empty result set is delivered as the overall result.
Note that the subquery CrowdSubquery is not beeing compiled on this level. So it
can be considered as a black box. But the compiler needs to know, how the result
records look like. Therefore the user has to specify each data field by an arbitrary
name and by its datatype (CrowdFieldspecList).
3.12.2. Crowd Errors
Example:
The following query delivers all errors of the previously executed crowd query:
crowd_level is the nested level where the error occurred. Level zero corresponds to
the database, where the crowd query was executed, level one corrresponds to its
members and so on.
3.13. TableFunction
A TableFunction may appear in the FROM clause of a SELECT statement. It is a built-
in or user-defined procedure and acts like a basetable or view in the surrounding
SQL statement. The fields of the result records may be treated any like other
basetable fields in the WHERE, GROUP BY, SELECT and ORDER BY clause.
For details on adding further user-defined Table Functions see Stored Procedures and
User-Defined Functions Guide .
Currently there are two built-in table functions available. First, the Java based
JDBCReader offers read-only access to any reachable JDBC data source. The built-in
JDBCReader requires a Java runtime environment to be configured for this database.
The Transbase JDBCReader is called with:
SELECT * FROM FUNCTION JDBCReader(
'jdbc:transbase://hostname:2024/dbname',
'user','passwd','select * from sometable')
The following steps show the necessary configuration for using third-party JDBC
drivers to be used by the JDBCReader.
1. Add the third-party driver to the JRE's CLASSPATH. Make sure that the file is
accessible for the Transbase service. Note that the CLASSPATH points to the JAR file,
not only to the directory:
2. Make sure the driver registers with the system's JDBC driver manager by
providing the driver's fully qualified class name
3. Allow the driver to access network resources. Possibly other permissions are also
required:
Now the third-party database may be accessed by calling the JDBCReader using the
appropriate connection URL.
The native OraReader offers a high performance read-only access to Oracle
databases.
The OraReader requires an OCI client software installation on the machine where the
Transbase engine is running. Because of its ease of handling, the Oracle Instant
client is recommenced over the standard Oracle client installation for this purpose.
This software is freely available from Oracle. Please follow the installation
instructions and make sure that PATH (Windows) or LD_LIBRARY_PATH (Unix
platforms) environment variables are set to include the Instant Client, before the
Transbase service is started. Please consult the Oracle documentation on the
environment variables ORACLE_HOME, TNS_ADMIN et cetera, if you are planning to
use the standard Oracle client and connect via Oracle TNS services. Note that these
environment variables also must be set before the Transbase service is started.
These built-in table functions are provided as powerful data import facilities. Their
input parameters consist of a connection string, username and password for opening
a connection to the data source. Note that all parameters are normal StringLiterals
and the escape syntax conventions apply. These are followed by arbitrary SQL
statements to be processed by the database engine of the data source. The result is
automatically mapped to Transbase data types where possible. If a datatype (e.g.
Oracle's LONG and LONG RAW) cannot be mapped, try to cast it to a mappable type
on the remote system. In the next step the data is processed according to an
arbitrary Transbase SQL that encloses the table function. In particular, this Transbase
SQL statement may be an INSERT or SPOOL statement.
3.14. JoinedTable (Survey)
A JoinedTable combines tables with an explicit join operator.
Syntax:
Explanation: CROSS JOIN is a syntactic variant for Cartesian product, i.e. the
following expressions are semantically equivalent:
A CROSS JOIN B
SELECT * FROM A,B
The expression A UNION JOIN B (where A has a fields and B has b fields) is
semantically equivalent to :
SELECT A.*, NULL, NULL, ... -- b NULLs
FROM A
UNION
SELECT NULL, NULL, ..., B.* -- a NULLs
FROM B
The result table has a+b fields and each record either has the first a or the last b
fields all NULL.
CorrelationNames:
Case (1), ON Clause specified: Let searchcond be the search condition. The
expression
Case (2), USING Clause specified: If the USING Clause is specified then let
C1,~…,~Cn denote the FieldList. All Ci's must be fields of both A and B. The
expression
semantically is equivalent to
Each of the result fields Ci appears only once in the result table (i.e. the number
of result fields isa+b-n). The result fields C1, ..., Cn have no CorrelationNames
(even if the constituting TableReferences A and B export CorrelationNames, say
''a'' and ''b''). Thus, in the surrounding SelectExpression, C1, …, Cn can only be
referenced by their unqualified name. The remaining fields of A and B have
CorrelationNames ''a'' and ''b'' of A and B (if they exist). Note that also a.* and
b.* refer to the remaining fields in their original order without any Ci.
is equivalent to
where C1, …, Cn are the fields (in the order of A) which are common to A and B.
1.
SELECT q.partno, q.suppno, q.price, q.delivery_time,
q.qonorder, i.description, i.qonhand
FROM quotations q, inventory i
WHERE q.partno= i.partno
2.
SELECT q.partno, q.suppno, q.price, q.delivery_time,
q.qonorder, i.description, i.qonhand
FROM quotations q JOIN inventory i
ON q.partno= i.partno
3.
SELECT partno, q.*, i.*
FROM quotations q JOIN inventory i USING (partno)
4.
SELECT partno, q.*, i.*
FROM quotations q NATURAL JOIN inventory i
Note the meaning of q.* and i.* in the context of USING and NATURAL. Note also
that suppno and partno are opposite to their original order in quotations.
Case (1), ON Clause specified: Assume the expressions LJ, RJ, FJ, IJ as:
Let innerjoin denote the result of IJ. Then the result sets of LJ, RJ, FJ are
defined as:
The USING variant works like the ON variant except that the specified common
fields appear only once in the result (always the not-NULL part of the field if any
appears). Note that the COALESCEd fields do not have a CorrelationName and
that the CorrelationNames exported by A and B do not include the COALESCEd
fields.
where C1, …, Cn are all fields with identical names in A and B (in the order as
they appear in A).
For all following examples, we assume excerpts S and Q from suppliers and
quotations as shown in tables S and Q below.
Example Data:
suppno name
51 DEFECTO PARTS
52 VESUVIUS,INC
53 ATLANTIS CO.
suppno partno
50 221
51 221
53 222
53 232
Examples:
Note that the first result field can only be referenced by the unqualified name
suppno.
SELECT * FROM S NATURAL FULL JOIN Q
Note that the first result field can only be referenced by the unqualified name
suppno.
In the following example, the JoinedTable exports CorrelationNames ''q'' and ''s'':
In the following example, the JoinedTable also exports CorrelationNames ''q'' and
''s'', but the common result field ''suppno'' has no CorrelationName and q.* and s.*
do not include the field ''suppno'':
Syntax:
The ORDER-BY-clause sorts the result records of the TableExpression. If more than
one SortElem is specified, a multi-field sort is performed with the first SortElem
being the highest sort weight etc.
If a SortElem is given via an IntegerLiteral i, it refers to the i-th result field of the
TableExpression. Field numbering starts at 1. Otherwise there must be an identically
named result field of the TableExpression and the SortElem refers to that field. The
next example below shows two equivalent sort specifications.
By default the sort order is ascending unless explicitly descending order is required
('DESC').
Note
A SelectStatement is updatable if no ORDER BY-clause is
specified and if the TableExpression itself is updatable (see
TableExpression ).
Caution
There is no predictable ordering of result records, if no order-by-
clause is specified.
Privileges: The current user must have SELECT privilege on each table (base table
or view) specified in any FROM-clause of any QueryBlock which occurs in the
SelectStatement.
Locks: All tables and views referenced in the SelectStatement are automatically
read locked.
If the FOR UPDATE is given, the (single) table in the outermost QueryBlock is update
locked.
Syntax:
Explanation:
Each WithElement creates a (temporary) table with the specified table name. The
main TableExpression in the SelectStatment can refer to all tables defined by the
WithElements, and each WithElement may refer inside the defining TableExpressions
to the preceding WithElements. The field names of the temporary tables are
exported either by an explicit FieldList or by the field names of the SELECT lists or
(in case of expressions) via Aliases introduced via the keyword AS.
WithElements are processed and stored in the order of writing. After the evaluation
of the main TableExpression of the SelectStatement the temporary tables are
deleted.
WITH
WE1 (w1,w2) as (SELECT r1+r2 , r0 FROM R WHERE r0 > 0)
SELECT MAX(w1) FROM WE1
UNION ALL
SELECT w2 FROM WE1
In this example, the field names of WE1 are exported via an explicit
FieldList in WE1.
3.18. InsertStatement
The InsertStatement inserts one or several constant records or a computed set of
records into a table or updatable view. Optionally, for each inserted record a result
record is returned.
Syntax:
[337]InsertStatement ::= INSERT [ OrClause ] INTO TableSpec
[ ( FieldList )] Source [
ReturningClause ]
[338] OrClause ::= OR { IGNORE | REPLACE | UPDATE }
[313] TableSpec ::= LocalTableSpec | RemoteTableSpec
[99] FieldList ::= FieldIdentifier [ , FieldIdentifier ]...
[339] Source ::= VALUES ( ValueList ) |
TABLE ( ( ValueList ) [, (ValueList)
]... ) |
TableExpression |
DEFAULT VALUES
[340] ValueList ::= Expression [, Expression ]... )
[341]ReturningClause ::= RETURNING ( ExpressionList )
Explanation: The table specified by TableSpec must be updatable (i.e. a base table
or an updatable view.
All fields in the specified FieldList must be unique and must be fields of the specified
table.
If no FieldList is specified then there is an implicitly specified FieldList with all fields
of the specified table in the order of the corresponding CreateTableStatement or
CreateViewStatement, resp.
The number of Expressions in a ValueList or the number of fields of the result of the
TableExpression must match the number of fields in the FieldList and the
corresponding types must be compatible.
3.18.3. Insertion on Views
If the specified table is a view, then insertion is effectively made into the underlying
base table and all fields of the base table which are not in the view are filled up with
their DEFAULT values.
For a view v where the WITH CHECK OPTION is specified the insertion of a record t
fails if t does not fulfill the SearchCondition of the view definition of v or any other
view on which v is based.
The InsertStatement returns an error if a type exception occurs (see Type Exceptions
and Overflow) or if a NULL constraint is violated. The InsertStatement fails if a key
constraint or a UNIQUE constraint (defined by a CREATE UNIQUE INDEX ... ) would be
violated.
The 4 variants of the InsertStatement behave differently w.r.t. key collisions: The
InsertIntoStatement returns an error on a key collision and no record is inserted.
The other 3 variants never return error. All records which do not conflict on the key
are inserted and conflicting records are handled as follows:
The InsertUpdateStatement replaces those fields of the target record which are
specified in the FieldList with the corresponding values of the source records. Other
field values of the target record remain unchanged.
The RETURNING clause might also contain subqueries, but in case of a distributed
INSERT, the table references of the RETURNING clause must be against the same
database as the target table reference.
The current user must have INSERT privilege on the specified table.
Assume a table T with an INTEGER field "key" which should get its values from a
sequence S (i.e. "key" has been specified with a DEFAULT S.nextval):
The RETURNING clause makes the value assigned to the key field available for
further processing.
Assume that table suppliers contains the record (80, 'TAS', 'Munich'):
Syntax:
Explanation: The table specified by TableName must be updatable (i.e. a base table
or an updatable view).
All records from the specified table which satisfy the SearchCondition are deleted.
If the SearchCondition is omitted, all records from the specified table are deleted.
If records are deleted from an updatable view, the deletion is made on the
underlying base table.
Note
It is allowed to refer to the table to be modified in a subquery of
the DeleteStatement (in the SearchCondition). See the examples
below and also General Rule for Update .
Deletion of many records may be slow if secondary indexes exist. However, deletion
of all records in a table (WHERE clause is omitted) is very fast.
Privileges: The current user must have DELETE-privilege on the specified table.
If TableExpressions occur in the SearchCondition, the user must additionally have the
corresponding SELECT-privileges as if the TableExpressions were run as
SelectStatements (see SelectStatement ).
If a remote table is specified as the target of the DELETE operation, all subqueries (if
any) must specify tables residing on the same database. However, if the target table
is local, any tables (remote or local) may be specified in subqueries.
3.20. UpdateStatement
The UpdateStatement updates a set of records in a table or an updatable view.
Syntax:
[343]UpdateStatement ::= UPDATE TableSpec [
CorrelationIdentifier ]
SET AssignList
[ WHERE { SearchCondition |
CURRENT } ]
[313] TableSpec ::= LocalTableSpec | RemoteTableSpec
[344] AssignList ::= Assignment [, Assignment ]...
[345] Assignment ::= FieldIdentifier = Expression
Explanation:
The effect of the UpdateStatement is that all records of the specified table which
fulfill the SearchCondition are updated. If no SearchCondition is specified then all
records of the table are updated.
For each record to be updated the fields on the left hand sides of the Assignments
are updated to the value of the corresponding Expression on the right hand side.
Unspecified fields remain unchanged.
If the specified table is a view then the update is effectively made on the underlying
base table and all fields of the base table which are not in the view remain
unchanged.
For a view v where the WITH CHECK OPTION is specified the update of a record t
fails if the updated record would not fulfill the SearchCondition of the view definition
of v or any other view on which v is based.
Note
It is allowed to update primary key fields, but this runs
considerably slower than update of non-key fields only.
Note
It is allowed to refer to the table to be updated in a subquery of
the UpdateStatement (in the AssignList or SearchCondition). See
the example and also General Rule for Update .
Privileges:
The current user must have UPDATE-privilege on all fields specified on the left hand
sides of the Assignments.
If there are TableExpressions on the right hand side of the Assignments or in the
SearchCondition, the user additionally must have corresponding SELECT-privileges as
if the TableExpressions were run as SelectStatements (see SelectStatement ).
Locks:
If a remote table is specified as the target of the UPDATE operation, all subqueries (if
any) must specify tables residing on the same database. However, if the target table
is local, any tables (remote or local) may be specified in subqueries.
UPDATE quotations
SET price = price * 1.1, delivery_time = 10
WHERE suppno = 53 AND partno = 222
UPDATE quotations@otherdb@server5 q
SET price = price * 1.1
WHERE price =
(SELECT MIN (price)
FROM quotations@otherdb@server5
WHERE suppno = q.suppno)
3.21. UpdateFromStatement
The UpdateFromStatement updates a target table from an existing or computed
source table.
Syntax:
Explanation:
The target table must not be a view. Field number and field types of
SourceExpression and TargetTable must be compatible.
With the WITHOUT DELETE option, the UpdateFromStatement thus is a special case
of the MergeStatement.
Caution
All triggers and referential constraints on the target table are
ignored for this statement. Secondary indexes are maintained.
Privileges: The current user must have INSERT, UPDATE and DELETE privileges on
the target table.
3.22. MergeStatement
The MergeStatement serves as a combination of the InsertStatement with the
UpdateStatement. It combines the effects of both these statements within a single
one.
Syntax:
[349] MergeStatement ::= MERGE INTO TargetTable USING
SourceExpression ON (
JoinPredicate ) MatchClause
NonMatchClause
[347] TargetTable ::= TableIdentifier
[348]SourceExpression ::= TableExpression
[350] JoinPredicate ::= SearchCondition
[351] MatchClause ::= WHEN MATCHED THEN UPDATE SET
AssignList
[352] NonMatchClause ::= WHEN NOT MATCHED THEN INSERT
[ ( FieldList ) ] VALUES ValueList
[340] ValueList ::= Expression [, Expression ]... )
Explanation:
The merge process can be seen as a loop executed on the SourceExression S. For
each record of S there must be either no record or exactly one record in TargetTable
which matches. In the first case, the NonMatchClause is executed which inserts fields
of the current record of S into the TargetTable. In the second case, the MatchClause
is executed to update fields of the matching record in TargetTable as specified in the
AssignList.
Privileges: The current user must have INSERT and UPDATE privilege on
TargetTable.
This principle allows to specify the modification referring to the old state of the table
and defines the modification as an atomic step.
3.24. Rules of Resolution
For the semantics of nested QueryBlocks it is necessary that any FieldReference and
any SetFunction can be resolved against exactly one QueryBlock. This block is then
called the resolution block of the Field or the SetFunction, resp.
3.24.1. Resolution of Fields
An unqualified Field fld (see FieldReference ) has as its resolution block the
innermost surrounding QueryBlock q whose FROM-clause contains a TableReference
which exports a field named fld. If there is more than one such TableReference in q,
the resolution fails and an error is returned.
A qualified Field r.fld has as its resolution block the innermost surrounding
QueryBlock q whose FROM-clause contains a TableName or CorrelationName r. If
the corresponding TableReference does not export a field named fld, the resolution
fails and an error is returned.
3.24.2. Resolution of SetFunctions
In most cases, the resolution block of a SetFunction is the innermost surrounding
QueryBlock.
SELECT partno
FROM quotations
GROUP BY partno
HAVING
MAX(price) - MIN(price)
>
AVG(price) * 0.5
The resolution of all three SetFunctions is the only (and innermost) QueryBlock.
Thus, they are computed for each group of parts.
Here, COUNT(*) refers to the inner QueryBlock whereas MIN(q1.price) refers to the
outer QueryBlock and thus computes as the minimum price over the current group of
parts.
For each SetFunction s with resolution block q, s must not appear in the WHERE-
clause of q.
They are only relevant and valid in Retrieval Databases and are explained in the
Transbase Publishing Guide.
5. Alter Session statements
ALTER SESSION Statements serve to configure runtime parameters of a Transbase
session. They are not subject to the transaction concept; i.e. they can be issued
inside or outside a transaction.
They are entered in interactive applications or used in application programs like any
other SQL statement.
The effect of a ALTER SESSION statement is restricted to the session, i.e. the
connection which issues it.
Syntax:
Explanation: Size is the desired size of the sortercache in KB. Big sizes favour the
computing time for sorter operations but need more resources.
Note that the reconfiguration of the sortercache is deferred until the start of the next
transaction. For the sake of clarity, it is thus recommended to issue that statement
outside of a transaction.
5.2. Multithreading Mode
This statement sets the Transbase query optimizer to one of several possible levels
of parallel query execution. This setting is valid throughout the current session.
Syntax:
Explanation:
According to the SQL standard, an INTEGER division delivers INTEGER again, i.e. the
fractional part is omitted.
With the following statement, the result of integer division can be defined to be a
NUMERIC which also contains the fractional part. With its counterpart, one can
return to the standard behaviour.
Syntax:
Syntax:
If TABLES is specified, all subsequent locks are on table basis. In this mode, at most
one lock is set for a table including all its secondary indexes and its LOBs.
If MIXED is specified, the locking strategy is chosen implicitly by the system which
also is the default.
Note
These statements do not lock any objects. Carefully distinguish
the SET LOCKMODE statements from the LOCK and UNLOCK
statements which set TABLE locks on random tables.
5.5. Evaluation Plans
This statement enables and disables the generation of evaluation plans.
Syntax:
[360]AlterSessionPlansStmt ::= ALTER SESSION SET
EVALUATION_PLANS =
{ COUNTERS | TIMES | OFF }
If mode COUNTERS is enabled, each operator of the QEP contains the number of
records processed by that part.
If mode TIMES is enabled, each operator of the QEP additionally contains the elapsed
time of processing the operator.
Note
Tracing the execution times via mode TIMES may cause a
significant overhead in the query's total elapsed time. The times
denoted in the produced QEP are adjusted to compensate this
additional overhead.
5.6. Schema Default
This statement serves to change the default schema of the database for the current
connection.
Syntax:
[361]AlterSessionSchemaStmt ::= ALTER SESSION SET
SCHEMA_DEFAULT =
SchemaIdentifier
[10] SchemaIdentifier ::= UserIdentifier
[9] UserIdentifier ::= Identifier | PUBLIC | USER
5.7. SQL Dialect
This statement serves to adapt the SQL to the dialect of ODBC or MySQL.
The only difference that we are currently aware of is that single quotes in string
literals are escaped using a backslash character in MySQL dialect.
In the Transbase dialect they are escaped by doubling them as specified in the SQL
standard.
[Transbase]
'It''s cold outside!'
[MySQL]
'It\'s cold outside!'
Syntax:
Carefully distinguish the LOCK and UNLOCK statements which set TABLE locks on
random tables from the ALTER SESSION SET LOCKMODE statement which influences
the lock granularity for automatic locking by Transbase.
6.1. LockStatement
Serves to explicitly lock tables and views.
Syntax:
Explanation: For each LockSpec, the specified lock is set on the specified object. If
a view is specified, the lock is effectively set on the underlying base table(s).
6.2. UnlockStatement
Serves to remove a READ lock.
Syntax:
Error occurs if the object is not locked or if the object is update locked, i.e. an
InsertStatement, UpdateStatement, DeleteStatement or an explicit
LockStatement with UPDATE or EXCLUSIVE mode has been issued within in the
transaction.
7.1.1. RangeSpec
The occupied components of a datetime value constitute its range. The components
have symbolic names. All names occur in 2 equivalent variants which come from the
original Transbase notation and from the evolving SQL standard. An overview is
given in the following table.
YY year 1 - 32767
MO month 1 - 12
DD day 1 - 31
HH hour 0 - 23
Notation Meaning Allowed Values
MI minute 0 - 59
SS second 0 - 59
MS millisecond 0 - 999
The range indices MS, …, YY are ordered. MS is the smallest, YY is the highest range
index. An explicit range spec is written as [ub:lb] where ub is the upperbound
range and lb is the lowerbound range. For example [YY:DD] is a valid range spec.
[DD:YY] is an invalid range spec.
Upperbound and lowerbound range may be identical, i.e. of the form [ab:ab]. A
datetime value with such a range has a single component only. The corresponding
range spec can be abbreviated to the form [ab].
DATE DATETIME[YY:DD]
SQL Type Transbase Equivalent
TIME DATETIME[HH:SS]
TIMESTAMP DATETIME[YY:MS]
Note that there are types that can be defined in Transbase but have no equivalent
SQL standard formulation. DATETIME[MO:DD] describes yearly periodic datetimes
based on day granularity, e.g. birthdays. DATETIME[HH:MI] describes daily periodic
datetimes based on minute granularity, e.g. time tables for public traffic.
7.1.3. DatetimeLiteral
DatetimeLiteral defines the syntax for a constant value inside the SQL query text or
inside a host variable of type char[]. Transbase supports a variety of literal notations,
namely a native Datetime literal representation with maximal flexibility, the SQL
standard representation and a notation compatible with the ODBC standard. All 3
variants are sufficiently explained by the following examples. A fourth variant is
supported in a Transbase spoolfile.
Variant Notation
Native TB DATETIME[YY:MS](2002-12-24 17:35:10.025)
DATETIME(2002-12-24 17:35:10.025)
Variant Notation
In contrast to the colon separators in the time part the dot separator between
SS and MS has the meaning of a fractional point.
All variants except ODBC are also supported in a spoolfile if enclosed in single
quotes. Thereby the quotes in the SQL variant has to be escaped by a
backslash.
Variant Notation
Native TB DATETIME[YY:DD](2002-12-24)
Native TB DATETIME(2002-12-24)
ODBC { d '2002-12-24' }
Variant Notation
Native TB DATETIME[HH:SS](17:35:10)
ODBC { t '17:35:10' }
There are literals which are only representable in native Transbase notation, because
SQL (as well as ODBC) only supports subtypes of the most flexible DATETIME type.
See the following examples:
DATETIME[MO:DD](12-24)
Twelve o'clock:
DATETIME[HH](12)
The range spec can be omitted if and only if the upperbound range is YY.
If upperbound and lowerbound range are identical, the range spec can be
abbreviated to the form [YX].
Only datetimes are accepted that might be legal values according to this premise.
If MO and DD are inside the range, then the DD value must not exceed the
highest existing day of the MO value.
If YY and DD are inside the range then leap year rules of the Julian/Gregorian
time periods specified above apply.
DATETIME[MO:DD](4-31) -- Illegal: no date with such components exists
DATETIME[MO:DD](2-29) -- Legal: there are dates with such components
DATETIME(1988-2-29) -- Legal: leap year
DATETIME(1900-2-29) -- Illegal: no leap year
DATETIME(1582-10-14) -- Illegal: 1582 was a rather short year anyway
This table would be appropriate to describe persons with their name and birthday
and the time when met first or talked to first.
Note that although all datetime values in the table are exactly of the specified
format, it is possible to insert records with datetime fields of a different precision.
Implicit conversions (CAST operators) then apply as described in the following
chapters.
The operator CURRENTDATE may be used wherever a datetime literal can be used.
When used in a statement the CURRENTDATE operator is evaluated only once and its
resulting value remains the same during the whole execution of the statement.
As long as a cursor is open, any interleaving cursor sees the same CURRENTDATE
result as the already open cursor. In other words, a consistent view of the
CURRENTDATE is provided for interleaving queries inside an application. In all other
situations (non interleaving queries), it is tried to evaluate successive calls of
CURRENTDATE such that different results are delivered (see above).
7.1.7. Casting Datetimes
A datetime value can be cast to a different range. A cast operation can be performed
explicitly by the CAST operator or implicitly occurs before an operation with the
datetime value is performed (see Type Exceptions and Overflow).
In an explicit CAST operation, the syntax of the type specifier is the same as the one
used in a CreateTableStatement .
The rules to construct the result datetime value from the given one are as follows:
DTC1: All components having a range index smaller than the source
lowerbound range are set to the smallest possible value (0 for MS, SS, MI, HH
and 1 for DD, MO, YY).
DTC2: All components having a range index higher than the source upperbound
range are set to the corresponding components of CURRENTDATE.
DTC3: The other components (i.e. having range index between source
lowerbound index and source upperbound index) are set as specified by the
source datetime fields.
Examples:
7.1.8. TRUNC Function
The TRUNC function is a shortcut to cast a datetime value to the type DATE, i.e.
DATETIME[YY:DD]
The SQL operators <=, <, = etc. as used for arithmetic types are also used for
comparison of datetimes.
If datetimes values with different ranges are compared, they are implicitly cast to
their common range before the comparison is performed. The common range is
defined by the maximum of both upperbound and the minimum of both lowerbound
ranges. Note, however, special rules for CURRENTDATE as described below!
Thus, it is possible that one or both datetime values are implicitly cast according to
the casting rules described in the preceding chapter.
yields FALSE, because the second operand is implicitly cast to the value
DATETIME[YY:MI] (1989-6-8 00:00)
The comparison
DATETIME[MO:DD](6-8) = DATETIME[YY:DD](2000-6-8)
will yield TRUE in the year 2000 and FALSE in other years.
To retrieve all persons who have been met since February this year:
Note that the CAST operator applied to birthday is necessary to restrict the common
range to [MO:DD]. If the explicit CAST were omitted, the common range would be
[YY:DD] and the constant comparison operators would be extended by the current
year so that the query would not hit any person.
To retrieve all persons ordered by their age (i.e. ordered by their birthday
descendingly):
This example retrieves all records whose field value ''talked'' matches the current
day (year, month, day). Without the type adaption rule for CURRENTDATE, one
would also have to cast CURRENTDATE on the range [YY:DD].
This means that the ranges of all TIMESPAN types must either be inside [YY:MO] or
inside [[DD:MS]. For example, the following type definitions are illegal:
TIMESPAN[YY:DD] -- illegal
TIMESPAN[MO:HH] -- illegal
The reason is that the number of days is not the same for all months. So the
arithmetic rules for timespan calculations would be compromised.
The set of allowed values on the components are also different from DATETIME.
Obviously, the day component of a TIMESPAN value may have the value 0 whereas a
DATETIME value containing a day component shows a value >= 1. The legal values in
a TIMESPAN are shown in table timespan .
If SQL notation is used, then the type is internally mapped to the corresponding
TIMESPAN type. Thereby the optional precision on the start range is ignored.
If the end range is SECOND, then a precision indicates a fractional part so the end
range effectiveley becomes milliseconds (MS).
If SECOND is start range (thereby automatically also end range) then a simple
precision like (3) is ignored like in all start ranges - especially this precision does not
specifiy a fractional part so the mapping is to SS.
If SECOND is start range (thereby automatically also end range) then a specification
of a fractional part must be given as (m,n) as it is done in the last example.
On all components different from the upperbound components only those values are
allowed which are below a unity of the next higher component. The allowed values
are shown in the table.
By these rules, it is possible for example to express a time distance of 3 days, 1 hour
and 5 minutes as 73 hours, 5 minutes i.e. as a TIMESPAN[HH:MI]. However, it is
illegal to express it as 72 hours and 65 minutes.
Table 7.6. Ranges of Timespan Components
YY YEAR 0 - MAXLONG
MO MONTH 0 - 11
DD DAY 0 - MAXLONG
HH HOUR 0 - 23
MI MINUTE 0 - 59
SS SECOND 0 - 59
MS -- 0 - 999
7.2.4. TimespanLiteral
There are 2 variants for TIMESPAN literals which correspond to the 2 variants of
TIMESPAN type definition (TIMESPAN and INTERVAL). The following table shows
examples in both notations.
Note that negative TIMESPANs are reasonable (e.g. as the result of a subtraction of
a DATETIME value from a smaller DATETIME value). In SQL syntax, literals with a
negative value incorporate the '-' sign within the literal syntax whereas in Transbase
native notation the '-' sign is written (as a separate token) in front of the TIMESPAN
token. See also chapter Sign_of_timespans Sign of Timespans.
7.2.5. Sign of Timespans
A timespan value is positive or zero or negative. It is zero if all components of its
range are zero. A negative timespan may result from a computation (see the
following chapters) or can also be explicitly represented as a timespan literal
prefixed with an unary '-' (in terms of the TB/SQL grammar this is an Expression).
Note
It is illegal to attach a '-' sign to any component of a timespan
literal.
Note that although all timespan values in the table are exactly of the specified
format, it is possible to insert records with timespan fields of a different precision.
Implicit conversions (CAST operators) then apply as described in the following
chapters.
7.2.7. Casting Timespans
Similarily to datetimes, a timespan value can be explicitly or implicitly cast to a
target range. Timespan casting, however, has a complete different semantics than
datetime casting (recall Chapter Datetime Casting). A timespan cast transfers a
value into another unit by keeping the order of magnitude of its value unchanged -
however a loss of precision or overflow may occur.
TSC1: The target range must be valid, i.e. it must not span the MO-DD border.
TSC2: The target range must be compatible with the source range, i.e. both
ranges must be on the same side of the MO-DD border.
TSC3: If the lowerbound target range is greater than the lowerbound source
range then a loss of precision occurs.
TSC4: If the upperbound target range is smaller than the upperbound source
range then the component on the upperbound target range is computed as the
accumulation of all higher ranged components. This may lead to overflow.
Examples:
Multiplication:
The semantics of multiplication is that all components of the timespan are multiplied
and the resulting value is normed according to the rules of valid timespans. Overflow
occurs if the upperbound range value exceeds MAXLONG.
Division:
The semantics of division is as follows: first the timespan value is cast to its
lowerbound range (a virtual cast which never yields overflow!), then the division is
performed as an INTEGER division and then the result is cast back to its original
range.
TIMESPAN [YY] (1) / 2 --> TIMESPAN [YY] (0)
TIMESPAN [YY:MO] (1-5) / 2 --> TIMESPAN [YY:MO] (0-8)
TIMESPAN [DD:MS] (5 3:20:30.572) / 4 --> TIMESPAN [DD:MS] (1 6:50:7.643)
The result is a timespan value whose range is the common range of the input values.
The common range is again defined by the maximum of both upperbounds and the
minimum of both lowerbounds. The input values are cast to their common range
before the operation is performed.
TIMESPAN [DD] (1000) + TIMESPAN [DD] (2000) --> TIMESPAN [DD] (3000)
TIMESPAN [YY] (1) + TIMESPAN [MO] (25) --> TIMESPAN [YY:MO] (3-1)
TIMESPAN [YY] (1) - TIMESPAN [MO] (27) --> -TIMESPAN [YY:MO] (1-3)
To retrieve the time difference between the winner and the looser of the Marathon as
well as the average time:
DATETIME [YY:DD] (1989-6-26) + TIMESPAN [DD] (30) --> DATETIME [YY:DD] (1989-7-26)
DATETIME [HH:MI] (12:28) - TIMESPAN [SS] (600) --> DATETIME [HH:SS] (12:18:00)
DATETIME [YY:MO] (1989-2) + TIMESPAN [DD:MI] (3 20:10) --> DATETIME [YY:MI] (1989-2-4 20:
10)
If the upperbound range of the input datetime value is less than YY, then the
datetime is always cast to [YY:lb] before the operation is performed (lb is the
lowerbound range of the datetime).
DATETIME [MO:DD] (2-28) + TIMESPAN [HH] (24) --> DATETIME [MO:HH] (2-29 0)
DATETIME [MO:DD] (2-28) + TIMESPAN [HH] (24) --> DATETIME [MO:DD] (3-1 0)
When the range of the input timespan is on the left side of the MO-DD border
Transbase requires the range of the input datetime to be completely on the left side
of the MO-DD border, too. The reason is to avoid the semantic ambiguities that
migth arise in some cases.
DATETIME [YY:DD] (1992-2-29) + TIMESPAN [YY] (1) --> Invalid
DATETIME [YY:MO] (1992-2) + TIMESPAN [YY] (1) --> DATETIME [YY:MO] (1993-2)
7.3.2. Datetime - Datetime
When two datetimes are subtracted from each other the result is a timespan.
DATETIME [YY:MO] (1989-3) - DATETIME [YY:MO] (1980-4) --> TIMESPAN [YY:MO] (8-11)
Except to one case (see below) the range of the result is again the common range of
the two input values. If the input ranges are different the two input values are cast
to their common range before the operation is performed.
DATETIME [HH:MI] (12:35) - DATETIME [HH] (14) --> TIMESPAN [HH:MI] (1:25)
One slight complication arises when the range of the resulting timespan would span
the MO-DD border and thus would be invalid. In this case, the upperbound of the
result range is always DD.
DATETIME [YY:DD] (1989-6-26) - DATETIME [YY:DD] (1953-6-8) --> TIMESPAN [DD] (13167)
The first week of a year begins on January 1st - regardless of its weekday.
Consequently, the first and last week of a year may contain less than 7 days. A leap
year ending on a Sunday has 54 weeks.
WEEK OF DATETIME (2000-12-31)
yields 54.
The first week of a year contains at least four days of the same year.
Consequently, there are no fragmented weeks, i.e. each week contains exactly 7
days. The 29th, 30th, and 31st of December may belong to the first week of the
following year. The 1st, 2nd, and 3rd of January may belong to the last week for the
previous year.
yields 4.
Error occurs if the selector is not inside the range of the value.
Note that selecting a component semantically is not the same as casting to the range
of the selector as shown by the examples:
yields 32.
yields
TIMESPAN [MS] (2032)
Note that the selector operator simply extracts one component without regarding the
semantics of DATETIME or TIMESPAN. However, the selector operators (as well as
the CONSTRUCT operator, see below) are useful because they provide for a bridge
between DATETIME/TIMESPAN and the basic arithmetic types of SQL. For example
an application program can retrieve the components of datetime or timespan values
into integer program variables for further arithmetic processing.
is equivalent to
If omitted the range is assumed to start with YY. The following literal therefore
denotes a range of [YY:MO].
Note that if all values are constants, the CONSTRUCT operator is in no way superior
to an equivalent datetime or literal representation which is also better readable.
The constructor and selector operators together allow to perform every manipulation
on datetime and timespan values and also to override the built-in semantics. This
may be necessary only occasionally as shown below.
Assume that in the table Persons several values for birthdays have been inserted
(falsely) without the century of the year (e.g. 53-6-8 instead of 1953-6-8). The
following statement would correct all such entries:
UPDATE Persons
SET birthday = CONSTRUCT DATETIME
(YY OF birthday + 1900,
MO OF birthday,
DD OF birthday)
WHERE YY OF birthday < 100
In effect, the above statement does not express a semantically reasonable operation
on datetimes but a correction of wrong datetime values. Note that this correction
cannot be performed by adding a timespan value TIMESPAN [YY] (1900) because of
the subtle semantics of the addition of timespans to datetimes.
Assume a table TI with a field FK of arbitrary type and a key field FI of type INTEGER
or SMALLINT or TINYINT.
FK FI(INTEGER)
a 1
a 4
a 7
a 8
b 3
b 10
b 11
A representation using bitvectors yields the following table TB with fields FK and FB
where FB is of type BITS(*):
FK FB (BITS(*))
a 0b10010011
b 0b00100000011
The used notation here is that of bits literals (0-1 sequence starting with 0b).
The number p in BITS(p) is the number of bits that a value or a field of that type
may hold. The maximum number of p is MAXSTRINGSIZE*8-4 , where
MAXSTRINGSIZE depends on the pagesize. A value of type BITS(p) or BITS(*)
semantically is a series of 0 or 1-bits. The bit positions are numbered and the
leftmost position has the number 1.
Analogously to the arithmetic types, the value of the lower level type is automatically
cast to the higher level type when an operation requires a higher level type input or
when two values of different types are compared or combined.
8.4. BITS and BINCHAR Literals
A BITS literal is a sequence of the digits 0 and 1 prefixed by 0b.
0b0(4)1(5)0
0b0000111110
No computed expression is allowed here. With a little bit care, also BINCHAR literals
can be used for constants of type BITS, because BINCHAR is implicitly cast to BITS
where needed. Note however that the values are not identical, e.g. the SIZE
operator delivers different results.
Note
When a BINCHAR value (e.g. a Literal) of type BINCHAR(p) is
used as input for an operation which requires the type BITS, it is
automatically cast to the type BITS(p*8).
BITNOT bexpr
Explanation: Computes the bitwise complement of its operand. The result type is
the same as the input type.
Explanation: BITAND computes the bitwise AND, BITOR the bitwise OR of its
operands. The shorter of the two input operands is implicitly filled with 0-bits up to
the length of the longer input operands. If one of bexpri is type BITS(*) then the
result type is also BITS(*) else the result type is BITS(p) where p is the maximum of
the input type lengths.
0b1100 BITAND 0b0101 --> 0b0100
0b1100 BITOR 0b0101 --> 0b1101
8.6.3. Comparison Operators
All comparison operators ( < , <= , = , <> , > , >= ) as known for the other
Transbase types are also defined for BITS. Length adaption is done as for BITAND
and BITOR. A BITS value b1 is greater than a BITS value b2 if the first differring bit
is 1 in b1 and 0 in b0.
Explanation: If both iexpr1 and iexpr2 are specified: iexpr1 and iexpr2 describe a
range of bit positions. Both expressions must deliver exactly one value which is a
valid bit position ( >= 1). MAKEBIT constructs a bits value which has 0-bits from
position 1 to iexpr1-1 and has 1-bits from position iexpr1 to iexpr2.
If only iexpr1 is specified: iexpr1 describes one bit position or (in case of a
subquery) a set of bit positions. MAKEBIT constructs a bits value which has 1-bits
exactly on those positions described by iexpr1.
COUNTBIT ( bexpr )
If iexpr2 is not specified then SUBRANGE returns the iexpr1-th bit from bexpr as a
INTEGER value ( 0 or 1 ).
FK FI(INTEGER)
a 1
a 4
a 7
a 8
b 3
b 10
b 11
FK FB (BITS(*))
a 0b10010011
b 0b00100000011
8.7.1. Compression into Bits with the SUM
function
The set function SUM, originally defined for arithmetic values, is extended for the
type BITS(p) and BITS(*). For arithmetic values, SUM calculates the arithmetic sum
over all input values. Applied to BITS values, SUM yields the BITOR value over all
input values where a start value of 0b0 is assumed.
In combination with a GROUP BY operator and MAKEBIT operator, the table TI can be
transformed to the table TB (see Purpose of Bits Vectors):
The following statement constructs table TI from table TB (see Purpose of Bits
Vectors):
SELECT * FROM RB
UNGROUP BY FB
The UNGROUP BY operator can be applied to exactly one field and this field must be
of type BITS.
The numbers at the left margin show the order in which the clauses are applied. It
shows that the UngroupClause takes the result of the WhereClause as input: it
constructs from each input record t a set of records where the BITS value of t at
position FieldName is replaced by INTEGER values representing those bit positions
of t which are set to 1.
9.1.1.1. Overview of operations
Transbase does not interpret the contents of a BLOB. Each field of type BLOB either
contains the NULL value or a BLOB object. The only operations on BLOBs are
creation, insertion, update of a BLOB, testing a BLOB on being the NULL value,
extracting a BLOB via the field name in the SELECT clause, extracting a subrange of
a BLOB (i.e. an adjacent byte range of a BLOB), and extracting the size of a BLOB.
9.1.1.2. Size of BLOBs
BLOB fields are variable sized. The size of a BLOB object is restricted to the positive
byte range of a 4-byte integer (231 Bytes) minus some per-page-overhead of about
1%. The sum of sizes of all BLOBs of one table is restricted to 242 Bytes (about 4
Terabytes) minus some overhead of about 1.5 %.
A BLOB field can be declared NOT NULL. No secondary index can be built on a BLOB
field.
BLOB fields can appear in the ExprList of the SELECT clause of a QueryBlock, either
explicitly or via the '*' notation.
No operators (except the subrange operator and the SIZE OF operator, see below)
are allowed on BLOB fields.
With the SIZE OF operator, one can retrieve the size in bytes of a BLOB field. SIZE
OF delivers NULL if the field is NULL. The following example retrieves the sizes of all
BLOB objects in the sample table.
A BLOB field can appear in the SearchCondition of the WHERE clause only inside a
NullPredicate. It is important to note that the DISTINCT clause in the ExprList of a
SELECT clause does not eliminate 'identical' BLOB objects. This means that any two
BLOB objects are considered different in the database even if their contents actually
are identical. Analogously, the GROUP BY operator if applied BLOB objects forms one
GROUP for every BLOB object.
BLOB objects have no meaningful order for the user. It is not an error to apply the
ORDER BY clause to BLOB fields but the ordering refers to internal BLOB addresses
and thus the result is of no use in the user's view.
9.1.3.3. Spooling BLOBs
The SPOOLing of tables with BLOB objects is described in Chapter 'Spooling Lob
Objects' within the Section 'The Data Spooler'.
9.2.1.1. Overview of operations
Each field of type CLOB either contains the NULL value or a CLOB object. The only
operations on CLOBs are creation, insertion, update of a CLOB, testing a CLOB on
being the NULL value, extracting a CLOB via the field name in the SELECT clause,
extracting a subrange of a CLOB (i.e. an adjacent byte range of a CLOB), extracting
a substring of a CLOB and extracting the size (number of characters) of a CLOB.
9.2.1.2. Size of CLOBs
CLOB fields are variable sized. The size (in bytes) of a CLOB object is restricted to
the positive byte range of a 4-byte integer (231 Bytes) minus some per-page-
overhead of about 1%. Thus the maximum number of characters of a CLOB
dependes on the size of the UTF8-encoded text. The sum of sizes of all LOBs of one
table is restricted to 242 Bytes (about 4 Terabytes) minus some overhead of about
1.5 %.
A CLOB field can be declared NOT NULL. No secondary indexes except a fulltext
index can be built on a CLOB field.
CLOB fields can appear in the ExprList of the SELECT clause of a QueryBlock, either
explicitly or via the '*' notation.
The following operators are allowed on CLOBS: SUBRANGE, SUBSTRING, SIZE OF,
CHARACTER_LENGTH and TO_CHAR (which is equivalent to a CAST VARCHAR(*)).
Note that without the CAST VARCHAR(n) operation (or the TO_CHAR function) the
result type would still be CLOB and not a representation as a printable string. This is
necessary to enable the insertion of a (possibly modified) CLOB object into another
CLOB field of a table.
With the SUBRANGE operator (n,m) where n and m are positive integers, a part of a
CLOB can be retrieved. The SUBSTRING function is equivalent. The following
examples retrieve the first 100 bytes of all book_content fields:
With the CHARACTER_LENGTH operator, one can retrieve the size in characters of a
CLOB field. The two operators deliver NULL if the field is NULL.
9.2.3.3. Spooling CLOBs
The SPOOLing of tables with CLOB objects is described in Chapter 'Spooling Lob
Objects' within the Section 'The Data Spooler'.
10. Fulltext Indexes
Transbase fulltext search is supported on fields of type CLOB, CHAR(p), CHAR(*),
VARCHAR(p) and VARCHAR(*).
10.1. FulltextIndexStatement
A FulltextIndexStatement is provided which creates a fulltext index on one field.
Syntax:
[128]FulltextIndexStatement ::= CREATE [POSITIONAL] FULLTEXT
INDEX IndexIdentifier
[FulltextSpec] ON TableIdentifier
(FieldIdentifier)
[ScratchArea]
[129] FulltextSpec ::= [ WITH SOUNDEX ] [ { Wordlist)
| Stopwords } ]
[Charmap] [Delimiters]
[130] Wordlist ::= WORDLIST FROM TableIdentifier
[131] Stopwords ::= STOPWORDS FROM
TableIdentifier
[132] Charmap ::= CHARMAP FROM TableIdentifier
[133] Delimiters ::= DELIMITERS FROM
TableIdentifier |
DELIMITERS NONALPHANUM
[134] ScratchArea ::= SCRATCH IntegerLiteral MB
Explanation: A fulltext index is the prerequisite for fulltext search on the specified
field (Fulltext-Predicate). Depending on whether POSITIONAL is specified or not, the
fulltext index is called positional index or word index.
A word index allows so called word search whereas a positional index additionally
offers so called phrase search. Word search and phrase search are explained below.
Beside the two variants called word index and positional index, fulltext indexes come
in three further independent variants. The specifications WORDLIST, STOPWORDS,
CHARMAP and DELIMITERS influence the contents of the fulltext index. They are
explained below. All four specifications include a TableName which is a user supplied
table. The contents of the table(s) supply information to the FulltextIndexStatement
at the time it is performed.
After the statement's execution, the contents of the tables are integrated into the
index and the tables themselves do not further influence the behaviour of the
created index. They can be dropped by the user if they are not needed any more for
other purposes.
The SCRATCH Clause is explained in Chapter 'Scratch Area for Index Creation'.
By WORDLIST, a positive list of words can be specified, i.e. specified words are
indexed only.
By STOPWORDS, a negative list of words is specified, i.e. all words except those in
the stopword list are indexed.
The tables supplied as Wordlist or Stopwords must have a single field of type
STRING or any other string type.
'ä' 'ae'
'ö' 'oe'
'ü' 'ue'
Note
The character mapping is applied to the indexed words as well
as to all search arguments in the FulltextPredicate. In the
example above, the word 'lösen' would be stored as 'loesen' and
a search pattern 'lö%' in a query would be transformed to
'loe%'.
It is also possible to specify the empty string as the target string for a certain
character. Consequently, this causes all occurrences of that character to be ignored.
For example, a record in CTable of the form
'.' ''
causes all occurrences of dot to be ignored. Thus, the word 'A.B.C.D' would be stored
as 'ABCD' (and search for 'A.B.C.D' would hit as well as a search for 'ABCD'). Note,
however, that in this example, a missing blank (delimiter, to be exact) after the
concluding dot of a sentence would have the undesired effect to combine 2 words
into one.
By default, a fulltext index works in case sensitive mode. Case insensitive search can
be achieved by supplying a character mapping table which maps each upper case
letter to its corresponding lower case letter.
10.1.3. DELIMITERS
The DELIMITERS clause specifies the word processing in the indexing process. If no
DELIMITERS clause is specified, the indexing procedure handles each longest
sequence of non-white-space characters as one word, i.e. by default, words are
separated by white-space characters (blank, tabulator and newline). Also non-
printable characters are treated as delimiters.
For example, the preceeding sentence would produce, among others, the words
'specified,' and 'non-white-space'. It is often convenient to supply additional word
delimiters like '(', '.' or '-'.
Note that search patterns in Fulltext Predicates are not transformed with respect to
delimiters (in contrast to CHARMAP!).
For example, if default delimiters have been used (white space) and a fulltext
predicate contains a search component with a blank ( e.g 'database systems'), then
no record fulfills the predicate. In this case, one would have to formulate a fulltext
phrase with two successive words ==- this is described later.
In all following examples for CreateIndexStatements, let f be a table which contains
a CLOB field fb, and wl, sw, del be unary tables containing a wordlist, a stopword list,
a delimiter character list, resp. Let cm be a binary table containing a character
mapping.
10.1.4. WITH SOUNDEX
The WITH SOUNDEX option adds the facility to search the fulltext index phonetically.
This can be specified in the fulltext search predicate "CONTAINS" by also adding a
SOUNDEX specification there: "CONTAINS SOUNDEX". See the example below and
the section FulltextPredicate .
The WITH SOUNDEX option triggers a moderate space overhead which tends to
become a negligible fraction as the base table grows.
... creating a fulltext index with phonetic search capability
CREATE FULLTEXT INDEX fbx WITH SOUNDEX ON f(fb)
... standard sarch is unaffected: searching for the name "Stuart" ...
SELECT * FROM f WHERE fb CONTAINS ( 'Stuart' )
... searching for names which sound like "Stuart" (also hits for "STEWART" e.g.) ...
SELECT * FROM f WHERE fb CONTAINS SOUNDEX ( 'Stuart' )
For each of the STOPWORDS, CHARMAP and DELIMITERS clause, another pseudo
table is created and is accessible like a normal table via a pseudo name. These
tables should not be confused with the tables specified in the Stopwords, Charmap
and Delimiters clause of a CreateIndexStatement. The latter are user defined tables
used to define the contents of the pseudo tables at statement execution time. Any
successive update to these user tables does not have any influence to the index and
its pseudo tables.
If the WITH SOUNDEX option has been specified, an additional pseudo table
SOUNDEX exists.
The names of the pseudo tables are derived from the name of the fulltext index. The
table and field names as well as their types are given as follows (assume that the
fulltext index has the name fbx):
FULLTEXT WORDLIST OF fbx ( word VARCHAR(*), wno INTEGER )
For example, to see the words indexed up so far or to see the valid delimiters (if a
DELIMITERS clause had been specified) one could say:
The pseudo tables are not recorded in the catalog table systable.
By modifications of these internal tables one can influence the indexing behaviour of
the fulltext index for future INSERTs into the base table. The current contents of the
fulltext index are not changed.
10.3. FulltextPredicate
Search expressions on fulltext-indexed fields are expressed with a FulltextPredicate.
Syntax:
FulltextPhrases are of different complexities. The simplest form is a single Atom (e.g.
a CHAR literal like 'database' or an application host variable). More complex forms
have sequences of Atoms separated by DistSpecs.
Note
If the FulltextPredicate is a phrase search then the fulltext index
must be a POSITIONAL fulltext index.
Wildcards:
Wildcard characters '%' and '_' have the same semantics as in the second operand
of the LIKE predicate.
Escaping Wildcards:
The character '\' is reserved to escape a wildcard. If '\' is needed as a character of a
word it must also be escaped. These rules are the same as in the LIKE predicate with
a specified ESCAPE '\'.
If Atom A is a MultiValueAtom with result set MV: WS(A) is the union of all
WS(A') where A' are Atoms for the single elements of MV.
Atom [m] Atom is equivalent to: Atom [0,m] Atom A missing distance
specification is equivalent to [0]. Especially, a phrase for a series of adjacent
words can be simply expressed as Atom Atom Atom ….
fb CONTAINS( 'object''database''systems' )
2.
SELECT * FROM F
WHERE fb CONTAINS ( 'object' 'database' 'systems' )
== yields TRUE for records where "fb"
== contains the series of the three specified words.
3.
SELECT word FROM FULLTEXT WORDLIST WT
WHERE EXISTS
(SELECT * FROM F WHERE fb CONTAINS ( WT.word ) )
== delivers the values of "word"
== which occur as words in the field "fb" of any record of F.
4.
SELECT * FROM F
WHERE fb contains ( ANY ( SELECT LOWER(word) FROM WT) )
== delivers the records of "F"
== whose "fb" value contains at least one lowercase word
== of the word set described by field "word" of table "WT".
Restriction 1:
A SingleValueAtom must not start with a '('.
'' || (SVA)
Restriction 2:
An Atom must not contain a field reference to the same block where the fulltext
table occurs.
Assume a table FT with fulltext field fb and fields fk and fc, where fc is of type
CHAR(*) and fk is the key.
SELECT * FROM FT
WHERE fb CONTAINS (fc) -- ILLEGAL
This query must be formulated by using a subquery which combines FT with FT via
the key fk:
Syntax:
Note that the arguments of the "CONTAINS SOUNDEX" operator are automatically
mapped onto their phonetic representation. It is thus unwise to apply the SOUNDEX
operator onto the argument itself.
It is also unwise in most cases to apply the standard "CONTAINS" operator with an
argument mapped to phonetic representation:
This concept stems from the fact that the arguments of "CONTAIN" might be
delivered also by a subquery processed at runtime.
10.5. Performance Considerations
10.5.1. Search Performance
The fulltext index enables very good search times for fulltext searches. It, however,
also causes some Performance limitations in database processing. This is described
in the following chapters.
The processing time considerably depends on the amount of available temporary disk
space. Transbase breaks all info to be fulltext-indexed into single portions to be
processed at at time. The performance increases with the size of the portions.
Note, however, that the scratch area is shared among all applications on the
database.
10.5.3. Record Deletion
Deletion of records from a table is slow if the table has at least one fulltext index.
The deletion takes time which is proportional (linear) to the size of the fulltext index.
Note additionally, that it is much faster to delete several records in one single
DELETE statement rather than to delete the records one at a time with several
DELETE statements.
11. Data Import/Export
Mass data import from various data sources is supported in Transbase®. Data can
origin from external data sources such as
databases,
or JSON files.
Also LOBs (Large OBjects) can be handled by the spooler, although - of course - the
corresponding files then do not contain text in general.
There is the possibility to choose between the DSV, the XML and the JSON mode of
the data spooler. Both modes are explained next.
11.1. SpoolStatement
Syntax:
[369] SpoolStatement ::= SpoolTableStatement |
SpoolFileStatement
[370]SpoolTableStatement ::= SPOOL LocalTableSpec
FROM [SORTED] FileLiteral
[LOCAL]
{ DSVSpec | XMLSpec | JSONSpec
}
[ClobInlineSpec] [BlobInlineSpec]
[371] SpoolFileStatement ::= SPOOL
INTO FileLiteral [LOCAL]
{ DSVSpec | XMLSpec | JSONSpec
}
[ClobInlineSpec] [BlobInlineSpec]
[LobFileSpec]
SelectStatement
[372] DSVSpec ::= [ FORMAT DSV ]
[CodePageSpec]
[NullSpec]
[DelimSpec]
[QuoteSpec]
[373] XMLSpec ::= FORMAT XML [NullSpec]
[374] JSONSpec ::= FORMAT JSON
[375] NullSpec ::= NULL [ IS ] StringLiteral
[376] DelimSpec ::= DELIM [ IS ] { TAB | StringLiteral
}
[377] QuoteSpec ::= QUOTE [ IS ] StringLiteral
[378] ClobInlineSpec ::= CLOBSINLINE
[379] BlobInlineSpec ::= BLOBSINLINE [HEX | BASE64]
[380] LobFileSpec ::= LOBFILESIZE = IntegerLiteral
[KB|MB]
[84] CodePageSpec ::= CODEPAGE [IS] CodePage
[ [ WITH | WITHOUT ] PROLOGUE
]]
[85] CodePage ::= UTF8 | UCS | UCS2 | UCS4 |
UCS2LE | UCS2BE | UCS4LE |
UCS4BE
Syntax:
[370]SpoolTableStatement ::= SPOOL LocalTableSpec
FROM [SORTED] FileLiteral
[LOCAL]
{ DSVSpec | XMLSpec |
JSONSpec }
[ClobInlineSpec] [BlobInlineSpec]
[371] SpoolFileStatement ::= SPOOL
INTO FileLiteral [LOCAL]
{ DSVSpec | XMLSpec |
JSONSpec }
[ClobInlineSpec] [BlobInlineSpec]
[LobFileSpec]
SelectStatement
[372] DSVSpec ::= [ FORMAT DSV ]
[CodePageSpec]
[NullSpec]
[DelimSpec]
[QuoteSpec]
[375] NullSpec ::= NULL [ IS ] StringLiteral
[376] DelimSpec ::= DELIM [ IS ] { TAB | StringLiteral
}
[377] QuoteSpec ::= QUOTE [ IS ] StringLiteral
[84] CodePageSpec ::= CODEPAGE [IS] CodePage
[ [ WITH | WITHOUT ] PROLOGUE
]]
[85] CodePage ::= UTF8 | UCS | UCS2 | UCS4 |
UCS2LE | UCS2BE | UCS4LE |
UCS4BE
Explanation:
The SpoolTableStatement inserts records from the specified file into the specified
table (base table or view). The specified table must exist (but need not necessarily
be empty). Thus, the SpoolTableStatement can be used as a fast means for insertion
of bulk data.
The SpoolTableStatement has very good performance if the records in the table are
ascendingly sorted by the table key(s) (best performance is achieved if the table
additionally is empty). If the records are not sorted then Transbase inserts on-the-fly
those records which fulfill the sortorder, the others are collected, then sorted, then
inserted. For very large unsorted spoolfiles, it can be advantageous to split and spool
them into pieces depending on the available disk space which is additionally needed
temporarily for sorting.
Usage of the keyword SORTED allows to test if the input is actually sorted (if
specified, an error is reported and the TA is aborted when a violation of the sort
order is detected). This feature does not influence the spool algorithm but only
checks if the input was suited to be spooled with maximal performance.
Without the LOCAL keyword, the specified file is read by the client application and
transferred to the Transbase service process. If the file is accessible by the service
process, the LOCAL clause can be used to speed up the spool process: in this case
the service process directly accesses the file under the specified name which must be
a complete path name.
For the checking of integrity constraints (keys, null values) the same rules as in
InsertStatement apply.
The SpoolFileStatement stores the result records of the specified SelectStatement
into the specified file (which is created if it does not yet exist, overwritten
otherwise).
The spool files are searched or created in the current directory by default if they are
not absolute pathnames.
The codepage specification UTF8 means that the external file is UTF8-coded.
The codepage specification UCS2LE means that the external file is UCS2 (2 byte
fixed length, little-endian). The codepage specification UCS2BE means that the
external file is UCS2 (2 byte fixed length, big-endian). The codepage specification
UCS2 means that the external file is UCS2 (2 byte fixed length, default format).
The codepage specification UCS4LE means that the external file is UCS4 (4 byte
fixed length, little-endian). The codepage specification UCS4BE means that the
external file is UCS4 (4 byte fixed length, big-endian). The codepage specification
UCS4 means that the external file is UCS4 (4 byte fixed length, default format).
The codepage specification UCS means that the external file is the default UCS in
default format, which is e.g. UCS2 on Windows platforms and UCS4 on UNIX
platforms.
The optional PROLOGUE clause can be applied if the external file is prologued with
the Unicode character 0uFEFF. If no PROLOGUE clause is given on input and no byte-
order is specified, the byte order is determined automatically. If a byte-order is
specified, and a differing PROLOGUE character is found, a runtime error is reported.
11.1.1.1. FILE Tables
Syntax:
[83]FileTableStatement ::= CREATE
FILE ( FileLiteral [CodePageSpec]
[NullSpec] [DelimSpec] )
TABLE [IF NOT EXISTS]
TableIdentifier
( FieldDefinition [ , FieldDefinition ]...
)
[90] FieldDefinition ::= FieldIdentifier DataTypeSpec
[ DefaultClause | AUTO_INCREMENT
]
[ FieldConstraintDefinition ]...
[91] DataTypeSpec ::= DataType | DomainIdentifier
Data stored in spool files or other compatible file formats may be integrated into the
database schema as virtual tables. These FILE tables offer read-only access to those
files via SQL commands. They can be used throughout SQL SELECT statements like
any other base table.
The table definition supplies a mapping of columns in the external file to column
names and Transbase datatypes.
Currently a File table can only be created WITHOUT IKACCESS and no key
specifications are allowed. Therefore the creation of secondary indexes is currently
not possible. These restrictions might be dropped in future Transbase versions.
FILE tables are primary designed as an advanced instrument for bulk loading data
into Transbase and applying arbitrary SQL transformations at the same time.
CREATE FILE (/usr/temp/data_file)
TABLE file_table WITHOUT IKACCESS
(a INTEGER, b CHAR(*))
If not explicitly stated differently, the following description of records in text files
both applies to the format generated by the spooler and to the format accepted by
the spooler:
By default, the character ? represents a null value of any type unless differently
specified by the NullSpec. The NullSpec always is exactly one character.
A non-empty string x1 ... xn is spooled out with single quotes and as sequence
of transformed characters 'T(x1) ... T(xn)'. In most cases T(xi) = xi holds.
However, characters which have a special meaning must be escaped. Thus, for
some characters x, T(x) is a two-character-sequence of a backslash ('\') and the
character x. Special characters and their representation are shown in the table
below.
As input for the spooler, the string can be represented as x1 … xn as well as 'x1 … xn'
(i.e. the spooler eliminates surrounding quotes).
Special characters and their representation inside strings are shown in the following
table.
' \'
<tab> \t
<newline> \n
\ \\
Special Rule for Type Binchar
As stated above, when spooling tables from external files, the spooler accepts strings
in the form xyz as well as 'xyz', although the form xyz is not a valid SQL literal for
the type (VAR)CHAR(p) or CHAR(*). This is comfortable for data migration into
Transbase but has the consequence that table spooling compromises type
compatibility in the case of CHAR and BINCHAR. Inside a spool file, values for a
BINCHAR field must be written as BinaryLiterals, e.g. in the form 0xa0b1c2 .
Whereas a value in the form xyz is accepted for a CHAR field, the same value is not
accepted for a BINCHAR field because special values in that form would not be
parsable in a unique manner, e.g. 0xa0b1c2 could be interpreted as a 8 byte CHAR
value or a 3 byte BINCHAR value.
11.1.1.3. Key Collisions
When a table is spooled then Transbase rejects the data if there are 2 different
records with the same key. In this situation the data to be spooled is inconsistent
with the table creation specification. It may be advantageous to use Transbase to
find out all records which produce a key collision. For this, recreate the table with the
desired key but extended to all fields. For example, if the table T has the key on k1
and other fields f2,f3,f4, then create a table TFK with the clause: KEY IS k1,f2,f3,f4.
Then spool the table which in any case now works (except there are syntactical
errors in the spoolfile). To find out all records with the same key, issue the query:
SELECT *
FROM TFK
WHERE k1 IN
( SELECT k1
FROM TFK
GROUP BY k1
HAVING COUNT(*) > 1
)
ORDER BY k1
This is the default behaviour. BLOBs and CLOBs may also be stored inline if
BLOBSINLINE or CLOBSINLINE, resp., is specified in the SPOOL statement.
Assume a 4-ary table ''graphik'' with field types CHAR(20), INTEGER, BLOB, CLOB.
Then a set of BLOBs whose size is not bigger than 10 MB is stored in a single
file instead of n different files. Without a KB or MB qualification, the specified
number is interpreted as MB.
BLOBs and CLOBs may also be spooled as inline values inside the main target
spool file.
The option CLOBSINLINE outputs all CLOB fields as inline values similar to CHAR
and VARCHAR fields.
The option BLOBSINLINE HEX writes BLOB fields in hexadecimal form. This
representaion is more space consuming than BASE64.
Note that for spooling a table from such a file also the corresponding INLINE
specifications are mandatory in the SPOOL command.
Example 11.2. Spooling a table with LOBs from a file
If BLOBs and/or CLOBs are given as INLINE values in the source spool file, then
the SPOOL command must look like:
On all three places mentioned above, Transbase SQL allows filenames in UNIX syntax
as described in the preceeding chapters. This means that all examples about data
spooling and LOB filenames in spoolfiles also would be legal when the application
and/or the database server run on MS WINDOWS.
Table 11.2. Special Characters
< <
> >
& &
" "
' '
1. The names of the elements have to be identical to the column labels of the
destination table.
2. The elements are labeled with Field and have to carry an field name whose
value displays the name of the table column.
<lname>Smith</lname>
<Field name="lname">Smith</Field>
Finally, values are presented as content of XML elements at the fourth level. The XML
elements also may carry attributes. At the first level, the attributes name and
nullrep are defined. The first defines the name of the destination table. The later
one is used for the definition of the null representation. Its meaning is explained in
section: The Representation of Null Values . For the second level (i.e. Row), only the
attribute name is known by the spooler. The attributes defined for the third level and
their meanings are declared in section: XML Attributes Known by the XML Spooler .
An example document with the document tree containing the four levels is shown in
Figure: Example of an XML Document and the Document Tree .
According to these syntax rules, there are tag names with special meaning in the
spool file called delimiter tags: Table, Row, Field and Column (see section: The
Usage of Format Information ). The XML spooler is case insensitive concerning these
labels, i.e. one can write ROW or row and so on.
In contrast to this, in the DSV data spooler (Delimiter-Separated Values mode) the
values of each record are presented in one line in the spool file. Usually, the values
are separated by a tabulator ('\t') sign. Each record must have the same number of
elements as there are columns in the destination table. Furthermore, these elements
need to have the same ordering as the table columns. NULL values are usually
presented as '?' per default.
In Figure: DSV spool File , a spool file suited for the spooler in the DSV mode is
shown. It is used to transfer data into the table SUPPLIERS. The CREATE statement
of that table is defined as follows:
The spool file shown in Figure: DSV spool File has three records. In the XML spool
file, additionally the structural information, as described above, is required.
Figure 11.3. XML Spool File
<Table>
<Row>
<Field name="supno">5</Field>
<Field name="name">DEFECTO PARTS</Field>
<Field name="address">16 BUM ST., BROKEN HAND WY</Field>
</Row>
<Row>
<Field name="supno">52</Field>
<Field name="name">VESUVIUS, INC.</Field>
<Field name="address">512 ANCIENT BLVD., POMPEII NY</Field>
</Row>
<Row>
<Field name="supno">53</Field>
<Field name="name">ATLANTIS CO.</Field>
<Field name="address">8 OCEAN AVE., WASHINGTON DC</Field>
</Row>
</Table>
Figure: XML Spool File shows an XML spool file containing the same data as shown in
the DSV spool file from Fig. dsv_spf .
In contrast to the DSV spool file, within an XML spool file the order of the record
fields does not matter. Furthermore, additional elements may be present or elements
can be missing (see also section: Transfering XML Data Into the Database ). This
provides more flexibility in order to transfer query results into a database table
whose scheme does not exactly match the output of the query.
Note
The Transbase XML spooler is not able to read XML documents
containing a Document Type Description nor is it able to manage
documents with namespace declarations.
11.1.2.2. Principal Functionality of the XML Spooler
11.1.2.2.1. Transfering XML Data Into the Database
Syntax:
Explanation:
<tablename> is the name of the destination table where the records of the spool file
are inserted.
DSV stands for delimiter separated values. If the statement contains no format
option the DSV mode is used per default. XML signals the XML spooling mode.
With the 'NULL IS' option, the null representation can be defined (see also section:
The Representation of Null Values ).
In case of the XML mode, the spooler scans the provided spool file and reads until
the end of a record is reached (signaled by the end tag</Row>). If a field is missing,
the default value is inserted in the database. If no default value is available, the
NULL value is used for that field. If there are additional fields for which no column in
the destination table can be found, these fields are ignored.
<Table>
<Row>
<address>64 TRANQUILITY PLACE, APOLLO MN</address>
<anything>?</anything>
<supno>57</supno>
</Row>
</Table>
So for example, the spool file of Figure: Complex XML spool File contains one record
for the table SUPPLIERS (see section: The Syntax of the XML Spool File). The
ordering of the fields does not match with the ordering of the table columns. The
field 'name' is missing and since no default value is present, this field gets the value
NULL. Furthermore, the record of the spool file contains a field labeled 'anything'
which is ignored because the table SUPPLIERS does not have any column of that
name.
11.1.2.2.2. Extracting Query Results Into an XML Document
Syntax:
[371]SpoolFileStatement ::= SPOOL
INTO FileLiteral [LOCAL]
{ DSVSpec | XMLSpec | JSONSpec
}
[ClobInlineSpec] [BlobInlineSpec]
[LobFileSpec]
SelectStatement
[373] XMLSpec ::= FORMAT XML [NullSpec]
[375] NullSpec ::= NULL [ IS ] StringLiteral
Explanation:
If the format option XML is used the result of the entered statement is formatted to
XML. The SelectStatement can be any valid select statement.
As explained in section: The Syntax of the XML Spool File, the root of an XML spool
file is labeled Table. The information of each record is presented within the begining
and ending Row tag. For each record field, the name of the associated column is
presented as attribute of the beginning Field tag. Between the beginning and the
ending Field tags, the query result for this field is printed (see Figure XML Spool File
).
The XML Spooler notices only the value of the encoding attribute within the
declaration. All other information is ignored. However, at the moment, the XML
spooler supports only UTF-8 encoded XML documents.
11.1.2.3.2. The Usage of Format Information
The XML spooler provides the opportunity to add format information as a header in
front of the records. Such information are declared for the datetime and timespan
types, so far. They are used to specify the range of these types for the complete
spool file. Nevertheless, another format description may be entered as attributes
within a record field. Furthermore, within the format information, the null
representation and the default value of a table column may be defined. The format
information has to be placed after the root tag (i.e. before the first record). For each
column of the destination table, which requires additional information, an XML
element named Column carrying several attributes is defined. This kind of
information is called format information header here after wards.
<Table>
<column name="bdate" type="datetime[yy:dd]" nullrep="x"/>
<column name="age" type="timespan[dd:ss]" default="22592 12:2:4"/>
<Row>
...
</Row>
</Table>
Figure Format Information Header shows an example of the format information
header at the beginning of the spool file. For the Column element the attributes name,
type, nullrep, and default are defined. With the value of the name attribute, the
column of the destination table is identified. Accordingly, the type attribute carries
the type definition and the range specification of this column as value. If the nullrep
and/or the default attributes are present they define the representation of the null
and/or the default value for the according table column. Because of the format
information shown in Figure: Format Information Header, the XML Spooler supposes
that values of the column bdate for example are of type datetime and are formatted
beginning with the year and ending with days. Accordingly, values of the column age
are of type timespan, beginning with days and ending with seconds. If this field is
missing in one of the record, the default value '22592 12:2:4' is inserted.
The Usage of Format Information when Transferring Data Into the Database
Together with the option explained above, there are four possibilities how the XML
spooler determines which format should be used for a record field:
1. Datatime or timespan values can be represented in the TB/SQL syntax, i.e. the
format information is written in front of the data (see section External File
Format).
<Table>
<Row>
...
<age>timespan[dd:ms](19273 12:2:4:1)</age>
</Row>
</Table>
<Table>
<Row>
...
<bdate type="datetime[yy:dd]">1945-12-8</bdate>
</Row>
</Table>
If the parser determines this attribute, it remembers its value until it can be
used for type checking before inserting the record in the database.
Note
There is also the possibility to enter format information as
TB/SQL syntax and additionally provide the concerning XML
attributes. In this case, the attributes are ignored.
<today type="datetime[yy:dd]">
datetime[yy:hh](2007-12-11 15)
</today>
In this case, the spooler assumes that the range specification of [yy:hh] is
correct.
3. A header containing the format information as described above may be present.
This information is only used, if the parser did not find such information within
the field declaration (either as XML attributes or as TB/SQL representation).
4. If there is neither any format information within the field declaration nor any
format information header present, the XML spooler assumes that the
appropriate value has the format as defined in the column of the destination
table.
Note
If the format of the value does not match the format to be used
according to the added format information or the database
scheme, an error handling is started (see section: Error Reports
).
<Table>
<column name="bdate" type="datetime[yy:dd]"/>
<column name="age" type="timespan[dd:ss]"/>
<Row>
...
</Row>
</Table>
If an 'x' is scanned for a field value, the spooler generates a NULL to be inserted in
the database. If the value 'x' should be inserted instead of NULL, in the spool file the
attribute null has to be set to false.
<Table>
<Row>
<Field name="lname" null="false">x</Field>
...
</Row>
...
</Table>
Note
The XML spooler also supports the representation of null values
by setting the null attribute of the Field element to true.
Hence, the following two lines have the same meaning, if the
null representation is set to 'x':
<Field name="lname">x</Field>
<Field name="lname" null="true"/>
The Null Representation for the Complete Document
As mentioned in section: The Syntax of the XML Spool File , the Table element may
have an attribute named nullrep. Its value displays the representation of the null
value for the remaining document. In contrast to the representation of the table
spool statement, this value may be a string, not only a single character. If the
nullrep attribute is present within the Table tag, the null representation of the
spool statement - if any - is ignored. Again, if for a record field the same value as for
the null representation should be inserted in the database, the null attribute has to
be set to false.
<Table nullrep="xyz">
<Row>
<Field name="lname">x</Field>
<Field name="rname" null="false>xyz</Field>
<Field name="address">xyz</Field>
...
</Row>
</Table>
Since the nullrep attribute is present, the value 'x' for the field lname is not
interpreted as null although it was defined as null representation by the spool table
statement. Thus, the following record values are inserted in the database: x, xyz,
NULL, ...
<Table nullrep="xyz">
<Column name="lname" nullrep="abc"/>
<Row>
<Field name="lname">abc</Field>
<Field name="rname">xyz</Field>
...
</Row>
<Row>
<Field name="lname" null="false">abc</Field>
<Field name="rname" null="false">xyz</Field>
...
</Row>
<Row>
<Field name="lname">xyz</Field>
<Field name="rname">x</Field>
...
</Row>
</Table>
Although, if in the spool statement the NULL IS 'x' option was used, the following
record values are generated and inserted in the database:
spool into test.xml format xml null is 'x' select * from employee
<Table nullrep="x">
<Row>
<Field ....
</Row>
...
</Table>
In the following example, a spool file is used to transfer data in a table called
employee. The CREATE TABLE statement of this destination table looks as follows:
For each record, where the field fname is not present, the value "Mary" is inserted.
If the default value of the table description represents a sequence, this sequence has
to be updated each time the default value is used.
In the following, parts of a spool file are shown that should be transfered into a data
base table:
<Table>
<Row>
<Field name="ssn">20</Field>
...
</Row>
...
</Table>
The field 'ssn' is of type integer and has as default a sequence. In the first record,
this field is present. In all other records not shown, the field is missing and hence,
the sequence is increased each time the default value is inserted.
Note
If there are more than one sequences per table, all sequences
are increased at the same time. Hence, more sequences may
result in confusing values.
In this case, for missing fields with the name fname, the default value "Max" is
inserted.
If the attribute default is present within the format information header, the XML
spooler checks if its value is valid according to the type declaration of the table
description. If an uncorrect value was entered the definition of the default value is
ignored.
Note
The definition of the default value within the format information
header has a higher priority than that of the table description.
I.e. if both, the table description and the format information
header contain a default value for the same field, the default
value of the table description is ignored.
1. The null attribute is set to true and no value is entered. In this case, usually no
closing tag is required because the opening tag is closed after the attribute
declaration.
<today null="true"/>
2. Although no value is entered, it is valid to use the opening and closing tag
within the XML document.
<today null="true"></today>
<today null="true">2007-12-07</today>
Since the attribute value null is set to true, the data entered between opening
and closing tag is ignored and a NULL is inserted for this record field.
Attributes Defining Lob Characteristics
The attributes blobfile, clobfile, blobsize, clobsize and offset are used when
spooling lob. More details for these attributes can be found in chapter: Spooling of
Lobs with the XML Spooler .
11.1.2.4. Error Reports
The transbase error handling differs between hard and soft errors. If a hard error
occurs, the insertion process is stopped immediately and a roll back is performed.
Hence, in case of an hard error, no record from the spool file is inserted. If a soft
error is encountered, the appropriate record is ignored and skipped and all correct
records are inserted.
11.1.2.4.1. Hard Errors
Concerning the XML spool mode, hard errors occur in connection with lobs or if an
unexpected tag is encountered. If at least one column of the destination table is of
type blob, each encountered error is handled as an hard error. The error of an
unexpected tag occurs especially in connection with the delimiter tags defined in
section: The Syntax of the XML Spool File. So for example, an XML spool file my
begin only with an XML declaration or with the Table tag. After the beginning Table
tag, the XML spooler accepts only a Column or a Row Tag. At the end of a Column tag,
a further Column tag or a beginning Row tag is required. Finally, after a closing Row
tag, only a beginning Row or an ending Table tag is allowed. If the spooler
encounters another tag as expected, the spool process is aborted since no realistic
recovery point is available.
11.1.2.4.2. Soft Errors
XML Syntax Errors
According to the error treating, there are three classes of XML syntax errors: hard
errors as unexpected tags, syntax errors forcing the skipping of a record, and syntax
errors leading in a scanning to the next recovery point. The first error class is
already explained in section: Hard Errors, the other two classes will be explained
next.
1. XML Syntax Errors Leading in a Skip Operation: If an XML syntax error occurs
that still allows the correct interpretation of the following XML segments (i.e.
tag, attribute, value, ...), the rest of the record is skipped. This means, the
record is not inserted into the database but is written in the error report with an
appropriate error message as XML comment. The end tag differs from the
beginning tag as shown next.
<Row>
<fname>John</fname>
<lname>Smith</lname>
<ssn>123456789</ssn>
<address>731 Fondren, Houston, TX</add>
<sex>M</sex>
</Row>
In the example, the fourth record field starts with an address tag and ends with
an add tag. In this case, the complete record is written in the error file that
contains all incorrect records along with the proper error message. The spooling
then starts at the beginning of the next record.
Error Message:
<Row>
<fname>John</fname>
<lname>Smith</lname>
<ssn>123456789</ssn>
<!== missmatch between open and closing tag ==>
<address>731 Fondren, Houston, TX</add>
<sex>M</sex>
</Row>
2. XML Syntax Errors Leading in an Ignore Operation: If in the XML spool file an
unexpected sign occurs, the spooler is not able to interpret the meaning of the
following segments. Hence, nothing of the record is inserted in the database.
The spooler ignores everything until to the next recovery point. A recovery point
can be found at the beginning of each record, i.e. if a beginning Row tag is
encountered. Such errors are for example missing angles, forgotten inverted
commas, and so forth. Due to the restricted syntax of XML spool files, the
transbase spooler also interprets mixed content as syntax error.
<Row name="row1">
<field name="fname">?</field>
this text is mixed content
<lname>?</lname>
...
</Row>
<Row name="row2"'>
...
</Row>
After a closing Field tag, only an opening Field, a closing Row tag, or a XML
comment is expected. When scanning the text after the first record field, the
spooler ignores the rest of the record and starts the spooling process at the
begin of the next record <Row name="row2>). In the error report, the
following error message is written.
<Row name="row1">
<field name="fname">?</field>
<!== XML Syntax error found, scanning to begin of next record ==>
</Row>
1. Invalid Null Definition: If in the table description a field is declared to be not null
and in the spool file for this field the null attribute is set to true or the value for
the null representation was entered, this record is skipped. In the following
example, the field address must not be null according to the table description.
<Row>
<fname>Franklin</fname>
<lname>Wong</lname>
<ssn>333445555</ssn>
<address null="true"/>
<sex>M</sex>
</Row>
Error Message:
<Row>
<fname>Franklin</fname>
<lname>Wong</lname>
<ssn>333445555</ssn>
<!== field must not be null ==>
<address null="true"/>
<sex>M</sex>
</Row>
2. Wrong Types: Such errors occur for example, if a string is added where a
numeric value is supposed.
<Row>
<fname>Joyce</fname>
<lname>English</lname>
<ssn>453453453</ssn>
<address>563 Rice, Houston, TX</address>
<sex>M</sex>
<salary>error</salary>
</Row>
In the example above, the field salary is of type numeric. During the record
spool, a string value is scanned and hence the error handling is started.
Error Message:
<Row>
<fname>Joyce</fname>
<lname>English</lname>
<ssn>453453453</ssn>
<address>563 Rice, Houston, TX</address>
<sex>M</sex>
<!== numeric error ==>
<salary>error</salary>
</Row>
Errors Concerning XML Attributes: In table: Attributes and Their Values, the
attributes and their possible values are listed. Errors concerning XML attributes
are encountered if a not expected value was entered. The attributes can be
classified in three categories: attributes describing format information,
attributes describing characteristics of lobs and attributes belonging to the
Field element. The error handling for attributes depends on this classification.
3. a. Errors Within Field Attributes: Usually, errors are encountered during the
spooling stage which causes the skipping of the record and the generation
of an appropriate error message. An example of an incorrect attribute
value and the according error message is shown below.
<Row>
<fname>James</fname>
<lname>Borg</lname>
<ssn>888665555</ssn>
<bdate null="nonsense">1975-11-30<bdate>
<sex>M</sex>
</Row>
In this case, the attribute 'null' of the XML element 'bdate' has the value
'nonsense'. Since for this attribute only the values 'true' or 'false' are
defined, the following error message is generated.
<Row>
<fname>James</fname>
<lname>Borg</lname>
<ssn>888665555</ssn>
<!== not expected attribute value (nonsense) ==>
<bdate null="nonsense">1975-11-30<bdate>
</Row>
b. Errors at Attributes Describing Lob Functionality: As mentioned above, if
the destination table of the spool statement contains at least one lob
column, each error is classified to be an hard error. Due to this, wrong
values for the attributes offset, and blobsize result in an hard error.
Hence, in such a case, no record of the spool file is inserted in the
database.
<Row>
<fname>James</fname>
<lname>Borg</lname>
<ssn>888665555</ssn>
<sex>M</sex>
</Row>
In this example, the field address which was declared to be not null is not present
within the record. Hence, if no default value is available, the following error message
is generated.
Error Message
2. There is at least one error for each line: If an XML document is used with the
DSV spooling mode usually no correct record is encountered in the spool file,
i.e. there are as many errors as spooled lines.
If the XML mode is used, the file names of the blobs are entered as values of the
attribute blobfile and the file names of the clobs are entered as values of the
attribute clobfile at the according record fields. The spool statement is the same
as explained in section: Transfering XML Data Into the Database. Figure XML Spool
File Containing Blobs shows an example of an XML spool file containing blob file
names. It is used to spool records in the table blobex which was created with the
following statement:
CREATE TABLE blobex (nr INTEGER NOT NULL,
picture BLOB,
PRIMARY KEY (nr))
<Table>
<Row>
<nr>1</nr>
<picture blobfile="B0000001.001"/>
</Row>
<Row>
<nr>4</nr>
<picture blobfile="maske.jpg"/>
</Row>
<Row>
<nr>6</nr>
<picture blobfile="photo.jpg"/>
</Row>
</Table>
11.1.2.5.3. Inline Lobs
As in the DSV spooling mode, the lob data may also be entered as inline information.
In an XML spool file, the inline lob data is presented as value between the proper
opening and closing field tags. For inline blobs, the attributes blobfile, blobsize
and offset are not present. Hence, if none of those attributes was entered, the
spooler assumes that the value between open and closing tag belongs to an inline
blob. Inline lobs are only useful if the lob is not too large. Inline lobs have to be
encoded with hex representation or with the base64 (for pictures).
In this example, the value of the blob represents parts of the code of a jpg-encoded
picture.
While in the DSV spooling mode, mixing of inline lobs and lobs data located in a file
is not possible, this mechanism is allowed in the XML spooling mode. The spooler
decides because of the attributes that are available or not if the lob data is located in
the spool file or if it has to be loaded from a file.
11.1.2.5.4. Storing Several Lobs in One File
Spooling Several Lobs into One File
As in the delimiter separated value mode, also in the XML mode it is possible to
spool several lobs into one file.
Figure: Output DSV Spool File shows the output document that is generated for the
statement above when using the DSV mode. For each lob optionally the file name
and a the byte offset is printed. The size of the lob always has to be present (see
Figure: Output DSV Spool File ).
Figure 11.7. Output DSV Spool File
1 'B0000001.001<0:11505>' 'M'
4 '<11505>' 'M'
6 '<11505>' 'M'
7 '<11505>' 'M'
This output specifies that the first lob can be found in the file B0000001.001 at byte
offset 0 and has a size of 11,505 bytes. Since for the second and all further lobs no
file name is add the same file is used. For those lobs only the size is specified. This
means, the lob starts with the end of the lob before.
In the XML mode, the size and offset values are written as XML attributes (see
Figure: Output XML Spool File ).
Figure 11.8. Output XML Spool File
<Table>
<Row>
<nr>1</nr>
<picture offset="0" blobsize="11505" blobfile="B0000001.001"/>
<sex>M</sex>
</Row>
<Row>
<nr>4</nr>
<picture blobsize="11505"/>
<sex>M</sex>
</Row>
<Row>
<nr>6</nr>
<picture blobsize="11505"/>
<sex>M</sex>
</Row>
<Row>
<nr>7</nr>
<picture blobsize="11505"/>
<sex>M</sex>
</Row>
<Row name="nr4">
<nr>8</nr>
<picture blobfile="photo.jpg"/>
</Row>
</Table>
11.1.3.1. Characteristics of JSON
JSON (JavaScript Object Notation) is an open-standard human-readable file format
commonly used e.g. for data exchange and consists auf attribute:value pairs within
object and array data types.
Therefore the overall structure of a Transbase® spool file looks like (assuming the
table has m records with n fields):
[
{
"<field_1>": <value_1-1>,
"<field_2>": <value_1-2>,
...
"<field_n>": <value_1-n>
},
{
"<field_1>": <value_2-1>,
"<field_2>": <value_2-2>,
...
"<field_n>": <value_2-n>
},
...
{
"<field_1>": <value_m-1>,
"<field_2>": <value_m-2>,
...
"<field_n>": <value_m-n>
}
]
unknown fields - i.e. fields not defined in the table - will be ignored simply;
missing fields - i.e. fields defined in the table but not specified in the JSON
spool file - are replaced by a NULL value or the default value if any has been
defined for this field.
Note
AUTO_INCREMENT fields are defined automatically as NOT NULL
and with default value AUTO_INCREMENT. If explicitly a NULL
value is specified in the JSON spool file an error is returned. If
the field is not specified in the JSON spool file the default value
is taken.
11.1.3.6. Default values
As mentioned above for all fields defined in the table without a specified value in the
JSON spool file the default value is taken. If no default value is defined for the field
the NULL value is taken.
11.1.3.7. LOB objects
Spooling large objects (LOBs) - as separate files, possibly summarized, or inline - all
criteria are valid as noted by the DSV spooler: Spooling LOB objects.
11.2. External data sources
11.2.1. Remote Database Access
Transbase offers direct and transparent read/write access to remote Transbase
databases for distributed queries and data import. Please consult TableReference
for details on how to connect to a remote Transbase site in an SQL statement.
INSERT INTO T
SELECT q.partno, supp.sno
FROM quotations@//server3/db1 q, suppliers@//server5/db2 supp
WHERE q.suppno = supp.sno
11.2.2. JDBCReader
Additionally it is possible to transfer data from arbitrary JDBC or other database data
sources via TableFunction s. These functions may be used throughout SQL SELECT
statements like any other base table and can be used for querying remote data, data
loading and data transformation.
The JDBCReader can be used for querying remote JDBC data sources or for data
import.
The OraReader can be used for querying remote Oracle data sources for data import.
11.2.4. FILE Tables
Data stored in files may be integrated into the database schema as virtual tables.
These FILE tables offer read-only access to those files via SQL commands. They can
be used throughout SQL SELECT statements like any other base table.
FILE tables are primarily designed as an advanced instrument for bulk loading data
into Transbase and applying arbitrary SQL transformations at the same time.
All users are allowed to read the data dictionary, i.e. to retrieve information
about the database structure. Reading the data dictionary is in no way different
from reading user tables.
Locks on SystemTables: For read access of system tables, a read lock is set
as for user tables. However, to avoid bottlenecks on the system tables the read
locks are released immediately after the evaluation of the corresponding query.
Repeated read accesses to the system tables might produce different results, if
- in the meantime - a DDL transaction has been committed.
Summary of SystemTables:
The data dictionary consists of the following tables:
sysblob contains imformation about LOB containers used for BLOBs and
CLOBs.
sysdatafile contains information about the data files the data spaces are
stored in.
The sysdatabases table in the admin database contains entries for all configuration
parameters of all databases managed by the corresponding Transbase service.
Field Explanation
database_name The name of the database concerned. [sysdatabases only]
property The database property or database configuration parameter.
value The current value of this property for the specified database.
The unit in which the current value is specified where this
unit
applies.
datatype The data type of the property.
Gives useful information on this property. Mostly the collection
comment
of possible values and the default value.
Column Type
username CHAR(*)
userclass CHAR(*)
passwd CHAR(*)
userid INTEGER
Primary key: (username)
dba has all access rights including the right to add or drop users and to
alter or drop objects without being the owner.
dba implicitly has all privileges on any database object including the right
to revoke privileges granted by others. Strictly speaking dba bypasses all
privilege checks.
passwd: Contains the encrypted password of the user. Note that the user's
password is not stored anywhere in unencrypted form.
userid: Gives a unique identification for the user. This userid is used in other
system tables to refer to users, e.g. in the systable table to denote the owner
and the schema of a table.
Explanation:
Upon creation of a database two users are already predefined and cannot be
dropped:
These users cannot be dropped but their userclass can be changed. The keywords
public and tbadmin are provided to refer to these users in case-insensitve form.
The user public is of particular importance in the GrantPrivilegeStatement.
Security Alert
The password for tbadmin should be changed asap. for security
reasons.
A.4. The systable Table
The systable table contains a single entry for each table, view or sequence defined in
the database.
Column Type
tname CHAR(*)
owner INTEGER
ttype CHAR(*)
segno INTEGER
colno INTEGER
date DATETIME[YY:MS]
cluster SMALLINT
version INTEGER
indextype CHAR(*)
schema INTEGER
Primary key: (schema, tname)
owner: Denotes the owner of the table or view by the user's userid. To retrieve
the owner 's username, join the tables sysuser and systable (see example
below).
A user table has the value ''R'' or ''r'', where ''R'' is used for a table created
WITH IKACCESS (this is the default) and ''r'' for a table created WITHOUT
IKACCESS.
User views are denoted with entry ''V''. System tables which are visible to the
user have entry ''v''. In effect, all system tables described in this manual are
views.
colno: Contains the number of the columns (fields) of the given table or view.
date: Contains the creation time of the table or view (type {tt
DATETIME[YY:MS]}).
cluster: Contains the dataspace number of the table. Tables created without
DATASPACE option contain the number 0 here (system data space). Tables
created with in a user defined DATASPACE carry a number > 0 which is an
internal identifier for the user created DATASPACE. The mapping between
dataspace names and numbers is visible in the (virtual) system table
"sysdataspace".
Column: Type:
tsegno INTEGER
cname CHAR(*)
ctype CHAR(*)
domainname CHAR(*)
defaultvalue CHAR(*)
suppressed INTEGER
cpos INTEGER
ckey INTEGER
notnull CHAR(*)
surrid INTEGER
surref INTEGER
domainschema INTEGER
Primary key: (tsegno, cpos)
tsegno: Identifies the table or view to which the entry belongs. The name of
the table can be retrieved via a join between systable and syscolumn on the
fields tsegno and segno of syscolumn and systable , resp.
ctype: contains the base data type of the column. The data type is given as
specified in the corresponding CREATE TABLE statement. Although data type
specifications in TB/SQL are case-insensitive, strings stored in field ctype are all
lower-case.
domainname: Contains the domainname if a domain has been used for field
definition else NULL. Note that even if a domain has been used, its base type is
recorded in field ctype.
cpos: Gives the position of the field in the table (first position is 1).
ckey: Contains the position of this column in the primary key of a table starting
with 1 or 0 if the column is not part of the primary key.
notnull: If the CREATE TABLE statement has specified a NOT NULL clause, this
field has the value "Y", otherwise "N".
Column Type
tsegno INTEGER
iname CHAR(*)
weight INTEGER
cpos INTEGER
isuniq CHAR(*)
isegno INTEGER
wlistsegno INTEGER
stowosegno INTEGER
charmapsegno INTEGER
delimsegno INTEGER
ftxttype CHAR(*)
wlisttype CHAR(*)
schema INTEGER
Column Type
func CHAR(*)
soundexsegno INTEGER
tsegno: Identifies the base table which the index refers to. To retrieve the
name of the table, perform a join between systable and sysindex on fields
tsegno,segno.
iname: Stores the name of the index. Index names are unique with respect to
the database.
weight: Serial number for the fields of one index, starting at 1 with the first
field specified in the CreateIndexStatement.
cpos: Identifies a field of the base table which the index refers to. To retrieve
the name of the field, perform a join between syscolumn and sysindex on the
fields (tsegno, cpos) in each table (see the example below).
isegno: The field isegno contains the physical segment number of the index.
Note that indexes are stored as B*-trees in physical segments.
wlistsegno: Contains for a fulltext index the segment number of the wordlist
table, else NULL.
wlisttype: Contains NULL value for a non-fulltext index. Contains value ''fix''
for a specified wordlist, ''var'' is the default.
The DDL statement
would produce the following two entries in sysindex (only some fields of sysindex are
shown):
To retrieve the names of the index fields in proper order, the following statement is
used:
vsegno: Contains (negative) integer value which uniquely identifies the view.
The same value is used e.g. in systable and syscolumn to refer to the view.
viewtext: Contains SQL SELECT statement which defines the view. This is is
used by Transbase whenever the view is used in a SQL statement.
checkopt: Contains ''Y'' if the view has been defined WITH CHECK OPTION else
''N''.
If a view is not updatable, only SELECT statements may be applied to the view.
If a view is updatable, UPDATE, INSERT, and DELETE statements may be
applied, too.
For the definition of updatability and for the semantics of updates on views see
the TB/SQL Reference Manual.
A view may be defined on base tables as well as on other previously defined views. If
a base table or view is dropped, all views depending on this base table or view are
dropped automatically. For this reason the dependency information is stored in
sysviewdep.
sysviewdep
basesegno INTEGER
vsegno INTEGER
basesegno: Contains the segment number of the base table or view on which
the view identified by vsegno depends. basesegno is positive or negative
depending on being a base table or view.
vsegno: Identifies the view which depends on the base table or view identified
by basesegno. vsegno always is negative.
A.9. The sysblob Table
Contains the information about all BLOB container segments. For each user base
table which has at least one column of type BLOB there is one record in the sysblob
table. All BLOB objects of the table are stored in the denoted BLOB container.
Column Type
rsno INTEGER
bcont INTEGER
rsno: Contains segment number of the base table containing BLOB field(s).
bcont: Contains segment number of the BLOB container of the base table.
systablepriv contains for each table all users who have privileges on this table.
Furthermore it contains who granted the privilege and what kind of privilege it is.
systablepriv
grantee INTEGER
tsegno INTEGER
grantor INTEGER
sel_priv CHAR(*)
systablepriv
del_priv CHAR(*)
ins_priv CHAR(*)
upd_priv CHAR(*)
Default: The owner of a table always has all privileges with GRANT OPTION on
the table, by default. Those privileges are recorded in systablepriv, too. Namely,
if user 34 creates a table identified by 73, the following entry in systablepriv is
made:
Column Type
grantee INTEGER
tsegno INTEGER
grantor INTEGER
cpos INTEGER
upd_priv CHAR(*)
Primary key: (grantee, tsegno, grantor, cpos)
grantee, tsegno, grantor, cpos: These four fields describe who (grantor) has
granted a privilege to whom (grantee) on which field (cpos) on which base table
or view (tsegno). The kind of privilege(s) is described in the other fields.
grantor and grantee refer to field userid of table sysuser. tsegno refers to field
segno of table systable.
upd_priv: Contains one of the strings ''Y'' or ''G''. ''Y'' means that grantee has
received from grantor the UPDATE privilege for the specified field on the
specified table or view. ''G'' includes ''Y'' and additionally the right to grant the
privilege to other users, too.
Note
If a user grantee has been granted the same update privilege
(''Y'' or ''G'') on all fields on tsegno from the same grantor, then
these privileges are recorded via a corresponding single record
in table systablepriv.
A.12. The sysdomain Table
Contains at least one record for each created DOMAIN of the database. Several
records for one domain are present if more than one check constraints are defined
on the domain.
Column Type
name CHAR(*)
owner INTEGER
basetype CHAR(*)
defaultvalue CHAR(*)
constraintname CHAR(*)
attributes CHAR(*)
constrainttext CHAR(*)
schema INTEGER
Column Type
segno INTEGER
constraintname CHAR(*)
attributes CHAR(*)
constrainttext CHAR(*)
cpos INTEGER
segno: Contains the segment of the table the constraint refers to.
attributes: Field attributes has the constant value IMMEDIATE (for more
general use in later versions).
constrainttext: stores the constraint text which is either of the form PRIMARY
KEY (...) or CHECK(...).
cpos: has value -1 except in the case that the constraint was initially defined
in a domain that was used for the type of a field in the table the constraint
refers to. In this particular case the constrainttext contains the keyword VALUE
instead of an explicit field reference and field cpos contains the position of the
field.
Note
Reference constraints (FOREIGN KEY ...) are stored in table
sysrefconstraint.
Field Type
constraintname CHAR(*)
attributes CHAR(*)
srcsegno INTEGER
srccpos INTEGER
tarsegno INTEGER
tarcpos INTEGER
delact CHAR(*)
updact CHAR(*)
attributes: Contains value IMMEDIATE (for more general use in later versions).
srcsegno, tarsegno: Contain the segment number of the referencing table and
the referenced table, resp.
Assume that sampletable and reftable have numbers 13 and 19, resp., and that
reftable has fields key1 and key2 on positions 7 and 8.
Field Type
name VARCHAR(*)
autoextend INTEGER
dspaceno INTEGER
maxsize INTEGER
online BOOL
defaultpath VARCHAR(*)
Field Type
available_pg INTEGER
size_pg INTEGER
online: Contains false if the dataspace has been set offline else true .
defaultpath: The directory where the datafiles of this dataspace are stored by
default.
Field Type
name VARCHAR(*)
dspaceno INTEGER
datafile VARCHAR(*)
Field Type
size INTEGER
lobcontainer BOOL
available INTEGER
datafile: Pathname of file attached to this dataspace. The filenames for user
defined dataspaces are system generated. They contain 2 numbers where the
first number is the dspaceno of the dataspace where the file belongs to, and the
second number is a running number over all existing files in the dataspace.
For each page which is in the diskcache there is one record in loadinfo.
Field Type
segment INTEGER
rompage INTEGER
Field Type
diskpage INTEGER
flag INTEGER
Primary key: (segment, rompage)
rompage: Contains the page number of the page in the ROM address space.
diskpage: Contains the page number of the page in the diskfile address space.
flag: Contains the value 1 if the page has just been cached via a LOAD
STATEMENT and the value 2 if the page has been changed with some INSERT or
UPDATE or DELETE operation.
Field Type
keyword STRING
Primary key: (keyword)
Appendix B. Sample Database
The database SAMPLE used in the exercises throughout this manual consists of three
tables SUPPLIERS, QUOTATIONS and INVENTORY that are described below.
Table B.1. Table SUPPLIERS
suppno name address
Table B.2. Table INVENTORY
207 GEAR 75
209 CAM 50
295 BELT 85
Table B.3. Table QUOTATIONS
51 221 .30 10 50
51 231 0.10 10 0
53 222 0.25 15 0
53 241 0.08 15 0
suppno partno price delivery_time qonorder
54 209 18.00 21 0
57 285 21.00 4 0
57 295 8.50 21 24
61 221 0.20 21 0
61 241 0.05 21 0
64 207 29.00 14 20
64 209 19.50 7 7
Appendix C. Precedence of
Operators
The Table below defines the precedence of operators. A precedence of 1 means
highest precedence.
Precedence Operators
1 SUBRANGE , SIZE OF
2 <timeselector> OF , CONSTRUCT
3 CAST
4 PRIOR , + , - (unary) , BITNOT
5 BITAND , BITOR
6 *, /
7 + , - (binary), || , < <= = <> >= >
8 LIKE , MATCHES , IN , BETWEEN , SUBSET , CONTAINS
9 NOT
10 AND
11 OR
12 SELECT
13 INTERSECT
14 UNION , DIFF