1 CSE 480: Database Systems Lecture 5: Relational Data Model.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 5 The Relational Data Model and Relational Database Constraints.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 1.
Chapter 5 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
Database Systems Chapter 5 ITM 354. Chapter Outline Relational Model Concepts Relational Model Constraints and Relational Database Schemas Update Operations.
Chapter 5 The Relational Data Model and Relational Database Constraints.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
Chapter 5 Relational Model Concepts Dr. Bernard Chen Ph.D. University of Central Arkansas.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
The Relational Data Model 1.Relational Model Concepts 2.Characteristics of Relations 3.Relational Integrity Constraints 3.1Key Constraints 3.2Entity Integrity.
Chapter 4 Rational data model. Chapter 5 Rational data model Chapter 4 Rational data model.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints.
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and SQL.
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 1.
The Relational Data Model and Relational Database Constraints
10/9/20151 The Relational Data Model TCU Database Systems Last update: September 2004 Reference: Elmasri 4 th edition, chapter 5.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 1 Week4 Relational Model and Relational Database Constraints Relational Model Concepts.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 5 The Relational Data Model and Relational Database Constraints.
METU Department of Computer Eng Ceng 302 Introduction to DBMS The Relational Data Model and Relational Database Constraints by Pinar Senkul resources:
Ihr Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Chapter 5 The Relational Data Model and Relational Database Constraints.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 5 The Relational Data Model and Relational Database Constraints.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 3 The Basic Relational Model.
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 1.
Chapter 5 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe CHAPTER 5 The Relational Data Model and Relational Database Constraints Slide 1- 1.
The Relational Data Model and Relational Database Constraints
Lecture 03 Constraints. Example Schema CONSTRAINTS.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 5- 1.
The Relational Data Model and Relational Database Constraints.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
4/28/2017 Chapter 5 The Relational Data Model and Relational Database Constraints.
Chapter 3 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
Copyright © 2004 Pearson Education, Inc.
4/20/2018.
Lecture # 13 (After 1st Exam)
The Relational Data Model and Relational Database Constraints
The Relational Data Model and Relational Database Constraints
12/7/2018.
The Relational Data Model and Relational Database Constraints
2/28/2019 Chapter 5 The Relational Data Model and Relational Database Constraints.
Database Design: Relational Model
4/8/2019.
The Relational Data Model and Relational Database Constraints
The Relational Data Model and Relational Database Constraints
5/12/2019.
Copyright © 2004 Pearson Education, Inc.
The Relational Data Model and Relational Database Constraints
The Relational Data Model and Relational Database Constraints
Presentation transcript:

1 CSE 480: Database Systems Lecture 5: Relational Data Model

2 Lecture Outline l Relational Model Concepts –Relations, Attributes, Domains, Tuple, Schema –Characteristics of relations l Integrity Constraints –Domain constraints –Key constraints –Entity integrity –Referential integrity l Relational Update Operations and Dealing with Constraint Violations

3 Relational Model l Represents the database as a collection of relations –Informally, each relation is a table of values  The table contains a set of rows (called tuples)  Each column header corresponds to an attribute

4 Terminology Informal TermsFormal Terms TableRelation RowTuple Column HeaderAttribute Set of all possible column valuesDomain Table DefinitionRelation Schema Populated TableRelation State

5 Relation Schema l Schema corresponds to the definition of a relation –Represented as R(A 1, A 2,.....A n ) where R is the relation name and A’s are the attributes and their corresponding domains l Example : CUSTOMER (Cust-id: INT, Cust-name: VARCHAR(40), Address: VARCHAR(50)) –CUSTOMER is the relation name –Attributes: Cust-id, Cust-name, Address –INT and VARCHAR are domains of the attribute values l Relation schema also includes other integrity constraints (to be discussed later)

6 Relation State l A relation state r of a schema R(A 1, A 2,.....A n ) is a set of m-tuples r(R) = {t 1, t 2, …, t m } –where each tuple t is an ordered set of values and each value v is derived from an appropriate domain

7 Characteristics of a Relation l Ordering of tuples in a relation r(R) is irrelevant –Both relations are considered identical

8 Characteristics of a Relation l All attribute values must be atomic (indivisible) l A special null value is used to represent values that are unknown or inapplicable to certain tuples

9 Integrity Constraints l Constraints are conditions that specify restrictions on the database state –Protects database from errors l Types of constraints captured in the relational model (i.e., they are automatically checked by DBMS): –Domain constraints –Key constraints –Entity integrity constraints –Referential integrity constraints l Constraints not captured in the relational model –semantic constraints (business rules)  E.g., “Buy 5 Get 1 Free!”, “Can’t view profile unless you’ve logged in”

