Reputation: 408
Example / case:
Table_1
: all Soldiers [fullname, date of birth, ...]
Table_2
: all possible navy ranks [Seaman, ..., Fleet Admiral]
Task: Update Soldiers(Pedro A, ..., Alberto Z) from (current rank) Commander to (new rank) Captain
What's the most effective way to perform this?
Upvotes: 0
Views: 73
Reputation: 54302
Use more decritive table names: soldiers
instead of Table_1
and ranks
instead of Table_2
.
Table with ranks should have id for each rank. Name it rank_id
. Such rank_id
may by 1 for Seaman and 100 for Fleet Admiral. PostgreSQL has SERIAL
type for such key columns and this should be type of rank_id
.
In soldiers
table put column with rank_id
which will point to record in ranks
table.
Then you can promote soldiers:
UPDATE soldiers SET rank_id=(SELECT rank_id FROM ranks WHERE rank_name='Commander') WHERE rank_id=(SELECT rank_id FROM ranks WHERE rank_name='Captain')
Upvotes: 1