Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you ADD and DROP columns in a single ALTER TABLE

I tried the following but I got a syntax error

ALTER TABLE Grades ( 
DROP COLUMN (Student_FamilyName, Student_Name),
ADD Student_id INT );

Is it possible to perform a DROP and an ADD in the same ALTER TABLE statement?

like image 543
squeezy Avatar asked Apr 13 '13 15:04

squeezy


People also ask

How do you add multiple columns in a single ALTER statement?

First, you specify the table name after the ALTER TABLE clause. Second, you put the new column and its definition after the ADD COLUMN clause. Note that COLUMN keyword is optional so you can omit it. Third, MySQL allows you to add the new column as the first column of the table by specifying the FIRST keyword.

How do you add a column to an ALTER TABLE?

Syntax. The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows.

How do you drop a column with ALTER?

Syntax. The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.

How do you add a new column to a table?

SQL ALTER TABLE Statement. SQL ALTER TABLE Statement. The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER TABLE - ADD Column. To add a column in a table, use the following syntax:

How do I alter a table in a database?

Altering the created table will alter it in several different ways. Add columns (while adding columns we will also review several of the most important column types and their use). Drop columns (meaning remove the column). Creating a table by importing a CSV file and altering that table.

How to drop multiple columns from a single table in SQL?

I would like to write a single SQL command to drop multiple columns from a single table in one ALTER TABLE statement. From MSDN's ALTER TABLE documentation ... DROP { [CONSTRAINT] constraint_name | COLUMN column_name } Specifies that constraint_name or column_name is removed from the table.

How to drop a column part of an ALTER statement?

The Syntax as specified by Microsoft for the dropping a column part of an ALTER statement is this DROP { [ CONSTRAINT ] { constraint_name [ WITH ( <drop_clustered_constraint_option> [ ,...n ] ) ] } [ ,...n ] | COLUMN { column_name } [ ,...n ] } [ ,...n ]


2 Answers

If you look at the ALTER TABLE SYTAX

you'll see this

ALTER TABLE [ database_name . [ schema_name ] . | schema_name . ] table_name 
{ 
    ALTER COLUMN column_name 
    { 
        [ type_schema_name. ] type_name [ ( { precision [ , scale ] 
            | max | xml_schema_collection } ) ] 
        [ COLLATE collation_name ] 
        [ NULL | NOT NULL ] [ SPARSE ]

    | {ADD | DROP } 
        { ROWGUIDCOL | PERSISTED | NOT FOR REPLICATION | SPARSE }
    } 
        | [ WITH { CHECK | NOCHECK } ]

    | ADD 
    { 
        <column_definition>
      | <computed_column_definition>
      | <table_constraint> 
      | <column_set_definition> 
    } [ ,...n ]

    | DROP 
     {
         [ CONSTRAINT ] 
         { 
              constraint_name 
              [ WITH 
               ( <drop_clustered_constraint_option> [ ,...n ] ) 
              ] 
          } [ ,...n ]
          | COLUMN 
          {
              column_name 
          } [ ,...n ]
     } [ ,...n ]

This can be reduced to

ALTER TABLE { ALTER COLUMN column_name | ADD | DROP }

According to Transact-SQL Syntax Conventions (Transact-SQL) the | (vertical bar)

Separates syntax items enclosed in brackets or braces. You can use only one of the items.

So you can't Alter, Drop or Add in a single statement. You also have the parens and comma that won't work. So you'll need

ALTER TABLE Grades DROP COLUMN (Student_FamilyName, Student_Name);
ALTER TABLE Grades ADD  Student_id INT;

If you need them to be an atomic action you just need to wrap in transaction

like image 71
Conrad Frix Avatar answered Oct 23 '22 23:10

Conrad Frix


In case your database is MySQL, you can do it this way:

ALTER TABLE Grades
    DROP COLUMN Student_FamilyName, 
    DROP COLUMN Student_Name,
    ADD Student_id INT;

Works in MySQL 5.5.5

like image 44
Klesun Avatar answered Oct 23 '22 23:10

Klesun