Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products.

Similar presentations


Presentation on theme: "Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products."— Presentation transcript:

1 Database Systems Lecture # 8 11 th Feb,2011

2 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.

3 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.

4 Terminology S#:S#SNAME:NAMESTATUS:STATUSCITY:CITY S1 S2 S3 S4 S5 Smith Jones Blake Clark Adams 20 10 30 20 30 London Paris London Athens Relation Tuple Attributes Primary Key London, Tucson, Paris,... S#NAMESTATUSCITY Domains

5 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

6 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.

7 Terminologies Used: Formal Informal Relationtable Tuplerow or record Cardinalitynumber of rows Attributecolumn or field Degreenumber of columns Domainpool of legal values

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

9 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 Referential integrity: foreign keys Values of a column in one table match values from a source table Ensures valid references among tables

10 Integrity (Fig. from C.J. Date) S#SNAMESTATUSCITY S1 S2 S3 S4 S5 Smith Jones Blake Clark Adams 20 10 30 20 30 London Paris London Athens P#PNAMECOLORWEIGHT P1 P2 P3 P4 P5 P6 Nut Bolt Screw Cam Cog Red Green Blue Red Blue Red 12 17 14 12 19 CITY London Paris Rome London Paris London S# S1 S2 S3 S4 P# P1 P2 P3 P4 P5 P6 P1 P2 P4 P5 QTY 300 200 400 200 100 300 400 200 300 400 S P SP

11 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.

12 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.

13 Types of Constraints Type (domain) Constraint: specifies the legal values for a given type. Attribute constraint: specifies the legal values for a given attribute. Table (relation) constraint: specifies the legal values for a given table. Multiple attributes may be involved. Database constraint: specifies the legal values for a given database. Multiple tables may be involved.

14 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.

15 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

16 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-namechar(15), account-numberchar(10) not null, balanceinteger, ……) 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.

17 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.

18 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.

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

20 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

21 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

22 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. (Remember Entity Integrity) 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.

23 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#}

24 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.

25 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.

26 Integrity Rules Entity integrity No two rows with the same primary key value No null values in a primary key Referential integrity 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.

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

28 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

29 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. ON DELETE RESTRICT The deletion of the tuple from S is rejected.

30 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.

31 Integrity (Fig. from C.J. Date) S#SNAMESTATUSCITY S1 S2 S3 S4 S5 Smith Jones Blake Clark Adams 20 10 30 20 30 London Paris London Athens P#PNAMECOLORWEIGHT P1 P2 P3 P4 P5 P6 Nut Bolt Screw Cam Cog Red Green Blue Red Blue Red 12 17 14 12 19 CITY London Paris Rome London Paris London S# S1 S2 S3 S4 P# P1 P2 P3 P4 P5 P6 P1 P2 P4 P5 QTY 300 200 400 200 100 300 400 200 300 400 S P SP

32 SQL Syntax for Actions (Just a flavor) CREATE TABLE Enrollment ( OfferNoINTEGER NOT NULL, StdSSNCHAR(11) NOT NULL, EnrGradeDECIMAL(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 )


Download ppt "Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products."

Similar presentations


Ads by Google