Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Saturday, 20 June 2015

Add column in table in Oracle

To add a column to an existing table in Oracle, you can use the ALTER TABLE statement. Here's the syntax:


ALTER TABLE table_name
ADD (column_name data_type [constraint]);


Here's a breakdown of the syntax elements:

- ALTER TABLE: This clause indicates that you're modifying the structure of an existing table.
- table_name: This is the name of the table to which you want to add the column.
- ADD: This keyword specifies that you're adding a new column to the table.
- (column_name data_type [constraint]): Within parentheses, you specify the details of the new column. column_name is the name of the new column, data_type is the data type of the column, and [constraint] is an optional constraint that you can apply to the column (e.g., NOT NULL, DEFAULT, etc.).

Here's an example of adding a column named email of type VARCHAR2(100) to an existing table named employees:


ALTER TABLE employees
ADD (email VARCHAR2(100));


After executing this statement, the email column will be added to the employees table.

If you want to add a constraint to the new column, such as NOT NULL, you can include it in the ADD clause:


ALTER TABLE employees
ADD (email VARCHAR2(100) NOT NULL);


This statement adds the email column with a NOT NULL constraint, ensuring that every row in the table must have a value for the email column.

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.