A column is an attribute of a relation i.e. Table.Every column has a data type associated with it. Each record or row in a table will have information for every column of the table. The columns define the structure of a table. We will see how to add, delete or create a column in MySQL.
To add a column to an existing table, we do:
ALTER TABLE <table-name>
ADD COLUMN <column-name> <datatype><any-conditions>;
In the below example, we add a new column 'Notes' to the existing table 'Employees' using the below statement.
ALTER TABLE EMPLOYEES ADD COLUMN NOTES varchar(20);
By default, the columns are added as the last column, but we can specify if we want the column to be added as the first column, we specify the keyword First.
Example of modifying a column, we do:
ALTER TABLE EMPLOYEES MODIFY LAST_NAME VARCHAR(20) NULL;
We can modify:
USE
statement.For example, to drop a column, we do:
ALTER TABLE EMPLOYEES DROP COLUMN NOTES;