Академический Документы
Профессиональный Документы
Культура Документы
Only with the release of Oracle 8i has it been possible to drop a column from a table. Prior to this it was neccessary to drop the entire table and rebuild it. Now you can mark a column as unused (logical delete) or delete it completely (physical delete).
Logical Delete
On large tables the process of physically removing a column can be very time and resource consuming. For this reason you may decide to logically delete it.
ALTER TABLE table_name SET UNUSED (column_name); ALTER TABLE table_name SET UNUSED (column_name1, column_name2);
Once this is done the columns will no longer be visible to the user. If at a later date you have time to physically delete the columns this can be done using the following.
Physical Delete
To physically drop a column you can use one of the following syntaxes, depending on whether you wish to drop a single or multiple columns.
ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE table_name DROP (column_name1, column_name2);
Dropping a column from a table will cause all unused columns in that table to be dropped at the same time.
NOTE
Usually unused columns have NULL value
CREATE TABLE TEST ( TEST_ID TEST_NAME STATUS NUMBER NOT NULL, VARCHAR2(128), VARCHAR2(7), DATE, VARCHAR(30), NUMBER(9), DATE,
In above table, we have created three extra columns MISC_ALPHA1, MISC_NUM1 and MISC_DATE1 which we will drop it. In order to use DROP UNUSED COLUMN we have to first set column as unused.
OR
Marking column as unused is a logical approach instead of dropping column physically. Once columns are marked as unused, for all practical purposes, end user cannot see it. SELECT statement will not return the columns marked as unused. Even describe command also ignores the unused columns. Take a look at the result set below.
TEST_ID TEST_NAME STATUS CREATE_DATE NOT NULL NUMBER VARCHAR2(128) VARCHAR2(7) DATE
We can also query the USER_UNUSED_COL_TABS view to see which table has how many columns unused. For our example, we rendered three columns unused.
TABLE_NAME TEST 3
COUNT
Unused column retains the data until they are dropped. Also if you have one LONG column marked as unused, you cannot add new column with LONG data type until unused column is dropped but that is not the case with columns with other data type. Oracle will allow you to add new column with the same name as unused column. Run following command to drop all the unused columns from the table.
Marking column as unused is advantageous in the environment where data set is very large. Dropping column physically may take long time instead marking them as unused is done in no time and columns are not viewable to the outside world. Then later on during down time, we can issue command to drop the columns. If you use DROP COLUMN command to drop the column other than the columns marked as unused, Oracle will also drop the column.