Link to home
Create AccountLog in
Avatar of aprillougheed
aprillougheedFlag for United States of America

asked on

Autoincrement using value from a separate table

I'm not sure how to do this.  I just need an additional unique identifying number for each web submitted form - besides the primary Table autoincremented value.

I need to have an additional autoincrement column in Table1 - besides the primary key.  I've been advised to "link" to a separate table (Table2) and use that value in Table1.  My problem is I do not know how to get the value from Table 2 to show up in Table 1.  

Table1
column one primary key - id_table1
column two other column - master_idfromtable2

Table 2
column one primary key - master_idfromtable2

Will MySQL figure out to insert the master_idfromtable2 into Table1 or do I have to set up set up some kind of Index or Foreign Key or something.  Or do I have to write to two different tables from

Thanks so much.  April

   
Avatar of T911
T911

Why do you need 2 unique keys instead of just your primary key?
ASKER CERTIFIED SOLUTION
Avatar of Michael701
Michael701
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Kiran Paul VJ
How about using only a single table and using Stored Procedures/Triggers to increment the value of the second column. Dont know will it work fine, just a shot in the dark.

Kiran
Why not just do it yourself?

Insert the new record into Table 1, then call mysql_insert_id() to get its unique auto incremented ID. As this will always be unique you can then use it to generate your second unique ID.

For example, you could simply add 1 to it and store it in the extra unique ID column by updating the record once you have generated the second unique ID.

This may seem too simple, but it's easy to do and it would give you 2 auto incretmented ID's per record.
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of aprillougheed

ASKER

I think I'm confusing everyone here.  So I awarded points.  I'm going to open a new question under category PHP and MySQL.

What I'm really trying to do is create a unique ID number for a Contact Us form.  Anyway, let's close this question and I'll try asking it in a different way.

Thanks, April