Oracle OCP 考试系列培训                之         1Z0-007 Lesson9                   www.OracleOnLinux.cn9-1   Copyright © 2012, h...
9      Using DDL Statements   to Create and Manage TablesCopyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Objectives      After completing this lesson, you should be able to do      the following:       • Categorize the main dat...
Database Objects      Object            Description      Table             Basic unit of storage; composed of rows      Vi...
在 Oracle 数据库中的表      用户表:      • 由用户创建和维护的一些表      • 存放用户数据的信息      数据字典表:      • 由Oracle数据库创建和维护      • 存放Oracle数据库自身的信息、...
查询数据字典      • 查看用户拥有的表:       SELECT table_name       FROM   user_tables ;      • 查看用户拥有的所有对象:       SELECT object_name   ...
Naming Rules      Table names and column names:       • Must begin with a letter       • Must be 1–30 characters long     ...
CREATE TABLE Statement      •   You must have:          – CREATE TABLE privilege          – A storage area      CREATE TAB...
Referencing Another User ’s Tables                              User’      •   Tables belonging to other users are not in ...
DEFAULT Option       •   Specify a default value for a column during an           insert.       ... hire_date DATE DEFAULT...
Creating Tables       •   Create the table.       CREATE TABLE dept               (deptno                NUMBER(2),       ...
Data Types       Data Type           Description       VARCHAR2( size ) Variable-length character data       CHAR( size ) ...
9-13   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
ROWID Format9-14   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
ROWID       •   Get the rowid:       SELECT ROWID FROM employees;       •   Get the detail of rowid:       select rowid,  ...
ROWID       •   Get the detail of rowid:       select rowid ,       substr(rowid,1,6) "OBJECT",       substr(rowid,7,3) "F...
ROWID Format9-17   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
ROWID Format       Locating a Row Using ROWID       •   Because a segment can only reside in one tablespace, by       usin...
Including Constraints       •   Constraints enforce rules at the table level.       •   Constraints prevent the deletion o...
Constraint Guidelines       •   You can name a constraint, or the Oracle server           generates a name by using the SY...
Defining Constraints       •   Syntax:       CREATE TABLE [schema.]table             (column datatype [DEFAULT expr]      ...
Defining Constraints       •   Column-level constraint:       CREATE TABLE employees(         employee_id NUMBER(6)       ...
NOT NULL Constraint       Defined at the column level only.                              level       Ensures that null val...
UNIQUE Constraint                                                           UNIQUE constraint       EMPLOYEES       …     ...
UNIQUE Constraint       Defined at either the table level or the column level:                                            ...
PRIMARY KEY Constraint       DEPARTMENTS                           PRIMARY KEY          …  Not allowed                    ...
FOREIGN KEY Constraint             DEPARTMENTSPRIMARY  KEY             … EMPLOYEES                                        ...
FOREIGN KEY Constraint       Defined at either the table level or the column level:        CREATE TABLE employees(        ...
FOREIGN KEY Constraint:                         Keywords       •   FOREIGN KEY: Defines the column in the child           ...
CHECK Constraint       •     Defines a condition that each row must satisfy       •     CHECK constraints can be defined a...
CREATE TABLE : Example                            TABLE:       CREATE TABLE employees           ( employee_id    NUMBER(6)...
Violating Constraints       UPDATE    employees       SET       department_id = 55       WHERE     department_id = 110;   ...
Violating Constraints       You cannot delete a row that contains a primary key       that is used as a foreign key in ano...
Creating a Table                        by Using a Subquery       •   Create a table and insert rows by combining the     ...
Creating a Table                        by Using a Subquery       CREATE TABLE dept80         AS            SELECT employe...
The ALTER TABLE Statement       Use the ALTER TABLE statement to:        • Add a new column        • Modify an existing co...
The ALTER TABLE Statement       Use the ALTER TABLE statement to add, modify, or       drop columns.        ALTER TABLE ta...
Adding a Column       •       You use the ADD clause to add columns.           ALTER TABLE dept80           ADD        (jo...
Adding a Column       •   You use the ADD clause to add columns.       •   The new column becomes the last column.       •...
Modifying a Column       •     You can change a column’s data type, size, and                              column’        ...
Dropping a Column       Use the DROP COLUMN clause to drop columns you no       longer need from the table.        ALTER T...
Dropping a Column       Use the DROP COLUMN clause to drop columns you no       longer need from the table.        • 删除字段,...
The SET UNUSED Option       •     You use the SET UNUSED option to mark one or             more columns as unused.       •...
The SET UNUSED Option       •   SET UNUSED 并非真正从目标表上删除数据,占存储           空间       •   SET UNUSED 的列不再可以使用,DESC无效       •   S...
Notes Only9-45   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Adding a Constraint Syntax       Use the ALTER TABLE statement to:        • Add or drop a constraint, but not modify its  ...
Adding a Constraint       Add a FOREIGN KEY constraint to the EMP2 table       indicating that a manager must already exis...
ON DELETE CASCADE       Delete child rows when a parent key is deleted.        ALTER TABLE Emp2 ADD CONSTRAINT emp_dt_fk  ...
Dropping a Constraint       •     Remove the manager constraint from the EMP2             table.           ALTER TABLE emp...
Disabling Constraints       •     Execute the DISABLE clause of the ALTER TABLE             statement to deactivate an int...
Enabling Constraints       •     Activate an integrity constraint currently disabled             in the table definition b...
Notes Only9-52   Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
Cascading Constraints       •   The CASCADE CONSTRAINTS clause is used along           with the DROP COLUMN clause.       ...
Cascading Constraints       Example:        ALTER TABLE emp2        DROP COLUMN employee_id CASCADE CONSTRAINTS;        Ta...
查看约束条件       查询 USER_CONSTRAINTS 表可以看到用户的所有约束条       件的名字和定义.       SELECT constraint_name,constraint_type,              s...
查看与约束相关的列       在 USER_CONS_COLUMNS         USER_CONS_COLUMNS视图中可以看到与约束相关的列       的信息.       SELECT       constraint_name,...
Dropping a Table       •   All data and structure in the table are deleted.       •   Any pending transactions are committ...
改变表的名字       •     为了改变表、视图、序列或者同义词的名字,执行             RENAME语句.             RENAME           RENAME dept TO detail_dept;  ...
截断表                TABLE语句:       TRUNCATE TABLE       •   从表中删掉所有的行       •   释放被表使用的存储空间        TRUNCATE TABLE detail_de...
为表增加注释       可以使用 COMMENT 语句为表或者列增加注释信息.        COMMENT ON TABLE employees        IS Employee Information;        Comment ...
Summary       In this lesson, you should have learned how to use the       CREATE TABLE statement to create a table and in...
Practice 9: Overview       This practice covers the following topics:        • Creating new tables        • Creating a new...
Upcoming SlideShare
Loading in...5
×

Lesson09

205

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
205
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lesson09

  1. 1. Oracle OCP 考试系列培训 之 1Z0-007 Lesson9 www.OracleOnLinux.cn9-1 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  2. 2. 9 Using DDL Statements to Create and Manage TablesCopyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  3. 3. Objectives After completing this lesson, you should be able to do the following: • Categorize the main database objects • Review the table structure • List the data types that are available for columns • Create a simple table • Understand how constraints are created at the time of table creation • Describe how schema objects work9-3 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  4. 4. Database Objects Object Description Table Basic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric values Index Improves the performance of some queries Synonym Gives alternative names to objects9-4 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  5. 5. 在 Oracle 数据库中的表 用户表: • 由用户创建和维护的一些表 • 存放用户数据的信息 数据字典表: • 由Oracle数据库创建和维护 • 存放Oracle数据库自身的信息、数据库对象信息9-5 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  6. 6. 查询数据字典 • 查看用户拥有的表: SELECT table_name FROM user_tables ; • 查看用户拥有的所有对象: SELECT object_name object_name FROM user_objects ; • 查看用户拥有的表、视图、 同义词和序列: SELECT * FROM user_catalog ;9-6 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  7. 7. Naming Rules Table names and column names: • Must begin with a letter • Must be 1–30 characters long 1– • Must contain only A–Z, a–z, 0–9, _, $, and # A– a– 0– • Must not duplicate the name of another object owned by the same user • Must not be an Oracle server reserved word9-7 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  8. 8. CREATE TABLE Statement • You must have: – CREATE TABLE privilege – A storage area CREATE TABLE [schema.]table (column datatype [DEFAULT expr][, ...]); • You specify: – Table name – Column name, column data type, and column size P79 Q1329-8 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  9. 9. Referencing Another User ’s Tables User’ • Tables belonging to other users are not in the user’s schema. user’ • You should use the owner ’s name as a prefix to owner’ those tables. USERA USERB SELECT * SELECT * FROM userB.employees; FROM userA.employees;9-9 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  10. 10. DEFAULT Option • Specify a default value for a column during an insert. ... hire_date DATE DEFAULT SYSDATE, ... • Literal values, expressions, or SQL functions are legal values. • Another column’s name or a pseudocolumn are column’ illegal values. • The default data type must match the column data type. CREATE TABLE hire_dates (id NUMBER(8), hire_date DATE DEFAULT SYSDATE); Table created.9-10 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  11. 11. Creating Tables • Create the table. CREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(14), loc VARCHAR2(13), create_date DATE DEFAULT SYSDATE); Table created. • Confirm table creation. DESCRIBE dept9-11 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  12. 12. Data Types Data Type Description VARCHAR2( size ) Variable-length character data CHAR( size ) Fixed-length character data NUMBER( p,s) Variable-length numeric data DATE Date and time values LONG Variable-length character data (up to 2 GB) CLOB Character data (up to 4 GB) RAW and LONG Raw binary data RAW BLOB Binary data (up to 4 GB) BFILE Binary data stored in an external file (up to 4 GB) ROWID A base-64 number system representing the unique address of a row in its table9-12 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  13. 13. 9-13 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  14. 14. ROWID Format9-14 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  15. 15. ROWID • Get the rowid: SELECT ROWID FROM employees; • Get the detail of rowid: select rowid, dbms_rowid.rowid_object(rowid) object_id, dbms_rowid.rowid_relative_fno(rowid) file_id, dbms_rowid.rowid_block_number(rowid) block_id , dbms_rowid.rowid_row_number(rowid) num , rowidtochar(rowid) from employees;9-15 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  16. 16. ROWID • Get the detail of rowid: select rowid , substr(rowid,1,6) "OBJECT", substr(rowid,7,3) "FILE", substr(rowid,10,6) "BLOCK", substr(rowid,16,3) "ROW" from TableName;9-16 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  17. 17. ROWID Format9-17 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  18. 18. ROWID Format Locating a Row Using ROWID • Because a segment can only reside in one tablespace, by using the data object number, the Oracle server can determine the tablespace that contains a row. • The relative file number within the tablespace is used to locate the file, the block number is used to locate the block containing the row, and the row number is used to locate the row directory entry for the row. • The row directory entry can be used to locate the beginning of the row.Thus, ROWID can be used to locate any row within a database.9-18 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  19. 19. Including Constraints • Constraints enforce rules at the table level. • Constraints prevent the deletion of a table if there are dependencies. • The following constraint types are valid: – NOT NULL – UNIQUE – PRIMARY KEY – FOREIGN KEY – CHECK P60 Q96, P71 Q113,P79 Q1319-19 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  20. 20. Constraint Guidelines • You can name a constraint, or the Oracle server generates a name by using the SYS_Cn format. • Create a constraint at either of the following times: – At the same time as the table is created – After the table has been created • Define a constraint at the column or table level. • View a constraint in the data dictionary.9-20 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  21. 21. Defining Constraints • Syntax: CREATE TABLE [schema.]table (column datatype [DEFAULT expr] [column_constraint], ... [table_constraint][,...]); ][,...]); • Column-level constraint: column [CONSTRAINT constraint_name] constraint_type, • Table-level constraint: column,... [CONSTRAINT constraint_name] constraint_type (column, ...),9-21 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  22. 22. Defining Constraints • Column-level constraint: CREATE TABLE employees( employee_id NUMBER(6) CONSTRAINT emp_emp_id_pk PRIMARY KEY, 1 first_name VARCHAR2(20), ...); • Table-level constraint: CREATE TABLE employees( employee_id NUMBER(6), first_name VARCHAR2(20), ... job_id VARCHAR2(10) NOT NULL, 2 CONSTRAINT emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID));9-22 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  23. 23. NOT NULL Constraint Defined at the column level only. level Ensures that null values are not permitted for the column: … NOT NULL constraint NOT NULL Absence of NOT NULL (No row can contain constraint constraint a null value for (Any row can contain this column.) a null value for this column.)9-23 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  24. 24. UNIQUE Constraint UNIQUE constraint EMPLOYEES … INSERT INTO Allowed Not allowed: already exists9-24 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  25. 25. UNIQUE Constraint Defined at either the table level or the column level: level: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, ... CONSTRAINT emp_email_uk UNIQUE(email));9-25 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  26. 26. PRIMARY KEY Constraint DEPARTMENTS PRIMARY KEY … Not allowed INSERT INTO (null value) Not allowed (50 already exists)9-26 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  27. 27. FOREIGN KEY Constraint DEPARTMENTSPRIMARY KEY … EMPLOYEES FOREIGN KEY… Not allowed INSERT INTO (9 does not exist) Allowed 9-27 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  28. 28. FOREIGN KEY Constraint Defined at either the table level or the column level: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, ... department_id NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), CONSTRAINT emp_email_uk UNIQUE(email)); P75 Q1249-28 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  29. 29. FOREIGN KEY Constraint: Keywords • FOREIGN KEY: Defines the column in the child table at the table-constraint level • REFERENCES: Identifies the table and column in the parent table • ON DELETE CASCADE: Deletes the dependent rows in the child table when a row in the parent table is deleted • ON DELETE SET NULL: Converts dependent foreign key values to null9-29 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  30. 30. CHECK Constraint • Defines a condition that each row must satisfy • CHECK constraints can be defined at the column level or table level • The following expressions are not allowed: – References to CURRVAL, NEXTVAL, LEVEL, and ROWNUM pseudocolumns – Calls to SYSDATE, UID, USER, and USERENV functions – Queries that refer to other values in other rows ..., salary NUMBER(2) CONSTRAINT emp_salary_min CHECK (salary > 0),...9-30 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  31. 31. CREATE TABLE : Example TABLE: CREATE TABLE employees ( employee_id NUMBER(6) CONSTRAINT emp_employee_id PRIMARY KEY , first_name VARCHAR2(20) , last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn NOT NULL , email VARCHAR2(25) CONSTRAINT emp_email_nn NOT NULL CONSTRAINT emp_email_uk UNIQUE , phone_number VARCHAR2(20) , hire_date DATE CONSTRAINT emp_hire_date_nn NOT NULL , job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL , salary NUMBER(8,2) CONSTRAINT emp_salary_ck CHECK (salary>0) , commission_pct NUMBER(2,2) , manager_id NUMBER(6) , department_id NUMBER(4) CONSTRAINT emp_dept_fk REFERENCES departments (department_id));9-31 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  32. 32. Violating Constraints UPDATE employees SET department_id = 55 WHERE department_id = 110; UPDATE employees * ERROR at line 1: ORA-02291: integrity constraint (HR.EMP_DEPT_FK) violated - parent key not found Department 55 does not exist.9-32 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  33. 33. Violating Constraints You cannot delete a row that contains a primary key that is used as a foreign key in another table. DELETE FROM departments WHERE department_id = 60; DELETE FROM departments * ERROR at line 1: ORA-02292: integrity constraint (HR.EMP_DEPT_FK) violated - child record found9-33 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  34. 34. Creating a Table by Using a Subquery • Create a table and insert rows by combining the CREATE TABLE statement and the AS subquery option. CREATE TABLE table [(column, column...)] AS subquery; • Match the number of specified columns to the number of subquery columns. • Define columns with column names and default values.9-34 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  35. 35. Creating a Table by Using a Subquery CREATE TABLE dept80 AS SELECT employee_id, last_name, salary*12 ANNSAL, salary* ANNSAL, hire_date FROM employees WHERE department_id = 80; Table created. DESCRIBE dept809-35 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  36. 36. The ALTER TABLE Statement Use the ALTER TABLE statement to: • Add a new column • Modify an existing column • Define a default value for the new column • Drop a column9-36 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  37. 37. The ALTER TABLE Statement Use the ALTER TABLE statement to add, modify, or drop columns. ALTER TABLE table ADD (column datatype [DEFAULT expr] [, column datatype ]...); ALTER TABLE table MODIFY (column datatype [DEFAULT expr] [, column datatype ]...); ALTER TABLE table DROP (column);9-37 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  38. 38. Adding a Column • You use the ADD clause to add columns. ALTER TABLE dept80 ADD (job_id VARCHAR2(9)); Table altered. • The new column becomes the last column. …9-38 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  39. 39. Adding a Column • You use the ADD clause to add columns. • The new column becomes the last column. • 表初始有记录,增加字段后,没有指定默认值则该字段 所有记录对应值为NULL,否则,对应记录为指定默认 值 • 表初始有记录,不能增加非空字段 • 表初始无记录,才能增加非空字段9-39 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  40. 40. Modifying a Column • You can change a column’s data type, size, and column’ default value. ALTER TABLE dept80 MODIFY (last_name VARCHAR2(30)); Table altered. • A change to the default value affects only subsequent insertions to the table. • 改变字段的数据类型时,该字段初始值必须为NULL • Varchar2(10)--->Char(6) --->Varchar2(4), 注意字段长 Varchar2(10)--->Char(6)---> --->Varchar2(4), 度问题[宽变窄,窄变宽] P71 Q1129-40 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  41. 41. Dropping a Column Use the DROP COLUMN clause to drop columns you no longer need from the table. ALTER TABLE dept80 DROP COLUMN job_id; Table altered.9-41 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  42. 42. Dropping a Column Use the DROP COLUMN clause to drop columns you no longer need from the table. • 删除字段,不可逆操作(DDL) • 属于联合约束、组合索引的字段不可删,除非带有 cascade键字9-42 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  43. 43. The SET UNUSED Option • You 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. ALTER TABLE <table_name> SET UNUSED(< column_name> ); OR ALTER TABLE <table_name> SET UNUSED COLUMN <column_name>; ALTER TABLE < table_name> DROP UNUSED COLUMNS;9-43 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  44. 44. The SET UNUSED Option • SET UNUSED 并非真正从目标表上删除数据,占存储 空间 • SET UNUSED 的列不再可以使用,DESC无效 • SET UNUSED 的列可从 USER_UNUSED_COL_TABS视 USER_UNUSED_COL_TABS 图查看相关信息 ALTER TABLE dept80 SET UNUSED( last_name ); UNUSED(last_name last_name); ALTER TABLE dept80 DROP UNUSED COLUMN S; COLUMNS;9-44 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  45. 45. Notes Only9-45 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  46. 46. Adding a Constraint Syntax Use the ALTER TABLE statement to: • Add or drop a constraint, but not modify its structure • Enable or disable constraints • Add a NOT NULL constraint by using the MODIFY clause ALTER TABLE <table_name> ADD [CONSTRAINT < constraint_name> ] type (<column_name> );9-46 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  47. 47. Adding a Constraint Add 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 emp2 modify employee_id Primary Key; Table altered. ALTER TABLE emp2 ADD CONSTRAINT emp_mgr_fk FOREIGN KEY(manager_id) REFERENCES emp2(employee_id); Table altered.9-47 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  48. 48. ON DELETE CASCADE Delete child rows when a parent key is deleted. ALTER TABLE Emp2 ADD CONSTRAINT emp_dt_fk FOREIGN KEY (Department_id) REFERENCES departments (dept_id) departments(dept_id) ON DELETE CASCADE ; CASCADE; Table altered.9-48 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  49. 49. Dropping a Constraint • Remove the manager constraint from the EMP2 table. ALTER TABLE emp2 DROP CONSTRAINT emp_mgr_fk; Table altered. • Remove the PRIMARY KEY constraint on the DEPT2 table and drop the associated FOREIGN KEY constraint on the EMP2.DEPARTMENT_ID column. ALTER TABLE dept2 DROP PRIMARY KEY CASCADE; CASCADE; Table altered.9-49 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  50. 50. Disabling Constraints • Execute the DISABLE clause of the ALTER TABLE statement to deactivate an integrity constraint. • Apply the CASCADE option to disable dependent integrity constraints. ALTER TABLE emp2 DISABLE CONSTRAINT emp_dt_fk; Table altered.9-50 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  51. 51. Enabling Constraints • Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause. ALTER TABLE emp2 ENABLE CONSTRAINT emp_dt_fk; Table altered. • A UNIQUE index is automatically created if you enable a UNIQUE key or PRIMARY KEY constraint.9-51 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  52. 52. Notes Only9-52 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  53. 53. Cascading Constraints • The CASCADE CONSTRAINTS clause is used along with the DROP COLUMN clause. • The 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.9-53 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  54. 54. Cascading Constraints Example: ALTER TABLE emp2 DROP COLUMN employee_id CASCADE CONSTRAINTS; Table altered. ALTER TABLE test1 DROP (pk, fk, col1) CASCADE CONSTRAINTS; Table altered.9-54 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  55. 55. 查看约束条件 查询 USER_CONSTRAINTS 表可以看到用户的所有约束条 件的名字和定义. SELECT constraint_name,constraint_type, search_condition FROM user_constraints WHERE table_name=EMPLOYEES …9-55 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  56. 56. 查看与约束相关的列 在 USER_CONS_COLUMNS USER_CONS_COLUMNS视图中可以看到与约束相关的列 的信息. SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = EMPLOYEES; … P72 Q1179-56 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  57. 57. Dropping a Table • All data and structure in the table are deleted. • Any pending transactions are committed. • All indexes are dropped. • All constraints are dropped. • You cannot roll back the DROP TABLE statement. DROP TABLE dept80; Table dropped. P72 Q1169-57 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  58. 58. 改变表的名字 • 为了改变表、视图、序列或者同义词的名字,执行 RENAME语句. RENAME RENAME dept TO detail_dept; Table renamed. • 必须是对象的拥有者.9-58 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  59. 59. 截断表 TABLE语句: TRUNCATE TABLE • 从表中删掉所有的行 • 释放被表使用的存储空间 TRUNCATE TABLE detail_dept; Table truncated. TRUNCATE语句不可以回滚. TRUNCATE 相应的, 也可以使用 DELETE 语句删掉所有的行. P56 Q899-59 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  60. 60. 为表增加注释 可以使用 COMMENT 语句为表或者列增加注释信息. COMMENT ON TABLE employees IS Employee Information; Comment created. 注释可以通过下列数据字典查看: • ALL_COL_COMMENTS • USER_COL_COMMENTS • ALL_TAB_COMMENTS • USER_TAB_COMMENTS9-60 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  61. 61. Summary In this lesson, you should have learned how to use the CREATE TABLE statement to create a table and include constraints. • Categorize the main database objects • Review the table structure • List the data types that are available for columns • Create a simple table • Understand how constraints are created at the time of table creation • Describe how schema objects work9-61 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  62. 62. Practice 9: Overview This practice covers the following topics: • Creating new tables • Creating a new table by using the CREATE TABLE AS syntax • Verifying that tables exist • Dropping tables9-62 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×