1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Altering tables and contraints ppt

28 399 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Altering tables and constraints
Trường học Oracle University
Chuyên ngành Database Management
Thể loại bài giảng
Định dạng
Số trang 28
Dung lượng 208,61 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 const

Trang 1

Altering Tables and Constraints

12

Trang 2

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć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

Trang 3

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

Trang 4

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć4

Class Management Note:

Remind students that to make changes to database objects, they must havethe privileges for those specific commands

Trang 5

Once you have created your tables, you can modify their structure by using theALTER TABLE command Add columns, modify the column length, add or dropconstraints, 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, invokethe DROP TABLE command Other commands that affect tables that are covered inthis 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 thesestatements, an automatic commit occurs You cannot roll back DDL commands.Therefore, be very careful when you execute them

Trang 6

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć6

Trang 7

Adding a Column

You can add columns to a table by using the ALTER TABLE command with theADD 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 thelast column

Trang 8

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć8

Trang 9

Modifying a Column

You can modify a column definition by using the ALTER TABLE command with theMODIFY 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 thetable 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 VARCHAR2column to the CHAR datatype if the column contains null values or if you do notchange the size

D A change to the default value of a column only affects subsequent insertions tothe table

D Add a NOT NULL constraint only if there are no null values in the column

Trang 10

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć10

Trang 11

Adding and Dropping a Constraint

You can add or drop constraints for existing tables by using the ALTER TABLEcommand 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

column is the name of the column affected by the

D You can add a NOT NULL constraint to an existing column by using the

MODIFY clause of the ALTER TABLE command

Trang 12

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć12

Trang 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 CASCADEoption 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 theOracle7 Server and is no longer available in the data dictionary

Trang 14

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć14

Trang 15

Disabling and Enabling a Constraint

You can enable or disable constraints without dropping them or recreating them byusing 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 Allthe 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 TABLEcommand and the ALTER TABLE command

D The CASCADE clause disables dependent integrity constraints

Technical Note:

Additional options for the above syntax are USING INDEX,

EXCEPTIONS INTO, and ALL TRIGGERS clauses

Trang 16

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć16

Trang 17

Dropping a Table

The DROP TABLE command removes the definition of an Oracle7 table When youdrop a table, the database loses all the data in the table and all the indexes associatedwith 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, butare 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 Serverdoes not question the action when you issue the DROP TABLE command If you ownthat table or have a high level privilege, then the table is immediately removed AllDDL commands issue a commit, therefore making the transaction permanent

Trang 18

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć18

Trang 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 bythat 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 totruncate a table

The DELETE command can also remove all rows from a table, but it does not releasestorage space

Trang 20

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć20

Trang 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 dictionaryand can be viewed in one of the following data dictionary views in the COMMENTScolumn:

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

Trang 22

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć22

Trang 23

Data definition commands (DDL) allow you to create, modify, remove, and renameobjects When you issue a DDL command, an autocommit occurs You cannot rollback 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 dropconstraints, 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

Trang 24

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć24

Trang 25

D Adding a comment to a table

D Displaying information in data dictionary views

Class Management Note:

Duration: 40 minutes

Trang 26

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć26

Trang 27

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 named

p12q2.sql Note the types and names of the constraints.

3. Compare these constraints to those in the EMPLOYEE table Note the types andnames of the constraints

4. Add a table level PRIMARY KEY constraint to the WORKER table using the IDcolumn The constraint should be immediately enabled

5. Add a foreign key reference from the DEPARTMENT table to the DEPT_IDcolumn in the WORKER table Confirm that the constraints were added by

re-executing p12q2.sql.

6. Display the object names and types from the USER_OBJECTS data dictionaryview You may want to format the columns for readability Notice that the newtable and a new index were created

7. Drop the EMPLOYEE table, while leaving the WORKER table in the database

If you have time, complete the following exercises

8. Modify the WORKER table Add a TITLE column of VARCHAR2 datatype,length 30

9. Add a comment to the WORKER and DEPARTMENT table definitions

describing the tables Confirm your additions in the data dictionary

Trang 28

Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 12Ć28

Ngày đăng: 10/12/2013, 16:16

w