Answer to the revised question
Paraphrasing the question:
Why does this query not work against the schema?
SELECT sum(B.score) FROM A RIGHT JOIN B ON A.id=B.name_id GROUP BY B.name_id
Part of the answer must be "in what way does it 'not work'?" As written, the query should produce an answer row for every row in B.
100
10
110
The RIGHT OUTER JOIN (ROJ) really doesn't make any difference to the query result; you could as well have omitted the keyword RIGHT and would get the same result:
SELECT SUM(B.score)
FROM A
JOIN B ON A.id = B.name_id
GROUP BY B.name_id
If, as seems plausible, you want the name and aggregate score for each player listed in table A, then you'd write:
SELECT A.Name, SUM(B.Score) AS TotalScore
FROM A
LEFT JOIN B ON A.id = B.Name_ID
GROUP BY A.Name
The RIGHT OUTER JOIN has been replaced by a LEFT OUTER JOIN. This will list all the players.
Answer to the original version of the question
The original question applied to the schema shown and asked:
My question is how can I set a foreign key in this situation?
- You will have a primary key on TableA(ID).
- You will have a primary key on TableB(ID).
- You will create a foreign key on TableB(Name_ID) that references TableA(ID).