SET @preparedStatement = (SELECT IF( NOT EXISTS( SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = 'Reactions' AND table_schema = DATABASE() AND column_name = 'ChannelId' ), 'ALTER TABLE Reactions ADD COLUMN ChannelId varchar(26) NOT NULL DEFAULT "";', 'SELECT 1;' )); PREPARE addColumnIfNotExists FROM @preparedStatement; EXECUTE addColumnIfNotExists; DEALLOCATE PREPARE addColumnIfNotExists; UPDATE Reactions SET ChannelId = COALESCE((select ChannelId from Posts where Posts.Id = Reactions.PostId), '') WHERE ChannelId=""; SET @preparedStatement = (SELECT IF( ( SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS WHERE table_name = 'Reactions' AND table_schema = DATABASE() AND index_name = 'idx_reactions_channel_id' ) > 0, 'SELECT 1', 'CREATE INDEX idx_reactions_channel_id ON Reactions(ChannelId);' )); PREPARE createIndexIfNotExists FROM @preparedStatement; EXECUTE createIndexIfNotExists; DEALLOCATE PREPARE createIndexIfNotExists;