Which one of the following commands is used to modify a column inside a table?

The SQL ALTER TABLE command is used to change the structure of an existing table. It helps to add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself.

It can also be used to change the comment for the table and type of the table.

Syntax:

ALTER TABLE (
ADD column1    data_type[(size)],
ADD column2    data_type[(size)],
...);

Parameters:

NameDescriptiontable_nameName of the table where data is stored.column1,column2Name of the columns of a table.data_typeChar, varchar, integer, decimal, date and more.sizeMaximum length of the column of a table.

Contents:

SQL ALTER TABLE statement to add a column to a table

In the following topic, we are discussing the SQL ALTER TABLE statement, which adds a column to a table. If not specified otherwise, the column will be added at the end of the table.

Sample table: agent1

To add a new column 'email' at the end of the table 'agent1' with field name and data type

Field NameData TypeSizeDecimal PlacesNULLConstrainte-mailchar25 No 

the following SQL statement can be used :

SQL Code:

ALTER TABLE agent1 ADD email char(25);

Output:

Which one of the following commands is used to modify a column inside a table?

To see the modified structure of the said table:

SQL Code:

DESCRIBE agent1;

Structure

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to drop a column

In the following example, we are discussing, how a column can be dropped from a table if it exists in the table, using the SQL ALTER TABLE statement.

Sample table: agent1


To drop the existing column 'country' from the table 'agent1', the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 DROP(country); 

Output:

Which one of the following commands is used to modify a column inside a table?

To see the modified structure of the said table:

SQL Code:

DESCRIBE agent1;

Structure

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to change or drop default value of a column

In the following example, we are going to discuss how to drop the default value of a column from a table if the column exists in the table using SQL ALTER TABLE statement.

Example:

Sample table: agent1


To modify the existing column 'commission' of 'agent1' table with a default value .05,

the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 
MODIFY commission DEFAULT .05; 

Output:

Which one of the following commands is used to modify a column inside a table?

To see the modified structure of the said table :

SQL Code:

DESCRIBE agent1;

Structure

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to drop default value of a column

To drop the existing default value of column 'commission' of 'agent1' table, the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1
MODIFY commission NUMBER;

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to add individual column constraint

In the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add a constraint for a column and also drop an existing constraint of a column from a table if the column exists in the table.

Sample table: agent1


To add a UNIQUE CONSTRAINT named 'dup_che_con' for the the existing column 'agent_code' of 'agent1' table,

the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 ADD CONSTRAINT
dup_che_con UNIQUE(agent_code);

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to drop individual column constraint

To drop the existing UNIQUE CONSTRAINT 'dup_che_con' from the table 'agent1',

the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1
DROP CONSTRAINT dup_che_con;

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to change size and data type of a column

In the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to change the size and data type of a column in an existing table, if the column exists in the table.

Example:

Sample table: agent1


To modify the datatype and size of the column 'country' of 'agent1' table, the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 ADD email char(25);
0

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to add or drop PRIMARY KEY of a table

In the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add and drop primary key of a table.

Sample table: agent1


To add a PRIMARY KEY CONSTRAINT named 'pk_ag_code' for the column 'agent_code' of the 'agent1' table, the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 ADD email char(25);
1

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to drop existing PRIMARY KEY of a table

To drop the existing PRIMARY KEY CONSTRAINT named 'pk_ag_code' form the 'agent1' table, the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 ADD email char(25);
2

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to add or drop FOREIGN KEY of a table

In the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add and drop foreign key of a table.

Sample table: customer1


To add a FOREIGN KEY CONSTRAINT named 'fk_ag_code' for the column 'agent_code' of the 'customer1' table, the following SQL statement can be used:

SQL Code:

ALTER TABLE agent1 ADD email char(25);
3

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to drop existing FOREIGN KEY of a table

To drop the existing FOREIGN KEY CONSTRAINT named 'fk_ag_code' form the 'customer1' table, the following SQL statement can be used :

SQL Code:

ALTER TABLE agent1 ADD email char(25);
4

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to add CHECK CONSTRAINT

In the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to add and drop CHECK CONSTRAINT of column(s) of a table.

Sample table : customer1


To add a CHECK CONSTRAINT named 'du_che_con' for the column 'grade' of the 'customer1' table, which checks whether the values of 'grade' are within the range 1 to 3 at the time of inserting rows into the table, the following SQL statement can be used :

SQL Code:

ALTER TABLE agent1 ADD email char(25);
5

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to drop CHECK CONSTRAINT

To drop the existing CHECK CONSTRAINT named 'du_che_con' form the 'customer1' table, the following SQL statement can be used :

SQL Code:

ALTER TABLE agent1 ADD email char(25);
6

Output:

Which one of the following commands is used to modify a column inside a table?

SQL ALTER TABLE statement to change PRIMARY KEY CONSTRAINT

In the following example, we are going to discuss about the usage of SQL ALTER TABLE statement to modify the PRIMARY KEY and FOREIGN KEY constraint.

To modify a PRIMARY KEY and FOREIGN KEY constraint, firstly it is needed to remove the existing PRIMARY KEY and FOREIGN KEY constraint and then re-create it.

Sample table : agent1


Suppose there is a PRIMARY KEY CONSTRAINT named 'pk_ag_code' for the column 'agent_code' of the 'agent1' table.

To modify the PRIMARY KEY CONSTRAINT named 'pk_ag_code, the following SQL statements can be used :

SQL Code:

ALTER TABLE agent1 ADD email char(25);
2

SQL Code:

ALTER TABLE agent1 ADD email char(25);
1

SQL ALTER TABLE statement to change FOREIGN KEY CONSTRAINT

Sample table: customer1


Suppose, there is a FOREIGN KEY CONSTRAINT named 'fk_ag_code' for the column 'agent_code' of the 'customer1' table

Which one of the following command is used to modify a column inside a table using DDL command?

ALTER : This command is used to add, delete or change columns in the existing table.

Which one of the following commands is used to modify a column inside a table Option 1 Update Option 2 set Option 3 Drop Option 4 ALTER?

Explanation: The modify command is used to change one or more columns in the existing table.

Which command is used to modify column name or table structure?

The SQL ALTER TABLE command is used to change the structure of an existing table. It helps to add or delete columns, create or destroy indexes, change the type of existing columns, or rename columns or the table itself.

Which of these commands can be used to modify size of column in a table?

We can modify a column size with the help of ALTER command.