Allow Duplicates

A

acss

i have a table for invoices and at times i receive a duplicate invoice number
from another country. How do i set up a primary key (InvoiceID) and a
secondary key (country) that would allow a duplicate entry only if from a
different country?

Thanks
 
N

Noëlla Gabriël

Hi,

I always take an autonumber field as PK field. You can then create another
unique index on the combination of the invoice number field and the country
field
 
B

Bernard Peek

acss said:
i have a table for invoices and at times i receive a duplicate invoice number
from another country. How do i set up a primary key (InvoiceID) and a
secondary key (country) that would allow a duplicate entry only if from a
different country?

I'm assuming that InvoiceID is not an autonumber field.

Open the table in design view. Select both the InvoiceID and Country
fields (by shift- or Ctrl-clicking.) Click on the Primary Key button.
You should see the key symbol beside both of the fields.
 
A

acss

You are correct that it is not autonumber and is unique though duplication
happens from time to time dependant on country submitting invoices and still
need to be recorded regardless. I am receiving the error that there is
already a relationship so i must delete it before assigning these two keys.
Thanks
 
G

Graham Mandeno

If your Invoices table takes part on the primary key side of one or more
relationships, then I don't think the multi-field PK is a good idea. It
would mean that you have to add the Country field to the foreign key(s) in
the other table(s) as well, which could make a larger headache for you.

Also, since a field in a PK cannot be null, you would need to ensure ALL of
your records had a value in the Country field.

I suggest you go with Noëlla's idea and add an Autonumber field - InvoiceID.
(If your existing field is already called InvoiceID then rename it to
InvoiceNum or suchlike - I prefer to reserve the "ID" suffix for autonumber
fields as an aid to documentation).

After adding the autonumber field, save the table (but don't try to change
the primary key yet). Then edit the design of your related table(s) and add
a long integer field "InvoiceFK" for the foriegn key.

Now create an update query to populate the new field. Its SQL will look
something like this:

UPDATE [InvoiceTable] INNER JOIN [OtherTable] ON
[InvoiceTable].[InvoiceNum] = [OtherTable].[OldForeignKeyField]
SET [OtherTable].[InvoiceFK] = [InvoiceTable].[InvoiceID];

Then you can remove the old relationship, then change your PK to InvoiceID
and add a new relationship joining the two new fields.

Finally, you can add a composite index for the two fields, InvoiceNum and
Country, and delete the old FK field from the other table.
 
A

acss

Hello,
I am still working on this and i am curious as to how you create a unique
index on a combination of invoice and country field? The primary key is the
invoice number as an autonumber so the table can not have more than one
primary key....can you eloborate for me please?
 
G

Graham Mandeno

Hi Joe (?)

I'm confused. You say "The primary key is the invoice number as an
autonumber" but you also said that the invoice number was in the imported
data, so it can't be an autonumber!

I suggest you add a new autonumber field named InvoiceID and make that your
primary key. Then, make InvoiceNumber and Country both required fields and
make them a composite unique key.

To do this, open the Indexes window (View>Indexes) and in the first blank
row, enter "InvoiceNumberCountry" as the index name and select InvoiceNumber
as the field name. Then, in the index properties below, change Unique to
"Yes". Now, in the next blank line, choose Country as the field name (but
leave index name blank).
 
A

acss

Apologies for the confusion Graham,

I would like for the primary key to be unique in the invoice table and as a
foreign key in the details Table however i have the challenge that duplicate
invoices need to be entered and should be accepted only if it is from a
different country.

InvTable
InvID---PK
InvCtry
Invdate
etc

InvDetails
InvID-FK
ProductID--FK---COMPOSITE KEY consisting of InvID and ProductID
Price
Qty

ProductTable
ProductID-----PK-Autonumber
ProductName
Category
Etc

I am stuck since i need a one to many relationship between Inv and
details(one invoice can have many details).The problem occurs as the same
invoice number needs to be entered 3 times once for china another for ecuador
and another for brazil..that is why i need acess to accept same invoice
number only if by different countries. Can you suggest some direction please?

