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:
- Add a column: ALTER TABLE table_name ADD column_name data_type;
- Change the data type of a column: ALTER TABLE table_name MODIFY column_name new_data_type;
- Change the name of a column: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
- Remove column: ALTER TABLE table_name DROP COLUMN column_name;
- 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);
- 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);
- Create an index: CREATE INDEX index_name on table_name (column_name);
- 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.