Sie sind auf Seite 1von 32

Database Systems

The Relational Model of Data

The term relation is basically just a mathematical term for a table. DBMS products that are based on the relational model of data have come to dominate the database marketplace. The introduction of the relational model by E. F. Codd in 1969-70 was the most important event in the entire history of the database field.

The Relational Model continued

C. J. Date was the first to recognize the significance of the relational model and has been the most recognized author, lecturer, researcher, and consultant of the relational technology He is best known for his book An Introduction to Database Systems (seventh edition published in 2000), which has sold nearly 650,000 copies and is used by several hundred colleges and universities worldwide.

Terminology
Domains

S#

NAME

STATUS

CITY London, Tucson, Paris, ...

Primary Key
S#:S# SNAME:NAME STATUS:STATUS CITY:CITY

S1 S2 S3 S4 S5

Smith Jones Blake Clark Adams

20 10 30 20 30

London Paris Paris London Athens

Relation

Attributes

Tuple

Relational Data Objects: Domains and Relations


A relation is table A tuple corresponds to a row of such a table and attribute to a column The number of tuples is called the cardinality and number of attributes are called degree

Domain

A domain is a pool of values, from which specific attributes of specific relations draw their actual values. Synonym of data type (Type for short).

Can be system-defined, e.g., text, number, currency, yes/no, date/time, hyperlink, autonumber. Or user-defined, e.g., POINT.

For example, the domain labeled S# is the set of all legal supplier numbers, and the set of S# values appearing in relation S at any given time is constrained to be some subset of that set.

Terminologies Used:
Formal Relation Tuple Cardinality Attribute Degree Domain Informal table row or record number of rows column or field number of columns pool of legal values

Tables

Relational database is a collection of tables Heading: table name and column names Body: rows, occurrences of data

Student
StdSSN 123-45-6789 124-56-7890 234-56-7890 StdLastName WELLS NORBERT KENDALL StdMajor IS FIN ACCT StdClass FR JR JR StdGPA 3.00 2.70 3.50

Integrity (Fig. from C.J. Date)


S
S# S1 S2 S3 S4 S5 P# P1 P2 P3 P4 P5 P6 SNAME STATUS CITY Smith 20 London Jones 10 Paris Blake 30 Paris Clark 20 London Adams 30 Athens PNAME Nut Bolt Screw Screw Cam Cog COLOR WEIGHT CITY Red 12 London Green 17 Paris Blue 17 Rome Red 14 London Blue 12 Paris Red 19 London

SP

S# S1 S1 S1 S1 S1 S1 S2 S2 S3 S4 S4 S4

P# P1 P2 P3 P4 P5 P6 P1 P2 P2 P2 P4 P5

QTY 300 200 400 200 100 100 300 400 200 200 300 400

Integrity

The term integrity refers to the accuracy and correctness of data in the database. The DBMS should automatically enforce specified integrity constraints.

Entity integrity: primary keys


Each table has column(s) with unique values Ensures entities are traceable Values of a column in one table match values from a source table Ensures valid references among tables

Referential integrity: foreign keys

Integrity (Cont)
The term integrity refers to the accuracy or correctness of data in the database. Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. A given database might be subject to any number of integrity constraints of arbitrary complexity. The DBMS needs to be informed of such constraints, needs to enforce them somehow. Basically by rejecting any update that would otherwise violate them.

Integrity (Cont)

When a new constrain is declared, The system must first make sure the database currently satisfies it; If not, the new constraint is rejected; Otherwise it is accepted and enforced from that point forward. We can get rid of existing constraints.

Types of Constraints

Type (domain) Constraint:

specifies the legal values for a given type.


specifies the legal values for a given attribute. specifies the legal values for a given table. Multiple attributes may be involved. specifies the legal values for a given database. Multiple tables may be involved.

Attribute constraint:

Table (relation) constraint:


Database constraint:

Type or Domain Constraints

Type constraints are the most elementary form of integrity constraint. A type constraints is an enumeration of the legal value of the type, a specification of the values that make up the type in question. Type constraints are checked immediately.

Type or Domain Constraints (Cont.)

The check clause in SQL permits domains to be restricted: Use check clause to ensure that an hourly-wage domain allows only values greater than a specified value. create domain hourly-wage numeric(5,2) constraint value-test check(value > = 4.00) The domain hourly-wage is declared to be a decimal number with 5 digits, 2 of which are after the decimal point The domain has a constraint that ensures that the hourly-wage is greater than 4.00

Attribute Constraints

An attribute constraint is just a declaration to the effect that a specified attribute is of a specified type. For example: create table account (branch-name char(15), account-number char(10) not null, balance integer, ) Attribute constraints are part of the definition of the attribute. Any attempt to introduce an attribute value into the database that is not a type of the relevant type will simply rejected. Such a situation should never arise.

Table Constraint