Thanks,


Graham Mandeno said:
Hi Joe (?)

I'm confused. You say "The primary key is the invoice number as an
autonumber" but you also said that the invoice number was in the imported
data, so it can't be an autonumber!

I suggest you add a new autonumber field named InvoiceID and make that your
primary key. Then, make InvoiceNumber and Country both required fields and
make them a composite unique key.

To do this, open the Indexes window (View>Indexes) and in the first blank
row, enter "InvoiceNumberCountry" as the index name and select InvoiceNumber
as the field name. Then, in the index properties below, change Unique to
"Yes". Now, in the next blank line, choose Country as the field name (but
leave index name blank).

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand


acss said:
Hello,
I am still working on this and i am curious as to how you create a unique
index on a combination of invoice and country field? The primary key is
the
invoice number as an autonumber so the table can not have more than one
primary key....can you eloborate for me please?
 
J

John W. Vinson

Apologies for the confusion Graham,

I would like for the primary key to be unique in the invoice table and as a
foreign key in the details Table however i have the challenge that duplicate
invoices need to be entered and should be accepted only if it is from a
different country.

InvTable
InvID---PK
InvCtry
Invdate
etc

InvDetails
InvID-FK
ProductID--FK---COMPOSITE KEY consisting of InvID and ProductID
Price
Qty

ProductTable
ProductID-----PK-Autonumber
ProductName
Category
Etc

I am stuck since i need a one to many relationship between Inv and
details(one invoice can have many details).The problem occurs as the same
invoice number needs to be entered 3 times once for china another for ecuador
and another for brazil..that is why i need acess to accept same invoice
number only if by different countries. Can you suggest some direction please?

It sounds like you need a composite, two-field primary key (InvoiceNo and
CountryCode), which will also require a composite, two-field foreign key in
the details table. You can use

InvCtry;InvID

as the Master Link Field and Child Link Field property of your subform.

You will not be able to use an Autonumber for InvID, but then it's
inappropriate to use an autonumber for that purpose in any case.
 
G

Graham Mandeno

My understanding was exactly correct then, and my suggestion still stands:
I suggest you add a new autonumber field named InvoiceID and make that
your
primary key. Then, make InvoiceNumber and Country both required fields
and
make them a composite unique key.

Your tables would then look like this:

InvTable
--------
InvID---PK - Autonumber
InvNum (as imported - used to be InvID)
InvCtry
Invdate
etc

InvDetails
----------
InvID-FK (long numeric related to the *new* InvID)
ProductID--FK---COMPOSITE PK consisting of InvID and ProductID
Price
Qty

Add a composite unique key comprising InvNum and InvCtry as I described
yesterday.

John V has suggested another alternative which is to add InvCtry to
InvDetails and have a two-field relationship. This is perfectly valid, but
my preference is (a) to avoid multi-field relationships as they can become
terribly messy and (b) to avoid using imported data as primary keys. The
reason for (b) is that you never know when the rules are going to change -
for example, your invoice number might be a numeric field and then the head
office in Brazil decides to use alpha characters in its invoice numbers, so
you need to change the data type in several places, not just one, and
destroy and recreate relationships. Or perhaps in China they decide that
they will reuse invoice numbers starting at the beginning of each year, so
you need to add another field (InvYear) to your PK and also add it to your
related tables.

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand

acss said:
Apologies for the confusion Graham,

I would like for the primary key to be unique in the invoice table and as
a
foreign key in the details Table however i have the challenge that
duplicate
invoices need to be entered and should be accepted only if it is from a
different country.

InvTable
InvID---PK
InvCtry
Invdate
etc

InvDetails
InvID-FK
ProductID--FK---COMPOSITE KEY consisting of InvID and ProductID
Price
Qty

ProductTable
ProductID-----PK-Autonumber
ProductName
Category
Etc

I am stuck since i need a one to many relationship between Inv and
details(one invoice can have many details).The problem occurs as the same
invoice number needs to be entered 3 times once for china another for
ecuador
and another for brazil..that is why i need acess to accept same invoice
number only if by different countries. Can you suggest some direction
please?

