php - MySQL having 1 primary key with auto increment for 2 tables -


i have 2 tables 'table1' & 'table2', both have column named 'id' (primary key) , both have auto increment. problem how make primary key viable both tables.

example: enter id '100' in 'table1' , after if try enter new record in 'table2' have '101' in 'table2'

i thought 'foreign key' job did opposite of need

strictly speaking, there's no way describe without resorting queries or locking.

you can simulate creating third table, insert generate new id's, use last_insert_id() insert either table1 or table2. see example in how have unique ids across 2 or more tables in mysql?

also see discussion of problem , solutions here @ popular mysql performance blog: sharing auto_increment value across multiple mysql tables

but agree comment @barmar, might indication of bad design database. if have requirement make auto-increment pk unique across multiple tables, may mean 2 tables should 1 table.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -