473,667 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ 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 16583
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,568 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 programmaticall y using VBA.

Mary
Mar 12 '07 #6
NeoPa
32,568 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,568 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
3214
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 table to enable users to select a pre-defined list of values. I have developed a practice myself of avoiding AutoNumber type data fields for primary keys where the primary key will be related to a child table. Nevertheless, what do most users do...
7
2950
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 trying to insert. I therefore do not think that I can use the sql "Insert Into" command. Here is a simplified illustration of my tables: tblFather NaturalKey1 NatuarlKey2
7
5343
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 needs a unique ID# based on its context. Primary Keys AUTHORS = AuthorID - NO Duplicates
4
1645
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 Hearings and Rulings were stored in 1 generic table called Case. I split it into 2 tables, Hearings and Rulings. The problem is that the primary key, CaseID, is an autonumber. I don't want the Hearings and Rulings to have the same number.
9
3904
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 eg: area.txt, school.txt, students.txt.... and so on (ok?!?) now, 1. area code used in the school.txt must be defined in the area.txt (Primary key in area => area_code defined in area.txt & Foreign key on school => area_code defined in...
115
6222
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 I use them as primary keys or should I define an autonum primary key? One table has information about the 50 states in the US. The table looks like this:
1
1507
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 what it thinks is the next key. But when I go to save the record, it fails. Access won't take the new key even though that key is not in the current database (but was once used and deleted). I am so confused about what to do here. Any help...
4
3825
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 rule of the business is that only band plays on the one night. The SQL statement prevents a Band name being repeated (as it is Unique). Similar statement for the Venues. CREATE TABLE Bands (BandID varchar(5) CONSTRAINT BandID PRIMARY KEY, Band...
6
2740
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) and this is working great, edit, insert etc is working bar when I try to delete a record in one of my subforms (I'm in test stage at the mo) I get a run time error 3022 'The changes you requested to the table where not successful because they would...
0
8457
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8883
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8563
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8646
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.