0% found this document useful (0 votes)
30 views

13.3 Modifying A Table

The document discusses modifying tables in a database through the use of Data Definition Language (DDL) statements. It explains that the ALTER, DROP, RENAME, and TRUNCATE statements allow tracking changes to data over time and modifying existing table structures. Specific DDL commands like ALTER TABLE ADD, MODIFY, and DROP allow adding, changing, and removing columns. The SET UNUSED command marks columns as unused to quickly eliminate them without deleting existing data. Flashback queries allow viewing past data states.

Uploaded by

Raul Perez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

13.3 Modifying A Table

The document discusses modifying tables in a database through the use of Data Definition Language (DDL) statements. It explains that the ALTER, DROP, RENAME, and TRUNCATE statements allow tracking changes to data over time and modifying existing table structures. Specific DDL commands like ALTER TABLE ADD, MODIFY, and DROP allow adding, changing, and removing columns. The SET UNUSED command marks columns as unused to quickly eliminate them without deleting existing data. Flashback queries allow viewing past data states.

Uploaded by

Raul Perez
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Modifying a Table

Data Definition Language - DD


Objectives

Explain why it is important to be able to


modify a table This lesson covers the
following objectives:
Explain and provide an example for each of
the DDL statements • Track the changes to data over a
Modifying a Table

• ALTER, period of time


• DROP,
• RENAME, and
• Explain the rationale for using
• TRUNCATE TRUNCATE versus DELETE for
tables
Construct a query and execute the ALTER
TABLE commands ADD, MODIFY, and DROP • Add a comment to a table using the
COMMENT ON TABLE command
Explain and perform FLASHBACK QUERY on • Name the changes that can and
a table cannot be made to modify a column
Explain and perform FLASHBACK table • Explain when and why the SET
operations UNUSED statement is advantageous

OEHB
Purpose

Remember the statement, "There is


nothing permanent except change"? Up to now, you've created
tables and made changes to
the row data inside tables, but
Wouldn't it be nice if we never
how do you make changes to
Modifying a Table

made mistakes or needed to change


anything? the tables themselves?

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

} You can decrease the width of a NUMBER


column if the column contains only null values, or
if the table has no rows.
} For VARCHAR types, you can decrease the width
down to the largest value contained in the Would be permitted only if columns were empty,
column.
or the largest name was 10 or less characters
} You can change the data type only if the column
contains null values.
} You can convert a CHAR column to VARCHAR2 or
convert a VARCHAR2 COLUMN to CHAR only if
the column contains null values, or if you do not
change the size to something smaller than any value
in the column.
} A change to the DEFAULT value of a column affects
only later insertions to the 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

database but cannot be accessed in


any way, so the effect is the same
as dropping the column.
} In fact, you could add a new
column to the database with the
same name as the unused column.
} The unused columns are there,
but invisible!
DROP UNUSED COLUMNS removes all columns
currently marked as unused.

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

} All data is deleted from the table.


} The table's description is removed from the
Data Dictionary.
} The Oracle Server does not question
your decision and it drops the table
immediately.
} you will see that you may be able to
restore a table after it is dropped, but it is
not guaranteed.
} Only the creator of the table or a user with
DROP ANY TABLE privilege (usually only
the DBA) can remove 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

FLASHBACK TABLE command.


} This command can be used to restore
a table, a view, or an index that was It is possible to see which objects can be restored by
dropped in error. querying the data dictionary view
USER_RECYCLEBIN.

Vaciar Papelera de Reciclaje

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

} } but it does not release storage


TABLE statement: space.
} You cannot roll back row removal. } TRUNCATE is faster than
} You must be the owner of the DELETE because it does not
table or have been given DROP generate rollback information.
ANY TABLE system privileges.

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

compare different versions of made.


a row over time. } So it keeps
} This facility is very useful. } a copy of the old column value for a
column update,
} it keeps the entire row for a delete, and
} Imagine, for instance, that someone } it keeps nothing for an insert statement.
accidently performs some DML on } These old copies are held in a special
a table, and then executes a place called the UNDO tablespace.
COMMIT on those changes. } Users can access this special area of the
Database using a flashback query.
} Oracle Application Express } You can look at older versions of data by
commits automatically, so mistakes using the VERSIONS clause in a SELECT
are easily made. statement.

OEHB
Modifying a Table
FLASHBACK QUERY

OEHB
Modifying a Table
FLASHBACK QUERY

OEHB

You might also like