简体   繁体   English

db2 varchar关键解决方法

[英]db2 varchar key workarounds

I'm beginning to understand the benefits of not using varchar for keying, as explained here 我开始不明白VARCHAR使用的密钥的好处,说明在这里

I am working with a DB2 database which I didn't setup which has a large table (20M+ rows) with a varchar key field. 我正在使用未设置的DB2数据库,该数据库具有带varchar键字段的大表(20M +行)。 The table structure is as so: 表结构如下:

key_field | matching_field1  
000-XXX | 123  
0000-XXX | 456  
00-XXXXX | 789  

The key_field is of variable length. key_field的长度可变。 I'm trying to use the key_field to link to a smaller table with 4k rows. 我正在尝试使用key_field链接到具有4k行的较小表。 The problem is that there is trailing space after the characters in the key_field . 问题在于key_field的字符后面有尾随空格。 I know this because if I search for a specific string using 我知道这是因为如果我使用以下命令搜索特定的字符串

SELECT * WHERE key_field LIKE '000-XXX' there is no match, but if I try '000-XXX%' then the row is selected. SELECT * WHERE key_field LIKE '000-XXX' ,但没有匹配项,但是如果我尝试使用'000-XXX%' ,则会选择该行。 I assume this is the same reason I cannot key to my second table which contains a corresponding key_field but which has been trimmed so there are no trailing whitespaces. 我以为是因为相同的原因,我无法键入包含相应key_field第二张表,但已对其进行了修剪,因此没有尾随空格。

Assuming I cannot edit, duplicate or transpose this large table (at least not en masse due to resource limitations), is there a way to key it to the smaller table (4k rows) containing the same key_field ? 假设我无法编辑,复制或转置这个大表(由于资源限制,至少不能key_field ),有没有办法将其键到包含相同key_field的较小表(4k行)中?

I can edit and manipulate the smaller table. 我可以编辑和操作较小的表。

Update: running the query below using RTRIM , I'm getting an error. 更新:使用RTRIM在下面运行查询,出现错误。

Query: 查询:

SELECT RTRIM(foreign_key)  
FROM Small_Table  
EXCEPT SELECT RTRIM(key_field)  
FROM Big_Table

The error: 错误:

NUMBER OF ROWS DISPLAYED IS 0
SQLCODE = -904, ERROR:  UNSUCCESSFUL EXECUTION CAUSED BY AN
UNAVAILABLE RESOURCE. REASON 00C90084, TYPE OF RESOURCE 00000100, AND
RESOURCE NAME DB2-MANAGED SPACE WITHOUT SECONDARY ALLOCATION OR
USER-MANAGED SPACE IN MYDB_NAME

use this commands for join your tables 使用此命令联接表

if you have only spaces to the end of string 如果字符串末尾只有空格

rtrim(key_field)=rtrim(keyothertable)

if you have only spaces to the start of string 如果字符串的开头只有空格

ltrim(key_field)=ltrim(keyothertable)

if you have spaces to the start or/and end of string (solution 1) 如果您在字符串的开头或结尾处有空格(解决方案1)

trim(key_field)=trim(keyothertable)

if you have spaces to the start or/and end of string (solution 21) 如果字符串的开头或/和结尾有空格(解决方案21)

strip(key_field)=strip(keyothertable)

The drawback when you use "trim/rtrim/ltrim/strip" functions on your keys, is that it is possible that the indexes are not used (to check) 当您在键上使用“ trim / rtrim / ltrim / strip”功能时,缺点是可能不使用索引(以进行检查)

May be can you trasnform your columns key to varchar if indexes are not used... 如果不使用索引,您可以将列关键字转换为varchar吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM