Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Set a default value for the column, such as ALTER TABLE emp ADD
(home_zip VARCHAR2(10) NOT NULL DEFAULT 27607);
Create the column without the NOT NULL constraint, add the data, and apply
the NOT NULL constraint afterwards.
The second option leads directly to the discussion about altering existing columns.
To make these alterations, you would use the modify keyword and a similar expres-
sion that you would use if you were creating the column from scratch.
ALTER TABLE emp ADD (
home_zip VARCHAR2(10));
. . . add the data home zip data.
ALTER TABLE emp MODIFY (
home_zip NOT NULL);
You dont need to include anything about the data type, since that would remain the
same. However, if you want to change the data type, you can. But there are restrictions.
Anything goes if the column has no data in it. If you want, you can even change the data
type entirely from varchar2 to date, date to number, number to varchar2, and so
on. If, however, there is any data in the column, this kind of modification isnt allowed.
Table 8.17 lists the rules for modifying columns that already contain data.
Oracle SQL 165
Table 8.17 Modifying Columns Containing Data
ACTION DESCRIPTION OKAY? EXAMPLE
Assigning Using the DEFAULT Always ALTER TABLE emp
default value keyword to specify a MODIFY (home_zip
default value for the DEFAULT 27609)
column. The default
value will only be
used moving forward;
null and other values
already in the system
will remain the same.
Widening strings Increasing the number Always ALTER TABLE emp
of characters allowed. MODIFY (home_zip
VARCHAR2(12))
Narrowing strings Decreasing the Only to ALTER TABLE emp
number of characters longest MODIFY (ename
allowed. Oracle won't string VARCHAR2(7));
do this if it would length
require any existing
string to be shortened.
Increasing scale Increasing the number Always ALTER TABLE emp
For numbers of digits to the left MODIFY (empno
of the decimal point. NUMBER(6,0))
Increasing Increasing the number Always ALTER TABLE emp
precision For of digits to the right MODIFY (empno
numbers of the decimal point. NUMBER(8,2))
Decreasing scale Shrinking the size of No
and precision a number column on
For numbers either the left or
right of the column.
Our last topic for this section concerns dropping a column altogether, the ultimate
modification. This can be accomplished by way of the drop column clause. If there is
data in the column, it will be deleted.
alter table emp drop column home_zip;
Dropping Tables
Dropping tables is the process of removing a table and all of its data from your data-
base. To drop a table, you need the drop table privilege for the table. Also, you need
to be very careful; if you are having a bad day, you might end up inadvertently drop-
ping the incorrect table.
166 Chapter 8
There are two ways to drop tables. The following way youve already seen:
DROP TABLE table_to_drop;
Often when trying to drop a table, you will encounter the following error message:
ORA-02449: unique/primary keys in table referenced by foreign keys
You get this error message because foreign key constraints are being used. You learn
more about foreign key constraints later; for now, its important to know only that
there are rows in other tables that reference keys in this table. If you just go and drop
this table, those other tables will be very, very upset. Thus, Oracle doesnt allow you to
do it. Here are ways around this problem:
Getting rid of the foreign key constraints; then dropping the table