Can we create multicolumn unique indexes on MS access databases?
Asked Answered
Z

6

37

We'd like to prevent record duplication in our MS access database using a multicolumn unique index. Because of how the data is sent (via network), duplicate data is sometimes received. The data source does not send a unique ID, so the simplest option is to prevent duplicate records being inserted.

According to Unique Index Design Guidelines:

With multicolumn unique indexes, the index guarantees that each combination of values in the index key is unique. For example, if a unique index is created on a combination of LastName, FirstName, and MiddleName columns, no two rows in the table could have the same combination of values for these columns.

This is for SQL 2005 however, so I'm not sure it's possible using MS access.

I guess an alternative is to perhaps use the query (pseudo code):

insert into foobar (a, b, c) values ('x', 'y', 'z')
where (a <> 'x') and (b <> 'y') and (c <> 'z')

... but I feel like an index would be better.

Zuleika answered 24/1, 2010 at 16:11 Comment(11)
Why not give the tables in question a unique index? I don't see what this has to do specifically with C++, BTW.Erelia
Yeah, I wasn't sure if it was that relevant. We already have a unique index; but the source of data does not provide this. I will update my question.Zuleika
Your unique index needs to be on the actual data.Erelia
The data comes from a piece of equipment that does not generate unique indices, just data -- are we on the same page, or do I not understand what you're saying? Please could you advise on my hash idea?Zuleika
A unique index specifies that certain combinations of data in a row must be unique, or the data it cannot be added to the row - it has nothing to do with the source of the data. It sounds like you need to get a day's worth of database design consultancy for your app.Erelia
Is not a unique index for only 1 column? We need the entire row to be unique... Are you saying that we need to generate an ID in our code, or somehow in the DB?Zuleika
No - any index can be created on more than one column - indexes would be pretty useless if that were not the case. This is why I say you need some help - you don't seem to understand the basics of database design.Erelia
Neil, please could you explain how to create an index on more than one column using MS access? This would be very helpful.Zuleika
Sorry, I don't use access myself.Erelia
Aha, I believe the term I'm searching for is "multicolumn unique indexes". I've never used these before... Maybe this answer will be helpful: #179585Zuleika
What kind of data? What kind of equipment? Why can't some records be the same if they are coming from a piece of equipment?Torquay
M
13

Open the table in design view in MS Access, select the three columns that you want to make into the unique index, and then click the little key on the toolbar. You cannot have null values in a primary key (set).

Mendelssohn answered 24/1, 2010 at 20:17 Comment(6)
The Null issue is crucial -- if any of the fields can be Null, then you can't enforce uniqueness on the index (because no two Nulls are ever equal, two records with 5 identical fields and a 6th field Null in both would not be considered duplicates).Derte
This answer is right. But somebody should tell Nick that what he's really looking for is a unique CONSTRAINT, not a unique index. If he creaes a multicolumn PK constraint, he'll get a multicolumn unique index along with it. But the index is just a tool. The logical feature he's looking for is the constraint.Salmonella
@Walter Mitty I think you're right - see the comment on my answer.Zuleika
There is no way in Access via the UI to create a UNIQUE constraint -- it can be done only via DAO or DDL.Derte
This is how to create a Primary Key in MSAccess.... it is a unique index, but is not the way to just create a unique index on any fields. If you don't need a Primary Key for anything else, this can solve the problem for a single Unique index. But the answer by @Ilmari Karonen is much more to the point.Explode
Primary keys are used to uniquely identify your record. The intention of primary key is not to prevent duplication. Use Index Unique constrains as @Ilmari Karonen mentioned.Ginseng
Z
59

Turns out you can create a multi-column unique index on an MS access database, but it's a little crazy if you want to do this via the GUI. There's also a limitation; you can only use 10 columns per index.

Anyway, here's how you create a multi-column unique index on an MS access database.

  1. Open the table in design mode, and Design, select Indexes.
  2. Create a new row and enter a value in the Index Name cell,
  3. Choose the first column from the drop down menu.
  4. Add a new row and leave the Index Name cell blank.
  5. Choose the second column, and so on.

Here's what it should look like:

multicolumn index in MS access

