Use of Alter Keyword
Syntax :
Alter Table Table_name
ADD column_name datatype
Example : Consider the following table
Table: Salary
Emp_id | Emp_name | Emp_reg | Emp_salary |
01 | chandan | E_01 | 25000 |
02 | adhiraj | E_02 | 30000 |
03 | raghu | E_03 | 35000 |
04 | raghunath | E_04 | 36000 |
05 | Alax | E_05 | 40000 |
and it is required to add new column(city) in table then we will use alter keyword
Query :
Alter table Salary
ADD column city varchar(50)
Result:
Emp_id | Emp_name | Emp_reg | Emp_salary | city |
01 | chandan | E_01 | 25000 | Null |
02 | adhiraj | E_02 | 30000 | Null |
03 | raghu | E_03 | 35000 | Null |
04 | raghunath | E_04 | 36000 | Null |
05 | Alax | E_05 | 40000 | Null |
Query to update column datatype :
In Mysql :
ALTER TABLE table_name
MODIFY COLUMN column_name datatype
In sql :
ALTER TABLE table_name
ALTER COLUMN column_name datatype
Query to drop a column in Table
Syntax :
ALTER TABLE tablename
DROP COLUMN columnname
Example :
ALTER TABLE tablename
DROP COLUMN city
Now column city will be droped from table salary
Tag :Alter keyword with example, use of alter keyword in SQL
No comments:
Post a Comment