Principles for integrating 4D and the 4D SQL engine

4D - Documentation   Français   English   German   Japanese   4D SQL Reference, Table of Contents   4D SQL Reference, Index   Back   Previous   Next

version 11.3 (Modified)


Basically, the 4D SQL engine is SQL-92 compliant. This means that for a detailed description of commands, functions, operators or the syntax to be used, you may refer to any SQL-92 reference. These can be found, for instance, on the Internet.

However, the 4D SQL engine does not support 100% of the SQL-92 features and also provides some specific additional features.

This section covers the main implementations and limitations of the 4D SQL engine.

General Limitations

Since the SQL engine of 4D has been integrated into the heart of the 4D database, all the limitations concerning the maximum number of tables, columns (fields) and records per database, as well as the rules for naming tables and columns, are the same as for the standard internal 4D engine (DB4D). They are listed below.

Maximum number of tables: Theoretically two billion but for compatibility reasons with 4D v11: 32767.

Maximum number of columns (fields) in a table: Theoretically two billion columns (fields), but for compatibility reasons with 4D v11: 32767.

Note: In 4D v11 "Standard edition," the maximum number of tables and columns per table are limited to 255 and 511 respectively.

Maximum number of rows (records) in a table: one billion.

Maximum number of index keys: one billion x 64.

A primary key cannot be a NULL value and must be unique. It is not necessary to index the primary key columns (fields).

Maximum number of characters allowed for the table and field names: 31 characters (4D limitation).

Tables with the same name created by different users are not allowed. The standard 4D control mechanism will be applied.

Data Types

The following table indicates the data types supported in 4D SQL and their corresponding type in 4D:

4D SQLDescription4D v11
VarcharAlphanumeric textText
RealFloating point number in the range of +/-3,4E38Real
NumericNumber between +/- 2E64Integer 64 bits
FloatFloating point number (virtually infinite)Real
SmallintNumber between -32 768 and 32 767Integer
Int Number between -2 147 483 648 and 2 147 483 647Longint
Bit A field that can only take the values TRUE or FALSEBoolean
Boolean A field that can only take the values TRUE or FALSEBoolean
BlobUp to 2 GB; any binary object such as a graphic,
another application, or any documentBlob
Bit varyingUp to 2 GB; any binary object such as a graphic,
another application, or any documentBlob
ClobText up to 2 GB characters. This column (field) cannot be
indexed. It is not saved in the record itself.Text
TextText up to 2 GB characters. This column (field) cannot be
indexed. It is not saved in the record itself.Text
TimestampDate&Time in Day Month Year Hours:Minutes:Seconds:
Milliseconds for-matDate and Time parts
handled separately
(automatic conversion)
DurationTime in Day:Hours:Minutes:Seconds:Milliseconds formatTime
IntervalTime in Day:Hours:Minutes:Seconds:Milliseconds formatTime
PicturePICT picture up to 2 GBPicture

Automatic data type conversion is implemented between numeric types.

A string that represents a number is not converted to a corresponding number. There are special CAST functions that will convert values from one type to another.

The following SQL data types are not implemented:

NCHAR

NCHAR VARYING.

NULL Values in 4D

The NULL values are implemented in the 4D SQL language as well as in the 4D database engine. However, they are not supported in the 4D language. It is nevertheless possible to read and write NULL values in a 4D field using the Is field value Null and SET FIELD VALUE NULL commands.

Compatibility of Processing and Map NULL Values to Blank Values Option

For compatibility reasons in 4D v11, NULL values stored in 4D database tables are automatically converted into default values when being manipulated via the 4D language. For example, in the case of the following statement:

myAlphavar:=[mytable]MyAlphafield

... if the MyAlphafield field contains a NULL value, the myAlphavar variable will contain "" (empty string).

The default values depend on the data type:

For Alpha and Text data types: ""

For Real, Integer and Long Integer data types: 0

For the Date data type: "00/00/00"

For the Time data type: "00:00:00"

For the Boolean data type: False

For the Picture data type: Empty picture

For the Blob data type: Empty blob

On the other hand, this mechanism in principle does not apply to processing carried out at the level of the 4D database engine, such as queries. In fact, searching for an "blank" value (for example myvalue=0) will not find records storing the NULL value, and vice versa. When both types of values (default values and NULL) are present in the records for the same field, some processing may be altered or require additional code.