10 University Database

11 Domain Constraint l Domain constraint –The value of each attribute in a tuple must be atomic and consistent with its domain (or it could be null, if allowed for that attribute)

12 Violations of Domain Constraints CREATE TABLE Student ( Id INT, Name CHAR(30), Status ENUM( ‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’), Address CHAR(30) ); Inserting the tuple ( , ‘Billy Bob’, 3, ‘46 Grand Ave.’) Inserting the tuple (‘ ’, ‘Mark Smith’, ‘Senior’, ‘1 Oak St.’) Inserting the tuple ( , ‘Bart Simpson’, ‘Graduate’, ‘Fox 5 TV’); OK

13 Key Constraint l Every relation must have a key l A key is a subset of attributes {A 1,…,A m } of a relation schema R with the following properties: –Uniqueness:  No two tuples in any valid relation state will have the same combination of values for A 1,…,A m –Minimality:  No subset of A 1,…,A m has the uniqueness property l Sometimes row-ids or sequential numbers are assigned as keys to identify the rows in a table –Called artificial key or surrogate key

14 Key Constraint l Superkey – a set of attributes containing a key –Satisfies uniqueness property (minimality poperty not necessary) –Every key is a superkey, but not vice-versa –{LicenseNo} is a key  It is also a superkey of CAR –{LicenseNo, Make} is a superkey but not a key

15 Key Constraint l A relation can have several keys: –If a relation has several keys, they are referred to as candidate keys (one of them is designated as the primary key) –The primary key attributes are underlined in the relation schema –Examples:  CAR(LicenseNo, EngineSerialNo, Make, Model, Year) –Candidate keys: {LicenseNo} or {EngineSerialNo} –Primary key: {LicenseNo}  STUDENT(Id, Name, Address, PhoneNo) –Candidate keys: {Name, Address} or {Id} –Primary key: {Id}

16 Exercise Identify the candidate and primary keys for each of the following schema: DEPARTMENT(Id: STRING, Name: STRING, Address: STRING)

17 Entity Integrity Constraint l Primary key attributes PK cannot have null values in any tuple –This is because primary key values are used to identify the individual tuples. –If PK has several attributes, null is not allowed in any of these attributes l Note: Other attributes of R can also be constrained to disallow null values, even though they are not members of the primary key. CREATE TABLE Student ( Id INT PRIMARY KEY, Name CHAR(30) NOT NULL, Status ENUM( ‘Freshman’, ‘Sophomore’, ‘Junior’, ‘Senior’), Address CHAR(30) );

18 Relationship Constraints COURSE PROFESSOR TEACH Semester IdCrsCode In relational databases, it is common for tuples in one relation to reference tuples in the same or other relations

19 Referential Integrity l Referential integrity constraint –Tuples in the referencing relation must refer to tuples that exist in the referenced relation l Referential integrity is violated if the referenced tuple does not exist in the appropriate relation –A tuple ( , MGT123, F1994) in TEACHING relation violates referential integrity constraint because there is no tuple with Id = in PROFESSOR relation l Referential integrity constraint is enforced in the DB using FOREIGN KEY on attributes in the referencing relation

20 Referential Integrity l Tuples in the referencing relation R 1 have attributes FK (called foreign key attributes) that reference the primary key** attributes PK of the referenced relation R 2 Referencing relation R 1 Referenced relation R 2 ** The book considers PK to be primary key, but in general, PK can be any candidate key

21 Referential Integrity R 1 (FK) references R 2 (PK) l FK and PK are attributes that have the same domains (although not necessarily the same attribute names) l The value in the FK of the referencing relation R 1 can be either: –An existing value of a corresponding PK in the referenced relation R 2, or –Null  In this case, the FK cannot be part of the primary key for R 1 l R 1 and R 2 need not be distinct relations –Employee(Id:INT, MgrId:INT, ….)

22 Example TEACHING (ProfId) REFERENCES PROFESSOR (Id) l Not all rows in PROFESSOR need to be referenced –Some PROFESSORs may not have taught any classes yet l Names of FK and PK attributes need not be the same –ProfId of Teaching references Id of Professor l Value of a foreign key can be NULL –The PROFESSOR who teaches a course might not be known yet –Caveat: ProfId cannot be part of the primary key for TEACHING

23 Exercise l Identify all the referential integrity constraints in the student registration system DEPARTMENT(Id: STRING, Name: STRING, Address: STRING)

