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

Tài liệu Altering tables and contraints ppt

28 398 0

Đ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

Định dạng
Số trang 28
Dung lượng 208,61 KB

Nội dung

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 [...]... 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 Technical Note: Additional options for the above syntax are USING INDEX, EXCEPTIONS INTO, and ALL TRIGGERS clauses Altering Tables and Constraints 12Ć15 12Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Dropping... s_emp.last_name IS ’’; Comment created Altering Tables and Constraints 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... comment to a table or a column D Query the data dictionary to view the comment Altering Tables and Constraints 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... using the MODIFY clause of the ALTER TABLE command Altering Tables and Constraints 12Ć11 12Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder 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... 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... The DELETE command can also remove all rows from a table, but it does not release storage space Altering Tables and Constraints 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... 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 Altering Tables and Constraints 12Ć17 12Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure... constraint is no longer enforced by the Oracle7 Server and is no longer available in the data dictionary Altering Tables and Constraints 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... 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... 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 Class Management Note: Duration: 40 minutes Altering Tables and Constraints 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 . 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 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

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

w