Oracle ALTER TABLE: Complete Guide

In Oracle database, the ALTER statement is used to modify the structure of a database table, such as adding, changing, or deleting columns, constraints, indexes, etc. Here are some common uses of the ALTER statement:

  1. Add a column: ALTER TABLE table_name ADD column_name data_type;
  2. Change the data type of a column: ALTER TABLE table_name MODIFY column_name new_data_type;
  3. Change the name of a column: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
  4. Remove column: ALTER TABLE table_name DROP COLUMN column_name;
  5. Add a primary key constraint to the table by using the following SQL statement: ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column_name);
  6. Add a foreign key constraint to the table by altering it as follows: ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES other_table (other_column);
  7. Create an index: CREATE INDEX index_name on table_name (column_name);
  8. Delete an index: DROP INDEX index_name;

In conclusion, the ALTER statement allows users to modify database tables in order to meet various needs and requirements.

bannerAds