Alter table | SQL Tutorial and Query Example

Text copied!

Alter table


  • The syntax of the ALTER TABLE statement generally looks like this :
    To alter SQL table, you can use the graphical user interface (GUI) or Transact-SQL (T-SQL).
    Here's an example of how to alter table using T-SQL :

    Suppose you have a table called "[Product]" with columns 'DepartmentID', 'Name', 'GroupName' and 'ModifiedDate'.

    ALTER TABLE table_name
    ADD [column_name] data_type [column_constraint],
    ALTER COLUMN [column_name] data_type [column_constraint],
    DROP COLUMN [column_name],
    ADD CONSTRAINT constraint_name constraint_definition,
    DROP CONSTRAINT constraint_name;
            
    Here's an example of how to alter table using GUI :

    Let's modify the [ModifiedDate] column data type from "DATE" to "DATETIME".

    Alter table
    ALTER TABLE [Product]
    ADD [ProductName] VARCHAR(150) DEFAULT 0;
            
    ALTER TABLE [Product] 
    ALTER COLUMN [ModifiedDate] DATETIME;
            
    ALTER TABLE [Product]
    DROP COLUMN [ModifiedDate];
            
    ALTER TABLE [Product]
    ADD CONSTRAINT pk_product PRIMARY KEY (DepartmentID);
            
    ALTER TABLE [Product]
    DROP CONSTRAINT pk_product;
            
    Alter table Alter table Alter table Alter table

    Frequently Asked Questions :

    ALTER TABLE in SQL is a command used to modify the structure of an existing table.
    To edit a SQL table, use ALTER TABLE followed by the specific modifications such as adding, removing, or modifying columns.
    The ALTER TABLE method in SQL allows for the modification of table structure, including adding or dropping columns, changing data types, or altering constraints.
    You can replace a table in SQL by creating a new table with the desired structure and then copying data from the old table into the new one using INSERT INTO and SELECT statements.