简体   繁体   English

当长文本/备注字符串是主键字段或“已索引:是(允许重复)”时,将长文本/备注字符串截断为255个字符

[英]Truncates Long Text/Memo string to 255 characters when it is a primary key field or “Indexed: Yes (no-duplicates) allowed”

I created a table in MS Access 2013 with only one column of "Long Text" type (called as Memo earlier) and made it the primary key of the table. 我在MS Access 2013中创建了一个表,其中只有一列“长文本”类型(之前称为Memo),并使其成为该表的主键。 I stored a long string of 255+ characters and then I tried to store another string whose first 255 characters were same as previous stored string but all other characters after first 255 were different and MS Access gave "duplicate data" error. 我存储了一个由255个以上的字符组成的长字符串,然后尝试存储另一个字符串,其前255个字符与先前存储的字符串相同,但是前255个字符之后的所有其他字符均不同,并且MS Access提供了“重复数据”错误。 In the new string I changed the characters that were after 255th position, using different combinations of characters and all gave error. 在新的字符串中,我使用不同的字符组合更改了位于第255位之后的字符,所有字符均出错。 But when I change any character before the 255th position it does not give any error. 但是,当我在第255位之前更改任何字符时,不会出现任何错误。 So, I concluded that MS Access checks only the first 255 characters of "Long Text" data type for checking duplicates in that column. 因此,我得出结论,MS Access仅检查“长文本”数据类型的前255个字符,以检查该列中的重复项。 Is it so? 是这样吗? What else could be reason? 还有什么可能是原因?

String Stored of 256 characters: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelectr 存储的256个字符的字符串:LoremIpsumiss隐含印刷和类型设置行业的伪文本LoremIpsum自1500年代以来一直是工业标准的伪文本,当不明的印刷字体版本和加扰类型变为标本样本时,仍然可以存活五个世纪

String Gave Error: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelect1 字符串给定错误:LoremIpsum暗示了印刷和类型设置行业的伪文本LoremIpsum自1500年代起就一直是行业标准的伪文本,当时未知的ertert agalley的类型和内容都乱七八糟,使它成为一种类型的样本书,只有五个世纪才得以生存,但是

String Gave Error: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelect2 字符串给定错误:LoremIpsum暗示了印刷和类型设置行业的伪文本LoremIpsum自1500年代起就一直是行业标准的伪文本,当时未知的ertert agalley类型和加扰使它成为一种类型的样本书Ithassurvived不仅五个世纪而且还

String Gave Error: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelect123 字符串给定错误:LoremIpsum暗示了印刷和类型设置行业的虚拟文本,LoremIpsum自1500年代起就一直是行业标准的虚拟文本,当时一种未知的印刷品种类繁多,并且乱七八糟地使它成为一种类型的标本,Isas幸存下来只有五个世纪,但是

Does Not Give Error: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelec1 不给出错误:LoremIpsum暗示着印刷和版式设置行业的虚拟文本LoremIpsum自1500年代起就一直是行业标准的虚拟文本,当一种未知的印刷字体版本和拼写成一种类型的样本书时,它只能存活五个世纪,而且还保留了一个世纪。

Does Not Give Error: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelec2 不给出错误:LoremIpsum暗示着印刷和版式设置行业的虚拟文本LoremIpsum自1500年代起就一直是行业标准的虚拟文本,当不明类型的印刷品和杂乱无章地拼写成一种标本样本Ithas存活下来时,也只有五个世纪了

Does Not Give Error: LoremIpsumissimplydummytextoftheprintingandtypesettingindustryLoremIpsumhasbeentheindustrysstandarddummytexteversincethe1500swhenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbookIthassurvivednotonlyfivecenturiesbutalsotheleapintoelec3 不给出错误:LoremIpsum暗示着印刷和类型设置行业的虚拟文本LoremIpsum自1500年代起就一直是行业标准的虚拟文本,当不明类型的印刷品库和类型样本乱七八糟时,它只能存活五个世纪,而且还保留了三个世纪。

Please notice the difference in the last few characters of above samples. 请注意上述示例的最后几个字符的区别。 The first stored string has 256 characters. 第一个存储的字符串包含256个字符。 Even if the column is not the primary key, the problem remains same if "Indexed: Yes (no-duplicates) allowed" value is set true in the table design for that column. 即使该列不是主键,如果在该列的表设计中将“索引:是(允许(无重复)允许”)值设置为true,问题仍然存在。

