SQL: ALTER Command - Modifying Table Structures, Assignments of Database Programming

The SQL ALTER command is utilized for modifying table structures, including adding or renaming columns, changing column datatypes, and dropping columns. syntax and examples for various ALTER command uses, such as adding a single column, adding multiple columns, adding a column with a default value, modifying an existing column, and renaming or dropping columns.

Typology: Assignments

2021/2022

Uploaded on 03/30/2022

asimahsan45
asimahsan45 🇵🇰

5

(1)

40 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
SQL:ALTERcommand
altercommand is used for altering the table structure, such
as,
to add a column to existing table
to rename any existing column
to change datatype of any column or to modify its size.
to drop a column from the table.
ALTERCommand: Add a new Column
UsingALTERcommand we can add a column to any existing
table. Following is the syntax,
ALTER TABLE table_name ADD(
column_name datatype);
Here is an Example for this,
ALTER TABLE student ADD(
address VARCHAR(200)
);
The above command will add a new columnaddressto the
tablestudent, which will hold data of typevarcharwhich is
nothing but string, of length 200.
pf3
pf4
pf5

Partial preview of the text

Download SQL: ALTER Command - Modifying Table Structures and more Assignments Database Programming in PDF only on Docsity!

SQL: ALTER command

alter command is used for altering the table structure, such as,  to add a column to existing table  to rename any existing column  to change datatype of any column or to modify its size.  to drop a column from the table.

ALTER Command: Add a new Column

Using ALTER command we can add a column to any existing table. Following is the syntax, ALTER TABLE table_name ADD( column_name datatype); Here is an Example for this, ALTER TABLE student ADD( address VARCHAR( 200 ) ); The above command will add a new column address to the table student , which will hold data of type varchar which is nothing but string, of length 200.

ALTER Command: Add multiple new Columns

Using ALTER command we can even add multiple new columns to any existing table. Following is the syntax, ALTER TABLE table_name ADD( column_name1 datatype1, column-name2 datatype2, column-name3 datatype3); Here is an Example for this, ALTER TABLE student ADD( father_name VARCHAR( 60 ), mother_name VARCHAR( 60 ), dob DATE); The above command will add three new columns to the student table

ALTER Command: Add Column with default value

Here is an Example for this, ALTER TABLE student MODIFY( address varchar( 300 )); Remember we added a new column address in the beginning? The above command will modify the address column of the student table, to now hold upto 300 characters.

ALTER Command: Rename a Column

Using ALTER command you can rename an existing column. Following is the syntax, ALTER TABLE table_name RENAME old_column_name TO new_column_name; Here is an example for this, ALTER TABLE student RENAME address TO location; The above command will rename address column to location.

ALTER Command: Drop a Column

ALTER command can also be used to drop or remove columns. Following is the syntax, ALTER TABLE table_name DROP( column_name); Here is an example for this, ALTER TABLE student DROP( address); The above command will drop the address column from the table student.