Table Columns MySQL Essential Training

Table Columns MySQL Essential Training

MYSQL

Table Columns MySQL Essential Training




In our previous lesson we’ve learned about what are the various SQL Commands are available and their functionalities. In this Table Columns MySQL Essential Training lesson we will see as mentioned in earlier lesson, Columns used in the tables. The column only gives the basic structure of the tables.
The columns are created when the table is created. So if a small change in a single column can affect the entire table. So whenever we wants to modify a columns, its better to make a backup of the table. Data backup are explained in the upcoming lessons.
By altering or modify a column we can add a column, delete a column, modify a particular table column. Here we can see all the functionalities as one by one. Now we add a column inside the table named emp_details. This is a table we used in our previous examples.
To do this open your shell or MySQL Command Line Interface. You can find it how to do that in our previous lesson.
ADD A COLUMN
Syntax: ALTER TABLE table_name
ADD COLUMN [column_definition];
// table_name is we’ve to give our table name we want to add a column
Lets see this in real time example,

Before entering the alter command in the table lets see the exist structure of the table using desc commands.
Table Columns MySQL Essential Training
In this image it shows only 5 rows are set in the table. And now we are ready to insert another column inside the table.




Table Columns MySQL Essential Training
// In this Table Columns MySQL Essential Training we alter the table named emp_details, by add a column named salary. Its data type is integer and the storing size is 10 which is also a NOT NULL value. Finish this command and hit enter and it adds another column named salary. Cross check it by desc table_name command.
Table Columns MySQL Essential Training
This is the way how we add the column in the already exist tables. Like add a column, we can also change the column name we already have in our tables. In Table Columns MySQL Essential Training we do like we did for add a column with little differences.
CHANGE A COLUMN
Syntax: ALTER TABLE table_name
CHANGE COLUMN column_name[column_definition];
// table_name is we’ve to give our table name we want to add a column
Here we change the column name from salary to category. And here we can also increase the width of the column. Lets see how to do that.

Table Columns MySQL Essential Training
In the above example we change the column name variable type and its width to another values. This is the way we can change the column name using the alter command in CLI interface.
DELETE A COLUMN
We can also delete a particular column from the entire table permanently. And remember we can’t revoke the deleted column and its relevant row data are cannot be recovered in any case. So be aware about that in this case. Lets see how to delete a column in the real time example.
Syntax: ALTER TABLE table_name
DROP COLUMN column_name;
// table_name is we’ve to give our table name we want to delete a column
In this Table Columns MySQL Essential Training lesson here is the solution to delete a column from the table. And see it in example,

Table Columns MySQL Essential Training
These are the various commands we can use over the table to change the column details. In future we can also discuss some other commands too.




Leave a Reply