Character Types
Fixed length character strings.
Data Type | Size | Size (Not Null) | Synonyms |
---|---|---|---|
CHAR [(length_num)] | (length_num * 4) + 2 | (length_num * 4) + 1 | VARCHAR, CHARACTER |
CHAR (length_num OCTETS) | length_num + 2 | length_num + 1 | VARCHAR, CHARACTER |
The default character set for the CHAR data type is UTF-8. This means that for every character, 4 bytes will be required for the 4-byte character encoding. If it is determined that a column will ONLY contain ASCII characters, the OCTETS modifier can be used to change the allocation 1 byte per character. The CHAR data type will always be allocated with space for the null string terminator.
The CHAR type and VARCHAR types are synonymous.
Trailing spaces in a CHAR/VARCHAR column are truncated.
The length is the number of characters and NOT the number of bytes.
Example Usage
Schema 1 example:
Refer to the NULL Values section for more information about the generated *_has_value
elements.
CREATE TABLE t1 ( id ROWID PRIMARY KEY, name CHAR(50) );
Structure generated by rdm-compile
utility with the "--c-structs
" command line option.
/** \brief struct definition for table T1 */ typedef struct T1_S { RDM_ROWID_T ID; char NAME[201]; RDM_HAS_VALUE_T _NAME_has_value; } T1;
Schema 2 example:
The following is the same schema as Schema 1 above but shows the effect of using the OCTETS modifier on the length. The structure generated reserves 51 bytes for the name
element rather than 201 per the schema above. Refer to the NULL Values section for more information about the generated *_has_value
elements.
CREATE TABLE t1 ( id ROWID PRIMARY KEY, name CHAR(OCTETS 50) );
Structure generated by rdm-compile
utility with the "--c-structs
" command line option.
/** \brief struct definition for table T1 */ typedef struct T1_S { RDM_ROWID_T ID; char NAME[51]; RDM_HAS_VALUE_T _NAME_has_value; } T1;
Schema 3 example:
The following is the same schema as Schema 2 above but shows the effect of using the NOT NULL modifier on the column definition. The resulting structure has the _NAME_has_value
element removed. Refer to the NULL Values section for more information about the generated *_has_value
elements.
CREATE TABLE t1 ( id ROWID PRIMARY KEY, name CHAR(OCTETS 50) NOT NULL );
Structure generated by rdm-compile
utility with the "--c-structs
" command line option.
Consider using LONGVARCHAR (CLOB) type for very large text storage (more than 1000 characters). For more information, refer to BLOB/CLOB Types section.
See Also