Mark Dixon 1 06 – JSP Databases: Multiple Tables.

Slides:



Advertisements
Similar presentations
Database Relationships in Access As you recall, the data in a database is stored in tables. In a relational database like Access, you can have multiple.
Advertisements

Mark Dixon Page 1 23 – Object Oriented Programming in PhP.
Normalisation Ensuring data integrity in database design 1.
Mark Dixon Page 1 05 – Database Design: Sub-forms.
Mark Dixon Page 1 04 – Database Design: Forms. Mark Dixon Page 2 Session Aims & Objectives Aims –To allow easier data entry using forms Objectives, by.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 06 – Expression Builder. Mark Dixon Page 2 Session Aims & Objectives Aims –To use expressions to perform more complex calculations in.
Mark Dixon Page 1 03 – Database Design: Multiple Tables.
SUNY Morrisville-Norwich Campus-Week 10 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 SOFT – Module Introduction.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Entity-relationship diagrams.
Mark Dixon, SoCCE SOFT 131Page 1 22 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 18 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon Page 1 20 – Modular Design in ASP. Mark Dixon Page 2 Session Aims & Objectives Aims –Highlight modular design techniques in ASP Objectives,
Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Databases: Multiple Tables.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Relational Databases What is a relational database? What would we use one for? What do they look like? How can we describe them? How can you create one?
Page 1 ISMT E-120 Desktop Applications for Managers Introduction to Microsoft Access.
Mark Dixon 1 20 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 5 – Persistent data storage: relational databases.
2.3 Organising Data for Effective Retrieval
Unit 18: Database Modelling
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
M Dixon 1 Soft – Module Introduction and HTML.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Lecture 2 An Overview of Relational Database IST 318 – DB Admin.
RELATIONSHIPS Generally there are two main database types: flat-file and relational.
PLUG IT IN 3 Fundamentals of Relational Database Operations.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 9 1 Microsoft Office Access 2003 Tutorial 9 – Using Action Queries, and Defining Table Relationships.
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL.
Mark Dixon 1 05 – JSP Databases: Multiple Tables.
Mark Dixon 1 09 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
Chapter 1Introduction to Oracle9i: SQL1 Chapter 1 Overview of Database Concepts.
Mark Dixon Page 1 Tech – Module Introduction.
Mark Dixon Page 1 Soft – Module Introduction and HTML.
Database collection of related information stored in an organized form Database program software tool for storage & retrieval of that information.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Mark Dixon 1 21 – Databases: Multiple Tables. Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the.
1 The Information School of the University of Washington Dec 1fit advdatabases © 2006 University of Washington Advanced Database Concepts INFO/CSE.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data.
Mark Dixon 1 08 – In-class test. Mark Dixon 2 In-class Test Date: Wednesday 16 th November 2011 Time: 09:05-10:55 Location: SMB109 Type: Individual 40%
Lesson 2: Designing a Database and Creating Tables.
Mark Dixon 1 9 – Case Study. Mark Dixon 2 Session Aims and Objectives Aims –To give an overview of the development of a web-page from initial idea to.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Web applications: Databases & ASP.
Chapter 56 Relational Database Design Compiled by Eddie Moorcroft.
Mark Dixon 1 11 – Java Servlets. Mark Dixon 2 Session Aims & Objectives Aims –To cover a range of web-application design techniques Objectives, by end.
BSA206 Database Management Systems Lecture 2: Introduction to Oracle / Overview of Database Concepts.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
Data modeling Process. Copyright © CIST 2 Definition What is data modeling? –Identify the real world data that must be stored on the database –Design.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
Mark Dixon 1 07 – In-class test. Mark Dixon 2 In-class Test Date: Monday 5 th November 2012 Time: 11:05-120:55 Location: SMB109 Type: Individual 40% of.
Mark Dixon, SoCCE SOFT 131Page 1 14 – Databases: Multiple Tables.
Mark Dixon 1 Soft051 Examination Sample Questions.
Database Planning Database Design Normalization.
1 Management Information Systems M Agung Ali Fikri, SE. MM.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
18 – Databases: Structured Query Language
17 – Persistent data storage: relational databases and ADO
19 – Databases: Multiple Tables
Presentation transcript:

Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 2 Admin: Test Next week (Mon 5th) 11:00-13:00 Individual 40% of module mark Open book: Web-site: mdixon.soc.plymouth.ac.uk Printed slides Robbins J (2006) HTML & XHTML Pocket Reference (3rd edition). O'Reilly. ISBN: Bergsten H (2001) JavaServer Pages Pocket Reference. O'Reilly. ISBN: Gennick J (2006) SQL Pocket Guide (2nd edition). O'Reilly. ISBN: Google

