 |
|
Oracle Concepts - Altering Tables
Oracle Tips by Burleson Consulting |
Altering Tables
Having created tables, you will find there are times
that you will want to alter them in some respect. This is done with the alter
table command. Some things you might find you need to do with the alter table
command include:
* Moving a table to another tablespace
* Adding a column to the table
* Dropping a column from the table
* Changing a column name
* Modify existing constraints or add new constraints
For example, we might want to move the BOOKS table to
another tablespace. The alter table command allows us to do this easily:
ALTER TABLE books MOVE TABLESPACE new_users;
You can add a column to a table with the alter table
command. In this case, we are adding a column called BOOK_PRICE to the BOOKS
table. This column is a number data type:
ALTER TABLE books ADD book_price NUMBER;
You can add multiple columns too as seen in this example
where we add a column called BOOK_PUB_NO and BOOK_ALT_TITLE to the BOOKS table:
ALTER TABLE
books
ADD
(
book_pub_no NUMBER,
book_alt_title VARCHAR2(50)
);
It used to be in Oracle that renaming or removing a
column from a table was a big deal. You actually had to save the data, drop and
re-create the table which can have a horrible impact if it?s a big table in a
production environment. Now, you can rename or drop columns from a table with
the alter table command.
We demonstrate renaming and dropping a column in the
following example. First we rename the BOOK_ISBN column to ISBN_NUMBER. In the
second example we drop the BOOK_PUB_NO column:
ALTER TABLE books RENAME COLUMN book_isbn TO isbn_number;
ALTER TABLE books DROP COLUMN book_pub_no;
We discussed primary key constraints earlier in this
section. Oracle actually has a number of different constraint types that we will
cover in more detail later in this section. However, we should take a moment to
see how you can rename a constraint with the alter table command:
ALTER TABLE books
RENAME CONSTRAINT pk_books TO pk_book;
This is an excerpt from the bestselling "Easy
Oracle Jumpstart" by Robert Freeman and Steve Karam (Oracle ACE and Oracle
Certified Master). It?s only $19.95 when you buy it directly from the
publisher
here.
 |
If you like Oracle tuning, you may enjoy the new book "Oracle
Tuning: The Definitive Reference", over 900 pages of BC's favorite tuning
tips & scripts.
You can buy it direct from the publisher for 30%-off and get instant access to
the code depot of Oracle tuning scripts. |
|