CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.

Slides:



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

Database Management Systems Chapter 3 The Relational Data Model (I) Instructor: Li Ma Department of Computer Science Texas Southern University, Houston.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Databases : Relational Model 2007, Fall Pusan National University Ki-Joune Li These slides are made from the materials that Prof. Jeffrey D. Ullman distributes.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Announcement Recitation time  Before midterm: 6-7pm, by Earl Wagner  After midterm: 5-6pm, by Yi Qiao Newsgroup safe to subscribe  Will not cause.
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.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
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.
CS 405G: Introduction to Database Systems Lecture 4: Relational Model Instructor: Chen Qian.
Database Systems Relational Model Concepts Toqir Ahmad Rana Database Management Systems 1 Lecture 17.
Chapter 5 Relational Model Concepts 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 Model These slides are based on the slides of your text book.
Relational Data Model, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 The Relational Model Chapter 3.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Topic 5 The Relational Data Model and Relational Database Constraints Faculty of Information Science and Technology Mahanakorn University of Technology.
DatabaseIM ISU1 Fundamentals of Database Systems Chapter 5 The Relational Data Model.
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:
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
12/2/2015CPSC , CPSC , Lecture 41 Relational Model.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
CS 405G: Introduction to Database Systems Lecture 5: Logical Design by Relational Model Instructor: Chen Qian.
Instructor: Jinze Liu Fall Phases of Database Design u Conceptual design begins with the collection of requirements and results needed from the.
 CS 405G: Introduction to Database Systems Lecture 6: Relational Algebra Instructor: Chen Qian.
Chapter 5 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
CS 405G: Introduction to Database Systems Relations.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
1 The Relational Data Model Tables Schemas Conversion from E/R to Relations.
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.
Translation of ER-diagram into Relational Schema
Relational Integrity Constraints
The Relational Model Relational Data Model
12/7/2018.
The Relational Data Model and Relational Database Constraints
The Relational Data Model
4/8/2019.
EECS 647: Introduction to Database Systems
CS 405G Introduction to Database Systems
CS 505: Intermediate Topics to Database Systems
5/12/2019.
Copyright © 2004 Pearson Education, Inc.
CS 405G: Introduction to Database Systems
CS 405G: Introduction to Database Systems
Presentation transcript:

CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009

6/5/20142 Review A data model is a group of concepts for describing data. What are the two terms used by ER model to describe a miniworld? Entity Relationship What makes a good database design Student (sid: string, name: string, login: string, age: integer, gpa:real) 26/5/2014

3 Topics Next More case study Conversion of ER models to Schemas 36/5/2014

4 ER Case study 6/5/20144 Design a database representing cities, counties, and states For states, record name and capital (city) For counties, record name, area, and location (state) For cities, record name, population, and location (county and state) Assume the following: Names of states are unique Names of counties are only unique within a state Names of cities are only unique within a county A city is always located in a single county A county is always located in a single state

6/5/20145 Case study : first design County area information is repeated for every city in the county Redundancy is bad. What else? State capital should really be a city Should reference entities through explicit relationships Cities In States name capital name population county_area county_name 56/5/2014

6 Case study : second design Technically, nothing in this design could prevent a city in state X from being the capital of another state Y, but oh well… Cities IsCapitalOf name population Counties name area name In States 66/5/2014

7 Database Design 76/5/2014

A Relation is a Table 8 name manf WinterbrewPetes Bud LiteAnheuser-Busch Beers Attributes (column headers) Tuples (rows)

Schemas 9 Relation schema = relation name + attributes, in order (+ types of attributes). Example: Beers(name, manf) or Beers(name: string, manf: string) Database = collection of relations. Database schema = set of all relation schemas in the database.

Why Relations? 10 Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today. But SQL uses bags, while the relational model is a set-based model.

From E/R Diagrams to Relations 11 Entity sets become relations with the same set of attributes. Relationships become relations whose attributes are only: The keys of the connected entity sets. Attributes of the relationship itself.

Entity Set -> Relation 12 Relation: Beers(name, manf) Beers name manf

Relationship -> Relation 13 DrinkersBeers Likes Likes(drinker, beer) Favorite Favorite(drinker, beer) Married husband wife Married(husband, wife) name addr name manf Buddies 1 2 Buddies(name1, name2)

Combining Relations 14 It is OK to combine the relation for an entity-set E with the relation R for a many-one relationship from E to another entity set. Example: Drinkers(name, addr) and Favorite(drinker, beer) combine to make Drinker1(name, addr, favBeer).

Risk with Many-Many Relationships 15 Combining Drinkers with Likes would be a mistake. It leads to redundancy, as: name addr beer Sally 123 Maple Bud Sally 123 Maple Miller Redundancy

Handling Weak Entity Sets 16 Relation for a weak entity set must include attributes for its complete key (including those belonging to other entity sets), as well as its own, nonkey attributes. A supporting (double-diamond) relationship is redundant and yields no relation.

Example 17 LoginsHostsAt name time

Example 18 LoginsHostsAt name Hosts(hostName) Logins(loginName, hostName, time) At(loginName, hostName, hostName2) Must be the same time At becomes part of Logins

19 A (Slightly) Formal Definition A database is a collection of relations (or tables) Each relation is identified by a name and a list of attributes (or columns) Each attribute has a name and a domain (or type) Set-valued attributes not allowed 19

6/5/ Schema versus instance Schema (metadata) Specification of how data is to be structured logically Defined at set-up Rarely changes Instance Content Changes rapidly, but always conforms to the schema Compare to type and objects of type in a programming language 206/5/2014