Thanks,


Graham Mandeno said:
Hi Joe (?)

I'm confused. You say "The primary key is the invoice number as an
autonumber" but you also said that the invoice number was in the imported
data, so it can't be an autonumber!

I suggest you add a new autonumber field named InvoiceID and make that
your
primary key. Then, make InvoiceNumber and Country both required fields
and
make them a composite unique key.

To do this, open the Indexes window (View>Indexes) and in the first blank
row, enter "InvoiceNumberCountry" as the index name and select
InvoiceNumber
as the field name. Then, in the index properties below, change Unique to
"Yes". Now, in the next blank line, choose Country as the field name
(but
leave index name blank).

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand


acss said:
Hello,
I am still working on this and i am curious as to how you create a
unique
index on a combination of invoice and country field? The primary key is
the
invoice number as an autonumber so the table can not have more than one
primary key....can you eloborate for me please?

:

Hi,

I always take an autonumber field as PK field. You can then create
another
unique index on the combination of the invoice number field and the
country
field
--
Kind regards
Noëlla


:

i have a table for invoices and at times i receive a duplicate
invoice
number
from another country. How do i set up a primary key (InvoiceID) and
a
secondary key (country) that would allow a duplicate entry only if
from
a
different country?

Thanks
 
A

acss

Hello John,
I tried using your recommendation yet in trying the join in the relationship
window i continue to get inderterminate error. I set up the two field
composite key using the fields InvID, InvCtry by holding the shift key and
highlighting both fields. The same was done for details table yet no sucess.
Any further direction is greatly appreciated.
 
J

John W. Vinson

Hello John,
I tried using your recommendation yet in trying the join in the relationship
window i continue to get inderterminate error. I set up the two field
composite key using the fields InvID, InvCtry by holding the shift key and
highlighting both fields. The same was done for details table yet no sucess.
Any further direction is greatly appreciated.

If you look at the parent table in design view are there key icons on InvID
and on InvCtry?

When you're creating the join, are you dragging InvID to InvID, InvCtry to
InvCtry, so that there are two lines, prior to setting the relationship's
properties?
 
A

acss

Yes. In the Invoice Table and Details table there are two key icons. Not sure
as your description of two lines since trying to drag more than one field
onto another table will bring an error of there can only be one relationship.
The previous autonumber as primary was working in that it created a new
record yet it did not allow for a control to be in effect. That control is to
allow duplicates of the invoice number entered as long as it is from a
different country. If from same country then invoice number cannot be
entered. Apologies for causing you this grief...any other suggestions for me
to follow...really appreciate your support.
 
J

John W. Vinson

Yes. In the Invoice Table and Details table there are two key icons.

Then you will - MUST!!! - have a *one to one* relationship.

The two fields should be the Primary Key of the Invoice table.

Since the Invoice Table is related one-to-many to the Details table, the
Details table must NOT have these two fields as its primary key. They must be
foreign keys, non-unique.
Not sure
as your description of two lines since trying to drag more than one field
onto another table will bring an error of there can only be one relationship.

Incorrect.

There is only one relationship... but it can consist of up to TEN fields in
each table, which would be displayed onscree as TEN lines.

Did you try it?
The previous autonumber as primary was working in that it created a new
record yet it did not allow for a control to be in effect. That control is to
allow duplicates of the invoice number entered as long as it is from a
different country. If from same country then invoice number cannot be
entered. Apologies for causing you this grief...any other suggestions for me
to follow...really appreciate your support.

Try what I suggested, for starters. It will work.
 
A

acss

Hello John,

I created a field called PRIME and assigned that as primary key then I
assigned both the InvID and InvCntry as composite keys. When clicking the
indexs icon it displays Prime as unique while the other two fields allows
duplicates. The same set up applys with the Details Table. I went into the
relationship window to drag and drop muliple fields to the details table and
this resulted in 3 relationship lines of one to many. I cannot set the
(enforce referential integrity on the relationship) yet in testing data entry
within the invoices table it accomplishes what i needed...it accepts
duplicate invoices as long as a different country is entered and rejects if
same invoice and same country is entered.
INV-TABLE
Prime----PK-text
InvID---Composite-text
InvCntry--Composite-text

