Advanced Data Definition Commands to Alter Columns
Learn about advanced data definition commands, including commands to add columns, delete columns, and update the constraints of a table.
Adding columns
To add a column to a table, use the ALTER TABLE command with the ADD COLUMN clause.
Here is the syntax for this:
For example, to add a phone_number column with the datatype VARCHAR(10) to the Customer table, the command would be as follows:
Lines 1 and 9: The
\dcommand followed by the name of an object (e.g.,Customer) will show detailed information about that object.Line 2: This statement returns a new line as a column called
" "in the results. The\ncharacter is a new line character used to create a new line in the output. The--indicates that the remainder of the line is a comment and is ignored by the database.Lines 4–7: The
ALTER TABLEcommand adds aphone_numbercolumn with the data typeVARCHAR(10)to theCustomertable.
Dropping columns
To delete a column, use the ALTER TABLE command with the DROP COLUMN clause.
The syntax for this is given below:
For example, to delete the phone_number column from the Customer table, the command would be as follows:
...