4D v16

4D SQL engine implementation

Home

 
4D v16
4D SQL engine implementation

4D SQL engine implementation  


 

 

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.

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: 32767.
  • Maximum number of columns (fields) in a table: Theoretically two billion columns (fields), but for compatibility reasons with 4D: 32767.
  • Maximum number of rows (records) in a table: one billion.
  • Maximum number of index keys: 128 billions for alpha, text, and float indexes; 256 billlions for other index types (scalar data).
  • 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.

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

4D SQLDescription4D
VarcharAlphanumeric textText or Alpha
RealFloating point number in the range of +/-1.7E308Real
NumericNumber between +/- 2E64Integer 64 bits
FloatFloating point number (virtually infinite)Float
SmallintNumber between -32 768 and 32 767Integer
Int Number between -2 147 483 648 and 2 147 483 647Longint, Integer
Int64 Number between +/- 2E64Integer 64 bits
UUID16-byte number (128 bits) represented by 32 hexadecimal charactersUUID Alpha format
Bit A field that can only take the values TRUE/FALSE or 1/0Boolean
Boolean A field that can only take the values TRUE/FALSE or 1/0Boolean
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, Date in 'YYYY/MM/DD' format and Time in 'HH:MM:SS:ZZ' formatDate and Time parts handled separately (automatic conversion)
DurationTime in 'HH:MM:SS:ZZ' formatTime
IntervalTime in 'HH:MM:SS:ZZ' 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.

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.

For compatibility reasons in 4D, 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 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.

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 a 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.

The integrated SQL server of 4D supports date and time constants in accordance with the ODBC API. Here is the syntax for sequences of ODBC date and time constants:

{constant_type 'value'}

constant_typevalueDescription
dyyyy-mm-ddDate only
thh:mm:ss[.fff]Time only
tsyyyy-mm-dd hh:mm:ss[.fff]Date and time (timestamp)

Note: fff indicates milliseconds.

For example, you can use the following constants:

{ d '2013-10-02' }
{ t '13:33:41' }
{ ts '1998-05-02 01:23:56.123' }

The SQL date parser rejects any date expression specifying "0" as the day or month. Expressions such as {d'0000-00-00'} or CAST('0000-00-00' AS TIMESTAMP) generate an error. To perform SQL queries on blank dates (not to be confused with null dates), you must use an intermediate 4D expression. For example:

 C_LONGINT($count)
 $nullDate:=!00-00-00!
 Begin SQL
            SELECT COUNT(*) FROM Table_1
            WHERE myDate = :$nullDate
            INTO :$count;
 End SQL

The "Available via SQL" property, available for project methods, allows you to control the execution of 4D project methods via SQL.

When 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 Database Settings 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.

Hier gibt es zwei Optionen, um die Funktionsweise der 4D SQL Engine zu verändern.

  • Auto-commit Transactions: Mit dieser Option können Sie Auto commit in der 4D SQL Engine aktivieren. Dieser Modus sorgt für referentielle Datenintegrität. Ist die Option aktiv, werden alle Suchläufe mit SELECT, INSERT, UPDATE und DELETE (SIUD), die noch nicht innerhalb einer Transaktion ausgeführt werden, automatisch in eine ad-hoc Transaktion eingesetzt. So ist garantiert, dass die Suchen vollständig ausgeführt oder bei einem Fehler vollständig annulliert werden.
    Suchen, die bereits in einer Transaktion laufen, werden dabei nicht berücksichtigt (individuelle Verwaltung der Datenintegrität).
    Ist diese Option nicht markiert, werden keine automatischen Transaktionen erzeugt. Davon ausgenommen sind Suchläufe mit SELECT... FOR UPDATE (siehe SQL Befehl SELECT). Diese Option ist standardmäßig nicht markiert.
    Über den 4D Befehl SET DATABASE PARAMETER können Sie die Option auch per Programmierung verwalten.
    Hinweis
    : Auto commit gilt nur für lokale Datenbanken, die über die 4D SQL Engine durchsucht werden. Bei externen Datenbanken wird Auto commit über remote SQL Engines verwaltet
  • Groß-/Kleinschreibung bei Textvergleich beachten: Mit dieser Option können Sie die Berücksichtigung von Groß-/Kleinschreibung und Akzenten in SQL Anfragen verändern. Diese Option ist standardmäßig aktiviert, d.h. die SQL Engine unterscheidet beim Vergleichen von Strings zwischen Groß- und Kleinschreibung und Akzenten. Das gilt insbesondere für Sortieren und Suchen. Beispiel: “ABC”=“ABC” aber “ABC” # “Abc und abc # âbc.”
    In bestimmten Fällen, z.B. um die Funktionsweise der SQL Engine an die 4D Engine anzupassen, sollen beim Vergleichen von Strings die Groß- und Kleinschreibung und Akzente nicht berücksichtigt werden (“ABC”=“Abc”="âbc"). Dazu deaktivieren Sie einfach diese Option in den Datenbank-Eigenschaften.
    Über den 4D Befehl SET DATABASE PARAMETER können Sie die Option auch per Programmierung verwalten.

Schemas  

4D 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 palette 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.

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.
  • To open and use another 4D database instead of current database using the USE DATABASE command.

In the SQL language, a primary key is used to identify the table column(s) (field(s)) responsible for uniquely specifying the table records (rows). In particular, setting a primary key is required for the record replication function in a 4D table (see Replication via SQL) and for logging 4D tables as of v14.

4D allows you to manage primary keys in two ways:

  • Via the SQL language
  • Using the 4D Structure editor.

Note:

You can set a primary key when a table is created (via the CREATE TABLE command) or when adding or modifying a column (via the ALTER TABLE command). The primary key is specified using the PRIMARY KEY clause followed by the column name or a list of columns. For more information, refer to the section.

4D lets you create and remove primary keys directly via the context menu of the structure editor.

For more information about this point, refer to Primary keys in the 4D Design Reference manual.

The integrated SQL engine of 4D supports standard SQL views. A view is a virtual table with data that may come from several different database tables. Once a view is defined, you can use it in a SELECT statement just like a real table.

Data found in a view are defined using a definition query based on the SELECT command. Real tables used in the definition query are called "source tables". An SQL view contains columns and rows just like a standard table, but it does not actually exist; it is only a representation resulting from processing and stored in memory during the session. Only the definition of the view is actually saved temporarily.

Two SQL commands are used to manage views in 4D v14: CREATE VIEW and DROP VIEW.



Siehe auch 

sql_data_type_name

 
EIGENSCHAFTEN 

Produkt: 4D
Thema: Using SQL in 4D

 
GESCHICHTE 

 
ARTIKELVERWENDUNG

4D - SQL Reference ( 4D v16)