Quantcast
Channel: partitions – Cloud Data Architect
Viewing all articles
Browse latest Browse all 413

SQL ALTER TABLE Statement Example | How To Alter Table in SQL Tutorial

$
0
0

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.

  1. Add Column
  2. Modify Column
  3. Rename Column
  4. Drop Column
  5. Add Index
  6. Drop Index
  7. Add Constraint
  8. 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.

SQL ALTER TABLE Statement Example

 

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.

Adding a column in SQL

Dropping a column in SQL

See the following query.

Alter table Student drop column weight;

See the following output.

Dropping a column in SQL

 

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);

Changing the Datatype of a column

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 Column name in a table

 

Renaming a Table

See the following query of Renaming a table in SQL.

Alter table student rename to new_student;

See the following output.

Renaming a Table

MISCELLANEOUS OPERATIONS

See the following query.

ALTER TABLE student ADD  primary key(roll);

Output:

MISCELLANEOUS OPERATIONS

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.

For Dropping Primary key

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:

For Adding NOT NULL constraint

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:

For Adding Unique Key to a column.

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.


Viewing all articles
Browse latest Browse all 413

Trending Articles