Question

I have two tables:

Friends :

id name

1 jhon

2 peter


Teammates:

id name

3 juan


i am looking for a way two auto increment the id of the second table (teammates) according to the first table ( Friends ). When I add a new register to Teammates it never match with an id of Friends

Was it helpful?

Solution

I think this is not good practice. If you do so, you are introducing an implicit functional dependency between both tables outside of the declared design. If you want to it anyway, you can use a trigger to asign the value instead of making the column autoincrement. I would suggest to have a table for all people with the real autoincrement id, then you can use several approaches:

i) Make your two actual tables take id values as foreign keys of this new table, with the corresponding integrity constraint.

ii) Simply create 2 views of the table: One for friends, other for teammates.

Table_Friends: (id, name, role)

View_Friends: Select id, name from table_Friends where role = value_for_friend_role

View_Mates: Select id, name from table_Friends where role = value_for_teammate_role

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top