To avoid these inconveniences, an option can be used to standardize all the processing in the 4D v11 language: Map NULL values to blank values. This option, which is found in the field Inspector window of the Structure editor, is used to extend the principle of using default values to all processing. Fields containing NULL values will be systematically considered as containing default values. This option is checked by default.

The Map NULL values to blank values property is taken into account at a very low level of the database engine. It acts more particularly on the Is field value Null command.

Reject NULL Value Input Attribute

The Reject NULL value input field property is used to prevent the storage of NULL values:

When this attribute is checked for a field, it will not be possible to store the NULL value in this field. This low-level property corresponds exactly to the NOT NULL attribute of SQL.

Generally, if you want to be able to use NULL values in your 4D database, it is recommended to exclusively use the SQL language of 4D.

Note: In 4D, fields can also have the "Mandatory" attribute. The two concepts are similar but their scope is different: the "Mandatory" attribute is a data entry control, whereas the "Reject NULL value input" attribute works at the level of the database engine.

If a field having this attribute receives a NULL value, an error will be generated.

"Available through SQL" Option

A security property has been added for 4D project methods: Available through SQL:

When it is checked, this option allows the execution of the project method by the 4D SQL engine. It is not selected by default, which means that 4D project methods are protected and cannot be called by the 4D SQL engine unless they have been explicitly authorized by checking this option.

This property applies to all SQL queries, both internal and external — whether executed via the ODBC driver, or via SQL code inserted between the Begin SQL/End SQL tags, or via the QUERY BY SQL command.

Notes:

Even when a method is given the "Available through SQL" attribute, the access rights set at the Preferences level and at the level of the method properties are nevertheless taken into account when it is executed.

The ODBC SQLProcedure function only returns project methods having the "Available through SQL" attribute.

SQL Engine Options

Two options found on the SQL/Configuration page of the 4D Preferences can be used to modify the functioning of the 4D SQL engine.

Auto-commit Transactions: This option can be used to activate the auto-commit mechanism of the SQL engine. The purpose of the auto-commit mode is to preserve the referential integrity of the data. When this option is checked, any SELECT, INSERT, UPDATE and DELETE (SIUD) queries not already carried out within a transaction are automatically included in an ad hoc transaction. This guarantees that the queries will be executed in their entirety or, in the case of an error, completely cancelled.

Queries already included in a transaction (custom management of referential integrity) are not affected by this option.

When this option is not checked, no automatic transaction is generated (except for the SELECT... FOR UPDATE queries, please refer to the SELECT command). By default, this option is not checked.

You can also manage this option by programming using the SET DATABASE PARAMETER command.

Note: Only local databases queried by the 4D SQL engine are affected by this parameter. In the case of external databases, the auto-commit mechanism is handled by the remote SQL engines.

Case-sensitive String Comparison: This option can be used to modify the case sensitivity of characters in SQL queries. It is checked by default, which means that the SQL engine differentiates between upper and lower case letters when comparing strings (sorts and queries). For example "ABC"="ABC" but "ABC" # "Abc."

In certain cases, for example to align the functioning of the SQL engine with that of the 4D engine, you may want string comparisons not to be case sensitive ("ABC"="Abc"). To do this, you simply need to deselect this option.

You can also manage this option by programming using the SET DATABASE PARAMETER command.

Schemas

4D v11 SQL implements the concept of schemas. A schema is a virtual object containing the tables of the database. In SQL, the purpose of schemas is to assign specific access rights to different sets of database objects. Schemas divide the database into independent entities which together make up the entire database. In other words, a table always belongs to one and only one schema.

To create a schema, you must use the CREATE SCHEMA command. You can then use the GRANT and REVOKE commands to configure the types of access to the schemas.

To associate a table with a schema, you can call the CREATE TABLE or ALTER TABLE commands. You can also use the "Schemas" pop-up menu of the Inspector in the Structure editor of 4D. This menu lists all the schemas defined in the database:

The DROP SCHEMA command can be used to delete a schema.

Note: The control of access via schemas only applies to connections from the outside. The SQL code executed within 4D via Begin SQL/End SQL tags, SQL EXECUTE, QUERY BY SQL, and so on, always has full access.

