Ads Here

Thursday 11 January 2018

Alter Table

Now, you have created a table with some columns and datatype. Now, suppose you want to made changes or want to make some modifications on table then you have the Alter Statement.
Some functions you can perform with Alter Statement are -
1) You can add new columns or fields to the table. 
2) You can modify datatype of a column.
3) You can delete a column from the table.
Now, for each operation we mentioned above we have some Alter Statements for each operation. Let's check out them one by one.
Add new column:
Now, you have created a table with some columns and now you want to add more columns to the table then you have to use this Alter Statement -
Syntax:

So, from the above syntax you can see first use the ALTER TABLE keyword. After that you have to place the table name you want to alter then you place ADD keyword and after this keyword you place all the new columns you want to add with there datatypes between two first brackets. You separate each column & datatype by coma symbol & at last you place a semicolon to finish the SQL statement. When you execute this SQL statement this will show you Table Altered, which means you have successfully changed the table.
Modify Column:

This SQL statement is used for change the datatype of a column. So, if you entered any wrong datatype in any column then you can change that by using this SQL Statement.
Delete Column:

So, as you can see when you execute this SQL statement it will drop means delete the column from your table which you want.
Now, let see by an example how to implement this SQL statements.
Example:
Now, in the previous post we have made a table Learn_World with some columns and some datatypes. Now, lets do some alter operations on that table. Now, first we add a new column DOJ(Date Of Joining) in the table.
So, as you can see in the above picture i have added a new column DOJ  in the Learn_World. For view the change in table you use the  DESC keyword.
So, you can see the DOJ column added successfully to the table. Now, we perform some modification operation on the table. Now suppose we want to chnage the datatype of the column DOJ  from Date to Character. So, lets check this -
So, now you can see previously the data type of  DOJ is Date. But, now it is changed to Character. So, using this SQL Statement you can change any column datatype.
Now, suppose i don't need the Fees column in the table. So, i want to drop or delete the column. So, i can do that in this way -
So, you can see that when we execute the alter delete statement it will delete the Fees column form the table. So, in this way you can delete any column you want.
So, this are the Alter Statements we can use on a table. If you have any questions or facing any problems then ask me in the comments section below, thank you.

No comments:

Post a Comment