Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Similar presentations


Presentation on theme: "CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."— Presentation transcript:

1 CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak

2 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 2 Data Independence  Goal: Loose coupling between applications and the data repository. Manage complexity. Manage change.  Applications should not need to know: Where the data is stored (location of the repository) How it is stored (file formats and organizations, etc.) Access mechanisms

3 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 3 Back-End Data Repository Issues  Redundancy and inconsistency Multiple copies of data (good for backup) Different versions that don’t match (bad) Consistent updates and deletions  Access How to access data Timeliness  Disparity Data stored in multiple and scattered locations Data stored in different formats and media

4 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 4 Back-End Data Repository Issues, cont’d  Concurrency Handle multiple clients accessing and updating the data simultaneously.  Security Prevent unauthorized accesses and updates.  Integrity Data values must meet constraints.  Example: Minimum and maximum value ranges Data values must agree with each other.  Example: Medical records for a male patient should not include pregnancy data.

5 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 5 Data Modeling  A data model describes what data an application works with and how the data is used. Include data that will be persisted (written to and read from a data repository) in the data model.  For databases, objects are called entities. Model entities and their relationships to each other. Similar to Java classes and their relationships.

6 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 6 Data Modeling, cont’d  Conceptual data model A high-level user-oriented description of the data.  entities  relationships among the entities  who will use the data (access control)  how it will be used Visualize the model with a diagram that shows names, attributes, keys, and relations.  Logical data model Create the relational data model (in our case).

7 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 7 Data Modeling, cont’d  Physical data model The database itself.

8 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 8 Example Requirements  Student, teacher, and class entities and their attributes. Student  id  name  which teachers Teacher  id  name  which classes taught Class  class code  subject name  class room number

9 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 9 Sample Queries  Which teachers does this student have?  What classes does this teacher teach?  Who is the teacher of this class?  Which students are in this class?  Which students are in each of the classes taught by this teacher?

10 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 10 The Relational Data Model  Data element: values that are stored in the repository (i.e., the database) Values are typed. A value can be null.  Entity: a group of data elements that together are meaningful for a person or an application Similar to Java objects. Each data element is the value of an attribute of the entity.

11 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 11 The Relational Data Model, cont’d  Table: a conceptual two-dimensional structure that contains entities of a particular type. AKA relation  Each row (AKA record) contains the attribute values of one entity.  Each column (AKA field) holds an attribute value.  Table  relation  Row  entity  Rows and columns  records and fields

12 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 12 Logical Data Model  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

13 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 13 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.

14 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 14 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

15 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 15 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

16 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 16 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

17 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 17 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?

18 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 18 Final Database Structure 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.

19 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 19 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.

20 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 20 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.

21 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 21 Entity-Relationship (ER) Diagrams  Data modeling diagrams are called Entity-Relationship (ER) diagrams. Use an ER diagram (ERD) to visualize your conceptual data model. Very similar in concept to UML diagrams.  There are several styles of ER diagrams. One style is crow’s feet diagrams.

22 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 22 One-to-Many Relationship  One (each) teacher teaches 0, 1, or many classes. IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 onezeroonemany Database cardinality is only 0, 1, or many (more than 1). TeacherClass minimum maximum

23 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 23 Many-to-Many Relationship IdLastFirst 1001DoeJohn 1005NovakTim 1009KleinLeslie 1014JaneMary 1021SmithKim CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 KeyStudent_idClass_code 11001926 21001951 31001908 41005974 51005908 61014931 71021926 81021974 91021931 Student Class Student_Class  A student has 0, 1 or many classes.  A class has 1 or many students. Class Student Student-Class

24 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 24 Complete Entity Diagram code (PK) teacher_id (FK) subject room Class CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 Class

25 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 25 SQL  Structured Query Language (SQL) An industry standard But has many proprietary extensions  Language for managing data in a relational database. Create and drop (delete) databases Create, alter, and drop tables of a database Retrieve, insert, update, and delete data in the tables.

26 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 26 SQL Query Examples  What is the class code of the Java programming class? CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 Class SELECT code FROM class WHERE subject = 'Java programming' +------+ | code | +------+ | 926 | +------+ Source tablesDesired fields Selection criteria

27 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 27 SQL Query Examples, cont’d  Who is teaching Java programming? IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 ClassTeacher SELECT first, last FROM teacher, class WHERE id = teacher_id AND subject = 'Java programming' +-------+--------+ | first | last | +-------+--------+ | Tom | Rogers | +-------+--------+ Selecting from multiple tables is called a join.

28 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 28 SQL Query Examples, cont’d  What subjects does John Lane teach? SELECT code, subject FROM teacher, class WHERE last = 'Lane' AND first = 'John' AND id = teacher_id +------+----------------------+ | code | subject | +------+----------------------+ | 951 | Software engineering | | 974 | Operating systems | +------+----------------------+ IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming101 9317051Compilers222 9517012Software engineering210 9747012Operating systems109 ClassTeacher Demo

