mark dixon 1 06 – jsp databases: multiple tables

39
Mark Dixon 1 06 – JSP Databases: Multiple Tables

Upload: jordan-lyons

Post on 01-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 1

06 – JSP Databases: Multiple Tables

Page 2: 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.ukPrinted slidesRobbins J (2006) HTML & XHTML Pocket Reference

(3rd edition). O'Reilly. ISBN: 978-0-596-52727-3 Bergsten H (2001) JavaServer Pages Pocket

Reference. O'Reilly. ISBN: 978-0-596-00231-2Gennick J (2006) SQL Pocket Guide (2nd edition).

O'Reilly. ISBN: 978-0-596-52688-7Google

Page 3: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 3

Example: DistanceSPECIFICATION

• 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-functionalmust use JavaScriptshould be quick and easy to use

Page 4: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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-functionalmust use JSP server-side code (not JavaScript)should be quick and easy to use

Page 5: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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

• 1 furlong (furrow long) = 220 yds

22yds

220yds

Page 6: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 6

Example: CountriesSPECIFICATION

• 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-functionalmust use JSP server-side code (not JavaScript)should be quick and easy to use

Page 7: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 7

Questions: Databases• How many records are in the following table?• How many fields does the following table have?

64

CountryName Population Land Mass Continent

UK 60776238 241590 EuropeSpain 40448191 499542 EuropeGermany 82400996 349223 EuropeEgypt 80335036 995450 AfricaKenya 36913721 569250 AfricaChina 1321851888 9326410 Asia

Page 8: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 8

Questions: SQL• Write an SQL statement to display the name and

land mass of all countries in Africa.

SELECT Name, Land MassFROM Country WHERE Continent = 'Africa';

CountryName Population Land Mass Continent

UK 60776238 241590 EuropeSpain 40448191 499542 EuropeGermany 82400996 349223 EuropeEgypt 80335036 995450 AfricaKenya 36913721 569250 AfricaChina 1321851888 9326410 Asia

Page 9: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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("<br />Name").value

a = "<p>" + a "</p>"

html = html + <img src=face.gif />

h = "<table>" + h + "</table>"

Page 10: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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

Page 11: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 11

Data Duplication• Look for repeating data:

Track Title Artist Name Country

Paranoid Black Sabbath UK

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Page 12: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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)

Page 13: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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

Page 14: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 14

Relations (tables)

Track Title

Paranoid

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist Name Country

Black Sabbath UK

Page 15: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 15

Relations (tables)

Track Title

Paranoid

Falling in Love Aerosmith US

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

Artist

Artist Name Country

Black Sabbath UK

ID Artist Name Country

1 Black Sabbath UK

Page 16: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 16

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink Aerosmith US

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

Artist

ID Artist Name Country

1 Black Sabbath UKAerosmith US

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US2

Page 17: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 17

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator Aerosmith US

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

Page 18: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 18

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal Alien Ant Farm US

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

Page 19: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 19

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life Disturbed US

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

Page 20: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 20

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life

The Game Disturbed US

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

4

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Page 21: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 21

Relations (tables)

Track Title

Paranoid

Falling in Love

Pink

Love in an Elevator

Smooth Criminal

Meaning of Life

The Game

Voices Disturbed US

Down with the Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

4

4

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Page 22: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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 Sickness Disturbed US

Track

Artist ID

1

2

2

2

3

4

4

4

Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

Page 23: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 23

Relations (tables)

Track Title Artist ID

Paranoid 1

Falling in Love 2

Pink 2

Love in an Elevator 2

Smooth Criminal 3

Meaning of Life 4

The Game 4

Voices 4

Down with the Sickness 4

Track Artist

ID Artist Name Country

1 Black Sabbath UK

2 Aerosmith US

3 Alien Ant Farm US

4 Disturbed US

PrimaryKey

ForeignKey

Page 24: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 24

Question: Prescriptions• Identify duplication and separate:

Date Surname Forenames Drug Name

6 Jan 04 Jones Alison Co-codamol

11 Jan 04 Smith Bob Tegretol

