Chapter 9. SQL Reference
Abstract
SQL Reference.
Table of Contents
- 9.1. Datatypes
-
- 9.1.1. Date Literals
- 9.1.2. Casting
- 9.1.3. Time & Date Manipulation
- 9.1.4. Declaring Collations of Expressions
- 9.2. User Defined Types
-
- 9.2.1. CREATE TYPE Statement
- 9.2.2. ALTER TYPE Statement
- 9.2.3. DROP TYPE Statement
- 9.2.4. CREATE METHOD Statement
- 9.2.5. Type Instances
- 9.2.6. Instance References
- 9.2.7. NEW Operator
- 9.2.8. Finding Methods - Method Signatures Generation & Comparison
- 9.2.9. Getting & Setting Member Values of Type Instances (member observers & mutators)
- 9.2.10. Calling Static Methods
- 9.2.11. Calling Instance Methods
- 9.2.12. Serializing & Deserializing Type Instances
- 9.2.13. User Defined Types Utility Functions
- 9.2.14. Hosted Foreign Objects in Virtuoso
- 9.2.15. Using User Defined Types to Represent SOAP Structures
- 9.2.16. Consuming Third-Party SOAP Services via User Defined Types
- 9.2.17. UDT Security
- 9.3. XML Column Type
- 9.4. Identifier Case & Quoting
- 9.5. Wide Character Identifiers
- 9.6. Qualified Names
-
- 9.6.1. Qualifiers and Owners
- 9.6.2. Default Qualifiers
- 9.6.3. USE Statement, USE identifier
- 9.7. Literals, Brace Escapes
-
- 9.7.1. Strings
- 9.7.2. Numbers
- 9.7.3. ODBC Brace Escapes
- 9.7.4. Hexadecimal Literals
- 9.7.5. Binary Literals
- 9.8. CREATE TABLE Statement
-
- 9.8.1. Syntax
- 9.8.2. NOT NULL
- 9.8.3. IDENTITY (Auto Increment)
- 9.8.4. DEFAULT
- 9.8.5. PRIMARY KEY Constraint
- 9.8.6. UNDER
- 9.8.7. FOREIGN KEY Constraint
- 9.8.8. The CHECK Constraint
- 9.8.9. The WITH SCHEMA Constraint
- 9.9. DROP TABLE Statement
- 9.10. CREATE INDEX Statement
- 9.11. DROP INDEX Statement
- 9.12. ALTER TABLE Statement
-
- 9.12.1. Adding a CHECK Constraint
- 9.13. CREATE VIEW Statement
- 9.14. CREATE XML SCHEMA Statement
- 9.15. DROP XML SCHEMA Statement
- 9.16. Sequence Objects
- 9.17. INSERT Statement
-
- 9.17.1. INSERT SOFT
- 9.17.2. INSERT REPLACING
- 9.18. UPDATE Statement
- 9.19. SELECT Statement
-
- 9.19.1. Syntax
- 9.19.2. Description
- 9.19.3. Column Aliasing - AS Declaration
- 9.19.4. Join examples
- 9.19.5. Ordering and Grouping
- 9.19.6. Grouping Sets
- 9.19.7. Derived Tables
- 9.19.8. Query Expressions
- 9.19.9. LIKE Predicate & Search Patterns
- 9.19.10. The TOP SELECT Option
- 9.19.11. CASE, NULLIF, COALESCE, CAST Value Expressions
- 9.19.12. SELECT BREAKUP
- 9.20. COMMIT WORK, ROLLBACK WORK Statement
- 9.21. CHECKPOINT, SHUTDOWN Statement
-
- 9.21.1. Checkpoint & Page Remapping
- 9.22. Stored Procedures as Views & Derived Tables
-
- 9.22.1. Procedure Table Parameters
- 9.22.2. Procedure Table Result Sets
- 9.22.3. Procedure Tables & Security
- 9.22.4. Procedure Table Cost and Join Order
- 9.22.5. Limitations
- 9.22.6. Procedure Table Examples
- 9.23. GRANT, REVOKE Statement
- 9.24. SET Statement
-
- 9.24.1. ISOLATION
- 9.24.2. LOCK_ESCALATION_PCT
- 9.24.3. transaction_timeout
- 9.24.4. PARAM_BATCH
- 9.25. Anytime Queries
- 9.26. Best Effort Union
- 9.27. Standard and User-Defined Aggregate Functions
-
- 9.27.1. Create Aggregate Statement
- 9.27.2. Drop Aggregate Statement
- 9.27.3. Examples of User-Defined Aggregates
- 9.28. Virtuoso SQL Optimization
-
- 9.28.1. Optimization Techniques
- 9.28.2. Query Options
- 9.28.3. Query Optimization Diagnostics
- 9.28.4. ANY ORDER
- 9.28.5. VDB Statistics for the SQL Compiler Collection
- 9.29. SQL Inverse Functions
-
- 9.29.1. Updating through Inverses
- 9.30. SQL Grammar
- 9.31. Bitmap Indices
-
- 9.31.1. Bitmap Indices and Transactions
- 9.31.2. Performance Implications
- 9.31.3. Physical Structure and Overheads
- 9.32. Transitivity in SQL
- 9.33. Fast Phrase Match Processor
-
- 9.33.1. Phrases, Phrase Sets and Phrase Classes
- 9.33.2. Phrase Set Configuration API
- 9.33.3. Advertisers and Advertisement Rules
- 9.33.4. Example
- 9.34. Geometry Data Types and Spatial Index Support
-
- 9.34.1. Spatial References
- 9.34.2. Geometric Objects
- 9.34.3. Precision of Geometries
- 9.34.4. Predicates
- 9.34.5. Querying Geometric Relations
- 9.34.6. Defining a Geometry Index
- 9.34.7. Insert and Delete
- 9.34.8. Using Geometries in Client Applications and SQL Procedures
- 9.34.9. Virtuoso 7.1+ Geo Spatial Data type and function enhancements
- 9.35. SQL Bulk Load, ELT, File Tables and Zero Load Operations
-
- 9.35.1. File Tables
- 9.35.2. Parallel Insert With File Tables and Transactions
9.1. Datatypes
- CHARACTER
-
CHARACTER
VARCHAR
VARCHAR'('INTNUM')'
NVARCHAR
NVARCHAR'('INTNUM')'
CHAR'('INTNUM')'
- NUMERIC
-
NUMERIC
NUMERIC'('INTNUM')'
NUMERIC'('INTNUM','INTNUM')'
- DECIMAL
-
DECIMAL
DECIMAL'('INTNUM')'
DECIMAL'('INTNUM','INTNUM')'
- INTEGER
-
INT
INTEGER
SMALLINT
- FLOAT
-
FLOAT
FLOAT'('INTNUM')'
- REAL
-
REAL
DOUBLE PRECISION
- LONG VARCHAR
-
BLOB data
- VARBINARY [( precision )]
-
Binary data
- LONG VARBINARY
-
Binary BLOB data
- TIMESTAMP
-
TIMESTAMP
DATETIME
TIME
DATE
- <UDT>
-
User Defined Type with varbinary-like size properties.
- LONG <UDT>
-
User Defined Type with LONG varbinary-like size properties.
- LONG XML
-
LONG XML BLOB-like data type.
Note: | |
---|---|
User Defined Types can be created from native or external types, composites or classes from any hosted language such as Java or .Net. Any User Defined Type can be used to define a column in a CREATE TABLE statement. |
9.1.1. Date Literals
Virtuoso does not support date literals or the
DATE reserved keyword. Literal dates should be
enclosed in a conversion function such as
stringdate()
, as in this example:
select * from demo.demo.orders o where o.orderdate between stringdate('1994-01-01') And stringdate('1997-12-31')
Alternatively type casts can be used to explicitly instruct Virtuoso to assume a string as a date, see below.
9.1.2. Casting
Blob types can be cast to varchars. This will produce a string of up to 16 MB in length and an error for longer blobs.
Nothing can be cast to a blob type. Blobs only come from selecting blob columns and are created by assigning values to such columns.
Converting non-integer values to integers rounds towards 0.
Any data type can be converted to binary with the VARBINARY target data type. The result may or may not be meaningful. Specifically datetime can be converted to binary and back.
cast_expression : CAST '(' scalar_exp AS data_type [COLLATE collation_name ] ')' collation_name : identifier | owner '.' identifier | qualifier '.' owner '.' identifier
The CAST expression converts the type of the scalar_exp into the data_type, if possible. If the conversion is not meaningful, as from a float to a date, an error is signalled.
CAST is the recommended way of converting between data types, including any conversion between strings, different number types and datetime types.
Example 9.1. Examples:
select cast ('2000-1-3' as date); select cast (cast ('2000-1-3' as date) as varchar); = 2000-01-03 00-00-00 000000
9.1.3. Time & Date Manipulation
The SQL92 standard functions for time and date queries are available. These are mapped to Virtuoso internal functions as follows:
CURRENT_DATE
-
curdate()
|
CURRENT_TIME
-
curtime()
|
CURRENT_TIMESTAMP
-
curdatetime()
|
The results of the above functions can also be obtained using the
now()
function and
casting to the appropriate target type.
Dates and times should be input or compared as literals in a standard format. The following table describes the proper methods available:
Table 9.1. Time & date syntax
Datatype | ODBC Syntax | SQL92 Casting |
---|---|---|
Date | {d 'yyyy-mm-dd'} | cast('yyyy-mm-dd' as date) |
Time | {t 'hh:mm:ss'} | cast('hh:mm:ss' as time) |
Datetime/timestamp | {ts 'yyyy-mm-dd hh:mm:ss[.f...]'} | cast('yyyy-mm-dd hh:mm:ss[.f...]' as datetime) |
Example 9.2. Example: comparing dates
select datecol from table where datecol > cast('1900-01-01' as date) and datecol < cast(now() as date);
now() is cast for explicit compatibility although not required.
9.1.4. Declaring Collations of Expressions
A collation can be declared with CAST for string expressions. Suppose insensitive_ascii were a collation name specifying case insensitive comparison of ASCII strings, the predicate:
CAST 'foo' as varchar collate insensitive_ascii) = CAST 'FOO as varchar collate insensitive_ascii)
would be true.
This will alter the semantic of string comparison and can be used to define a collation where the collation would otherwise be default, for instance when comparing results of expressions or constants. This can also alter the default collation implied by the collation declaration in column options in CREATE TABLE.