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?
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.
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.
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.
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:
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.
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.
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 ]
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With