24 Exercise PROFESSOR(DeptId) REFERENCES DEPARTMENT(Id) COURSE(DeptId) REFERENCES DEPARTMENT(Id) TRANSCRIPT(StudId) REFERENCES STUDENT(Id) TRANSCRIPT(CrsCode, Semester) REFERENCES TEACHING(CrsCode, Semester) TEACHING(ProfId) REFERENCES PROFESSOR(Id) TEACHING(Crscode) REFERENCES COURSE(CrsCode) TRANSCRIPT(CrsCode) REFERENCES COURSE(CrsCode) is redundant since it is implied by Teaching(Crscode).

25 Relational Database Schema l A set of relation schemas that belong to the same database –S = {R 1, R 2,..., R n } –S is the name of the whole database schema –R 1, R 2, …, R n are the names of individual relation schemas within the database S l Referential integrity constraints are displayed as directed arcs from the foreign key attributes to the referenced table –Can also point to the primary key of the referenced relation for clarity

26 Example: COMPANY Database Schema

27 Referential Integrity Constraints Schema diagram with referential integrity constraints

28 Relational Database State l Relational database state is the union of all the relation states COMPANY Database

29 Update Operations on Relations l Whenever the database is changed/updated, a new database state arises l Basic operations for updating the database: –INSERT a new tuple into a relation –DELETE an existing tuple from a relation –UPDATE/MODIFY an attribute of an existing tuple in a relation l Integrity constraints must not be violated by the update operations –Updates may propagate to cause other updates automatically. This may be necessary to maintain integrity constraints.

30 Example If we change the SSN of John Smith to , this may require updates to some of the tuples in Works_ON relation (depending on the update option used)

31 Possible violations for INSERT l Domain constraint –if one of the attribute values provided for the new tuple is not of the specified attribute domain l Key constraint –if the value of a key attribute in the new tuple already exists in another tuple in the relation l Entity integrity –if the primary key value is null in the new tuple l Referential integrity –if a foreign key value in the new tuple references a primary key value that does not exist in the referenced relation

32 Example  Insert into EMPLOYEE will violate entity constraint  Insert into EMPLOYEE will violate key constraint  Insert into EMPLOYEE will violate domain constraint

33 Example  Insert into WORKS_ON will violate referential integrity constraint CREATE TABLE WORKS_ON ( Essn INT, Pno INT, Hours FLOAT, PRIMARY KEY (Essn, Pno), FOREIGN KEY Essn REFERENCES EMPLOYEE(Ssn) FOREIGN KEY Pno REFERENCES PROJECT(Pnum));

34 Possible violations for DELETE l DELETE may violate only referential integrity: –If the primary key value of the tuple being deleted is referenced by other tuples in the database  Can be remedied by several actions: RESTRICT, CASCADE, SET NULL (more details are given in Chapter 8) –One of the above options must be specified for each foreign key constraint

35 Example l Deletion from referenced relation Professor: –RESTRICT: Reject if row in Teaching references row to be deleted –SET NULL: Set value of foreign key in Teaching to NULL –SET DEFAULT: Set value of foreign key in Teaching to default value which must exist in B –CASCADE: Delete referencing row(s) in Teaching as well x x Teaching Professor Request to delete a row in Professor

36 Example (in SQL) CREATE TABLE Teaching ( ProfId INT, CrsCode CHAR(10), Semester CHAR(10), PRIMARY KEY (CrsCode, Semester), FOREIGN KEY (ProfId) REFERENCES Professor(Id) ON DELETE SET NULL, FOREIGN KEY (CrsCode) REFERENCES COURSE(CrsCode) ON DELETE RESTRICT ); x x Teaching Professor ** More details in Chapter 8

37 Possible violations for UPDATE l Domain, key, entity integrity, and referential integrity constraints may be violated, depending on the attribute being updated: –Updating the primary key (PK):  Similar to a DELETE operation followed by an INSERT operation  Need to specify similar options to DELETE –Updating a foreign key (FK):  May violate referential integrity –Updating an ordinary attribute (neither PK nor FK):  Can only violate domain constraints

38 Example (in SQL) CREATE TABLE Teaching ( ProfId INT, CrsCode CHAR(10), Semester CHAR(10), PRIMARY KEY (CrsCode, Semester), FOREIGN KEY ProfId REFERENCES Professor(Id) ON DELETE SET NULL ON UPDATE SET NULL, FOREIGN KEY CrsCode REFERENCES COURSE(CrsCode) ON DELETE RESTRICT ON UPDATE CASCADE ); x x Teaching Professor