`MODIFY COLUMN` vs `CHANGE COLUMN`

CHANGE COLUMN If you have already created your MySQL database, and decide after the fact that one of your columns is named incorrectly, you don’t need to remove it and make a replacement, you can simply rename it using change column. ALTER TABLE MyTable CHANGE COLUMN foo bar VARCHAR(32) NOT NULL FIRST; MODIFY COLUMN This … Read more

Alter table if exists or create if doesn’t

MySQL INFORMATION_SCHEMA database to the rescue: — First check if the table exists IF EXISTS(SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema=”db_name” AND table_name LIKE ‘wild’) — If exists, retreive columns information from that table THEN SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name=”tbl_name” AND table_schema=”db_name”; — do some action, i.e. ALTER TABLE if some columns … Read more