All Exams Test series for 1 year @ ₹349 only
Question

In SQL, which of the following command is used to modify a column in a table?

The correct answer is

Alter

Understanding SQL Commands for Table Modification

The question asks to identify the correct SQL command used specifically for modifying a column within an existing table. Modifying a column means changing its definition, such as its data type, size, constraints, or name.

Let's examine the provided options and their typical uses in SQL:

  • Create: The CREATE command is part of the Data Definition Language (DDL) and is used to build new database objects, such as databases, tables, views, indexes, etc. It is not used to modify existing objects.
  • Set: The SET clause is commonly used in conjunction with the UPDATE command (part of the Data Manipulation Language - DML) to assign new values to columns within specific rows of a table. It modifies the data stored in columns, not the column definition itself.
  • Update: The UPDATE command (DML) is used to change existing records in a table. It modifies the data values in one or more columns for one or more rows based on a specified condition. It does not modify the structure or definition of the table or its columns.
  • Alter: The ALTER command is part of the Data Definition Language (DDL) and is used to modify the structure of an existing database object. Specifically, ALTER TABLE is used to add, delete, or modify columns in an existing table, add or drop constraints, and perform other structural changes. Modifying a column (e.g., changing its data type or size) is a common use case for the ALTER TABLE command.

Based on the functions of these commands, the command used to modify a column in a table is ALTER, specifically as part of the ALTER TABLE statement.

For example, to change the data type of a column named 'email' in a table named 'customers', you might use a command like:

ALTER TABLE customers
ALTER COLUMN email VARCHAR(255);

Or, to add a constraint:

ALTER TABLE customers
ADD UNIQUE (email);

Revision Table: SQL Command Functions

SQL Command Purpose Language Type Used for Modifying a Column?
CREATE Create new database objects DDL No
SET Assign values in UPDATE statements DML (used within DML) No (modifies data, not definition)
UPDATE Modify existing data in rows DML No (modifies data, not definition)
ALTER Modify existing database objects (structure) DDL Yes

Additional Information: ALTER TABLE Syntax

The specific syntax for modifying a column using ALTER TABLE can vary slightly depending on the database system (e.g., MySQL, PostgreSQL, SQL Server, Oracle). However, the general principle involves specifying the table name and then an action like ALTER COLUMN (or MODIFY COLUMN in some systems like MySQL) followed by the column name and its new definition.

Key actions performed using ALTER TABLE related to columns include:

  • Adding a new column.
  • Dropping an existing column.
  • Modifying the data type of a column.
  • Modifying the size or precision of a column.
  • Adding, dropping, or modifying constraints (like PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL) on columns.
  • Renaming a column.

Understanding the distinction between DDL commands (like ALTER TABLE) that modify the database structure and DML commands (like UPDATE) that modify the data within that structure is crucial for database management.

Was this answer helpful?

Important Questions from SQL

  1. In SQL, _______ is an Aggregate function.

  2. Match the following -

    List IList II
    (a)DDL(i)LOCK TABLE
    (b)DML(ii)COMMIT
    (c)TCL(iii)Natural Difference
    (d)Binary operation(iv)REVOKE
  3. Which of the following keyword is used to eliminate duplicate records in SQL?

  4. _______ SQL command changes one or more fields in a record.

  5. Given two relations R 1(A, B) and R 2(C, D), the result of following query

    Select distinct A, B

    from R 1, R 2

    is guaranteed to be same as R 1 provided one of the following condition is satisfied.

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App