A table constraint is a constraint on an individual table. Example:


Suppliers in London must have status 20. Two attributes, CITY and STATUS, of table S are involved.

Database Constraint

A database constraint is a constraint that interrelates two or more distinct tables. Example:

No suppliers with status less than 20 can supply any part in a quantity greater than 500. Two tables, S and SP, are involved.

Reference:

Chapter No 4 of the book by (C. J. Date) Home work: Read the chapter 4 of the book

Keys

The concept of Keys is very important to the relational model. We'll discuss the following types of keys.

Candidate Keys

Primary Keys and Alternative Keys

Foreign Keys

Formal Definitions

Candidate key: minimal number of attributes with unique values. Null value: special value meaning value unknown or inapplicable Primary key: a designated candidate key; cannot contain null values Foreign key: column(s) whose values must match the values in a candidate key of another table

Candidate Keys

Let R be a table. By definition, at any given time, no two tuples in the value of R are duplicates of one another.
Let K be a set of attributes of R. Then K is a candidate key for R if and only if it satisfies:
(1) Uniqueness: No legal value of R ever contains two distinct tuples with the same value for K. (2) Irreducibility (or minimality): No proper subset of K has the uniqueness property.

Candidate Key Examples


For S {S#, SNAME, STATUS, CITY}, S# and SNAME do not have duplicates, so Candidate KEY {S#} Candidate KEY {SNAME}
For SP {S#, P#, QTY}, {S#, P#} does not have duplicates, so Candidate KEY {S#, P#}

Primary and Alternative Keys

Exactly one candidate key must be chosen as the primary key, and the others are alternative keys. Primary key and alternative keys are both candidate keys. For S {S#, SNAME, STATUS, CITY}, if we choose KEY {S#} as the primary key, then KEY {SNAME} is an alternative key.

For SP {S#, P#, QTY}, We must choose KEY {S#, P#} as the primary key, and there will be no alternative key.

Foreign Keys

A foreign key is a set of attributes of one table R2 whose values are required to match values of some candidate key of some table R1. {S#} is a foreign key of SP that references the primary key {S#} of S. Any S# value of SP must exist in S. Similarly, {P#} is another foreign key of SP that references the primary key {P#} of P.

Integrity Rules

Entity integrity

No two rows with the same primary key value No null values in a primary key Foreign keys must match candidate key of source table Foreign keys in some cases can be null The database must not contain any unmatched foreign key values. If B references A, A must exist.

Referential integrity

Referenced Rows

Referenced row

Foreign keys reference rows in the associated primary key table Each supply row references supplier and part rows. Delete a referenced row Change the primary key of a referenced row Referential integrity should not be violated

Actions on referenced rows


Possible Actions

Restrict: do not permit action on the referenced row Cascade: perform action on related rows Nullify: only valid if foreign keys accept null values Default: set foreign keys to a default value

Referential Actions on Foreign Keys

When we delete a tuple from a table, say S, that is referenced by another table, say SP, if the primary key {S#} value of S exists in SP, there are several choices of referential actions:

ON DELETE CASCADE

The corresponding tuples in SP will be deleted too. The deletion of the tuple from S is rejected.

ON DELETE RESTRICT

Referential Actions (Cont.)


When we update a tuple from a table, say S, that is referenced by another table, say SP, There are similar choices of referential actions:

ON UPDATE CASCADE ON UPDATE RESTRICT

There could be other choices besides these three. , e.g.,


ON UPDATE SET DEFAULT. ON UPDATE SET NULL.

Integrity (Fig. from C.J. Date)


S
S# S1 S2 S3 S4 S5 P# P1 P2 P3 P4 P5 P6 SNAME STATUS CITY Smith 20 London Jones 10 Paris Blake 30 Paris Clark 20 London Adams 30 Athens PNAME Nut Bolt Screw Screw Cam Cog COLOR WEIGHT CITY Red 12 London Green 17 Paris Blue 17 Rome Red 14 London Blue 12 Paris Red 19 London

SP

S# S1 S1 S1 S1 S1 S1 S2 S2 S3 S4 S4 S4

P# P1 P2 P3 P4 P5 P6 P1 P2 P2 P2 P4 P5

QTY 300 200 400 200 100 100 300 400 200 200 300 400

SQL Syntax for Actions (Just a flavor)


CREATE TABLE Enrollment ( OfferNo INTEGER NOT NULL, StdSSN CHAR(11) NOT NULL, EnrGrade DECIMAL(3,2), CONSTRAINT PKEnrollment PRIMARY KEY(OfferNo, StdSSN), CONSTRAINT FKOfferNo FOREIGN KEY (OfferNo) REFERENCES Offering ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT FKStdSSN FOREIGN KEY (StdSSN) REFERENCES Student ON DELETE RESTRICT ON UPDATE CASCADE )

Das könnte Ihnen auch gefallen