Most of the time I use the US qwerty layout but from time to time I also have to use the German layout which is qwertz and it is very annoying.
How can I swap the Y and Z keys on the German layout?
The solution was at this link: Custom Keyboard Layout in Ubuntu (or just Linux :) kindly provided by gertvdijk in his answer.
NB: In recent versions of Ubuntu you have to issue sudo dpkg-reconfigure xkb-data
for the changes to take effect. See this answer.