INV-DETAILS TABLE
Prime----PK-text
InvID---FK-text
InvCntry-FK-text

Is this what you were referring to or at the very least a hint of progress?
 
J

John W. Vinson

Hello John,

I created a field called PRIME and assigned that as primary key then I
assigned both the InvID and InvCntry as composite keys. When clicking the
indexs icon it displays Prime as unique while the other two fields allows
duplicates. The same set up applys with the Details Table. I went into the
relationship window to drag and drop muliple fields to the details table and
this resulted in 3 relationship lines of one to many. I cannot set the
(enforce referential integrity on the relationship) yet in testing data entry
within the invoices table it accomplishes what i needed...it accepts
duplicate invoices as long as a different country is entered and rejects if
same invoice and same country is entered.
INV-TABLE
Prime----PK-text
InvID---Composite-text
InvCntry--Composite-text

INV-DETAILS TABLE
Prime----PK-text
InvID---FK-text
InvCntry-FK-text

Is this what you were referring to or at the very least a hint of progress?

Again... *you are misunderstanding what a primary key is*.

It's PRIMARY! It is - must be - *unique* within the table. It can occur once,
and only once!

Therefore, it's appropriate to have it as the primary key of the INV-TABLE but
it is *WRONG* to have it as the primary key of the INV-DETAILS table. The
INV-DETAILS table should have its *own*, separate, independent, unrelated
primary key; it should have the PRIME field as a non-unique foreign key,
related to the Prime field in a one-to-many relationship.

What I had suggested is a multifield primary key:

INV-TABLE
InvID <Text, Primary Key>
InvCntry <Text, Primary Key>
<other fields>

INV-DETAILS TABLE
DetailID <Autonumber, Primary Key>
InvID <Text, matching INV-TABLE.InvID in size>
InvCntry <Text, matching INV-TABLE.InvCntry in size>
<other detail fields>

If you then open the Relationships window you can join InvID to InvID, and
join InvCNtry to InvCntry; enforce relational integrity and you should get a
one to many relationship.

Creating a separate Prime field is an alternative; it could be an Autonumber
in INV-TABLE, with a separate unique two-field index on InvID and InvCntry. In
this approach you would have a Long Integer Prime field as a foreign key (NOT
a primary key) in INV-DETAILS, and the InvID and InvCntry fields would not
exist in the details table at all (since that would be redundant, and they can
be found by linking to INV-TABLE).

How are you assigning a value to the Prime field anyway?
 
A

acss

Hello John,

I still could not get the results as using the fields you displayed:

INV-TABLE
InvID <Text, Primary Key>
InvCntry <Text, Primary Key>
<other fields>

INV-DETAILS TABLE
DetailID <Autonumber, Primary Key>
InvID <Text, matching INV-TABLE.InvID in size>
InvCntry <Text, matching INV-TABLE.InvCntry in size>
<other detail fields>

