Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009."— Presentation transcript:

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

2 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 10101 11101 Student (sid: string, name: string, login: string, age: integer, gpa:real) 26/5/2014

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

4 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

5 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 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 7 Database Design 76/5/2014

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

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

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

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

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

13 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)

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

15 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

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

17 Example 17 LoginsHostsAt name time

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

20 6/5/201420 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 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 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

23 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 Liu @ University of Kentucky

24 6/5/201424 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 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

26 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 Liu @ University of Kentucky

27 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 Liu @ University of Kentucky

28 6/5/201428 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

29 Jinze Liu @ 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.

30 6/5/201430 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

31 Jinze Liu @ 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

32 6/5/2014 Jinze Liu @ 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

33 6/5/2014 Jinze Liu @ 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 1234647B sidcidgrade 1234647A sidcidgrade

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

35 6/5/2014 Jinze Liu @ 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 Smith3.5 1123Mary Carter3.8 ciddepartment 647EECS 108MATH sidcidgrade 1234108B

36 6/5/2014 Jinze Liu @ 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 Smith3.5 1123Mary Carter3.8 ciddepartment 647EECS 108MATH sidcidgrade 1234108B ciddepartment 647EECS 109MATH sidcidgrade 1234109B

37 6/5/2014 Jinze Liu @ 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


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

Similar presentations


Ads by Google