18 Jan 04 Hope John Co-codamol

5 Feb 04 Johnson Sally Co-codamol

8 Feb 04 Smith Bob Tegretol

10 Feb 04 Smith Bob Sorbitol

Prescription

Page 25: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 25

Question: Solution

Date PatientID DrugID

6 Jan 04 1 1

11 Jan 04 2 2

18 Jan 04 3 1

5 Feb 04 4 1

8 Feb 04 2 2

10 Feb 04 2 3

Prescription

PatientID Surname Forenames

1 Jones Alison

2 Smith Bob

3 Hope John

4 Johnson Sally

Patient

DrugID Drug Name

1 Co-codamol

2 Tegretol

3 Sorbitol

Drug

Page 26: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 26

People Database (with Hobbies)ID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected]

2 Smith John 01752 111111 [email protected]

3 Jones Sally 01752 888888 [email protected]

4 Bloggs Fred 01752 123123 [email protected]

5 Anderson Genny 01752 987987 [email protected]

HobbyID Description PersonID

1 Archery 1

2 Herpetology 1

3 Music 1

4 Football 2

5 Rugby 2

6 Hitting people with swords 1

Hobby

Person

Page 27: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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)

Person Hobby

Page 28: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 28

Relationship Types• One-to-one

• One-to-many

• Many-to-one