Mark Dixon 3 Example: Distance SPECIFICATION User Requirements –need to convert a given distance in miles to kilometres (1 mile = 1.6 km) Software Requirements –Functional: –User should be able to enter the distance (in miles) –the distance (in kilometres) should be calculated and displayed –Non-functional must use JavaScript should be quick and easy to use

Mark Dixon 4 Example: Land Area (what) SPECIFICATION User Requirements –need to know the land area in acres, given the length and width in metres (1 m = 1.09 yds) Software Requirements –Functional: –User should be able to enter the length and the width (in metres) –the length and width (in yds), and the area (in acres) should be calculated and displayed –Non-functional must use JSP server-side code (not JavaScript) should be quick and easy to use

Mark Dixon 5 Example: Land Area (how) Acre – unit of measurement of land area –long strip of land: can be ploughed in a day 1 Acre = 220 yds x 22 yds (= 4840 yds 2 ) 1 furlong (furrow long) = 220 yds 22yds 220yds

Mark Dixon 6 Example: Countries SPECIFICATION User Requirements –Manage population data Software Requirements –Functional: –Country List page: should list all countries in the database (each item should be a link to the Country page). –Country page: should list all details for specific country. –Non-functional must use JSP server-side code (not JavaScript) should be quick and easy to use

Mark Dixon 7 Questions: Databases How many records are in the following table? How many fields does the following table have? 6 4

Mark Dixon 8 Questions: SQL Write an SQL statement to display the name and land mass of all countries in Africa. SELECT Name, Land Mass FROM Country WHERE Continent = 'Africa';

Mark Dixon 9 Questions: HTML in VB Are these correct (assume variables and fields exist)? f = f + rs.Fields("Description").value h = h + rs.Fields(" Name").value a = " " + a " " html = html + h = " " + h + " "   

Mark Dixon 10 Session Aims & Objectives Aims –To add dealing with multiple tables and writing data to your understanding of databases Objectives, by end of this week’s sessions, you should be able to: –identify a suitable primary key for a table –identify duplicated data in a single table –split that table to reduce data redundancy, using a suitable foreign key –generate SQL statements to (temporarily) join tables, and use these in your code –create a web page that allows the user to write (store) data in database

Mark Dixon 11 Data Duplication Look for repeating data: Track TitleArtist NameCountry ParanoidBlack SabbathUK Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track

Mark Dixon 12 Problem: Data Duplication takes up lots of space can become inconsistent (misspellings) difficult to change (need to change each instance) difficult to search (misspellings)

Mark Dixon 13 Solution: Normalisation Part of database design Process of breaking data down (splitting) Codd –7 stages of normalisation Mathematical Difficult to apply stages Most professionals do it instinctively

Mark Dixon 14 Relations (tables) Track Title Paranoid Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist NameCountry Black SabbathUK

Mark Dixon 15 Relations (tables) Track Title Paranoid Falling in LoveAerosmithUS PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 Artist Artist NameCountry Black SabbathUK IDArtist NameCountry 1Black SabbathUK

Mark Dixon 16 Relations (tables) Track Title Paranoid Falling in Love PinkAerosmithUS Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID 1 Artist IDArtist NameCountry 1Black SabbathUK AerosmithUS IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 2

Mark Dixon 17 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an ElevatorAerosmithUS Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS

Mark Dixon 18 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth CriminalAlien Ant FarmUS Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS

Mark Dixon 19 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of LifeDisturbedUS The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS

Mark Dixon 20 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of Life The GameDisturbedUS VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

Mark Dixon 21 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of Life The Game VoicesDisturbedUS Down with the SicknessDisturbedUS Track Artist ID Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

Mark Dixon 22 Relations (tables) Track Title Paranoid Falling in Love Pink Love in an Elevator Smooth Criminal Meaning of Life The Game Voices Down with the SicknessDisturbedUS Track Artist ID Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

Mark Dixon 23 Relations (tables) Track TitleArtist ID Paranoid1 Falling in Love2 Pink2 Love in an Elevator2 Smooth Criminal3 Meaning of Life4 The Game4 Voices4 Down with the Sickness4 Track Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS Primary Key Foreign Key

Mark Dixon 24 Question: Prescriptions Identify duplication and separate: DateSurnameForenamesDrug Name 6 Jan 04JonesAlisonCo-codamol 11 Jan 04SmithBobTegretol 18 Jan 04HopeJohnCo-codamol 5 Feb 04JohnsonSallyCo-codamol 8 Feb 04SmithBobTegretol 10 Feb 04SmithBobSorbitol Prescription

Mark Dixon 25 Question: Solution DatePatientIDDrugID 6 Jan Jan Jan Feb Feb Feb 0423 Prescription PatientIDSurnameForenames 1JonesAlison 2SmithBob 3HopeJohn 4JohnsonSally Patient DrugIDDrug Name 1Co-codamol 2Tegretol 3Sorbitol Drug

Mark Dixon 26 People Database (with Hobbies) IDSurnameForenamesPhone 1DixonMark SmithJohn JonesSally BloggsFred AndersonGenny01752 HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person

Mark Dixon 27 Entity-relationship diagrams Each table in db –stores details of entity shown as rectangular box Relationships between tables –represent relationships between entities shown as line between entities (boxes) PersonHobby

Mark Dixon 28 Relationship Types One-to-one One-to-many Many-to-one Many-to-many –(can't be implemented in relational database) ABABABAB

Mark Dixon 29 Question: Which relationship type? IDSurnameForenamesPhone 1DixonMark SmithJohn JonesSally BloggsFred AndersonGenny01752 HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person Hobby

Mark Dixon 30 SQL: Joining tables SELECT * FROM Person, Hobby; Two tables Cartesian set (all record combinations):

Mark Dixon 31 SQL: Joining tables SELECT * FROM Person, Hobby WHERE Person.ID = Hobby.PersonID; Two tables Matching records IDSurnameForenamesPhone HobbyIDDescriptionPersonID 1DixonMark DixonMark DixonMark DixonMark01752 people with swords1 2SmithJohn SmithJohn01752

Mark Dixon 32 SQL: Joining tables IDSurname 1Dixon Smith 2 SELECT ID, Surname FROM Person, Hobby WHERE Person.ID = Hobby.PersonID;

Mark Dixon 33 SQL: More Loads more: –group by –aggregate functions: average, count –inner joins –outer joins (left and right) Have a look at: –

Mark Dixon 34 Questions: Databases How many primary keys? How many foreign keys? 3 2

Mark Dixon 35 Example: Person v1 (Specification) User requirement: –Display people's details from database online –need 2 pages: smith jones dixon list of people jones person's details

Mark Dixon 36 Example: PeopleList.jsp v1 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; while(r.next()){ html += r.getString("Surname") + " "; } cn.close(); %>

Mark Dixon 37 Example: PeopleList.jsp v2 <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person;"); String html = ""; String id; while(r.next()){ id = Integer.toString(r.getInt("PersonID")); html += " "; html += r.getString("Surname") + " "; } cn.close(); %> now links

Mark Dixon 38 Example: Person.jsp v2 <% String id = request.getParameter("id"); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection cn = DriverManager.getConnection("jdbc:odbc:PeopleDB", "", ""); Statement st = cn.createStatement(); ResultSet r = st.executeQuery("SELECT * FROM Person WHERE PersonID = " + id + ";" ); String surname = ""; if(r.next()){ surname = r.getString("Surname"); } cn.close(); %> Person Surname: " /> reads querystring (from previous page) displays data for selected record only

Mark Dixon 39 Tutorial Exercise: Person Task 1: Get the Person (v1) example from the lecture working. Task 2: Modify your code, so that forename is displayed as well as surname (use a table). Task 3: Get the Person (v2) example from the lecture working.