To thouroughly understand the relationship between melodies and chords you must have a thourough understanding of major scales and their key signatures. They are the foundation of music and nearly all music theory is based upon it.
Key:
A key refers to a system of notes which together are cohesive with a common tonic. The idea of a key includes both single notes and chords!
Key Signature:
The key signature is placed after the clef at the start of a musical piece. Wether it is made of sharps or flats it is used to determine two things: (1) which notes are sharp or flat throughout the song and (2) the key the song is written in. The latter will allow you to play the seven notes of the major scale!
Sharp Order:
Here is the order and a little saying that can help you remember them!
To determine the major key you simply take the last sharp in the key signature and move a half step (one fret) up from that note! Then that is the name of the major scale. To spell out the major scale, you merely begin at the root (tonic) and continue through the musical alphabet (A-G) inserting the sharps named in the key signature.
Example 1:
Example 2:
G-A-B-C-D-E-F#
Flat Order:
Here is the Flat Order and a little saying that will help you remember them. Hint - Backwards from the Sharp Order:
To determine the major key you simply take the next-to-last flat. To spell the major scale, you merely begin at the root(tonic) and continue through the musical alphabet (A-G) inserting the flats named in the key signature.
Example 1:
Example 2:
NOTE: If only Bb appears, the key is F major (F-G-A-Bb-C-D-E). The key of C major has no sharps or flats (C-D-E-F-G-A-B). The key of C# major has seven (all) sharps (C#-D#-E#-F#-G#-A#-B#). The key of Cb major has seven (all) flats (Cb-Db-Eb-Fb-Gb-Ab-Bb).