473,395 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to create unique primary key?

jinalpatel
I want to create a primary key that is combined with several fields

Autonumber+InspectorNo(1 to 5)+CountyNo(1 to 35)+Date

How to create it ? Where to write code for it?
Please help!!
Jun 25 '09 #1
9 5753
Add another field to your table (such as primarykey and set the datatype to text).

using an update query, update that field:
[ID] & [InspectorNo] & [CountyNo] & [Date]

or if Date is not a field name in the table
[ID] & [InspectorNo] & [CountyNo] & Date()

The ID field in this instance is AutoNumber and is the primary key.
Jun 26 '09 #2
ADezii
8,834 Expert 8TB
@jinalpatel
  1. Open the Table containing the above Fields in Design View.
  2. Select the [Autonumber] Field.
  3. Hold the CTRL Key Down, then select the [InspectorNo], [CountyNo], and [Date] Fields.
  4. All 4 Fields should now be selected.
  5. On the Menu Bar, Click Edit ==> Primary Key.
  6. You have now just created a Composite, Primary Key which means that the combination of ALL 4 Fields must be Unique within the Table.
  7. The following Records would not be a Primary Key violation, since the combination of these Fields is Unique:
    Expand|Select|Wrap|Line Numbers
    1. Autonumber]    [InspectorNo]    [CountyNo]    [Date]
    2.     1                4              51        6/26/2009
    3.     2                3              51        6/26/2009
    4.     3                2              37        6/26/2009
    5.     4                5              37        6/26/2009
    6.  
P.S. - You mentioned writing code in order to create this Key. This would be much more difficult, and I'm not exactly sure why you would need to do this programmatically.
Jun 26 '09 #3
OldBirdman
675 512MB
Why is such a primary key is needed? Although I understand the answer of how to do this, I don't understand the reason. Any dependent tables would not remain related if a value were changed, perhaps because of an entry error.

With a field of type AutoNum, there is a unique key. ADezii's concern
6. You have now just created a Composite, Primary Key which means that the combination of ALL 4 Fields must be Unique within the Table.
7. The following Records would not be a Primary Key violation, since the combination of these Fields is Unique:
is no concern as a non-unique key cannot be generated here.

Is this better addressed in a discussion about Indexes vs. keys?
Jun 26 '09 #4
mshmyob
904 Expert 512MB
I think the problem lies in what the OP is really asking for.

Does he want to create a new table with a primary key value consisting of all the field values he mentioned - answered in 2nd post by DT how to do that.

or

Does the OP want to create a composite primary key with the existing table. - this was answered by Adezii post.

OB's concern about existing table relations is valid if the OP now wishes to change the PK to a composite.

Just as a side note if the OP actually wants a composite PK then the AUTONUMBER field would not be needed to be included if 'Each inspector cannot inspect the same county on the same day'

Just my 2 cents for this discussion.

cheers,
Jun 26 '09 #5
ADezii
8,834 Expert 8TB
@mshmyob
Makes perfect sense to me, mshmyob. Let's wait and see what the OP is really looking for.
Jun 26 '09 #6
OK I am sorry for replying late but I was on vacation. I need this kind of primary key because I am working with pendragon software which syncs with PDA units. It is possible that if two inspectors are inspecting the same nursery at the same time(thats why I am taking "seconds" in consideration), if the nursery farm is more than 300 acres.
I need to create this unique primary key to keep the master records unique.
Thanks everyone for answering and meaning ful discussions!! Thanks much!!
Jul 6 '09 #7
also, pendragon software creates its own tables. I want half the data feeled on pc and other half on PDA. I want something like this. When user enters new records, that unique primary key should be created like autonumber is incrmented by itself when you are entering new record.
Jul 6 '09 #8
ChipR
1,287 Expert 1GB
Use a timestamp as a field that is part of the composite primary key, and insert the current time when the record is created. That will prevent duplicates, while allowing you to merge data. As mshmyob pointed out, the autonumber is not necessary in this case.
Jul 6 '09 #9
mshmyob
904 Expert 512MB
A primary key consisting of just INSPECTORID, COUNTYID, and DATE is all that is needed if the following rules apply.

1. Each inspector may inspect many counties.
2. Each county may be inspected by many inspectors,

with the following constraints

1. Each inspector may inspect the same county many times but not on the same date
2. Many inspectors may inspect the same county on the same date


No need for time field or autonumber.

cheers,

@jinalpatel
Jul 6 '09 #10

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

Similar topics

1
by: js | last post by:
I am trying to create a primary key constraint on a view in the following statement. However, I got an error ORA-00907: missing right parenthesis. If the CONSTRAINT clause is removed, then the...
4
by: Baoqiu Cui | last post by:
Hi, I was playing with MySQL (4.1.9) during the weekend, but noticed a minor problem and would like someone to explain this to me. Basically I could successfully create a table with one unique...
2
by: deko | last post by:
I'd like to use DDL (Data Definition Language) rather than VBA code to create a new table. The below DDL statement creates the table very nicely: CREATE TABLE tblTx03 (Subject Text (100),...
3
by: blindsey | last post by:
Is there a tool that can take an Access database and generate SQL "CREATE TABLE" statements for all the tables in it?
7
by: sea | last post by:
Is it a good idea to programatically create a primary key? For example in a table called names, I have the following fields, (1) firstname (2)lastname (3) ID - will it be ok to create a primary...
24
by: flkeyman | last post by:
Work in legal office. Trying to create solid designed database structure. This is list of tables w/fields and primary keys. Any comments/advice greatly appreciated. tbl-Defendants CaseNumber...
2
by: Maverick | last post by:
If i try to create foxpro table by the following "sql" statment, the C# compiler will only return an error "xxxx not support in non-dbc version". The "index on" command statement return some kind...
0
by: BRINER Cedric | last post by:
Synopsis CREATE { TEMPORARY | TEMP } ] TABLE /table_name/ ( { /column_name/ /data_type/ ] | /table_constraint/ } ) ) ] ... ...
3
by: DeanL | last post by:
Hi guys, Does anyone know of a way to create multiple tables using information stored in one table? I have a table with 4 columns (TableName, ColumnName, DataType, DataSize) and wanted to know...
6
by: Alvin SIU | last post by:
Hi all, I have a table in Db2 v8 like this: Team Name Role ------ -------- --------------------- A Superman Leader A Batman Member A WonderWoman Member B ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.