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

Tài liệu Altering tables and constraints pptx

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 (194.59 KB, 28 trang )

Altering Tables and Constraints
12
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć2
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
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.
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.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć12
Altering Tables and Constraints 12Ć13
Adding and Dropping a Constraint continued
To drop a constraint, you can identify the constraint name from the
USER_CONSTRAINTS and USER_CONS_COLUMNS data dictionary views.
Then, use the ALTER TABLE command with the DROP clause. The CASCADE
option of the DROP clause causes any dependent constraints also to be dropped.
Syntax
ALTER TABLE table
DROP PRIMARY KEY | UNIQUE (column) |
CONSTRAINT constraint [CASCADE];
where: table is the name of the table.
column is the name of the column affected by the
constraint.
constraint is the name of the constraint.
When you drop an integrity constraint, that constraint is no longer enforced by the
Oracle7 Server and is no longer available in the data dictionary.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć14
Altering Tables and Constraints 12Ć15
Disabling and Enabling a Constraint
You can enable or disable constraints without dropping them or recreating them by
using the ALTER TABLE command with the ENABLE or DISABLE clause.
Syntax
ALTER TABLE table

DISABLE | ENABLE CONSTRAINT constraint [CASCADE];
Guidelines
D If you enable a constraint, that constraint applies to all the data in the table. All
the data in the table must fit the constraint.
D If you enable a UNIQUE or PRIMARY KEY constraint, a UNIQUE or
PRIMARY KEY index is automatically created.
D You can use the ENABLE and DISABLE clauses in both the CREATE TABLE
command and the ALTER TABLE command.
D The CASCADE clause disables dependent integrity constraints.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć16
Altering Tables and Constraints 12Ć17
Dropping a Table
The DROP TABLE command removes the definition of an Oracle7 table. When you
drop a table, the database loses all the data in the table and all the indexes associated
with it. The CASCADE CONSTRAINTS option will also remove dependent
referential integrity constraints.
Syntax
DROP TABLE table [CASCADE CONSTRAINTS];
where: table is the name of the table.
Guidelines
D All data is deleted from the table.
D Any views, synonyms, stored procedures, functions, or packages will remain, but
are invalid.
D Any pending transactions are committed.
D Only the creator of the table or a user with the DROP ANY TABLE privilege can
remove a table.
The DROP TABLE command, once executed, is irreversible. The Oracle7 Server
does not question the action when you issue the DROP TABLE command. If you own
that table or have a high level privilege, then the table is immediately removed. All
DDL commands issue a commit, therefore making the transaction permanent.

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć18
Altering Tables and Constraints 12Ć19
Renaming and Truncating a Table
Additional DDL commands include the RENAME command, which is used to
rename a table, view, sequence, or synonym, and the TRUNCATE TABLE command,
which is used to remove all rows from a table and to release the storage space used by
that table.
SyntaxĊRENAME Command
RENAME old_name TO new_name;
You must be the owner of the object you rename.
SyntaxĊTRUNCATE Command
TRUNCATE TABLE table;
You must be the owner of the table or have DELETE TABLE system privileges to
truncate a table.
The DELETE command can also remove all rows from a table, but it does not release
storage space.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć20
Altering Tables and Constraints 12Ć21
Adding a Comment to a Table
You can add a comment of up to 2000 bytes about a column, table, view, or snapshot
by using the COMMENT command. The comment is stored in the data dictionary
and can be viewed in one of the following data dictionary views in the COMMENTS
column:
D ALL_COL_COMMENTS
D USER_COL_COMMENTS
D ALL_TAB_COMMENTS
D USER_TAB_COMMENTS
Syntax
COMMENT ON TABLE table | COLUMN table.column
IS ’text’;

where: table is the name of the table.
column is the name of the column in a table.
text is the text of the comment.
Examples
Add a comment on the S_EMP table.
SQL> COMMENT ON TABLE s_emp IS ’Employee Information’;
Comment created.
Remove a comment from a column.
SQL> COMMENT ON COLUMN s_emp.last_name IS ’’;
Comment created.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć22
Altering Tables and Constraints 12Ć23
Summary
Data definition commands (DDL) allow you to create, modify, remove, and rename
objects. When you issue a DDL command, an autocommit occurs. You cannot roll
back your commands.
CREATE TABLE
D You can create a table and the indicated constraints.
D Create a table based on another table by using a subquery.
ALTER TABLE
D Modify table structures and constraints.
D Change column widths, change column datatypes, add columns, add or drop
constraints, and enable or disable constraints.
DROP TABLE
D Remove rows and a table structure.
D Once executed, this command cannot be rolled back.
RENAME
D Rename a table, view, sequence, or synonym.
TRUNCATE
D Remove all rows from a table and release the storage space used by the table.

D DELETE command only removes rows.
COMMENT
D Add a comment to a table or a column.
D Query the data dictionary to view the comment.
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć24
Altering Tables and Constraints 12Ć25
Practice Overview
In this practice, you will create, modify, and drop tables related to the EMPLOYEE
and DEPARTMENT tables using the commands covered in this lesson.
Practice Contents
D Creating a new table by using the CREATE TABLE AS syntax
D Adding constraints
D Modifying column definitions
D Dropping tables
D Adding a comment to a table
D Displaying information in data dictionary views

×