To add a column to an existing table in Oracle PL/SQL, you can use the ALTER TABLE statement. The basic syntax is as follows:
ALTER TABLE table_name ADD (column_name datatype [NULL | NOT NULL] [DEFAULT value]);
Table Students
STUDENT_ID | FIRST_NAME | LAST_NAME | GENDER | CITY |
---|---|---|---|---|
1 | Daniel | SCOTT | M | New York |
2 | Anthony | SIMMONS | M | Chicago |
3 | Sophia | THOMPSON | F | Los Angeles |
Add single column to table
For example, to add a column named “country” with a datatype of VARCHAR2(150) and a default value of ‘N/A’ to a table named “STUDENTS”, you would use the following statement:
ALTER TABLE STUDENTS ADD country VARCHAR2(150);
Add columns to table
Also you can add multiple columns to a table in a single ALTER TABLE statement by separating the column definitions with commas.
ALTER TABLE STUDENTS ADD ( address VARCHAR2(4000), country VARCHAR2(150) );
Query the table
SELECT STUDENT_ID, FIRST_NAME, LAST_NAME, ADDRESS, COUNTRY FROM STUDENTS;
STUDENT_ID | FIRST_NAME | LAST_NAME | ADDRESS | COUNTRY |
---|---|---|---|---|
1 | Daniel | SCOTT | ||
2 | Anthony | SIMMONS | ||
3 | Sophia | THOMPSON |