My understanding :
INV-TABLE
InvID----IS TEXT PRIMARY KEY
InvCntry---is part of a composite key consisting of InvID & InvCntry (set
InvCntry as unique-index-no duplicates

INV-DETAILS
DetailID --AUTONUMBER -If this is the autonumber should i have FK field as
number long integer in the INV-TABLE. ( Have tried this and it creates a one
to many from the details table. I need a one to many from the INV-TABLE to
the INV DETAILS TABLE.
InvID--set as index-allow duplicates--FK
InvCntry--set as index-allow duplicates--FK

I have tried yet not able to obtain referential integrity or the one to many
from Invoice table to the Invoice Details table. Is it imperative to have
referential Integrity for what i need?

Thank you for th support
 
J

John W. Vinson

Hello John,

I still could not get the results as using the fields you displayed:

INV-TABLE
InvID <Text, Primary Key>
InvCntry <Text, Primary Key>
<other fields>

INV-DETAILS TABLE
DetailID <Autonumber, Primary Key>
InvID <Text, matching INV-TABLE.InvID in size>
InvCntry <Text, matching INV-TABLE.InvCntry in size>
<other detail fields>

My understanding :
INV-TABLE
InvID----IS TEXT PRIMARY KEY
InvCntry---is part of a composite key consisting of InvID & InvCntry (set
InvCntry as unique-index-no duplicates

Incorrect.

InvCntry should be a unique index, no duplicates, ONLY if you want to enter
each country in the table ONLY ONCE. That is, if you have an item in Brazil,
that is the *only item in the entire table* that can be in Brazil.

My understanding - and maybe I'm mistaken - is that you want an InvID of "ABC"
in the US, an InvID of "ABC" in Brazil, and an InvID of "ABC" in Argentina;
and you may also want an InvID of "XYZ" in the US, Argentina and Kenya. Right?

If so, *neither* InvID *nor* InvCntry can be unique. There will be duplicates
in the table on both fields. What needs to be unique is the combination of the
two fields! That's what the two-field joint primary key does. Either field can
have duplicates (you can have product ABC in the US and in Argentina; you can
have products ABC and XYZ in Argentina) but the combination is unique (you
can't have ABC - United States in the table twice).

The only unique index in INV-DETAILS table should be the primary key, since
you say you want to have multiple details records for the ABC-United States
inventory item.


INV-DETAILS
DetailID --AUTONUMBER -If this is the autonumber should i have FK field as
number long integer in the INV-TABLE.

Incorrect, and I never said that. The foreign key is in the *MANY* side table,
not the One side table.
( Have tried this and it creates a one
to many from the details table. I need a one to many from the INV-TABLE to
the INV DETAILS TABLE.
InvID--set as index-allow duplicates--FK
InvCntry--set as index-allow duplicates--FK

I have tried yet not able to obtain referential integrity or the one to many
from Invoice table to the Invoice Details table. Is it imperative to have
referential Integrity for what i need?

Yes. And you would do it by having *two* join lines in the relationships
window: from INV-TABLE.INVID to INV-DETAILS.INVID, and from INV-TABLE.INVCNTRY
to INV-DETAILS.InvCntry. There would be no join line to DetailID.
 
G

Graham Mandeno

InvCntry---is part of a composite key consisting of InvID & InvCntry (set
InvCntry as unique-index-no duplicates

No, if you make InvCntry a unique index then you will not be able to add two
invoices fron the same country!

Have you seen my posts from more than two weeks ago where I suggested using
a surrogate (autonumber) primary key and having the InvNum/InvCntry as a
secondary unique index? As I've explained before, I believe this is by far
the best approach.
 
J

JUNEBUG

Hello - what would the criteria for deleting a duplicate record, e.g
PO#1, LINE 1, RECEIVED1
PO#1, LINE 1, SHIPPED1
I am looking to delete whatever is the Same PO#, Same LINE but
SHIPPED(instead of Received) I only need one record or RECEIVED.
thank you


Graham Mandeno said:
Hi Joe (?)

I'm confused. You say "The primary key is the invoice number as an
autonumber" but you also said that the invoice number was in the imported
data, so it can't be an autonumber!

I suggest you add a new autonumber field named InvoiceID and make that your
primary key. Then, make InvoiceNumber and Country both required fields and
make them a composite unique key.

To do this, open the Indexes window (View>Indexes) and in the first blank
row, enter "InvoiceNumberCountry" as the index name and select InvoiceNumber
as the field name. Then, in the index properties below, change Unique to
"Yes". Now, in the next blank line, choose Country as the field name (but
leave index name blank).

--
Good Luck :)

Graham Mandeno [Access MVP]
Auckland, New Zealand


acss said:
Hello,
I am still working on this and i am curious as to how you create a unique
index on a combination of invoice and country field? The primary key is
the
invoice number as an autonumber so the table can not have more than one
primary key....can you eloborate for me please?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top