data integrity an empty database is a correct database

23
Data Integrity An empty database is a correct database

Upload: philip-simpson

Post on 16-Jan-2016

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Integrity An empty database is a correct database

Data Integrity

An empty database is a correct database

Page 2: Data Integrity An empty database is a correct database

Overview

• Review

• Domains

• What are integrity constraints?

• Gulf between the theory and the implementation

• Testing insertions into database?

• NULLS

Page 3: Data Integrity An empty database is a correct database

Review

• Tuple

• Cardinality

• Attribute

• Degree

• Domain

Page 4: Data Integrity An empty database is a correct database

Domains• Conceptual pool of values from which one

or more columns draw their values• Named set of scalar values all the same type

– scalar is smallest semantic unit of data, individual data value

• CREATE DOMAIN …? ?• Domain contains ALL permitted values,

static• Which relations in the database contain any

information pertaining to suppliers?

Page 5: Data Integrity An empty database is a correct database

Comparisons with Domains• Domains constrain comparisons, = ,<>• domain-check override Codd• Logically equivalent statements must have

same semantic interpretation • Logic cannot be nullified

a = c Tb = c Ta = b F

• Arithmetic operators

Page 6: Data Integrity An empty database is a correct database

Data Types

• Hide complexity within domain

• DATE domain

– three integers (d,m,y)

– interpreted by operators

• Address domain

– USA - street, city, state, zip

Page 7: Data Integrity An empty database is a correct database

ADT in Oracle

• Simulate a domain• CREATE TYPE ADDR_TY AS OBJECT

– (STREET VARCHAR2(50),– CITY VARCHAR2(25),– STATE CHAR(2),– ZIP VARCHAR(9));

• CREATE TABLE NEW_CUSTOMER– ( CUSTID VARCHAR2(9) PRIMARY KEY,– CUST_NAME VARCHAR2(25),– ADDRESS ADDR_TY);

Page 8: Data Integrity An empty database is a correct database

Relations• Abstract object• A relation, r, on a collection of domains, D1, D2

…Dn consists of two parts a heading and a body– heading - fixed set of attributes, <attribute-

name:domain-name> pairs

– body - set of tuples, <attribute-name:attribute-vale> pairs

• Named relation is really a variable• Relations dynamic• Table is concrete picture

Page 9: Data Integrity An empty database is a correct database

Candidate Keys

• Subset of all attributes that are– Unique– Irreducibile

• Does not relate to current data set but to set of ALL POSSIBLE values– simple– composite

• Provide tuple-level addrressing mechanism• Primary key is selected from candidate key.

Others called Alternate keys

Page 10: Data Integrity An empty database is a correct database

Is our information Correct ?

Consistent ?Logical ?

Page 11: Data Integrity An empty database is a correct database

Start with a consistent database

Update the database

Result a consistent database

Page 12: Data Integrity An empty database is a correct database

Is the data consistent?

Is the data consistent between tables? Are the same values equivalent? Do foreign keys reference existent primary

keys?Has logical consistency been

maintained?Have the business rules been enforced?All constraints valid?

Page 13: Data Integrity An empty database is a correct database

How do we maintain correct, consistent data ?

Data Integrity Rules– Candidate keys– Domains

Referential Integrity Rules– Foreign keys

Business Rules– Constraints– Stored Procedures, Triggers

Page 14: Data Integrity An empty database is a correct database

Where do we maintain correct, consistent data ?

• Database creation– base tables– views

• Implementation– back end– front end applications– middleware

Integrity rules are database specific

Page 15: Data Integrity An empty database is a correct database

Foreign Keys

• If the foreign key contains either matching values or nulls, the table(s) that make use of such a foreign key are said to exhibit referential integrity.

• An attribute in one table refers to a primary key in another table– can it be a candidate key?– can it be the same table?

• A FK is composite if the primary key is composite.Referential Integrity assures that no invalid foreign keys permitted

• Relationships formed through foreign keys but not exclusively

Page 16: Data Integrity An empty database is a correct database

Maintaining Integrity with Foreign Keys

Correct DBCorrect DBTransform thru update

•Delete Parent•restrict•cascades

•Update Parent•restrict•cascade

•How many levels of cascade?•Define procedures

Note: Correct database satisfies logical AND of all known rules

Page 17: Data Integrity An empty database is a correct database

Entity Integrity• No component of the primary key of a base

relation is allowed to accept NULLS.

• In a relational database, we never record information about something we cannot identify.

• No attribute included in composite primary key can be NULL.

• Can alternate keys have NULLS?

Page 18: Data Integrity An empty database is a correct database

Null Foreign Keys

• Can a foreign key be null?– definition - matches primary key or is null

• Can a composite foreign key have some attributes null?

• ON DELETE SET NULL– allows child record to remain but deleted key

attribute becomes null

Page 19: Data Integrity An empty database is a correct database

Rules or Constraints• Theory

– CREATE INTERGIRTY RULE PR4• FORALL PX(PX.WEIGHT>0)• ON ATTMPTED VIOLATION REJECT;

• Oracle– CREATE TABLE PX ...

• WEIGHT NUMBER• CONSTRAINT PR4• CHECK (WEIGHT >0);

• Components name, truth-statement, violation response

Page 20: Data Integrity An empty database is a correct database

Relation Rules or Constraints

• Theory– CREATE INTERGIRTY RULE SR8

• FORALL S(IF S.CITY = ‘LONDON’ THEN S.STATUS = 20)

• ON ATTMPTED VIOLATION REJECT;

• OracleMust be closed WFF

Page 21: Data Integrity An empty database is a correct database

Database Rules

• CREATE INTEGRITY RULE C95– FORALL SX (FORALL SPX– (IF SX.STATUS < 20 AND – SX.S# = SPX.S#– THEN SPX>QTY <=500));

• Oracle

Note required join condition

Page 22: Data Integrity An empty database is a correct database

Database Rules

• How do we execute the following:– For every orderline there must exist an order.– Every customer must place an order.– No salesrep can have more than 200 customers.– Every orderline must contain a valid part.

• When do we execute

Page 23: Data Integrity An empty database is a correct database

Transition Rules

• Applied as go from one state to another

• Employee marries

• CREATE INTEGRITY RULE S12– IF S’.S# = S.S# THEN– S’.STATUS < S.STATUS;