29 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 29 SQL Query Examples, cont’d  Who is taking Java programming? 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 SELECT id, last, first FROM student, class, student_class WHERE subject = 'Java programming' AND code = class_code AND id = student_id +------+-------+-------+ | id | last | first | +------+-------+-------+ | 1001 | Doe | John | | 1021 | Smith | Kim | +------+-------+-------+ Class Student_Class Student

30 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 30 SQL Query Examples, cont’d  What classes is John Doe taking? 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 SELECT code, subject FROM student, class, student_class WHERE last = 'Doe' AND first = 'John' AND id = student_id AND code = class_code +------+----------------------+ | code | subject | +------+----------------------+ | 908 | Data structures | | 926 | Java programming | | 951 | Software engineering | +------+----------------------+ ClassStudent Student_Class

31 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 31 SQL Query Examples, cont’d  Who are John Lane’s students and in which subjects? 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 SELECT student.first, student.last, subject FROM student, teacher, class, student_class WHERE teacher.last = 'Lane' AND teacher.first = 'John' AND teacher_id = teacher.id AND code = class_code AND student.id = student_id ORDER BY subject, student.last +-------+-------+----------------------+ | first | last | subject | +-------+-------+----------------------+ | Tim | Novak | Operating systems | | Kim | Smith | Operating systems | | John | Doe | Software engineering | +-------+-------+----------------------+ IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher StudentClass Student_Class

32 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak SQL to Create and Drop a Database  Create examples:  Drop examples: 32 CREATE DATABASE school3; CREATE DATABASE IF NOT EXISTS school3; DROP DATABASE school3; DROP DATABASE IF EXISTS school3;

33 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak CREATE TABLE class ( code INT PRIMARY KEY, teacher_id INT NOT NULL, subject VARCHAR(32) NOT NULL, room INT NOT NULL ); SQL to Create a Table  First we create a new database and connect to it:  Create the Class table: 33 CREATE DATABASE school3; USE school3; CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming 101 9317051Compilers222 9517012Software engineering 210 9747012Operating systems 109

34 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 34 Database Record Insert, Update, and Delete  There are SQL statements to insert, update, and delete records. See the SQL tutorial. INSERT INTO teacher (id, last, first) VALUES (7088, 'Mak', 'Ron'), (7090, 'Wilson', 'Brian') UPDATE teacher SET first = 'Ronald' WHERE first = 'Ron' DELETE FROM teacher WHERE id = 7090 This can update multiple records!

35 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak SQL to Add Rows  Add rows to the Class table: 35 CodeTeacher_idSubjectRoom 9087008Data structures114 9267003Java programming 101 9317051Compilers222 9517012Software engineering 210 9747012Operating systems 109 INSERT INTO class (code, teacher_id, subject, room) VALUES (908, 7008, 'Data structures', 114), (926, 7003, 'Java programming', 101), (931, 7051, 'Compilers', 222), (951, 7012, 'Software engineering', 210), (978, 7012, 'Operating systems', 109);

36 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak SQL Script create_school.sql 36 DROP DATABASE IF EXISTS school3; CREATE DATABASE school3; USE school3; CREATE TABLE class ( codeINTPRIMARY KEY, teacher_id INT NOT NULL, subject VARCHAR(32)NOT NULL, room INT NOT NULL, ); INSERT INTO class (code, teacher_id, subject, room) VALUES(908, 7008, 'Data structures', 114), (926, 7003, 'Java programming', 101), (931, 7051, 'Compilers', 222), (951, 7012, 'Software engineering', 210), (978, 7012, 'Operating systems', 109);

37 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak SQL Script create_school.sql, cont’d 37 CREATE TABLE contact_info ( idINTPRIMARY KEY, email_addressVARCHAR(32)NOT NULL ); INSERT INTO contact_info (id, email_address) VALUES(1, 'mjane@sjsu.edu'), (2, 'ksmith@sjsu.edu'), (3, 'jdoe@sjsu.edu'), (4, 'tnovak@sjsu.edu'), (5, 'lklein@sjsu.edu'), (6, 'trogers@sjsu.edu'), (7, 'athompson@sjsu.edu'), (8, 'jlane@sjsu.edu'), (9, 'mflynn@sjsu.edu');

38 Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak SQL Script create_school.sql, cont’d 38 CREATE TABLE teacher ( idINTPRIMARY KEY, lastVARCHAR(32)NOT NULL, firstVARCHAR(32)NOT NULL, contact_idINTREFERENCES contact_info(id) ); INSERT INTO teacher (id, last, first, contact_id) VALUES(7003, 'Rogers','Tom',6), (7008, 'Thompson','Art',7), (7012, 'Lane','John’,8), (7051, 'Flynn','Mabel',9);  Use the MySQL source command: source create_school.sql


Download ppt "CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak."

Similar presentations


Ads by Google