473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using Autonumber Primary Keys with text prefixes

9 New Member
Hi,

I want to use primary key IDs in my tables which are autonumbers, with a text prefix. I.e. for table tblVillage, the ID's will by V1, V2, V3... and for table tblHumanCases, the ID's will be H1, H2, H3...

I have a few questions:

1. I have heard its not good to do this. Is this true and if so why?

2. I am also having problems creating the relationships to related tables due to the field types. In the parent table the field type is autonumber, but using number as field type in the child table doesnt work, as the ID has text in it, but I cant create the join if the field type is set to text as it is then a different type to the parent table. Is there an easy way around this, or perhaps this is the reason its not good to use primary keys with text and autonumber?

3. Is it possible to create a primary key ID for a related table which concatenates the ID of the parent table, with another unique ID? For example the parent tables ID might be A1 for animal number 1, and the child tables ID might then be A1S1 for animal 1 sample 1, and be linked to the ID A1. Is this possible, and if so how can I go about doing it?

Thanks,

Nicola
Mar 9 '07 #1
8 16551
mycall
9 New Member
>>>>1. I have heard its not good to do this. Is this true and if so why?

First of all i dont think it is possible in access. You would need to use code in real time to go through and make your own autonumbertext. Its prolly not a good idea cos it makes the task a lot more complicated. If you want a text field as a primary key then you are better off having it as a user defined input otherwise having code for when a record is added to go through using string manipulation and determining what would be the next autotext. Ie, u need to know the basics of VB.

>>>>2. I am also having problems creating the relationships to related tables due to the field types. In the parent table the field type is autonumber, but using number as field type in the child table doesnt work, as the ID has text in it, but I cant create the join if the field type is set to text as it is then a different type to the parent table. Is there an easy way around this, or perhaps this is the reason its not good to use primary keys with text and autonumber?

Yes correct both fields need to be the same Data type. If the parent table is an autonumber then it cannot contain text so how could u possibly link it to a field which did contain text??? You will need to change the child table and delete any text out of the PK and then change its datatype to numeric.

>>>>>3. Is it possible to create a primary key ID for a related table which concatenates the ID of the parent table, with another unique ID? For example the parent tables ID might be A1 for animal number 1, and the child tables ID might then be A1S1 for animal 1 sample 1, and be linked to the ID A1. Is this possible, and if so how can I go about doing it?

No its not possible they need to be the same. Data integrity.

>>>>Thanks,

Cheers
Mar 9 '07 #2
NeoPa
32,556 Recognized Expert Moderator MVP
Hi,

I want to use primary key IDs in my tables which are autonumbers, with a text prefix. I.e. for table tblVillage, the ID's will by V1, V2, V3... and for table tblHumanCases, the ID's will be H1, H2, H3...

I have a few questions:

1. I have heard its not good to do this. Is this true and if so why?

2. I am also having problems creating the relationships to related tables due to the field types. In the parent table the field type is autonumber, but using number as field type in the child table doesnt work, as the ID has text in it, but I cant create the join if the field type is set to text as it is then a different type to the parent table. Is there an easy way around this, or perhaps this is the reason its not good to use primary keys with text and autonumber?

3. Is it possible to create a primary key ID for a related table which concatenates the ID of the parent table, with another unique ID? For example the parent tables ID might be A1 for animal number 1, and the child tables ID might then be A1S1 for animal 1 sample 1, and be linked to the ID A1. Is this possible, and if so how can I go about doing it?

Thanks,

Nicola
This is a strange question - by that I mean it's hard to understand exactly what you mean. Let's try to explain some basic ideas then you can come back with more questions if that seems appropriate.
  • An AutoNumber field, by its very nature, is unique.
  • Any unique item can be a PK on its own and doesn't require any qualification.
  • Adding a text component to an AutoNumber field to create a Primary Key can ONLY be done by including it as a separate field within a composite PK.
To create a relationship between any such table and another table with FKs pointing to this one, the FK would have to be set up as two fields too. A matching text field and a matching number field.
In answer to your question 3 - Yes it is possible. You would use a similar concept (composite keys) to that described above.
This is often used to link two tables in a Many-to-Many relationship where they don't share the same key fields.
Mar 10 '07 #3
MMcCarthy
14,534 Recognized Expert Moderator MVP
Nicola

Just to clarify on point 3. Although you can concatenate two unique ID's from Table A and Table B as the ID (PK) for Table C. This cannot be one field if you want to create relationships with the other tables. However, you can do the following:

Table A
A_ID (Primary key)

Table B
B_ID (Primary key)

Table C
C_ID (Primary key made up of concatenating the two foreign keys below)
A_ID (Foreign key referencing the primary key of Table A)
B_ID (Foreign key referencing the primary key of Table B)

Although you don't say why you want the letters on the primary keys of the other tables if it is just for display purposes you can do that using the format while only the number would be stored.

