开发者

Are unique indices on Access text fields always case insensitive?

I created an MS Access table using the following code:

  tbl := Database.CreateTableDef('English', 0, '', '');
  try
    fld := tbl.CreateField('ID', dbLong, 0);
    fld.Attributes := dbAutoIncrField + dbFixedField;
    tbl.Fields.Append(fld);

    fld := tbl.CreateField('Content', dbText, 255);
    fld.Required := true;
    fld.AllowZeroLength := false;
    tbl.Fields.Append(fld);

    Database.TableDefs.Append(tbl);

    idx := tbl.CreateIndex('PrimaryKey');
    idx.Fields.Append(idx.CreateField('ID', EmptyParam, EmptyParam));
    idx.Primary := True;
    idx.Unique := true;
    tbl.Indexes.Append(idx);

    idx := tbl.开发者_如何学编程CreateIndex('IX_Content');
    idx.Fields.Append(idx.CreateField('Content', EmptyParam, EmptyParam));
    idx.Primary := false;
    idx.Unique := true;
    tbl.Indexes.Append(idx);
  finally
    tbl := nil;
  end;

This works fine until I try to insert the two strings 'Field type' and 'Field Type' into this table. I get an error telling me that the unique index restricts me from doing that. As you can see they only differ in the case of the second word. Since I did not explicitly make the index case insensitive (I wouldn't even know how to do that), I don't quite understand why this happens. Are indices on text fields always case insensitive in MS Access? If not, what am I doing wrong?


Access Jet databases are fundamentally case insensitive. That is your problem. As far as I know there is no way to make an Access index case sensitive.


Use Binary field

Case insensivity problem in Microsoft Access was long time ago addressed in article KB244693 published by Microsoft and still can be found in Web archive.

Basically, the solution is to add a Binary field into your MS Access table and that one is finally case sensitive (uses Unicode for storing binary content) and still can be used as a text field with operators =, LIKE etc.

The field of type Binary cannot be added via the UI, but you add it to your existing table using SQL statement like this:

ALTER TABLE Table1
  ADD COLUMN BinaryField1 BINARY(50)

Then you can manage it normally via the Access UI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