21 Example Schema Student (SID integer, name string, age integer, GPA float) Course (CID string, title string) Enroll (SID integer, CID integer) Instance { h 142, Bart, 10, 2.3 i, h 123, Milhouse, 10, 3.1 i,...} { h CPS116, Intro. to Database Systems i,...} { h 142, CPS116 i, h 142, CPS114 i,...} 216/5/2014

22 Relational Integrity Constraints Constraints are conditions that must hold on all valid relation instances. There are four main types of constraints: 1. Domain constraints 1. The value of an attribute must come from its domain 2. Key constraints 3. Entity integrity constraints 4. Referential integrity constraints 226/5/2014

Primary Key Constraints A set of fields is a candidate key for a relation if : 1. No two distinct tuples can have same values in all key fields, and 2. This is not true for any subset of the key. Part 2 false? A superkey. If theres >1 key for a relation, one of the keys is chosen (by DBA) to be the primary key. E.g., given a schema Student(sid: string, name: string, gpa: float) we have: sid is a key for Students. (What about name?) The set {sid, gpa} is a superkey. 236/5/2014Jinze University of Kentucky

6/5/ Key Example CAR (licence_num: string, Engine_serial_num: string, make: string, model: string, year: integer) What is the candidate key(s) Which one you may use as a primary key What are the super keys 246/5/2014

25 Entity Integrity Entity Integrity: The primary key attributes PK of each relation schema R in S cannot have null values in any tuple of r(R). Other attributes of R may be similarly constrained to disallow null values, even though they are not members of the primary key. 256/5/2014

Foreign Keys, Referential Integrity Foreign key : Set of fields in one relation that is used to `refer to a tuple in another relation. (Must correspond to primary key of the second relation.) Like a `logical pointer. E.g. sid is a foreign key referring to Students : Student(sid: string, name: string, gpa: float) Enrolled(sid: string, cid: string, grade: string) If all foreign key constraints are enforced, referential integrity is achieved, i.e., no dangling references. Can you name a data model w/o referential integrity? Links in HTML! 266/5/2014Jinze University of Kentucky

Foreign Keys Only students listed in the Students relation should be allowed to enroll for courses. Enrolled Students Or, use NULL as the value for the foreign key in the referencing tuple when the referenced tuple does not exist 276/5/2014Jinze University of Kentucky

6/5/ In-Class Exercise (Taken from Exercise 5.16) Consider the following relations for a database that keeps track of student enrollment in courses and the books adopted for each course: STUDENT(SSN, Name, Major, Bdate) COURSE(Course#, Cname, Dept) ENROLL(SSN, Course#, Quarter, Grade) BOOK_ADOPTION(Course#, Quarter, Book_ISBN) TEXT(Book_ISBN, Book_Title, Publisher, Author) Draw a relational schema diagram specifying the foreign keys for this schema. 286/5/2014

Jinze University of Kentucky 29 In-Class Exercise (Taken from Exercise 5.16) Consider the following relations for a database that keeps track of student enrollment in courses and the books adopted for each course: STUDENT(SSN, Name, Major, Bdate) COURSE(Course#, Cname, Dept) ENROLL(SSN, Course#, Quarter, Grade) BOOK_ADOPTION(Course#, Quarter, Book_ISBN) TEXT(Book_ISBN, Book_Title, Publisher, Author) Draw a relational schema diagram specifying the foreign keys for this schema.

6/5/ Other Types of Constraints Semantic Integrity Constraints: based on application semantics and cannot be expressed by the model per se e.g., the max. no. of hours per employee for all projects he or she works on is 56 hrs per week A constraint specification language may have to be used to express these SQL-99 allows triggers and ASSERTIONS to allow for some of these 306/5/2014

Jinze University of Kentucky 31 Update Operations on Relations Update operations INSERT a tuple. DELETE a tuple. MODIFY a tuple. Constraints should not be violated in updates

6/5/2014 Jinze University of Kentucky 32 Example We have the following relational schemas Student(sid: string, name: string, gpa: float) Course(cid: string, department: string) Enrolled(sid: string, cid: string, grade: character) We have the following sequence of database update operations. (assume all tables are empty before we apply any operations) INSERT into Student sidnamegpa 1234John Smith3.5

6/5/2014 Jinze University of Kentucky 33 Example (Cont.) INSERT into Courses INSERT into Enrolled UPDATE the grade in the Enrolled tuple with sid = 1234 and cid = 647 to A. DELETE the Enrolled tuple with sid 1234 and cid 647 sidnamegpa 1234John Smith3.5 ciddepartment 647EECS sidcidgrade B sidcidgrade A sidcidgrade

6/5/2014 Jinze University of Kentucky 34 Exercise INSERT into Courses INSERT into Enrolled INSERT into Student sidnamegpa 1234John Smith3.5 ciddepartment 647EECS 108MATH sidcidgrade B ciddepartment 647EECS sidcidgrade sidnamegpa 1234John Smith Mary Carter3.8

6/5/2014 Jinze University of Kentucky 35 Exercise (cont.) A little bit tricky INSERT into Student Fail due to domain constraint INSERT into Enrolled Fail due to entity integrity INSERT into Enrolled Failed due to referential integrity sidnamegpa 1234John Smith Mary Carter3.8 ciddepartment 647EECS 108MATH sidcidgrade B

6/5/2014 Jinze University of Kentucky 36 Exercise (cont.) A more tricky one UPDATE the cid in the tuple from Course where cid = 108 to 109 sidnamegpa 1234John Smith Mary Carter3.8 ciddepartment 647EECS 108MATH sidcidgrade B ciddepartment 647EECS 109MATH sidcidgrade B

6/5/2014 Jinze University of Kentucky 37 Update Operations on Relations In case of integrity violation, several actions can be taken: Cancel the operation that causes the violation (REJECT option) Perform the operation but inform the user of the violation Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option) Execute a user-specified error-correction routine