System Tables

The SQL catalogue of 4D includes seven system tables, which can be accessed by any SQL user having read access rights: _USER_TABLES, _USER_COLUMNS, _USER_INDEXES, _USER_CONSTRAINTS, _USER_IND_COLUMNS, _USER _CONS_ COLUMNS and _USER_SCHEMAS.

In accordance with the customs of SQL, system tables describe the database structure. Here is a description of these tables and their fields:

_USER_TABLES Describes the user tables of the database
TABLE_NAMEVARCHARTable name
TEMPORARYBOOLEANTrue if the table is temporary; otherwise, false
TABLE_IDINT64Table number
SCHEMA_IDINT32Number of schema

_USER_COLUMNSDescribes the columns of the user tables of the database
TABLE_NAMEVARCHARTable name
COLUMN_NAMEVARCHARColumn name
DATA_TYPEINT32Column type
DATA_LENGTHINT32Column length
NULLABLEBOOLEANTrue if column accepts NULL values; otherwise, false
TABLE_IDINT64Table number
COLUMN_IDINT64Column number

_USER_INDEXESDescribes the user indexes of the database
INDEX_IDVARCHARIndex number
INDEX_NAMEVARCHARIndex name
INDEX_TYPEINT32Index type
TABLE_NAMEVARCHARName of table with index
UNIQUENESSBOOLEANTrue if index imposes a uniqueness constraint; otherwise, false
TABLE_IDINT64Number of table with index

_USER_IND_COLUMNSDescribes the columns of user indexes of the database
INDEX_IDVARCHARIndex number
INDEX_NAMEVARCHARIndex name
TABLE_NAMEVARCHARName of table with index
COLUMN_NAMEVARCHARName of column with index
COLUMN_POSITIONINT32Position of column in index
TABLE_IDINT64Number of table with index
COLUMN_IDINT64Column number

_USER_CONSTRAINTSDescribes the user constraints of the database
CONSTRAINT_IDVARCHARConstraint number
CONSTRAINT_NAMEVARCHARConstraint name
CONSTRAINT_TYPEVARCHARConstraint type
TABLE_NAMEVARCHARName of table with constraint
TABLE_IDINT64Number of table with constraint
DELETE_RULEVARCHARDelete rule – CASCADE or RESTRICT
RELATED_TABLE_NAMEVARCHARName of related table
RELATED_TABLE_IDINT64Number of related table

_USER_CONS_COLUMNSDescribes the columns of user constraints of the database
CONSTRAINT_IDVARCHARConstraint number
CONSTRAINT_NAMEVARCHARConstraint name
TABLE_NAMEVARCHARName of table with constraint
TABLE_IDINT64Number of table withconstraint
COLUMN_NAMEVARCHARName of column with constraint
COLUMN_IDINT64Number of column with constraint
COLUMN_POSITIONINT32Position of column with constraint
RELATED_COLUMN_NAMEVARCHARName of related column in a constraint
RELATED_COLUMN_IDINT32Number of related column in a constraint

_USER_SCHEMASDescribes the schemas of the database
SCHEMA_IDINT32Schema number
SCHEMA_NAMEVARCHARName of schema
READ_GROUP_IDINT32Number of group having read-only access
READ_GROUP_NAMEVARCHARName of group having read-only access
READ_WRITE_GROUP_IDINT32Number of group having read-write access
READ_WRITE_GROUP_NAMEVARCHARName of group having read-write access
ALL_GROUP_IDINT32Number of group having full access
ALL_GROUP_NAMEVARCHARName of group having full access

Note: The system tables are assigned to a specific schema named SYSTEM_SCHEMA. This schema cannot be modified or deleted. It does not appear in the list of schemas displayed in the table Inspector palette. It can be accessed in read-only by any user.

Connections to SQL sources

Multi-database architecture is implemented at the level of the 4D SQL server. From within 4D it is possible:

To connect to an existing database using the SQL LOGIN command.

To switch from one database to another using the 4D SQL LOGIN and SQL LOGOUT commands.


4D - Documentation   Français   English   German   Japanese   4D SQL Reference, Table of Contents   4D SQL Reference, Index   Back   Previous   Next