Add Column Table if not exists


/ Published in: MySQL
Save to your folder(s)

Can add columns to a table checking before if there


Copy this code and paste it in your HTML
  1. delimiter '//'
  2.  
  3. IF NOT EXISTS(
  4. SELECT * FROM information_schema.COLUMNS
  5. WHERE COLUMN_NAME='new_column' AND TABLE_NAME='usuarios' AND TABLE_SCHEMA='the_schema'
  6. )
  7. ALTER TABLE `the_schema`.`the_table`
  8. ADD COLUMN `new_column` bigint(20) unsigned NOT NULL default 1;
  9.  
  10. //
  11.  
  12. delimiter ';'
  13.  
  14. CALL addcol();
  15.  
  16. DROP PROCEDURE addcol;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.