how to set unique primary key for all table in same database
You may add a new table to your schema called ID_Table
that will have only one numeric column called current_id
with default value of 0 ,when adding a new row to any other table of the schema you have to call a select on the ID_Table returning ID_Table.current_id + 1
as new id value.Then updating ID_Table must be done
Update ID_Tableset ID_Table.current_id = ID_Table.current_id + 1
the GetNewId
function could be implemented bylocking the ID_Table
Updating ID_Table
returning NewID
something like this (I have used Oracle syntax)
create table ID_Table( current_id number); Insert into ID_Table values(0);CREATE OR REPLACE Function GetNewId RETURN number is new_id ID_Table.current_id%type; row_count number;begin select nvl(ID_Table.current_id, 0) + 1 INTO new_id FROM ID_Table for update; update ID_Table set ID_Table.Current_Id = new_id; commit; RETURN new_id;end GetNewId;
You can get maximum ID
from all three tables then add it in your insert query. But you have to remove the auto_increment
attribute.
INSERT INTO TableASELECT MAX(ID)+1, 'jjj' FROM (SELECT MAX(ID) AS ID FROM TableA UNION SELECT MAX(ID) AS ID FROM TableB UNION SELECT MAX(ID) AS ID FROM TableC ) A;
See this SQLFiddle
Use the same sequence as the id generator for each inserted row, regardless of the table. Assuming you're using a DB that allows a sequence to be named as the id generator for the field.
This looks like it will do what you want in MySQL: http://devzone.zend.com/1786/mysql-sequence-generator/