Download presentation
Presentation is loading. Please wait.
Published byAlisha Heath Modified over 8 years ago
1
CS 160 and CMPE/SE 131 Software Engineering March 10 Class Meeting Department of Computer Science Department of Computer Engineering San José State University Spring 2016 Instructor: Ron Mak www.cs.sjsu.edu/~mak
2
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Free IDE for Ruby and Rails RubyMine https://www.jetbrains.com/student/ https://www.jetbrains.com/student/ 2
3
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 3 Conceptual Data Model Example Student, teacher, class, and contact info entities and their attributes. Student id first name last name contact info Teacher id first name last name contact info Class class code subject name classroom number which teacher Contact info id email address
4
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Conceptual Model: Entities and Attributes In an ER diagram (ERD), show an entity with a rectangle and its attributes with ovals. Underline the unique attribute. 4
5
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Conceptual Model: Relationships Each entity in an ER diagram must be related to at least one other entity. Show a relationship with a diamond and connect the diamond to the entities that are part of the relationship. 5 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
6
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Relationship Cardinality Show cardinality (how many instances of an entity) with symbols at the end of the relationship lines. Maximum symbol closest to the entity. Minimum symbol further away. Zero, one, many 6 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
7
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Relationship Cardinality, cont’d Read each relationship in both directions in this order: rectangle diamond cardinality rectangle 7 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
8
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Types of Relationships One-to-one (1:1) One-to-many (1:M) 8 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Each employee is allotted at most one vehicle. Each vehicle is allotted to exactly one employee. Each region has located in it at least one (i.e., many) stores.
9
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Types of Relationships, cont’d Many-to-many (M:N) 9 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Each employee is assigned to no or several (i.e., many) projects. Each project has assigned to it at least one (i.e., many) employee.
10
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Exact Cardinalities Indicate exact cardinalities with parenthesized minimum and maximum values. Example: (2, 6) Use M for a non-specific minimum or maximum. 10 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
11
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Relationship Attributes An relationship can also have attributes. 11 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
12
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Logical Database Model Map the ER diagram to a logical model represented as a relational schema. 12 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
13
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Conditions for a Table to be a Relation Each column must have a name. Within a table, each column name must be unique. All values in each column must be from the same (predefined) domain. Within a table, each row must be unique. Within each row, each value in each column must be single-valued. M ultiple values of the content represented by the column are not allowed in any rows of the table. 13
14
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Relational vs. Non-Relational Tables 14 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
15
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Additional Properties for a Relational Table The order of columns is irrelevant. The order of rows is irrelevant. 15
16
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Primary Key Each relation must have a primary key. A column or set of columns whose value uniquely identifies each row. Underline the primary key of the relational table. 16 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
17
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping Entities 17 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
18
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping Entities, cont’d 18 Attribute with a composite primary key. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
19
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping Entities, cont’d 19 Entity with an optional attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
20
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Entity Integrity Constraint No primary key column of a relational table can have null (empty) values. 20 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
21
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Entity Integrity Constraint, cont’d 21 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
22
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Foreign Keys A foreign key is a column in a table that refers to a primary key column in another table. In a relational schema, draw an arrow from the foreign key to the corresponding primary key. 22
23
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping 1:M Relationships 23 The foreign key on the M side of the 1:M relationship corresponds to the primary key on the 1 side. Mandatory participation on both sides. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
24
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping 1:M Relationships, cont’d 24 Optional participation on the 1 side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
25
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping 1:M Relationships, cont’d 25 Optional participation on the M side. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
26
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping 1:M Relationships, cont’d 26 Rename a foreign key to better reflect the role of a relationship. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
27
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping M:N Relationships 27 Use the bridge relation BELONGSTO with two foreign keys. AKA: linking table join table Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
28
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping M:N Relationships, cont’d 28 Relationship with an attribute. Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Logical Physical
29
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping 1:1 Relationships Map 1:1 relationships similarly to 1:M relationships. One table will have a foreign key pointing to the primary key of the other table. It can be an arbitrary choice of which table has the foreign key. Make the choice that is most intuitive or efficient. 29
30
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping 1:1 Relationships, cont’d 30 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Table VEHICLE has the foreign key. Conceptual Logical Physical
31
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Referential Integrity Constraint The value of a foreign key must either: Match one of the values of the primary key in the referred table. Be null. 31 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6
32
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping Example: ER Diagram 32 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Conceptual Data Model
33
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping Example: Relational Schema 33 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Logical Data Model
34
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Mapping Example: Sample Data 34 Database Systems by Jukić, Vrbsky, & Nestorov Pearson 2014 ISBN 978-0-13-257567-6 Physical Data Model
35
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 35 School Database Example Initial version IdNameClass_codeSubjectRoom 7003Rogers, Tom926Java programming101 7008Thompson, Art908Data structures114 7012Lane, John951Software engineering210 7012Lane, John974Operating systems109 7051Flynn, Mabel931Compilers222 John Lane teaches two classes. Each table has a primary key (PK) field whose value in each record uniquely identifies that record. IdNameTeacher_id_1Teacher_id_2Teacher_id_3 1001Doe, John700370127008 1005Novak, Tim70127008null 1009Klein, Leslienull 1014Jane, Mary7051null 1021Smith, Kim700370127051 Student Teacher Student id name which teachers Teacher id name which classes taught Class class code subject name class room number PK
36
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 36 Normalization Relational tables need to be normalized. Improve the stability of the model. More resilient to change. Faster record insertions and updates. Improve data quality. There are six normal forms, but we will only consider the first two. Each normal form includes the lower normal forms. Example: A database in second normal form is also in first normal form.
37
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 37 First Normal Form (1NF) Separate multi-valued data elements. Break the name fields into last name and first name fields. IdLastFirstTeacher_id_1Teacher_id_2Teacher_id_3 1001DoeJohn700370127008 1005NovakTim70127008null 1009KleinLeslienull 1014JaneMary7051null 1021SmithKim700370127051 IdLastFirstClass_codeSubjectRoom 7003RogersTom926Java programming101 7008ThompsonArt908Data structures114 7012LaneJohn951Software engineering210 7012LaneJohn974Operating systems109 7051FlynnMabel931Compilers222 Student Teacher IdNameTeacher_id_1Teacher_id_2Teacher_id_3 1001Doe, John700370127008 1005Novak, Tim70127008null 1009Klein, Leslienull 1014Jane, Mary7051null 1021Smith, Kim700370127051 IdNameClass_codeSubjectRoom 7003Rogers, Tom926Java programming101 7008Thompson, Art908Data structures114 7012Lane, John951Software engineering210 7012Lane, John974Operating systems109 7051Flynn, Mabel931Compilers222
38
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 38 First Normal Form, cont’d Move repeating data elements to a new table. IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim Student_idTeacher_id 10017003 10017012 10017008 10057012 10057008 10147051 10217003 10217012 10217051 Linking table IdLastFirstClass_codeSubjectRoom 7003RogersTom926Java programming101 7008ThompsonArt908Data structures114 7012LaneJohn951Software engineering210 7012LaneJohn974Operating systems109 7051FlynnMabel931Compilers222 Student Teacher Student_Teacher IdLastFirstTeacher_id_1Teacher_id_2Teacher_id_3 1001DoeJohn700370127008 1005NovakTim70127008null 1009KleinLeslienull 1014JaneMary7051null 1021SmithKim700370127051
39
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 39 Problem! Suppose Prof. Lane decides he doesn’t want to teach Operating Systems anymore and we delete that row. What other information do we lose as a result? We lose the fact that the class is taught in Room 109. The problem arises because the Teacher table really contains two separate sets of data: teacher data and class data. IdLastFirstClass_codeSubjectRoom 7003RogersTom926Java programming101 7008ThompsonArt908Data structures114 7012LaneJohn951Software engineering210 7012LaneJohn974Operating systems109 7051FlynnMabel931Compilers222 Teacher
40
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 40 Second Normal Form (2NF) Keep related data together (cohesiveness). IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Class_codeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 TeacherClass Primary key (PK) Foreign key (FK) How would you do this relation with a linking table?
41
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 41 Final Database Structure, cont’d IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 Student_idClass_code 1001926 1001951 1001908 1005974 1005908 1014931 1021926 1021974 1021931 IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher Student Class Student_Class John Doe takes Java programming, software engineering, and data structures.
42
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 42 Final Database Structure, cont’d IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 Student_idClass_code 1001926 1001951 1001908 1005974 1005908 1014931 1021926 1021974 1021931 IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher Student Class Student_Class The Java Programming class has John Doe and Kim Smith.
43
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak 43 Final Database Structure, cont’d IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 Student_idClass_code 1001926 1001951 1001908 1005974 1005908 1014931 1021926 1021974 1021931 IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher Student Class Student_Class Mabel Flynn teaches compilers.
44
Computer Science Dept. Spring 2016: March 10 CS 160 and CMPE/SE 131: Software Engineering © R. Mak Assignment #5 Initial draft of your Design Document. Document the design of your application. MVC architecture UML package and class diagrams UML sequence diagram Document the design of your database. Conceptual model: entity-relationship (ER) diagram Logical model: relational schema 44
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.