Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 28 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
28
Dung lượng
194,59 KB
Nội dung
AlteringTablesand Constraints
12
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć2
Altering TablesandConstraints 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 TablesandConstraints 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 TablesandConstraints 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 TablesandConstraints 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
[...]... 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 constraintsAlteringTablesandConstraints 12Ć15 12Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Dropping a Table The DROP TABLE command removes the definition of an Oracle7 table When you drop... s_emp.last_name IS ’’; Comment created AlteringTablesandConstraints 12Ć21 12Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... dictionary to view the comment AlteringTablesandConstraints 12Ć23 12Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... structures andconstraints 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... constraint is no longer enforced by the Oracle7 Server and is no longer available in the data dictionary AlteringTablesandConstraints 12Ć13 12Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... The DELETE command can also remove all rows from a table, but it does not release storage space AlteringTablesandConstraints 12Ć19 12Ć20 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... 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 AlteringTablesandConstraints 12Ć11 12Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Adding and Dropping... 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 AlteringTablesandConstraints 12Ć17 12Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure... 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... definitions D Dropping tables D Adding a comment to a table D Displaying information in data dictionary views AlteringTablesandConstraints 12Ć25 12Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Practice 12 1 Create a WORKER table, which copies the data from the EMPLOYEE table Describe the table to confirm its structure 2 View the constraints for this new table Save this command to a script . Altering Tables and Constraints
12
Introduction to Oracle: SQL and PL/SQL Using Procedure Builder12Ć2
Altering Tables and Constraints 12Ć3
Objectives
After. 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