Agregar una nueva columna SQL con un valor predeterminado


Estoy buscando la sintaxis para agregar una columna a una base de datos MySQL con un valor predeterminado de 0

Referencia

 179
Author: Jess Stone, 2010-08-25

10 answers

Prueba esto:

ALTER TABLE table1 ADD COLUMN foo INT DEFAULT 0;

De la documentación a la que ha vinculado:

ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
   alter_specification [, alter_specification] ...

alter_specification:
    ...
    ADD [COLUMN] (col_name column_definition,...)
    ...

Para encontrar la sintaxis de column_definition busque un poco más abajo en la página:

Las cláusulas Column_definition usan la misma sintaxis para AGREGAR y CAMBIAR que para CREAR TABLA. Consulte Sección 12.1.17, "CREAR sintaxis DE TABLA".

Y desde la página enlazada:

column_definition:  
   data_type [NOT NULL | NULL] [DEFAULT default_value]
   [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]  
   [COMMENT 'string']  
   [COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]  
   [STORAGE {DISK|MEMORY|DEFAULT}]  
   [reference_definition]  

Observe la palabra DEFAULT allí.

 314
Author: Mark Byers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-25 19:18:42

¿Así?

ALTER TABLE `tablename` ADD `new_col_name` INT NOT NULL DEFAULT 0;
 29
Author: Lekensteyn,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-25 19:11:15

Simplemente agregue default 0 al final de su declaración ALTER TABLE <table> ADD COLUMN <column> <type>

 3
Author: Eton B.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-25 19:11:28

Usuarios de la tabla (user_id int unsigned PK, username varchar(32))

alter table users add column verified tinyint unsigned default 0
 2
Author: Jon Black,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-08-25 19:10:12

Puedes probar esto,

ALTER TABLE table_name ADD column_name INT DEFAULT 0;
 2
Author: BIBIN K ONANKUNJU,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-19 05:59:08

Esto funcionará para el tipo de enumeración como valor predeterminado

ALTER TABLE engagete_st.holidays add column `STATUS` ENUM('A', 'D') default 'A' AFTER `H_TYPE`;
 1
Author: vpgodara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-23 11:42:57

Si está aprendiendo, es útil usar una GUI como SQLyog, realice los cambios utilizando el programa y luego vea la pestaña Historial para las instrucciones DDL que hicieron esos cambios.

 0
Author: Max Toro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-31 23:42:15

Prueba Esto :)

ALTER TABLE TABLE_NAME ADD COLUMN_NAME INT NOT NULL DEFAULT 0;
 0
Author: MAnoj Sarnaik,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-08 09:36:56

ALTER TABLE my_table ADD COLUMN new_field TinyInt (1) DEFAULT 0;

 0
Author: sandeep kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-10 08:23:28

Otra palabra clave útil es PRIMERO y DESPUÉS si desea agregarla en un lugar específico de su tabla.

ALTER TABLE `table1` ADD COLUMN `foo` AFTER `bar` INT DEFAULT 0;
 0
Author: Roman Rabinovich,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-23 16:25:36