SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. Alter table

Text copied!

« Previous
Next »

Alter table

Dec. 21, 2022, 9 p.m. under SQL

  • Hi! You should check Select table post first.

    Altering SQL table refers to the process of modifying the structure of an existing table in the database.

    You can use "ALTER TABLE" statement to modify the structure of an existing table(s) in the database. It enables the user to add, modify or remove columns and constraints in a table and is a powerful tool for maintaining and updating a table in the database.

    The syntax of the ALTER TABLE statement generally looks like this :
    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;
            

    Specify the table name after the 'ALTER TABLE' keywords. Specify the column name, data type and constraint that you want to add, remove or modify in the place of '[column_name]', 'data type' and '[column_constraint]' respectively.

    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

    1. To add a new column "[ProductName]" to the table and set its default value to 0, you could use the following query:

    ALTER TABLE [Product]
    ADD [ProductName] VARCHAR(150) DEFAULT 0;
            

    2. To change the data type of the "[ModifiedDate]" column from 'DATE' to 'DATETIME', you could use the following query:

    ALTER TABLE [Product] 
    ALTER COLUMN [ModifiedDate] DATETIME;
            

    3. To drop the "[ModifiedDate]" column from the "[Product]" table, you could use the following query:

    ALTER TABLE [Product]
    DROP COLUMN [ModifiedDate];
            

    4. To add a primary key constraint on the "[DepartmentID]" column, you could use the following query:

    ALTER TABLE [Product]
    ADD CONSTRAINT pk_product PRIMARY KEY (DepartmentID);
            

    5. Similarly, to drop this primary key constraint, you could use the following query:

    ALTER TABLE [Product]
    DROP CONSTRAINT pk_product;
            

    That's it! You have successfully altered the [Product] table using T-SQL.

    Here's an example of how to alter table using GUI :

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

    1. Right-click on the table name that you want to alter and select the "Design" option from the context menu.

    Alter table

    2. "Alter table window" will appear, you can add, modify or remove columns, data types and constraints. Let's change [ModifiedDate] column data type to "DATETIME".

    Alter table

    2. Next, click on "SAVE" button or close the window. it will promote to you new window asking whether you want to make changes or not >> Click on the "YES" button.

    Alter table

    3. There you go, You have changed [ModifiedDate] column data type to "DATETIME". To verify expand the "[Product]" table >> Expand "Columns folder".

    Alter table

    That's it! You have successfully altered the [Product] table using GUI.

    Frequently Asked Questions :

    What is ALTER TABLE in SQL?

    ALTER TABLE in SQL is a command used to modify the structure of an existing table.

    How do I edit a SQL table?

    To edit a SQL table, use ALTER TABLE followed by the specific modifications such as adding, removing, or modifying columns.

    What is the ALTER TABLE method?

    The ALTER TABLE method in SQL allows for the modification of table structure, including adding or dropping columns, changing data types, or altering constraints.

    How do you replace a table in SQL?

    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.
    Thank You! You should check SQL data types post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy