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.

Slides:



Advertisements
Similar presentations
Management Information Systems, Sixth Edition
Advertisements

Chapter Information Systems Database Management.
CS 185C/286: The History of Computing October 31 Class Meeting Department of Computer Science San Jose State University Fall 2011 Instructor: Ron Mak
The Relational Database Model:
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
CSC 2720 Building Web Applications Database and SQL.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Page 1 ISMT E-120 Introduction to Microsoft Access & Relational Databases The Influence of Software and Hardware Technologies on Business Productivity.
Page 1 ISMT E-120 Desktop Applications for Managers Introduction to Microsoft Access.
PHASE 3: SYSTEMS DESIGN Chapter 7 Data Design.
Chapter 5 Database Processing.
1 DATABASE TECHNOLOGIES BUS Abdou Illia, Fall 2007 (Week 3, Tuesday 9/4/2007)
CS 160: Software Engineering November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 8 Class Meeting
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Chapter 1 Overview of Database Concepts Oracle 10g: SQL
Database Technical Session By: Prof. Adarsh Patel.
CS 157B: Database Management Systems II January 30 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
1 Chapter 1 Overview of Database Concepts. 2 Chapter Objectives Identify the purpose of a database management system (DBMS) Distinguish a field from a.
Chapter 9 Designing Databases Modern Systems Analysis and Design Sixth Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich.
Concepts and Terminology Introduction to Database.
CHAPTER 8: MANAGING DATA RESOURCES. File Organization Terms Field: group of characters that represent something Record: group of related fields File:
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Lecture 2 An Overview of Relational Database IST 318 – DB Admin.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CMPE 226 Database Systems September 16 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
CS 174: Web Programming September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Component 4: Introduction to Information and Computer Science Unit 6: Databases and SQL Lecture 2 This material was developed by Oregon Health & Science.
CS 157B: Database Management Systems II February 6 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
Normalization (Codd, 1972) Practical Information For Real World Database Design.
CS 157B: Database Management Systems II February 4 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 157B: Database Management Systems II January 28 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 160: Software Engineering October 1 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 160: Software Engineering October 15 Class Meeting
CS 160: Software Engineering October 6 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Chapter 1Introduction to Oracle9i: SQL1 Chapter 1 Overview of Database Concepts.
DataBase Management System What is DBMS Purpose of DBMS Data Abstraction Data Definition Language Data Manipulation Language Data Models Data Keys Relationships.
Fanny Widadie, S.P, M.Agr 1 Database Management Systems.
PowerPoint Presentation for Dennis, Wixom, & Tegarden Systems Analysis and Design with UML, 3rd Edition Copyright © 2009 John Wiley & Sons, Inc. All rights.
Access Review. Access Access is a database application A database is a collection of records and files organized for a particular purpose Access supports.
CMPE 226 Database Systems September 23 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
Relational Theory and Design
Programming Logic and Design Fourth Edition, Comprehensive Chapter 16 Using Relational Databases.
CS 174: Web Programming October 14 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
BSA206 Database Management Systems Lecture 2: Introduction to Oracle / Overview of Database Concepts.
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
Understand Relational Database Management Systems Software Development Fundamentals LESSON 6.1.
CS 174: Web Programming November 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Chapter 3: Relational Databases
Huffman Trucking Fleet Vehicle Maintenance Database DBM380 June 13, 2012 Learning Team B.
CS 160 and CMPE/SE 131 Software Engineering March 10 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
CS 160 and CMPE/SE 131 Software Engineering March 8 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Lecture 5 Data Model Design Jeffery S. Horsburgh Hydroinformatics Fall 2012 This work was funded by National Science Foundation Grant EPS
CS 160 and CMPE/SE 131 Software Engineering March 15 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Rationale Databases are an integral part of an organization. Aspiring Database Developers should be able to efficiently design and implement databases.
CS 157B: Database Management Systems II January 23 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Introduction to Database Programming with Python Gary Stewart
CS4222 Principles of Database System
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 4 Relational Databases
Databases and Information Management
CMPE 226 Database Systems February 21 Class Meeting
CS 174: Server-Side Web Programming February 12 Class Meeting
Database Management Concepts
Databases and Information Management
Chengyu Sun California State University, Los Angeles
CMPE/SE 131 Software Engineering March 9 Class Meeting
A Very Brief Introduction to Relational Databases
CMPE/SE 131 Software Engineering March 7 Class Meeting
Presentation transcript:

CS 174: Web Programming September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak

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

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

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.

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.

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

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

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

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?

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.

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

Computer Science Dept. Spring 2015: February 17 CS 174: Web Programming © R. Mak 12 Logical Data Model  Initial version IdNameClass_codeSubjectRoom 7003Rogers, Tom926Java programming Thompson, Art908Data structures Lane, John951Software engineering Lane, John974Operating systems Flynn, 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, John Novak, Tim null 1009Klein, Leslienull 1014Jane, Mary7051null 1021Smith, Kim Student Teacher  Student id name which teachers  Teacher id name which classes taught  Class class code subject name class room number PK

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.

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 1001DoeJohn NovakTim null 1009KleinLeslienull 1014JaneMary7051null 1021SmithKim IdLastFirstClass_codeSubjectRoom 7003RogersTom926Java programming ThompsonArt908Data structures LaneJohn951Software engineering LaneJohn974Operating systems FlynnMabel931Compilers222 Student Teacher IdNameTeacher_id_1Teacher_id_2Teacher_id_3 1001Doe, John Novak, Tim null 1009Klein, Leslienull 1014Jane, Mary7051null 1021Smith, Kim IdNameClass_codeSubjectRoom 7003Rogers, Tom926Java programming Thompson, Art908Data structures Lane, John951Software engineering Lane, John974Operating systems Flynn, Mabel931Compilers222

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 Linking table IdLastFirstClass_codeSubjectRoom 7003RogersTom926Java programming ThompsonArt908Data structures LaneJohn951Software engineering LaneJohn974Operating systems FlynnMabel931Compilers222 Student Teacher Student_Teacher IdLastFirstTeacher_id_1Teacher_id_2Teacher_id_3 1001DoeJohn NovakTim null 1009KleinLeslienull 1014JaneMary7051null 1021SmithKim

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 programming ThompsonArt908Data structures LaneJohn951Software engineering LaneJohn974Operating systems FlynnMabel931Compilers222 Teacher

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 Data structures Java programming Compilers Software engineering Operating systems109 TeacherClass Primary key (PK) Foreign key (FK)  How would you do this relation with a linking table?

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 Data structures Java programming Compilers Software engineering Operating systems109 Student_idClass_code IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher Student Class Student_Class  John Doe takes Java programming, software engineering, and data structures.

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 Data structures Java programming Compilers Software engineering Operating systems109 Student_idClass_code IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher Student Class Student_Class  The Java Programming class has John Doe and Kim Smith.

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 Data structures Java programming Compilers Software engineering Operating systems109 Student_idClass_code IdLastFirst 7003RogersTom 7008ThompsonArt 7012LaneJohn 7051FlynnMabel Teacher Student Class Student_Class  Mabel Flynn teaches compilers.

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.

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 Data structures Java programming Compilers Software engineering Operating systems109 onezeroonemany Database cardinality is only 0, 1, or many (more than 1). TeacherClass minimum maximum

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 Data structures Java programming Compilers Software engineering Operating systems109 KeyStudent_idClass_code Student Class Student_Class  A student has 0, 1 or many classes.  A class has 1 or many students. Class Student Student-Class

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 Data structures Java programming Compilers Software engineering Operating systems109 Class

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.

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 Data structures Java programming Compilers Software engineering Operating systems109 Class SELECT code FROM class WHERE subject = 'Java programming' | code | | 926 | Source tablesDesired fields Selection criteria

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 Data structures Java programming Compilers Software engineering Operating 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.

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 Data structures Java programming Compilers Software engineering Operating systems109 ClassTeacher Demo

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 Data structures Java programming Compilers Software engineering Operating systems109 Student_idClass_code 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

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 Data structures Java programming Compilers Software engineering Operating systems109 Student_idClass_code 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

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 Data structures Java programming Compilers Software engineering Operating systems109 Student_idClass_code 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

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;

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 Data structures Java programming Compilers Software engineering Operating systems 109

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!

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 Data structures Java programming Compilers Software engineering Operating 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);

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

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, _addressVARCHAR(32)NOT NULL ); INSERT INTO contact_info (id, _address) VALUES(1, (2, (3, (4, (5, (6, (7, (8, (9,

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