All the compounds are crystalline solids and their solubility in water is guided by both lattice enthalpy and hydration enthalpy. The magnitude of lattice enthalpy is quite small in case of sodium and potassium compounds, hence they are readily dissolved in water, when compared to magnesium and calcium compounds.
However, in case of corresponding magnesium and calcium compounds, the cations have smaller sizes and more magnitude of positive charge. This means that their lattice enthalpies are more, when compared to the sodium and potassium compounds.
Therefore, the hydroxi des and carbonates of these metals are only sparingly soluble in water.