Covalent bond- The bond is formed by the sharing of electrons said to be a covalent bond, and the electronegativity difference between the atom should be low.
Ionic bond- Ionic bond is a type of electrostatic force of attraction due to the positive charge and negative charge atom. The electronegativity difference between the bonding atom should be high for the ionic bond formation.
In the electronegativity difference between magnesium and oxygen is high as magnesium is converted in and oxygen is converted in . So they form ionic compounds while interacting.