Zuleika answered 24/1, 2010 at 20:35 Comment(5)
Hmm, for some reason this makes Foo unique on it's own right, as opposed to combined with Foo, Bar, etc. I need to allow ("a", "b", "c", "d") only once, but also allow ("x", "b", "c", "d") -- and this arrangement of indexes doesn't seem to allow this because the last 3 values already exist.Zuleika
@Remou gave you an easier way to do it, i.e., select multiple rows and hit the PK button on the toolbar. This seems pretty intuitive to me, as it's the GUI way to set up any primary key.Derte
@Derte Yup, like Remou said.Zuleika
Ha, please discard my first comment! I just tried this again today and it works as expected. Not quite sure what I was doing wrong yesterday.Zuleika
Nick, there's one more step. First, click on the Index Name for the multi-column index created as you described above. Then set its Unique property to Yes in the small property sheet included within the Indexes dialog window. This permits duplicated values in either of the individual fields, but not their combination. Note also that there is an "Ignore Nulls" property that can be set on the index. This would prevent the index from blocking the insertion of a record when you're just not ready to set the value for the columns in the multi-column index.Dimidiate
M
13

Open the table in design view in MS Access, select the three columns that you want to make into the unique index, and then click the little key on the toolbar. You cannot have null values in a primary key (set).

Mendelssohn answered 24/1, 2010 at 20:17 Comment(6)
The Null issue is crucial -- if any of the fields can be Null, then you can't enforce uniqueness on the index (because no two Nulls are ever equal, two records with 5 identical fields and a 6th field Null in both would not be considered duplicates).Derte
This answer is right. But somebody should tell Nick that what he's really looking for is a unique CONSTRAINT, not a unique index. If he creaes a multicolumn PK constraint, he'll get a multicolumn unique index along with it. But the index is just a tool. The logical feature he's looking for is the constraint.Salmonella
@Walter Mitty I think you're right - see the comment on my answer.Zuleika
There is no way in Access via the UI to create a UNIQUE constraint -- it can be done only via DAO or DDL.Derte
This is how to create a Primary Key in MSAccess.... it is a unique index, but is not the way to just create a unique index on any fields. If you don't need a Primary Key for anything else, this can solve the problem for a single Unique index. But the answer by @Ilmari Karonen is much more to the point.Explode
Primary keys are used to uniquely identify your record. The intention of primary key is not to prevent duplication. Use Index Unique constrains as @Ilmari Karonen mentioned.Ginseng
F
0

We can make multi data to be unique data without set them as primary key.

(Note: only 1 data in the table can be primary key)

Step to set the data value as unique data (for MS ACCESS 2007 - 2010)

  1. Open selected table in Design View
  2. Click (Highlight) the specific column/attribute that you wish to set as unique
  3. At the bottom of the table you will see "Index Properties" for that specific column
  4. Find "Indexed" column, currently the data in Indexed text box is "No", change the data by click at the end of text box, choose "Yes(No Duplicates)"

Really hopes this methods can helps all of you! :)

Fungiform answered 11/11, 2015 at 3:34 Comment(3)
This works only for single columns. The question is about multiple columns.Rapids
@Andre451 you can use this method for multiple column Andre. Just set this method for every column you wish to set as unique. Have you try to set in more than one column? Because it's work on mine.Fungiform
Yes, of course you can set an unique index for multiple columns separately. But the question is about setting a single unique index that covers multiple columns.Rapids
P
0

I had the problem Nick Bolton reported above. Setting 2 fields (Foo, Bar) as PK set Foo to unique, when I wanted only the combination of Foo + Bar to be unique.

The problem turned out to be that I had created a 1:1 relationship to another table, linking on Foo. I deleted the relationship, set up the 2-field PK the way I wanted, and then reinstated the relationship, and it works as desired.

Plurality answered 5/8, 2021 at 19:26 Comment(0)
N
0

A trick I found is that in order to get a 2 column primary key (in the parent table) to be a child table´s 2(FK)+n primary key is to FIRST CHOOSE the indexed attribute as FISRT key attribute and THEN the not indexed attribute as a SECOND key attribute in the "Modify Relations Dialog Box" @Relations Window.

Naquin answered 16/12, 2021 at 23:2 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Hoboken
V
-1

Unique key

It will serve as Unique key in Ms Access 2007/2010

Vintage answered 13/3, 2017 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.