📜  oracle 新列 - SQL (1)

📅  最后修改于: 2023-12-03 15:33:19.967000             🧑  作者: Mango

Introduction to Adding New Columns in Oracle SQL

In Oracle SQL, adding new columns to an existing table is a common task. This can be done using the ALTER TABLE statement combined with the ADD COLUMN clause.

Syntax

The syntax for adding a new column to a table is as follows:

ALTER TABLE table_name
ADD COLUMN column_name data_type [DEFAULT default_value] [NULL|NOT NULL];
  • table_name: The name of the table to which the new column will be added.
  • column_name: The name of the new column.
  • data_type: The data type of the new column. Examples include VARCHAR2, NUMBER, DATE, etc.
  • DEFAULT default_value (optional): Specifies a default value for the new column.
  • NULL|NOT NULL (optional): Specifies whether the new column allows null values or not. If NULL is specified, the column allows null values. If NOT NULL is specified, the column does not allow null values.
Example

Let's consider an example where we want to add a new column email of type VARCHAR2(50) to the existing employees table with a default value of NULL and which allows null values.

ALTER TABLE employees
ADD COLUMN email VARCHAR2(50) DEFAULT NULL NULL;

This will add a new column email to the employees table with the specified properties.

Conclusion

Adding new columns to an existing table can be done easily using the ALTER TABLE statement in Oracle SQL. By specifying the table name, the new column name, its data type, default value, and whether it allows null values or not, you can add the column to the table in no time.