oracle视频g附赠d经典辅助学习les_第1页
oracle视频g附赠d经典辅助学习les_第2页
oracle视频g附赠d经典辅助学习les_第3页
oracle视频g附赠d经典辅助学习les_第4页
oracle视频g附赠d经典辅助学习les_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Manage Schema Objects ObjectivesAfter completing this lesson, you should be able to do the following: Add constraintsCreate indexesCreate indexes using the CREATE TABLE statementCreating function-based indexesDrop columns and set column UNUSEDPerform FLASHBACK operationsCreate and use external table

2、sThe ALTER TABLE StatementUse the ALTER TABLE statement to:Add a new columnModify an existing columnDefine a default value for the new columnDrop a columnThe ALTER TABLE StatementUse the ALTER TABLE statement to add, modify, or drop columns.ALTER TABLE tableADD (column datatype DEFAULT expr , column

3、 datatype.);ALTER TABLE tableMODIFY (column datatype DEFAULT expr , column datatype.);ALTER TABLE tableDROP (column);Adding a ColumnYou use the ADD clause to add columns.The new column becomes the last column.ALTER TABLE dept80ADD (job_id VARCHAR2(9);Table altered.Modifying a ColumnYou can change a

4、columns data type, size, and default value.A change to the default value affects only subsequent insertions to the table.ALTER TABLE dept80MODIFY(last_name VARCHAR2(30);Table altered.Dropping a ColumnUse the DROP COLUMN clause to drop columns you no longer need from the table.ALTER TABLE dept80DROP

5、COLUMN job_id; Table altered.ALTER TABLE SET UNUSED();ALTER TABLE SET UNUSED COLUMN ;The SET UNUSED OptionYou use the SET UNUSED option to mark one or more columns as unused.You use the DROP UNUSED COLUMNS option to remove the columns that are marked as unused.ORALTER TABLE DROP UNUSED COLUMNS;Notes

6、 OnlyAdding a Constraint SyntaxUse the ALTER TABLE statement to:Add or drop a constraint, but not modify its structureEnable or disable constraintsAdd a NOT NULL constraint by using the MODIFY clauseALTER TABLE ADD CONSTRAINT type ();ALTER TABLE emp2modify employee_id Primary Key;Table altered.Addin

7、g a ConstraintAdd a FOREIGN KEY constraint to the EMP2 table indicating that a manager must already exist as a valid employee in the EMP2 table.ALTER TABLE emp2ADD CONSTRAINT emp_mgr_fk FOREIGN KEY(manager_id) REFERENCES emp2(employee_id);Table altered.ON DELETE CASCADEDelete child rows when a paren

8、t key is deleted.ALTER TABLE Emp2 ADD CONSTRAINT emp_dt_fk FOREIGN KEY (Department_id) REFERENCES departments ON DELETE CASCADE); Table altered.Deferring ConstraintsConstraints can have the following attributes:DEFERRABLE or NOT DEFERRABLE INITIALLY DEFERRED or INITIALLY IMMEDIATEALTER TABLE dept2AD

9、D CONSTRAINT dept2_id_pk PRIMARY KEY (department_id)DEFERRABLE INITIALLY DEFERREDALTER SESSION SET CONSTRAINTS= IMMEDIATESET CONSTRAINTS dept2_id_pk IMMEDIATEDeferring constraint on creationChanging all constraints for a sessionChanging a specific constraint attributeDropping a ConstraintRemove the

10、manager constraint from the EMP2 table.Remove the PRIMARY KEY constraint on the DEPT2 table and drop the associated FOREIGN KEY constraint on the EMP2.DEPARTMENT_ID column.ALTER TABLE emp2DROP CONSTRAINT emp_mgr_fk;Table altered.ALTER TABLE dept2DROP PRIMARY KEY CASCADE;Table altered.Disabling Const

11、raintsExecute the DISABLE clause of the ALTER TABLE statement to deactivate an integrity constraint.Apply the CASCADE option to disable dependent integrity constraints.ALTER TABLE emp2DISABLE CONSTRAINT emp_dt_fk;Table altered.Enabling ConstraintsActivate an integrity constraint currently disabled i

12、n the table definition by using the ENABLE clause. A UNIQUE index is automatically created if you enable a UNIQUE key or PRIMARY KEY constraint.ALTER TABLEemp2ENABLE CONSTRAINT emp_dt_fk;Table altered.Notes OnlyCascading ConstraintsThe CASCADE CONSTRAINTS clause is used along with the DROP COLUMN cl

13、ause.he CASCADE CONSTRAINTS clause drops all referential integrity constraints that refer to the primary and unique keys defined on the dropped columns.The CASCADE CONSTRAINTS clause also drops all multicolumn constraints defined on the dropped columns.Cascading ConstraintsExample:ALTER TABLE emp2 D

14、ROP COLUMN employee_id CASCADE CONSTRAINTS;Table altered.ALTER TABLE test1DROP (pk, fk, col1) CASCADE CONSTRAINTS;Table altered.Overview of IndexesIndexes are created:Automatically PRIMARY KEY creation UNIQUE KEY creationManually CREATE INDEX statement CREATE TABLE statementCREATE INDEX with CREATE

15、TABLE Statement CREATE TABLE NEW_EMP(employee_id NUMBER(6) PRIMARY KEY USING INDEX (CREATE INDEX emp_id_idx ON NEW_EMP(employee_id),first_name VARCHAR2(20),last_name VARCHAR2(25);Table created.SELECT INDEX_NAME, TABLE_NAME FROM USER_INDEXESWHERE TABLE_NAME = NEW_EMP;Notes OnlyCREATE INDEX upper_dept

16、_name_idxON dept2(UPPER(department_name);Index created.SELECT *FROM dept2WHERE UPPER(department_name) = SALES;Function-Based IndexesA function-based index is based on expressions. The index expression is built from table columns, constants, SQL functions, and user-defined functions.Notes OnlyRemovin

17、g an IndexRemove an index from the data dictionary by using the DROP INDEX command.Remove the UPPER_DEPT_NAME_IDX index from the data dictionary.To drop an index, you must be the owner of the index or have the DROP ANY INDEX privilege.DROP INDEX upper_dept_name_idx;Index dropped.DROP INDEX index;DRO

18、P TABLE PURGEDROP TABLE dept80 PURGE; The FLASHBACK TABLE Statement Repair tool for accidental table modifications Restores a table to an earlier point in time Benefits: Ease of use, availability, fast execution Performed in placeSyntax:FLASHBACK TABLEschema.table, schema.table .TO TIMESTAMP | SCN e

19、xpr ENABLE | DISABLE TRIGGERS ;The FLASHBACK TABLE Statement DROP TABLE emp2;Table droppedFLASHBACK TABLE emp2 TO BEFORE DROP;Flashback complete SELECT original_name, operation, droptime, FROM recyclebin;External TablesNotes onlyCreating a Directory for the External TableCreate a DIRECTORY object th

20、at corresponds to the directory on the file system where the external data source resides.CREATE OR REPLACE DIRECTORY emp_dir AS /emp_dir; GRANT READ ON DIRECTORY emp_dir TO hr;Notes OnlyCreating an External TableCREATE TABLE ( , ) ORGANIZATION EXTERNAL (TYPE DEFAULT DIRECTORY ACCESS PARAMETERS ( ) ) LOCATION () ) REJECT LIMIT 0 | | UNLIMITED; Notes OnlyCreating an External Table Using ORACLE_LOADERCREATE TABLE oldemp ( fname char(25), lname CHAR(25) ORGANIZATION EXTERNAL (TYPE ORACLE_LOADER DEFAULT DIRECTORY emp_dir ACCESS PARAMETERS (RECORDS DEL

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论