C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
SQL ALTER TABLEThe ALTER TABLE statement in Structured Query Language allows you to add, modify, and delete columns of an existing table. This statement also allows database users to add and remove various SQL constraints on the existing tables. Any user can also change the name of the table using this statement. ALTER TABLE ADD Column statement in SQLIn many situations, you may require to add the columns in the existing table. Instead of creating a whole table or database again you can easily add single and multiple columns using the ADD keyword. Syntax of ALTER TABLE ADD Column statement in SQLALTER TABLE table_name ADD column_name column-definition; The above syntax only allows you to add a single column to the existing table. If you want to add more than one column to the table in a single SQL statement, then use the following syntax: ALTER TABLE table_name ADD (column_Name1 column-definition, column_Name2 column-definition, ..... column_NameN column-definition); Examples of ALTER TABLE ADD Column statement in SQLHere, we have taken the following two different SQL examples, which will help you how to add the single and multiple columns in the existing table using ALTER TABLE statement: Example 1: Let's take an example of a table named Cars:
Table: Cars
ALTER TABLE Cars ADD Car_Model Varchar(20); This statement will add the Car_Model column to the Cars table. Example 2: Let's take an example of a table named Employee:
Table: Employee
ALTER TABLE Employee ADD ( Emp_ContactNo. Number(13), Emp_EmailID varchar(50) ; This statement will add Emp_ContactNo. and Emp_EmailID columns to the Employee table. ALTER TABLE MODIFY Column statement in SQLThe MODIFY keyword is used for changing the column definition of the existing table. Syntax of ALTER TABLE MODIFY Column statement in SQLALTER TABLE table_name MODIFY column_name column-definition; This syntax only allows you to modify a single column of the existing table. If you want to modify more than one column of the table in a single SQL statement, then use the following syntax: ALTER TABLE table_name MODIFY (column_Name1 column-definition, column_Name2 column-definition, ..... column_NameN column-definition); Examples of ALTER TABLE MODIFY Column statement in SQLHere, we have taken the following two different SQL examples, which will help you how to modify single and multiple columns of the existing table using ALTER TABLE statement: Example 1: Let's take an example of a table named Cars:
Table: Cars
ALTER TABLE Cars ADD Car_Color Varchar(50); Example 2: Let's take an example of a table named Employee:
Table: Employee
ALTER TABLE Employee ADD ( Emp_ContactNo. Int, Emp_EmailID varchar(80) ; ALTER TABLE DROP Column statement in SQLIn many situations, you may require to delete the columns from the existing table. Instead of deleting the whole table or database you can use DROP keyword for deleting the columns. Syntax of ALTER TABLE DROP Column statement in SQLALTER TABLE table_name DROP Column column_name ; Examples of ALTER TABLE DROP Column statement in SQLHere, we have taken the following two different SQL examples, which will help you how to delete a column from the existing table using ALTER TABLE statement: Example 1: Let's take an example of a table named Cars:
Table: Cars
ALTER TABLE Cars DROP COLUMN Car_Color ;
SELECT * FROM Cars;
Table: Cars Example 2: Let's take an example of a table named Employee:
Table: Employee
ALTER TABLE Cars DROP COLUMN Emp_Salary ; ALTER TABLE Cars DROP COLUMN Emp_City ; ALTER TABLE RENAME Column statement in SQLThe RENAME keyword is used for changing the name of columns or fields of the existing table. Syntax of ALTER TABLE RENAME Column statement in SQLALTER TABLE table_name RENAME COLUMN old_name to new_name; Examples of ALTER TABLE RENAME Column statement in SQLHere, we have taken the following two different SQL examples, which will help you how to change the name of a column of the existing table using ALTER TABLE statement: Example 1: Let's take an example of a table named Cars:
Table: Cars
ALTER TABLE Cars RENAME COLUMN Car_Color to Colors; This statement will change the name of a column of the Cars table. To see the changes, you have to type the following query: SELECT * FROM Cars;
Table: Cars Example 2: Let's take an example of a table named Employee:
Table: Employee
ALTER TABLE Employee RENAME COLUMN Emp_City to Emp_Address; This statement will change the name of a column of the Employee table. To see the changes, you have to type the following query: SELECT * FROM Employee;
Table: Employee
Next TopicSQL SELECT
|