software:mysql:altering_tables
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | software:mysql:altering_tables [2020/09/25 19:56] (current) – created rodolico | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Altering Tables ====== | ||
+ | ===== Adding a column ===== | ||
+ | |||
+ | To add a column **if it does not exist** you can do a Prepare/ | ||
+ | <code sql> | ||
+ | SET @dbname = DATABASE(); | ||
+ | SET @tablename = " | ||
+ | SET @columnname = " | ||
+ | SET @preparedStatement = (SELECT IF( | ||
+ | ( | ||
+ | SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS | ||
+ | WHERE | ||
+ | (table_name = @tablename) | ||
+ | AND (table_schema = @dbname) | ||
+ | AND (column_name = @columnname) | ||
+ | ) > 0, | ||
+ | " | ||
+ | CONCAT(" | ||
+ | )); | ||
+ | PREPARE alterIfNotExists FROM @preparedStatement; | ||
+ | EXECUTE alterIfNotExists; | ||
+ | DEALLOCATE PREPARE alterIfNotExists; | ||
+ | </ | ||
+ | |||
+ | Other ways of doing this are available in the discussion at [[https:// | ||
+ | |||
+ | ===== Links ===== | ||
+ | * [[https:// |
software/mysql/altering_tables.txt · Last modified: 2020/09/25 19:56 by rodolico