13 Copyright © Oracle Corporation, 2001. All rights reserved. Maintaining Data Integrity.

Slides:



Advertisements
Similar presentations
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
12 Copyright © Oracle Corporation, All rights reserved. Managing Indexes.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Creating Triggers.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Managing Schema Objects
Data Integrity Constraints
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
12 Copyright © Oracle Corporation, All rights reserved. Other Database Objects.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
17 Copyright © Oracle Corporation, All rights reserved. Managing Roles.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
12 Copyright © 2004, Oracle. All rights reserved. Understanding and Influencing the PL/SQL Compiler.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Week 5 Lecture 2 Data Integrity Constraints. Learning Objectives  Learn the types and the uses of constraints  Examine the syntax and options for creating.
Module 4: Implementing Data Integrity
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
6 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
D Copyright © Oracle Corporation, All rights reserved. Loading Data into a Database.
Oracle Data Integrator Data Quality (Integrity Control)
10 Copyright © 2004, Oracle. All rights reserved. Recovering from User Errors.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
2 Copyright © 2006, Oracle. All rights reserved. Managing Schema Objects.
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
Chapter 4 Indexes. Indexes Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage; composed.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
7 Copyright © 2004, Oracle. All rights reserved. Managing Schema Objects.
23 Copyright © 2004, Oracle. All rights reserved. Preferring the Past: Flashback.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
11 Copyright © 2009, Oracle. All rights reserved. Enhancing ETL Performance.
15 Copyright © Oracle Corporation, All rights reserved. Managing Users.
Including Constraints
SQL Creating and Managing Tables
Module 5: Implementing Data Integrity by Using Constraints
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Creating and Managing Tables
Constraints.
Chapter 4 Indexes.
CH 4 Indexes.
CH 4 Indexes.
Including Constraints
Presentation transcript:

13 Copyright © Oracle Corporation, All rights reserved. Maintaining Data Integrity

13-2 Copyright © Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Implement data integrity constraints Maintain integrity constraints Obtain constraint information from the data dictionary

13-3 Copyright © Oracle Corporation, All rights reserved. Data Integrity Application code Table Data Integrity constraint Database trigger

13-4 Copyright © Oracle Corporation, All rights reserved.

13-5 Copyright © Oracle Corporation, All rights reserved. Types of Constraints Constraint NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK Description Specifies that a column cannot contain null values Designates a column or combination of columns as unique Designates a column or combination of columns as the table’s primary key Designates a column or combination of columns as the foreign key in a referential integrity constraint Specifies a condition that each row of the table must satisfy

13-6 Copyright © Oracle Corporation, All rights reserved. Constraint States ENABLE NOVALIDATE Existing dataNew data DISABLE NOVALIDATE DISABLE VALIDATE = = ENABLE VALIDATE

13-7 Copyright © Oracle Corporation, All rights reserved.

13-8 Copyright © Oracle Corporation, All rights reserved. Constraint Checking DML statement Check nondeferred constraints COMMIT Check deferred constraints

13-9 Copyright © Oracle Corporation, All rights reserved. Defining Constraints Immediate or Deferred Use the SET CONSTRAINTS statement to make constraints either DEFERRED or IMMEDIATE. The ALTER SESSION statement also has clauses to SET CONSTRAINTS to DEFERRED or IMMEDIATE.

13-10 Copyright © Oracle Corporation, All rights reserved. Primary and Unique Key Enforcement Is an index available for use? Yes No Yes No Create nonunique index Create unique index Do not use index Use existing index Key enabled? Constraint deferrable? Constraint Deferrable? Is the index nonunique? Yes No/Yes No

13-11 Copyright © Oracle Corporation, All rights reserved. Foreign Key Considerations Appropriate SolutionDesired Action Drop parent table Cascade constraints Truncate parent tableDisable or drop foreign key Perform DML on child tableEnsure that the tablespace containing the parent key is online Use the CASCADE CONSTRAINTS clause Drop tablespace containing parent table

13-12 Copyright © Oracle Corporation, All rights reserved.

13-13 Copyright © Oracle Corporation, All rights reserved. Defining Constraints While Creating a Table CREATE TABLE hr.employee( id NUMBER(7) CONSTRAINT employee_id_pk PRIMARY KEY DEFERRABLE USING INDEX STORAGE(INITIAL 100K NEXT 100K) TABLESPACE indx, last_name VARCHAR2(25) CONSTRAINT employee_last_name_nn NOT NULL, dept_id NUMBER(7)) TABLESPACE users;

13-14 Copyright © Oracle Corporation, All rights reserved.

13-15 Copyright © Oracle Corporation, All rights reserved.

13-16 Copyright © Oracle Corporation, All rights reserved.

13-17 Copyright © Oracle Corporation, All rights reserved. Guidelines for Defining Constraints Primary and unique constraints: –Place indexes in a separate tablespace. –Use nonunique indexes if bulk loads are frequent. Self-referencing foreign keys: –Define or enable foreign keys after the initial load. –Defer constraint checking.

13-18 Copyright © Oracle Corporation, All rights reserved. Enabling Constraints No locks on table Primary and unique keys must use nonunique indexes ENABLE NOVALIDATE ALTER TABLE hr.departments ENABLE NOVALIDATE CONSTRAINT dept_pk;

13-19 Copyright © Oracle Corporation, All rights reserved.

13-20 Copyright © Oracle Corporation, All rights reserved.

13-21 Copyright © Oracle Corporation, All rights reserved. Enabling Constraints Locks the table Can use unique or nonunique indexes Needs valid table data ENABLE VALIDATE ALTER TABLE hr.employees ENABLE VALIDATE CONSTRAINT emp_dept_fk;

13-22 Copyright © Oracle Corporation, All rights reserved.

13-23 Copyright © Oracle Corporation, All rights reserved. Using the EXCEPTIONS Table Create the EXCEPTIONS table by running the utlexcpt1.sql script. Execute the ALTER TABLE statement with EXCEPTIONS option. Use subquery on EXCEPTIONS to locate rows with invalid data. Rectify the errors. Reexecute ALTER TABLE to enable the constraint.

13-24 Copyright © Oracle Corporation, All rights reserved.

13-25 Copyright © Oracle Corporation, All rights reserved.

13-26 Copyright © Oracle Corporation, All rights reserved. Obtaining Constraint Information Obtain information about constraints by querying the following views: DBA_CONSTRAINTS DBA_CONS_COLUMNS

13-27 Copyright © Oracle Corporation, All rights reserved.

13-28 Copyright © Oracle Corporation, All rights reserved.

13-29 Copyright © Oracle Corporation, All rights reserved. Summary In this lesson, you should have learned how to: Implement data integrity Use an appropriate strategy to create and maintain constraints Obtain information from the data dictionary

13-30 Copyright © Oracle Corporation, All rights reserved. Practice 13 Overview This practice covers the following topics: Creating constraints Enabling unique constraints Creating an EXCEPTIONS table Identifying existing constraint violations in a table, correcting the errors, and reenabling the constraints

13-31 Copyright © Oracle Corporation, All rights reserved.

13-32 Copyright © Oracle Corporation, All rights reserved.