Sqlite preserves primary keys when copying/altering tables.

Backport #2312.
Fixes #9367.

I also added a test-case to make sure that renaming or removing
a column preserves the primary key.