SQL Alter Command

SQL Alter command can be applied on various levels of databases like the databases, the tables, and the columns. As the term says, it is used to make any alteration to the database system, and so it is considered to be one of the DML (Data Manipulation Language) commands. The most commonly used Alter operations are Add, Drop, Modify, change commands for adding, deleting, or changing the contents of the table or column.

Syntax

ALTER command can be used to add columns to an existing table, drop a column from a table, rename an existing column, and change the data type of a column. Below are the syntax used for the different operations which can be performed using Alter command.

The syntax for adding a new column to an existing table is as below:

ALTER TABLE table_name ADD COLUMN column_namedatatype;

The syntax for adding a column with a default value to an existing table is as below:

ALTER TABLE table_name ADD COLUMN column_namedatatype DEFAULT ‘value’;

The syntax for changing the data type of an existing column is as below:

ALTER TABLE table_name MODIFY column_namedatatype;

The syntax for renaming a column as below:

ALTER TABLE table_name RENAME old_column_nameTonew_column_name;

The syntax for dropping a column as below:

ALTER TABLE table_name DROP column_name;

The syntax for adding the NOT NULL constraint to a column as below:

ALTER TABLE table_name MODIFY column_namedatatype NOT NULL;

Advantages

Below are the advantages of using Alter command:

  • Alter command is very useful when we want to modify the database or tables contained in a database.
  • With the help of Alter command, many modifications can be made if any change in designing of the database is needed without disturbing the existing database or objects related to it.
  • We can add constraints to a column even after the table is created.


Copyright 1999- Ducat Creative, All rights reserved.

Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.