An to reiterate the point that's been made twice already you cannot have a relationship between a text and a number field. They must have the same datatype. The reason that numbers are used is that they are indexed fields and numbers will index and sort faster than text.

Mary
Mar 10 '07 #4
Imicola
9 New Member
Ok thanks.

I wasn't going to use text in my IDs to begin with, but then discussed my database with a colleague who suggested it might make it easier for people analysing the data at a later point as they will be able to see easily which table the data is coming from.

I think that I will just stick to a normal autonumber, as by adding text to it seems to just confuse matters!

I have worked with databases in the past which have used primary keys which started with text (ie SE06) and then this was followed by an autonumber (ie SE0601, SE0602, SE0603...). In this case it was used to identify records which were being imported from different databases. However, this isn't going to be the case for my database so I'll just simplify it and stick to the normal autonumber!


Thanks again for your help,

Nicola
Mar 12 '07 #5
MMcCarthy
14,534 Recognized Expert Moderator MVP
Ok thanks.

I wasn't going to use text in my IDs to begin with, but then discussed my database with a colleague who suggested it might make it easier for people analysing the data at a later point as they will be able to see easily which table the data is coming from.

I think that I will just stick to a normal autonumber, as by adding text to it seems to just confuse matters!

I have worked with databases in the past which have used primary keys which started with text (ie SE06) and then this was followed by an autonumber (ie SE0601, SE0602, SE0603...). In this case it was used to identify records which were being imported from different databases. However, this isn't going to be the case for my database so I'll just simplify it and stick to the normal autonumber!


Thanks again for your help,

Nicola
Nicola

I think that's a good idea but if it becomes more important in the future then the only way to do this is you will have to drop the Access autonumber and create your own number programmatically using VBA.

Mary
Mar 12 '07 #6
NeoPa
32,556 Recognized Expert Moderator MVP
Ok thanks.

I wasn't going to use text in my IDs to begin with, but then discussed my database with a colleague who suggested it might make it easier for people analysing the data at a later point as they will be able to see easily which table the data is coming from.

I think that I will just stick to a normal autonumber, as by adding text to it seems to just confuse matters!

I have worked with databases in the past which have used primary keys which started with text (ie SE06) and then this was followed by an autonumber (ie SE0601, SE0602, SE0603...). In this case it was used to identify records which were being imported from different databases. However, this isn't going to be the case for my database so I'll just simplify it and stick to the normal autonumber!


Thanks again for your help,

Nicola
I think what you're getting hung up on here is the difference between a number and an AutoNumber field.
Numbers come in all shapes and sizes. An AutoNumber field is a specific and tightly defined entity. "SE0603" is not an example of an AutoNumber but of a simple number converted to a string before it is stored.
Mar 12 '07 #7
hyperpau
184 Recognized Expert New Member
Ok thanks.

I wasn't going to use text in my IDs to begin with, but then discussed my database with a colleague who suggested it might make it easier for people analysing the data at a later point as they will be able to see easily which table the data is coming from.

I think that I will just stick to a normal autonumber, as by adding text to it seems to just confuse matters!

I have worked with databases in the past which have used primary keys which started with text (ie SE06) and then this was followed by an autonumber (ie SE0601, SE0602, SE0603...). In this case it was used to identify records which were being imported from different databases. However, this isn't going to be the case for my database so I'll just simplify it and stick to the normal autonumber!


Thanks again for your help,

Nicola

There is actually a way to get what you want.

Go to the design view of your table.

On the autonumber field, type this in the Format property (including the quotes)
--- "V"0 ------

As soon as access generates the numbers automatically, it will put the
Letter V in front of the number. It works for me so good.
Jun 14 '07 #8
NeoPa
32,556 Recognized Expert Moderator MVP
Be warned - this will not change the AutoNumber data, just how it is displayed. This may not be important, but you should understand what it means.
Jun 14 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
Hello group: I've done alot of reading on this subject somewhat and have found that many people have many different opinions on this subject. My question centers mainly around using a lookup...
7
by: Ilan Sebba | last post by:
I am trying to add a record using SQL. My problem is that the primary keys are foreign keys, and these foreign keys are autonumbers. I therefore do not know the primary keys of the record I am...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
4
by: Megan | last post by:
Hi- I need some help/ advise on how to code unique numbers for the primary keys of my 2 tables. I inherited a database that covers information about Hearings and Rulings. Information about the...
9
by: sonal | last post by:
Hi all, I hv started with python just recently... and have been assigned to make an utility which would be used for data validations... In short we take up various comma separated data files for...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
1
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
I have an MDB database and am using tableadapters, etc. I have the primary keys in the database as NUMBER (not autonumber). When you hit the ADD button on the datanavigator, it of course selected...
4
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
6
by: babamc4 | last post by:
I have a main form (mainformlung) with 5 subforms (followupacute, followuplate, biochemresults, haemresults and pftresults). I have copied Allen Browne's Audit Trail code (thanks to Allen Browne)...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.