13.3 Modifying A Table
13.3 Modifying A Table
OEHB
Purpose
As you know by now, databases are This lesson presents the DDL
dynamic entities.
commands that are used to
alter, rename, empty, or simply
They probably wouldn't be very eliminate a table altogether.
useful if they couldn't be changed.
OEHB
ALTER TABLE
} ALTER TABLE statements are used to:
} Add a new column
} Modify an existing column
} Define a DEFAULT value for a column
} Drop a column
You can add or modify a column in a
Modifying a Table
}
table, but you cannot specify where
the column appears.
} A newly added column always
becomes the last column of the table.
} Also, if a table already has rows of
data and you add a new column to
the table, the new column is initially
null for all of the pre-existing the
rows.
OEHB
ALTER TABLE: Modifying a Column
} Modifying a column can include changes to a
column's data type, size, and DEFAULT value.
} Rules and restrictions when modifying a
column are:
} You can increase the width or precision of a
numeric column.
} You can increase the width of a character
column.
Modifying a Table
OEHB
ALTER TABLE: Dropping a Column
} When dropping a column the
following rules apply:
} A column containing data may be
dropped.
} Only one column can be dropped
Modifying a Table
at a time.
} You can't drop all of the columns
in a table; at least one column
must remain.
} Once a column is dropped, the
data values in it cannot be
recovered.
OEHB
SET UNUSED Columns
} Dropping a column from a large
table can take a long time.
} A quicker alternative is to mark
the column as unusable.
} The column values remain in the
Modifying a Table
OEHB
ALTER TABLE Summarized
Syntax Outcomes
ALTER TABLE tablename ADD Adds a new column to a table You cannot specify where the
(column name data type [DEFAULT column is to appear in the table. It
expression], column name data type becomes the last column.
[DEFAULT expression], ...
ALTER TABLE tablename MODIFY Used to change a column's data A change to the default value of a
Modifying a Table
(column name data type [DEFAULT type, size, and default value column affects only subsequent
expression], column name data type, insertions to the table.
...
ALTER TABLE tablename DROP Used to drop a column from a table The table must have at least one
COLUMN column name; column remaining in it after it is
altered. Once dropped, the column
cannot be recovered.
ALTER TABLE tablename SET Used to mark one or more columns Does not restore disk space.
UNUSED (column name); so they can be dropped later Columns are treated as if they were
dropped.
ALTER TABLE tablename DROP Removes from the table all columns Once set unused, there is no access
UNUSED COLUMNS currently marked as unused to the columns; no data displayed
using DESCRIBE. Permanent
removal; no rollback.
OEHB
DROP TABLE
} The DROP TABLE statement removes the
definition of an Oracle table.
} The database loses all the data in the
table and all the indexes associated with
it.
} When a DROP TABLE statement is
issued:
Modifying a Table
OEHB
FLASHBACK TABLE
} If you drop a table by mistake, you
may be able to bring that table and its
data back.
} Each database user has his own
recycle bin into which dropped
objects are moved, and they can be
recovered from here with the
Modifying a Table
OEHB
RENAME
} To change the name of a table,
use the RENAME statement.
} we can rename other types of
objects such as views, sequences,
and synonyms.
Modifying a Table
OEHB
TRUNCATE
} Truncating a table removes all
rows from a table and releases
the storage space used by that } The DELETE statement also
table. removes rows from a table,
When using the TRUNCATE
Modifying a Table
OEHB
COMMENT ON TABLE
} You can add a comment of up
to 2,000 characters about a
column, table, or view by using
the COMMENT statement.
Modifying a Table
OEHB
FLASHBACK QUERY
} You may discover that data in a } You can use the FLASHBACK QUERY
table has somehow been facility to examine what the rows
looked like BEFORE those changes
inappropriately changed. were applied.
} Oracle has a facility that allows } When Oracle changes data, it always
you to view row data at specific keeps a copy of what the amended data
points in time, so you can looked like before any changes were
Modifying a Table
OEHB
Modifying a Table
FLASHBACK QUERY
OEHB
Modifying a Table
FLASHBACK QUERY
OEHB