As @HansUp stated in the comments, Access (specifically the Jet/ACE db engine) only uses the first 255 characters of a Memo/Long Text field to create its index. 正如@HansUp在注释中所述,Access(特别是Jet / ACE数据库引擎)仅使用“备注/长文本”字段的前255个字符来创建其索引。 Hence, it only uses the first 255 characters to enforce No Duplicates. 因此,它仅使用前255个字符来强制执行“无重复”。

@HansUp's advice to use a different db engine that provides better support for long strings and Full Text search is probably the best approach, but I understand there are often other considerations that may be limiting you to solving your problem in Access. @HansUp建议使用不同的数据库引擎为长字符串提供更好的支持,而全文本搜索可能是最好的方法,但是我知道通常还有其他一些考虑因素可能会限制您解决Access中的问题。

As such, here is an Access-only approach to solving your problem. 因此,这是解决问题的“仅访问”方法。 This assumes the requirement you listed in the comments is valid; 假设您在注释中列出的要求是有效的; ie, you need to store unique strings of between 400 and 1000 characters. 即,您需要存储400到1000个字符的唯一字符串。


Alternative 1 选择1

  1. Keep your initial Memo/Long Text field: Notes 保留您的初始备注/长文本字段:注意
  2. Create four text fields (not Memo/Long Text) of 250 characters max: Notes1, Notes2, Notes3, Notes4 创建四个最多250个字符的文本字段(非备注/长文本):Notes1,Notes2,Notes3,Notes4
  3. Set all four text fields: Required -> True and Allow Zero Length -> True (this is required to ensure the unique index is enforced for strings less than 751 characters) 设置所有四个文本字段:必需-> True和允许零长度-> True(这是确保对少于751个字符的字符串强制执行唯一索引所必需的)
  4. Create a unique index and add all four text fields to that index 创建一个唯一索引并将所有四个文本字段添加到该索引
  5. Don't ignore nulls in your index 不要忽略索引中的空值
  6. When you store the values, you will need to store them in the Notes field and also split the string among the four smaller NotesX fields 存储值时,需要将它们存储在Notes字段中,并将字符串拆分为四个较小的NotesX字段

Alternative 2: 选择2:

Keep your current setup and enforce the uniqueness at code level. 保持当前设置,并在代码级别实施唯一性。 Every time you update or insert a note, do a search on all notes that match the first 255 characters, read the value and perform the comparison in code. 每次更新或插入注释时,都对与前255个字符匹配的所有注释进行搜索,读取值并执行代码比较。


Alternative 3 ( thanks to @HansUp for suggesting this in the comments ): 备选方案3( 感谢@HansUp在评论中建议 ):

  1. Keep your initial Memo/Long Text field: Notes 保留您的初始备注/长文本字段:注意
  2. Create a 16 or 32 character text field to store the 256 bit or 512 bit hash of your long text: NotesHash 创建一个16或32个字符的文本字段,以存储长文本的256位或512位哈希:
  3. Add a unique index to your NotesHash field 向您的NotesHash字段添加唯一索引
  4. Every time the memo field is changed, re-compute the hash value and attempt to store it in the table 每次更改备注字段时,请重新计算哈希值,然后尝试将其存储在表中

Notes for this method : 此方法的注意事项

  • As the pigeonhole principle easily proves, there is the possibility that two different strings will generate the same hash (a collision). 正如信鸽原理很容易证明的那样,两个不同的字符串可能会生成相同的哈希(冲突)。 However, using a good hashing algorithm will make the actual probability approach zero. 但是,使用良好的哈希算法会使实际概率接近零。
  • This site offers some VB6/VBA/VBScript implementations of various hashing algorithms. 站点提供了各种哈希算法的一些VB6 / VBA / VBScript实现。 I can't vouch for their correctness, but they passed the eye test for me. 我不能保证它们的正确性,但是他们为我通过了视力测试。 Use at your own risk, but it's at least a good starting point. 使用需要您自担风险,但这至少是一个好的起点。
  • Really, you can use any deterministic function that returns a string of 255 characters or fewer given an arbitrarily large input. 实际上,您可以使用任何确定性函数 ,只要输入任意大的 ,该函数将返回255个字符或更少的字符串。 The difference between a crappy hash algorithm and a good one is how well it minimizes collisions. 糟糕的哈希算法与好的哈希算法之间的区别在于,它最大程度地减少了冲突。 For that reason, I would suggest you use one based on a popular standard. 因此,我建议您使用基于流行标准的产品。

And yes, I still highly recommend @HansUp's solution to simply use a different db engine. 是的,我仍然强烈建议@HansUp的解决方案仅使用其他数据库引擎。

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

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