sq22

In an earlier lesson, we created a table with the CREATE TABLE command. In this lesson, we will modify the table using the ALTER TABLE command.

Add a Column

SQL syntax

Here's the syntax for adding a column.
ALTER TABLE table_name
ADD column_name datatype;

Example SQL Statement

And here's a SQL statement that adds a column called age to the Individual table.
ALTER TABLE Individual
ADD age int;

Change the Datatype

SQL syntax

To change the data type, use ALTER COLUMN instead of ADD.
ALTER TABLE table_name
ALTER COLUMN column_name datatype;

Example SQL Statement

Here, we change the datatype of the age column to numeric.
ALTER TABLE Individual
ALTER COLUMN age numeric;

Drop a Column

"Dropping" a column means removing or deleting that column.

SQL syntax

Use DROP COLUMN followed by the name of the column you want to drop.
ALTER TABLE table_name
DROP COLUMN column_name;

Example SQL Statement

Here, we drop the age column that we added above.


ALTER TABLE Individual
DROP COLUMN age;

No comments:

Post a Comment

Hey, It's Been Grabbed