Fundamentos de Bases de datos
C贸mo dise帽ar bases de datos y hacer queries efectivos
C贸mo identificar y organizar datos en bases de datos
Introducci贸n pr谩ctica a SQL y bases de datos relacionales
Trabajo Directo con MySQL: Cliente, Servidor y Manejo de Errores
Instalaci贸n de MySQL en varios sistemas operativos
Quiz: Fundamentos de Bases de datos
Introducci贸n a Bases de Datos Relacionales
Conceptos b谩sicos de bases de datos: columnas, tuplas y relaciones
Creaci贸n de Tablas en MySQL con Tipos de Datos Esenciales
Creaci贸n de tablas en MySQL con Primary Key y Timestamp
Normalizaci贸n y relaciones en bases de datos relacionales
Creaci贸n y visualizaci贸n de bases de datos con DBML y DBDiagram
Quiz: Introducci贸n a Bases de Datos Relacionales
Manipulaci贸n de Datos
C贸mo crear tablas y utilizar 'Foreign Keys' en MySQL
Crear tablas y establecer llaves for谩neas en SQL
Tipos de tablas en una base de datos relacional
C贸mo Modificar Tablas en SQL con ALTER TABLE
C贸mo Insertar Datos y Manejar Errores en MySQL
Creaci贸n Avanzada de una Tabla Products en MySQL
Uso pr谩ctico del WHERE en SQL para filtrar datos con precisi贸n
Gu铆a pr谩ctica para modificar datos usando UPDATE en SQL
C贸mo eliminar datos en SQL: m茅todos l贸gicos y f铆sicos
Uso Avanzado del Comando SELECT en SQL
Quiz: Manipulaci贸n de Datos
Agrupaci贸n de Datos
Funciones agregadoras en MySQL para an谩lisis eficiente de datos
Insertar datos desde otra tabla en MySQL con Insert Into Select
C贸mo utilizar Left Join en bases de datos relacionales
C贸mo consultar y relacionar tablas en MySQL
You don't have access to this class
Keep learning! Join and start boosting your career
Do you know what to do when you need to add, modify or delete a column in an existing SQL table? The ALTER TABLE command is an essential SQL tool that allows you to make these adjustments safely and clearly. Learn how to use add column
, modify column
, drop column
, rename column
and even rename an entire table.
To add a new column in a SQL table, we use the basic syntax:
ALTER TABLE tableName ADD COLUMN columnName dataType;
For example, to add a price
column:
ALTER TABLE test ADD COLUMN price FLOAT;
You can place your new column at the beginning using FIRST
, or after a specific column with AFTER
:
ALTER TABLE test ADD COLUMN price FLOAT FIRST;ALTER TABLE test ADD COLUMN price FLOAT AFTER quantity;
If you need to remove a column completely:
ALTER TABLE test DROP COLUMN price;
It is important to check well before executing this command, as you will lose all the data in that column.
When you need to change the data type or properties of a column:
ALTER TABLE test MODIFY COLUMN price DECIMAL(10,3) NOT NULL;
In this case, the price
column will change from FLOAT
to DECIMAL
with 10 digits in total; 3 of them after the decimal point.
Changes to columns with existing information should be made with care. Make sure that the new data type is compatible with previously stored data.
Useful recommendations:
Renaming an existing column is accomplished with:
ALTER TABLE test RENAME COLUMN price TO prices;
Renaming the entire table is also easy:
ALTER TABLE test RENAME TO tests;
These changes do not affect the internal data, only the name of the structure.
If you are looking to modify certain attributes, such as a default value, use modify column
again keeping the parameters you do not want to change and including your additions:
ALTER TABLE tests MODIFY COLUMN prices DECIMAL(10,3) NOT NULL DEFAULT 10;
If you want to remove the default value, make sure the settings are congruent:
ALTER TABLE tests MODIFY COLUMN prices DECIMAL(10,3) DEFAULT NULL;
These examples will help you to keep your database tidy and clear. Share your experience or doubts in the comments!
Contributions 1
Questions 0
Want to see more contributions, questions and answers from the community?