When the key changes in a middle of a piece, this is called a modulation. Although changing from any of the 24 keys (12 major and 12 minor) to any other is a modulation, there are certain key changes that are far more common than others. The circle of fifths can help us understand which keys are closely related and therefore which are most likely to modulate to each other.
Put simply, keys that are close to each other on the circle of fifths are closely related. It is very common, both in art music and in popular music, to hear modulations between relative major and minor keys, for example. Another common modulation is moving from one key to the next going clockwise around the circle of fifths. Modulating from the key of D to the key of A would be an example of this. Counter-clockwise modulation (from the key of A to the key of D, for example) also occurs, but is less common and more surprising.
Even more surprising are modulations to distant keys. Keys that are not near each other on the circle of fifths share fewer pitches and chords, which makes it difficult for composers to create convincing and elegant modulations between them. Some styles emphasize this challenging maneuver, while others rarely modulate to distant keys.
Enharmonics and Note Spelling Within a Key
The collection of notes defined by a key signature includes only one pitch for each letter (A, B, C, D, E, F, G). These pitches may be the natural version of the note (A), the sharp version (A-sharp), or the flat version (A-flat), but there will never be more than one version in a given collection. That is, there is no key that includes both A and A-sharp - there is only one version of A in the collection.
However, B-flat, the enharmonic equivalent of A-sharp, does exist in two major keys along with A: the key of F and the key of B-flat. Even though they are enharmonically equivalent, A-sharp should not be used as a substitute for B-flat because it creates confusion by putting two versions of A in the collection of pitches. Therefore, when writing in the key of F or B-flat, it is necessary to use B-flat instead of A-sharp, since B-flat exists in the collection and A-sharp does not.
We can see these encharmonic principles most easily when a collection of pitches is organized into a scale. Here are all 12 major scales:
Here are all 12 natural minor scales:
Order of Sharps and Flats
When sharps or flats are added to a key signature, they are added in a specific order from left to right. If we take the order of flats, reverse them, and switch 'flat' to 'sharp', we get the order of sharps.
Every time a sharp or flat is added to a key signature, it changes the order of whole and half steps in the scale derived from the collection of pitches. Adding them in the order above ensures that the pattern used in major and natural minor scales (W-W-h-W-W-W-h) is always present. To illustrate this point, take the C major scale:
If we add an F-sharp , we change the order of whole and half steps like this:
The pattern is still the same because we have added the correct sharp (F-sharp is the first in the order of sharps), but the order of whole and half steps does not yet make a major or minor scale. So, we need to start on a different note to get the correct order for a major or minor scale. The new starting point becomes the new tonic pitch, which shares its name with both the scale and key that have been created by adding the new accidental. For example, if G becomes the new tonic, we are now doing the key of G major, which uses the G major scale. The other option is to make E the new tonic, which would make it an E minor scale, which is used in the key of E minor.
The use of sharps and flats together in key signatures is avoided because it too could disrupt this pattern. There is no starting point in this example that would give us a major or natural minor scale: