4D v13.4Principles for integrating 4D and the 4D SQL engine |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v13.4
Principles for integrating 4D and the 4D SQL engine
|
4D SQL | Description | 4D |
Varchar | Alphanumeric text | Text or Alpha |
Real | Floating point number in the range of +/-1.7E308 | Real |
Numeric | Number between +/- 2E64 | Integer 64 bits |
Float | Floating point number (virtually infinite) | Float |
Smallint | Number between -32 768 and 32 767 | Integer |
Int | Number between -2 147 483 648 and 2 147 483 647 | Longint, Integer |
Int64 | Number between +/- 2E64 | Integer 64 bits |
UUID | 16-byte number (128 bits) containing 32 hexadecimal characters | UUID Alpha format |
Bit | A field that can only take the values TRUE/FALSE or 1/0 | Boolean |
Boolean | A field that can only take the values TRUE/FALSE or 1/0 | Boolean |
Blob | Up to 2 GB; any binary object such as a graphic, another application, or any document | Blob |
Bit varying | Up to 2 GB; any binary object such as a graphic, another application, or any document | Blob |
Clob | Text up to 2 GB characters. This column (field) cannot be indexed. It is not saved in the record itself. | Text |
Text | Text up to 2 GB characters. This column (field) cannot be indexed. It is not saved in the record itself. | Text |
Timestamp | Date&Time, Date in 'YYYY/MM/DD' format and Time in 'HH:MM:SS:ZZ' format | Date and Time parts handled separately (automatic conversion) |
Duration | Time in 'HH:MM:SS:ZZ' format | Time |
Interval | Time in 'HH:MM:SS:ZZ' format | Time |
Picture | PICT picture up to 2 GB | Picture |
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:
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:
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 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.
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:
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.
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.
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_NAME | VARCHAR | Table name |
TEMPORARY | BOOLEAN | True if the table is temporary; otherwise, false |
TABLE_ID | INT64 | Table number |
SCHEMA_ID | INT32 | Number of schema |
_USER_COLUMNS | Describes the columns of the user tables of the database | |
TABLE_NAME | VARCHAR | Table name |
COLUMN_NAME | VARCHAR | Column name |
DATA_TYPE | INT32 | Column type |
DATA_LENGTH | INT32 | Column length |
NULLABLE | BOOLEAN | True if column accepts NULL values; otherwise, false |
TABLE_ID | INT64 | Table number |
COLUMN_ID | INT64 | Column number |
AUTOGENERATE | BOOLEAN | True if column value is generated automatically; otherwise, False |
AUTOINCREMENT | BOOLEAN | True if column value is incremented automatically; otherwise, False |
_USER_INDEXES | Describes the user indexes of the database | |
INDEX_ID | VARCHAR | Index number |
INDEX_NAME | VARCHAR | Index name |
INDEX_TYPE | INT32 | Index type (1=BTree / Composite, 3=Cluster / Keyword, 7=Auto) |
TABLE_NAME | VARCHAR | Name of table with index |
UNIQUENESS | BOOLEAN | True if index imposes a uniqueness constraint; otherwise, false |
TABLE_ID | INT64 | Number of table with index |
_USER_IND_COLUMNS | Describes the columns of user indexes of the database | |
INDEX_ID | VARCHAR | Index number |
INDEX_NAME | VARCHAR | Index name |
TABLE_NAME | VARCHAR | Name of table with index |
COLUMN_NAME | VARCHAR | Name of column with index |
COLUMN_POSITION | INT32 | Position of column in index |
TABLE_ID | INT64 | Number of table with index |
COLUMN_ID | INT64 | Column number |
_USER_CONSTRAINTS | Describes the integrity constraints of the database | |
CONSTRAINT_ID | VARCHAR | Constraint number |
CONSTRAINT_NAME | VARCHAR | Name associated with constraint definition |
CONSTRAINT_TYPE | VARCHAR | Type of constraint definition (P=primary key, R=referential integrity - foreign key, 4DR=4D relation) |
TABLE_NAME | VARCHAR | Name of table with constraint definition |
TABLE_ID | INT64 | Number of table with constraint |
DELETE_RULE | VARCHAR | Delete rule for a referential constraint – CASCADE or RESTRICT |
RELATED_TABLE_NAME | VARCHAR | Name of related table |
RELATED_TABLE_ID | INT64 | Number of related table |
_USER_CONS_COLUMNS | Describes the columns of user constraints of the database | |
CONSTRAINT_ID | VARCHAR | Constraint number |
CONSTRAINT_NAME | VARCHAR | Constraint name |
TABLE_NAME | VARCHAR | Name of table with constraint |
TABLE_ID | INT64 | Number of table withconstraint |
COLUMN_NAME | VARCHAR | Name of column with constraint |
COLUMN_ID | INT64 | Number of column with constraint |
COLUMN_POSITION | INT32 | Position of column with constraint |
RELATED_COLUMN_NAME | VARCHAR | Name of related column in a constraint |
RELATED_COLUMN_ID | INT32 | Number of related column in a constraint |
_USER_SCHEMAS | Describes the schemas of the database | |
SCHEMA_ID | INT32 | Schema number |
SCHEMA_NAME | VARCHAR | Name of schema |
READ_GROUP_ID | INT32 | Number of group having read-only access |
READ_GROUP_NAME | VARCHAR | Name of group having read-only access |
READ_WRITE_GROUP_ID | INT32 | Number of group having read-write access |
READ_WRITE_GROUP_NAME | VARCHAR | Name of group having read-write access |
ALL_GROUP_ID | INT32 | Number of group having full access |
ALL_GROUP_NAME | VARCHAR | Name 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.
Multi-database architecture is implemented at the level of the 4D SQL server. From within 4D it is possible:
In the SQL language, a primary key is used to identify the column(s) (field(s)) responsible for uniquely specifying the records (rows) in a table. Setting a primary key is more particularly necessary for the record replication function in a 4D table (see the section).
4D allows you to manage the primary key of a table in two ways:
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.
To create a primary key:
The fields included in the primary key then appear underlined in the editor and their SQL description displays the PRIMARY KEY keyword.
The field(s) belonging to the primary key must not contain any duplicated values. If any duplicated values already exist in the table records, a warning dialog box appears to indicate this.
Note: The column(s) belonging to the primary key do not accept NULL values.
To remove a primary key from a table:
Product: 4D
Theme: Using SQL in 4D