• Many-to-many– (can't be implemented in relational database)

A B

A B

A B

A B

Page 29: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 29

Question: Which relationship type?

ID Surname Forenames Phone email

1 Dixon Mark 01752 232556 [email protected]

2 Smith John 01752 111111 [email protected]

3 Jones Sally 01752 888888 [email protected]

4 Bloggs Fred 01752 123123 [email protected]

5 Anderson Genny 01752 987987 [email protected]

HobbyID Description PersonID

1 Archery 1

2 Herpetology 1

3 Music 1

4 Football 2

5 Rugby 2

6 Hitting people with swords 1

Hobby

Person

Person

Hobby

Page 30: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 30

SQL: Joining tables

SELECT * FROM Person, Hobby;

Two tables

Cartesian set(all recordcombinations):

ID Surname Forenames Phone email HobbyID Description PersonID1 Dixon Mark 01752 232556 [email protected] 7 Archery 12 Smith John 01752 111111 [email protected] 7 Archery 13 Jones Sally 01752 888888 [email protected] 7 Archery 14 Bloggs Fred 01752 123123 [email protected] 7 Archery 15 Anderson Genny 01752 987987 [email protected] 7 Archery 11 Dixon Mark 01752 232556 [email protected] 8 Herpetology 12 Smith John 01752 111111 [email protected] 8 Herpetology 13 Jones Sally 01752 888888 [email protected] 8 Herpetology 14 Bloggs Fred 01752 123123 [email protected] 8 Herpetology 15 Anderson Genny 01752 987987 [email protected] 8 Herpetology 11 Dixon Mark 01752 232556 [email protected] 9 Music 12 Smith John 01752 111111 [email protected] 9 Music 13 Jones Sally 01752 888888 [email protected] 9 Music 14 Bloggs Fred 01752 123123 [email protected] 9 Music 15 Anderson Genny 01752 987987 [email protected] 9 Music 11 Dixon Mark 01752 232556 [email protected] 10 Football 22 Smith John 01752 111111 [email protected] 10 Football 23 Jones Sally 01752 888888 [email protected] 10 Football 24 Bloggs Fred 01752 123123 [email protected] 10 Football 25 Anderson Genny 01752 987987 [email protected] 10 Football 21 Dixon Mark 01752 232556 [email protected] 11 Rugby 22 Smith John 01752 111111 [email protected] 11 Rugby 23 Jones Sally 01752 888888 [email protected] 11 Rugby 24 Bloggs Fred 01752 123123 [email protected] 11 Rugby 25 Anderson Genny 01752 987987 [email protected] 11 Rugby 21 Dixon Mark 01752 232556 [email protected] 12 Hitting people with swords 12 Smith John 01752 111111 [email protected] 12 Hitting people with swords 13 Jones Sally 01752 888888 [email protected] 12 Hitting people with swords 14 Bloggs Fred 01752 123123 [email protected] 12 Hitting people with swords 15 Anderson Genny 01752 987987 [email protected] 12 Hitting people with swords 1

Page 31: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 31

SQL: Joining tables

SELECT *FROM Person, HobbyWHERE Person.ID = Hobby.PersonID;

Two tables

Matching recordsID Surname Forenames Phone email HobbyID Description PersonID

1 Dixon Mark 01752 232556 [email protected] 1 Archery 1

1 Dixon Mark 01752 232556 [email protected] 2 Herpetology 1

1 Dixon Mark 01752 232556 [email protected] 3 Music 1

1 Dixon Mark 01752 232556 [email protected] 6 Hitting people with swords 1

2 Smith John 01752 111111 [email protected] 4 Football 2

2 Smith John 01752 111111 [email protected] 5 Rugby 2

Page 32: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 32

SQL: Joining tables

ID Surname

1 Dixon

1 Dixon

1 Dixon

1 Dixon

2 Smith

2 Smith

SELECT ID, SurnameFROM Person, HobbyWHERE Person.ID = Hobby.PersonID;

Page 33: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 33

SQL: More• Loads more:

– group by– aggregate functions: average, count– inner joins– outer joins (left and right)

• Have a look at:– http://www.w3schools.com/sql/sql_join.asp

Page 34: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 34

Questions: Databases• How many primary keys?• How many foreign keys?

32

PlantPlantID EnglishName ScientificName Price Toxic FileName

1 Foxglove Digitalis purpurea 2.5 TRUE Foxglove.jpg2 Daisy Bellis perennis 0.45 FALSE Daisy.jpg3 Hemlock Conium maculatum 8.79 TRUE Hemlock.jpg4 Marsh Mallow Althaea officinalis 3.25 FALSE MarshMallow.jpg5 Lords-and-Ladies Arum maculatum 2.25 TRUE Lords.jpg6 Wild Carrot Daucus carota 1.25 FALSE WildCarrot.jpg7 Bluebell Hyacinthoides non-scripta 1.8 FALSE Bluebell.jpg8 Common Poppy Papaver rhoeas 1.28 FALSE Poppy.jpg

OrderOrderID CustID PlantID Quantity Date Current

1 1 7 10 14-Mar-06 TRUE2 2 5 2 14-Mar-06 TRUE3 1 3 1 14-Mar-06 FALSE5 2 4 4 14-Mar-06 FALSE

46 1 2 9 09-Jun-06 FALSE

CustomerCustID Surname Forenames email Password

1 Dixon Mark [email protected] a2 Jones Sally [email protected] sally

Page 35: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 35

Example: Person v1 (Specification)

• User requirement:– Display people's details from database online– need 2 pages:

smithjonesdixon

list of people

jones

person's details

Page 36: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 36

Example: PeopleList.jsp v1<%@page import="java.sql.*"%><%@page contentType="text/html"%><%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") + "<br />"; } cn.close();%><!DOCTYPE html><html> <head><title></title></head> <body> <%=html%> </body></html>

Page 37: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 37

Example: PeopleList.jsp v2<%@page import="java.sql.*"%><%@page contentType="text/html"%><%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 += "<a href='Person2.jsp?id=" + id + "'>"; html += r.getString("Surname") + "</a><br />"; } cn.close();%><!DOCTYPE html><html> <head><title></title></head> <body> <%=html%> </body></html>

now links

Page 38: Mark Dixon 1 06 – JSP Databases: Multiple Tables

Mark Dixon 38

Example: Person.jsp v2<%@page import="java.sql.*"%><%@page contentType="text/html" pageEncoding="UTF-8"%><%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();%><!DOCTYPE html><html> <head><title>Person</title></head> <body> Surname: <input name="txtSurname" type="text" value="<%=surname%>" /> </body></html>

reads querystring(from previous page)

displays data forselected record only

Page 39: Mark Dixon 1 06 – JSP Databases: Multiple Tables

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.