Вы находитесь на странице: 1из 3

Dropping Columns

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 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.

ALTER TABLE table_name DROP UNUSED COLUMNS;


On large tables you can reduce the amount of undo logs accumulated by using the CHECKPOINT option which forces a checkpoint after the specified number of rows have been processed.

ALTER TABLE table_name DROP UNUSED COLUMNS CHECKPOINT 250;


The DBA_UNUSED_COL_TABS view can be used to view the number of unused columns per table.

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

Back to the Basics: Dropping UNUSED columns in Oracle


In our back to basic series today, we will show another approach of dropping columns from the table. Normally columns are dropped using ALTER TABLE..DROP COLUMN syntax. There is also a DROP UNUSED COLUMN clause, using which we can drop all the unwanted columns from the table. Let us see it by example.

CREATE TABLE TEST ( TEST_ID TEST_NAME STATUS NUMBER NOT NULL, VARCHAR2(128), VARCHAR2(7), DATE, VARCHAR(30), NUMBER(9), DATE,

CREATE_DATE MISC_ALPHA1 MISC_NUM1 MISC_DATE1

CONSTRAINT PK_TEST PRIMARY KEY(TEST_ID) ) /

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.

SQL> ALTER TABLE TEST SET UNUSED COLUMN MISC_ALPHA1;

OR

SQL> ALTER TABLE TEST SET UNUSED (MISC_NUM1, MISC_DATE1);

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.

SQL> desc TEST Name Null? Type

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.

SQL> SELECT * FROM USER_UNUSED_COL_TABS;

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.

SQL> ALTER TABLE TEST DROP UNUSED COLUMNS;

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.

Вам также может понравиться