Tải bản đầy đủ (.pdf) (28 trang)

Tài liệu Altering tables and contraints ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (208.61 KB, 28 trang )

Altering Tables and Constraints
12
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć2
Schedule: Timing Topic
30 minutes Lecture
40 minutes Practice
70 minutes Total
Class Management Note:
Files required for this lesson are:
Demonstration: None
Practice: None
Altering Tables and Constraints 12Ć3
Objectives
After you create your tables, you may need to change the table structures
because you omitted a column, your column definition needs to be changed, or
you want to enable or disable constraints. This lesson will demonstrate how you
can amend table structures as well as add and remove constraints.
At the end of this lesson, you should be able to
D
Add and modify table columns.
D
Add, enable, disable, or remove constraints.
D
Drop a table.
D
Remove all rows leaving the table definition intact.
D
Change object names.
D
Add comments to objects and view comments from the data dictionary.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć4


Class Management Note:
Remind students that to make changes to database objects, they must have
the privileges for those specific commands.
Altering Tables and Constraints 12Ć5
Overview
Once you have created your tables, you can modify their structure by using the
ALTER TABLE command. Add columns, modify the column length, add or drop
constraints, and enable or disable constraints by using this command.
If you want to remove a table, both the rows and the data structure of a table, invoke
the DROP TABLE command. Other commands that affect tables that are covered in
this lesson are
D
RENAME, to change a database object name.
D
TRUNCATE, to remove all rows from a table.
D
COMMENT, to add a comment about a database object to the data dictionary.
All of these commands are data definition commands (DDL). When you issue these
statements, an automatic commit occurs. You cannot roll back DDL commands.
Therefore, be very careful when you execute them.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć6
Altering Tables and Constraints 12Ć7
Adding a Column
You can add columns to a table by using the ALTER TABLE command with the
ADD clause.
Syntax
ALTER TABLE table
ADD (column datatype [DEFAULT expr][NOT NULL]
[, column datatype]...);
where: table is the name of the table.

column is the name of the new column.
datatype is the datatype and length of the new column.
DEFAULT expr specifies the default value for a new column.
NOT NULL adds a NOT NULL constraint to the new
column.
Guidelines
D
You can add or modify columns, but you cannot drop them from a table.
D
You cannot specify where the column is to appear. The new column becomes the
last column.
Technical Note:
If a table already contains rows when a column is added, then all of the
fields in the new column are initially NULL.
You can define a NOT NULL column only if the table contains no rows
because data cannot be specified for existing rows at the same time that the
column is added.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć8
Altering Tables and Constraints 12Ć9
Modifying a Column
You can modify a column definition by using the ALTER TABLE command with the
MODIFY clause. Column modification can include changes to a column’s datatype,
size, default value, and NOT NULL column constraint.
Syntax
ALTER TABLE table
MODIFY (column datatype [DEFAULT expr][NOT NULL]
[, column datatype]...);
where: table is the name of the table.
column is the name of the column.
datatype is the datatype and length of the column.

DEFAULT expr specifies the default value for a new column.
NOT NULL adds a NOT NULL constraint to the new
column.
Guidelines
D
Increase the width or precision of a numeric column.
D
Decrease the width of a column if the column contains only null values or if the
table has no rows.
D
Change the datatype if the column contains null values.
D
Convert a CHAR column to the VARCHAR2 datatype or convert a VARCHAR2
column to the CHAR datatype if the column contains null values or if you do not
change the size.
D
A change to the default value of a column only affects subsequent insertions to
the table.
D
Add a NOT NULL constraint only if there are no null values in the column.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć10
Altering Tables and Constraints 12Ć11
Adding and Dropping a Constraint
You can add or drop constraints for existing tables by using the ALTER TABLE
command with the ADD or DROP clause.
Syntax
ALTER TABLE table
ADD [CONSTRAINT constraint] type (column);
where: table is the name of the table.
constraint is the name of the constraint.

type is the constraint type.
column is the name of the column affected by the
constraint.
The constraint name syntax is optional, although recommended. If you do not name
your constraints, the system will generate constraint names.
Guidelines
D
You can add, drop, enable, or disable a constraint, but you cannot modify its
structure.
D
You can add a NOT NULL constraint to an existing column by using the
MODIFY clause of the ALTER TABLE command.

×