Introduction
We use key signatures to indicate which major key centre (this is including it's diatonic modes, but we'll get to tonal centres and implied harmony another time) we're deriving our notes from. Rather than write a sharp next to every single F note in an entire song or passage of music in the key of G major, we simply write an F# at the start of the section or song and before the time signature (and usually at the start of each line of notation) to indicate our tonality is G major, or a diatonic relative (ie. a mode such as E Aeolian [minor], which is also known as the relative minor to G Ionian [major]). This then allows both composer and anyone else who reads the music to "think" in G major, automatically sharpening every F note in any octave as they read it. Even if you're not reading the notes, the key signature play a vital role in reading a chord progression, as you can determine the overall implied harmony and not just focus on the chords themselves.
Note that this all goes beyond just reading music on a musical staff (although I will refer to it now and then) and right into understanding what any given key is doing.
For the benefits of this post and keeping things as simple as possible, we will be using only major keys.
Determining Key Signatures
Circle of Fifths from natural (C major) gives you all you keys with sharps in them
Cycle of Fourths from natural (again, C major) gives you all your keys with flats in them.
Note: I've seen it referred to as the Cycle of Fifths and the Circle of Fourths, or both Circle of Fifths and Circle of Fourths, or Cycle of Fifths and Cycle of Fourths. This is all largely irrelavent as the terminology used decribes the exact same thing. In this case I'll apply both terms.
Circle of Fifths
The Circle of Fifths, to put simply, is a sequence of major keys, ascending in intervals of fifths.
Every time we change key, we add a new sharp.
The order of keys for the circle of fifths is:
C G D A E B F#
Remember of course that C contains only natural notes, so there are no sharps.
The next key in the sequence is of course G major.
So, starting from C, fifth up we have G. That has one sharp, F#.
Then, up a fifth from G we go to D. That has two sharps, F# and C#.
Then up a fifth from D we arrive at A. This has three sharps, F#, C# and G#...
Can you see a pattern emerging here?
To check this, go from G major your intervals (TTS, TTTS)
You'll see your notes are: G A B C D E F#
D major: D E F# G A B C#
A major: A B C# D E F# G#
Each time we go to the next fifth, we add a sharp to the sequence. We build up the sharps in order. An an easy way to figure out which sharp to add to the sequence is simply by going a semitone from the new root note of the next key in the Circle of Fifths.
So in the case of G, a semitone below is F#
Then D major you add C# to the F#
Then for A you add G# to this sequence (F#, C#, G#)
Then E you have F#, C#, G# and D#...
...until we get seven sharps. But, as previously stated by mambo4, as C# is enharmonically the same as Db, and Db has less flats than C# has sharps, we use Db instead much more often than not, as it easier to read five sharps instead of seven sharps.
Another way we can remember this (perhaps easier for some) is to simply think that we sharpen the dominant seventh of the scale relative to the root. So in the case of G major, F would be the seventh, so we sharpen it to make it a major seventh. D major you add the sharpened C, which is dominant seventh, to create a major seventh.
Cycle of Fourths
The Cycle of Fourths is similar to the Circle of Fifths, but as the name implies we ascend using intervals of fourths.
As we go up a fourth from C, the next key has an extra flat.
We have an order of keys for the cycle of fourths:
C F Bb Eb Ab Db Gb
Again, C major is an all natural key. So no flats used.
The next is of course F major. Using again (TTS, TTTS) we have:
F major: F G A Bb C D E
Bb major: Bb C D Eb F G A
Eb major: Eb F G Ab Bb C D
The same rule of going to the next key and adding a flat to the previous sequence applies.
From C major we have all natural notes, then up a fourth to F major, where we have one flat, and a fourth up is Bb with two flats, etc...
An easy way to work out the next flat to add, simply take a fourth from the root.
F major has Bb (an interval of a fourth from the root being F)
A fourth from the next key (Bb) is Eb, so the order is: Bb, Eb
A fourth from Eb is Ab, so the order is: Bb, Eb, Ab
Again, you can see a pattern emerging here.
Using both circle and cycle (or both circles, or both cycles - depends on what you call them, but despite the labelling the idea remains the same) gives you all 12 keys. Note that F# major is enharmonically the same as Gb major. F# major has six sharps whilst Gb major has six flats... so you could use either one really.
Determining Keys From the Key Signature
So... now we can figure out the key signature of any given key, what's next?
Well, you can now read a key signature and determine the key quickly from there.
How do we do this? Well, I'm glad you asked...
Go back to the top and read how we arrived at determining the sharps for the keys of G, D and A. They have one, two and three sharps respectively.
If we see a key signature of one sharp, this will always mean G major. There is no other possible key that uses the same sequence of notes, other than the diatonic modes associated with G major (this includes it's relative minor, E minor).
If we see a key signature of two sharps it will always be D major, for the same reasons.
If we see a key signature of three sharps, it will always be A major.
Make sense? The same applies for the flats we use.
Keys With Sharpened Notes
My grade 12 teacher had a great way of teaching us how to remember the order, as all sharps and/or all flats in a key signature in any given piece of music must follow the same order.
Remember this little phrase:
Father Charlie Goes Down And Ends Battle
So if we see a key signature with three sharps, we count three words along:
Father Charlie Goes
So our three sharps in order are: F#, C# and G#. Going a semtione up from the final sharp gives us our key: A major.
Got that? Ok then...
Keys With Flattened Notes
The same goes for flats, only we read the phrase backwards:
Battle Ends And Down Goes Charlie's Father
So a key signature with three flats, using the same rules as before, is written in this order: Bb, Eb and Ab. To determine what key we're in, it will be the second last flat. So here the second last flat is Eb. So we're in the key of Eb.
Make sense?
Order of Sharps and Flats on the Staff or Stave
One last thing...
Remember the phrase for sharps?
Father Charlie Goes Down And Ends Battle
The order for writing sharps is the same as the phrase, and the order for writing flats is the same as the phrase reversed.
Meaning you ALWAYS ALWAYS ALWAYS write a key signature, up to seven sharps, in this order:
F# C# G# D# A# E# B#
And same for flats, in reverse phrase order:
Battle Ends And Down Goes Charlie's Father
Bb Eb Ab Db Gb Cb Fb
So for writing the key signature for E major, we would write on the staff: F#, C#, G#, D#
Writing the key of Eb we would write: Bb Eb Ab
That's at least a week's worth of classroom theory right there...