data integrity an empty database is a correct database
TRANSCRIPT
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
Review
• Tuple
• Cardinality
• Attribute
• Degree
• Domain
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?
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
Data Types
• Hide complexity within domain
• DATE domain
– three integers (d,m,y)
– interpreted by operators
• Address domain
– USA - street, city, state, zip
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);
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
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
Is our information Correct ?
Consistent ?Logical ?
Start with a consistent database
Update the database
Result a consistent 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?
How do we maintain correct, consistent data ?
Data Integrity Rules– Candidate keys– Domains
Referential Integrity Rules– Foreign keys
Business Rules– Constraints– Stored Procedures, Triggers
Where do we maintain correct, consistent data ?
• Database creation– base tables– views
• Implementation– back end– front end applications– middleware
Integrity rules are database specific
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
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
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?
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
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
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
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
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
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;