mysql – 使用带有UTF8的VARCHAR的SQL表(关于多字节字符长度)

就像在Oracle VARCHAR(60 CHAR)中一样,我想指定一个varchar字段,其长度可变,具体取决于插入的字符.

例如:

create table X (text varchar(3))
insert into X (text) VALUES ('äöü')

应该是可能的(使用UTF8作为数据库的默认字符集).

在DB2上我得到了这个错误:DB2 SQL错误:SQLCODE = -302,SQLSTATE = 22001
 (字符数据,右截断发生;例如,更新或插入值是对于列太长的字符串,或者日期时间值无法分配给主变量,因为它太小.)

我正在寻找DB2,MsSql,MySql,Hypersonic的解决方案.

解决方法:

DB2

DB2 documentation说:

In multibyte UTF-8 encoding, each ASCII character is one byte, but non-ASCII characters take two to four bytes each. This should be taken into account when defining CHAR fields. Depending on the ratio of ASCII to non-ASCII characters, a CHAR field of size n bytes can contain anywhere from n/4 to n characters.

这意味着使用DB2数据库,您无法执行所要求的操作.

MySQL的

MySql documentation说:

UTF-8 (Unicode Transformation Format with 8-bit units) is an alternative way to store Unicode data. It is implemented according to RFC 3629, which describes encoding sequences that take from one to four bytes. Currently, MySQL support for UTF-8 does not include four-byte sequences. (An older standard for UTF-8 encoding, RFC 2279, describes UTF-8 sequences that take from one to six bytes. RFC 3629 renders RFC 2279 obsolete; for this reason, sequences with five and six bytes are no longer used.)

这意味着使用MySql数据库,您可以使用VARCHAR(3)CHARACTER SET utf8作为列定义来获取您要求的内容.

上一篇:java – 如何确定列大小是以字节为单位还是列元数据中的字符?


下一篇:DB2同平台迁移数据库