对于Oracle数据库而言,CHAR类型最大值是2000而VARCHAR2是4000,虽然达梦数据库绝大部分情况下,都比这个范围要大,不过个人并不认为这是一个很好的设计。不同数据库之前的迁移对于一个成熟系统而言是很常见的,而不同数据库的页面大小不同也是很平常的事情,达梦的这种设计虽然使得不同页面的最大值尽量扩大,但是却给不同页面间数据库的迁移埋下了问题。当数据库向页面更大的数据库迁移不会存在问题,但是反之可能造成表无法创建、数据丢失或数据被截断的情况。
而且感觉达梦数据库在实现VARCHAR类型的时候还有一定的问题:
SQL>insert into t_type values (1, 'a ');
insert into t_type values (1, 'a ')
1 rows affected
time used: 0.464(ms) clock tick:765250.
SQL>select id, name || 'a' from t_type;
select id, name || 'a' from t_type;
id
1 1 a a
1 rows got
insert into t_type values (1, 'a ')
1 rows affected
time used: 0.464(ms) clock tick:765250.
SQL>select id, name || 'a' from t_type;
select id, name || 'a' from t_type;
id
1 1 a a
1 rows got
至此没有问题,VARCHAR类型中的空格被保留,但是对VARCHAR类型进行查询的时候:
SQL>select * from t_type where name = 'a ';
select * from t_type where name = 'a ';
id name
1 1 a
1 rows got
time used: 29.588(ms) clock tick:49459510.
SQL>select * from t_type where name = 'a';
select * from t_type where name = 'a';
id name
1 1 a
1 rows got
time used: 0.314(ms) clock tick:517060.
SQL>select * from t_type where name = 'a ';
select * from t_type where name = 'a ';
id name
1 1 a
1 rows got
time used: 0.421(ms) clock tick:694830.
select * from t_type where name = 'a ';
id name
1 1 a
1 rows got
time used: 29.588(ms) clock tick:49459510.
SQL>select * from t_type where name = 'a';
select * from t_type where name = 'a';
id name
1 1 a
1 rows got
time used: 0.314(ms) clock tick:517060.
SQL>select * from t_type where name = 'a ';
select * from t_type where name = 'a ';
id name
1 1 a
1 rows got
time used: 0.421(ms) clock tick:694830.
显然在比较的时候,空格是没有被考虑在内的,这对于CHAR类型是没有问题的,但是VARCHAR类型似乎并不应该具有这样的特性:
SQL>insert into t_type values (2 ,'a');
insert into t_type values (2 ,'a')
1 rows affected
time used: 0.401(ms) clock tick:661320.
SQL>select * from t_type
2 where name = 'a';
select * from t_type
where name = 'a';
id name
1 1 a
2 2 a
2 rows got
time used: 0.397(ms) clock tick:653730.
SQL>alter table t_type add primary key (name);
alter table t_type add primary key (name);
违反唯一性约束.error code = -3100
insert into t_type values (2 ,'a')
1 rows affected
time used: 0.401(ms) clock tick:661320.
SQL>select * from t_type
2 where name = 'a';
select * from t_type
where name = 'a';
id name
1 1 a
2 2 a
2 rows got
time used: 0.397(ms) clock tick:653730.
SQL>alter table t_type add primary key (name);
alter table t_type add primary key (name);
违反唯一性约束.error code = -3100
显然在达梦中即使VARCHAR类型的字段也认为’a’和’a_’(这里用下划线来标识空格)是相等的,这种情况会引发歧义,我们认为已经相等的数据在经过相同的处理后就会变得又不相等了:
SQL>select id, name || 'a' from t_type;
select id, name || 'a' from t_type;
id
1 1 a a
2 2 aa
2 rows got
time used: 36.391(ms) clock tick:60834180.
select id, name || 'a' from t_type;
id
1 1 a a
2 2 aa
2 rows got
time used: 36.391(ms) clock tick:60834180.