Feed: Planet MySQL
;
Author: Krunal Lathiya
;
SQL ALTER TABLE Statement Example | How To Alter Table in SQL Tutorial is today’s topic. SQL ALTER TABLE statement is used to add, modify, or delete columns in the existing table. The ALTER TABLE query is also used to add and drop various constraints on a current table. ALTER TABLE statement in SQL is used to add, modify, or drop columns in a table. You can also use an ALTER TABLE command to add and drop various constraints on an existing table.
Content Overview
SQL ALTER TABLE Statement Example
Once a table is created in the database, there are many occasions where one may wish to change the structure of the table. ALTER TABLE modifies the table definition by altering, adding, or dropping columns and constraints. ALTER TABLE also reassigns and rebuilds the partitions, or disables and enables the constraints and triggers.
The ALTER specification is dependent on the type of alteration we wish to perform. We list a number of common changes below.
- Add Column
- Modify Column
- Rename Column
- Drop Column
- Add Index
- Drop Index
- Add Constraint
- Drop Constraint
We will see some of the above common alterations.
Syntaxes
Adding a column in a table.
ALTER TABLE table_name ADD column_name datatype;
Dropping column in a table.
ALTER TABLE table_name DROP column column_name;
To change Data type of a column name.
ALTER TABLE table_name MODIFY column column_name datatype;
Renaming a Column name in a table.
ALTER TABLE table_name rename column old_column to new_column;
Renaming a Table.
ALTER TABLE table_name rename To new_table_name;
Let’s clear this with an example.
Consider the STUDENT table. You can also create your table. After creating a table, insert rows with proper values.
Adding a column in SQL
See the following query.
Alter table Student add weight integer; Alter table Student add Course varchar(60);
Now, see the output.
Dropping a column in SQL
See the following query.
Alter table Student drop column weight;
See the following output.
Changing the Datatype of a column
See the following query of changing the datatype of a column.
Alter table Student MODIFY column course varchar(20);
Renaming a Column name in a table
See the following query of renaming the column of a table.
Alter table student rename column name to first_name;
See the below output.
Renaming a Table
See the following query of Renaming a table in SQL.
Alter table student rename to new_student;
See the following output.
MISCELLANEOUS OPERATIONS
See the following query.
ALTER TABLE student ADD primary key(roll);
Output:
Explanation:
Here, you can see that in key column roll has been assigned as a primary key.
For Dropping Primary key
ALTER TABLE student DROP PRIMARY KEY;
See the following output.
Explanation:
Previously, in key column roll was assigned as primary key now it has been removed.
For Adding NOT NULL constraint.
ALTER TABLE student MODIFY NAME varchar(255) NOT NULL;
Output:
Explanation
Here, you can see that in the Null column roll has been assigned as NO.
Adding Unique Key to a column.
ALTER TABLE student add UNIQUE(ROLL);
Output:
Explanation:
Here, you can see in the key column roll has been assigned as a primary key.
Finally, SQL ALTER TABLE Statement Example | How To Alter Table in SQL Tutorial is over.