Download presentation
Presentation is loading. Please wait.
Published byReynold Murphy Modified over 9 years ago
1
Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data
2
Mark Dixon 2 Questions: Databases How many records are in the following table? How many fields does the following table have? 6 4
3
Mark Dixon 3 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';
4
Mark Dixon 4 Questions: HTML in VB Are these correct (assume variables and fields exist)? f = f + r("Description") h = h + r(" Name") a = " " + a " " html = html + h = " " + h + " "
5
Mark Dixon 5 Advice Don’t –put anything on desktop –Especially database
6
Mark Dixon 6 Session Aims & Objectives Aims –To deal with multiple tables –To write data to 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 –use SQL to write data into database
7
Mark Dixon 7 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
8
Mark Dixon 8 Problem: Data Duplication takes up lots of space can become inconsistent (misspellings) difficult to change (need to change each instance) difficult to search (misspellings)
9
Mark Dixon 9 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
10
Mark Dixon 10 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
11
Mark Dixon 11 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
12
Mark Dixon 12 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
13
Mark Dixon 13 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 1 2 2 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS
14
Mark Dixon 14 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 1 2 2 2 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS
15
Mark Dixon 15 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 1 2 2 2 3 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS
16
Mark Dixon 16 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 1 2 2 2 3 4 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS
17
Mark Dixon 17 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 1 2 2 2 3 4 4 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS
18
Mark Dixon 18 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 1 2 2 2 3 4 4 4 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS
19
Mark Dixon 19 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
20
Mark Dixon 20 Question: Keys NamePopulationLand MassContID UK607762382415901 Spain404481914995421 Germany824009963492231 Egypt803350369954502 Kenya369137215692502 China132185188893264103 Country Continent IDName 1Europe 2Africa 3Asia Name a Primary Key Name a Foreign Key ID in the Continent table ContID in the Country table
21
Mark Dixon 21 People Database (with Hobbies) IDSurnameForenamesPhoneemail 1DixonMark01752 232556mark.dixon@plymouth.ac.uk 2SmithJohn01752 111111john.smith@john.smith.ac.uk 3JonesSally01752 888888sally.jones@sally.jones.com 4BloggsFred01752 123123fred.bloggs@aaaaaa.com 5AndersonGenny01752 987987genny@bbbb.cccc.com HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person
22
Mark Dixon 22 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
23
Mark Dixon 23 Relationship Types One-to-one One-to-many Many-to-one Many-to-many –(can't be implemented in relational database) ABABABAB
24
Mark Dixon 24 Question: Which relationship type? IDSurnameForenamesPhoneemail 1DixonMark01752 232556mark.dixon@plymouth.ac.uk 2SmithJohn01752 111111john.smith@john.smith.ac.uk 3JonesSally01752 888888sally.jones@sally.jones.com 4BloggsFred01752 123123fred.bloggs@aaaaaa.com 5AndersonGenny01752 987987genny@bbbb.cccc.com HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person Hobby
25
Mark Dixon 25 SQL: Joining tables SELECT * FROM Person, Hobby; Two tables Cartesian set (all record combinations):
26
Mark Dixon 26 SQL: Joining tables SELECT * FROM Person, Hobby WHERE Person.ID = Hobby.PersonID; Two tables Matching records IDSurnameForenamesPhoneemailHobbyIDDescriptionPersonID 1DixonMark01752 232556mark.dixon@plymouth.ac.uk1Archery1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk2Herpetology1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk3Music1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk6Hitting people with swords1 2SmithJohn01752 111111john.smith@john.smith.ac.uk4Football2 2SmithJohn01752 111111john.smith@john.smith.ac.uk5Rugby2
27
Mark Dixon 27 SQL: Joining tables IDSurname 1Dixon 1 1 1 2Smith 2 SELECT ID, Surname FROM Person, Hobby WHERE Person.ID = Hobby.PersonID;
28
Mark Dixon 28 Question: SQL Joining Tables Write an SQL query to join the following: 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
29
Mark Dixon 29 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
30
Mark Dixon 30 Example: Person v1 (Specification) User requirement: –Display people's details from database online –need 2 pages: smith jones dixon list of people jones sally person's details
31
Mark Dixon 31 Example: PeopleList.aspx v1 Sub Page_Load() Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub
32
Mark Dixon 32 Example: PeopleList.aspx v2 Sub Page_Load() Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String cmd = New OleDbCommand("SELECT * FROM Person;", cn) cn.Open() r = cmd.ExecuteReader() s = "" Do While r.Read() s = s & " " s = s & r("Surname") & " " Loop cn.Close parData.InnerHtml = s End Sub now links
33
Mark Dixon 33 Example: Person.aspx v2 Sub Page_Load() Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim sql As String Dim cn As New OleDbConnection(cs) Dim cmd As OleDbCommand Dim r As OleDbDataReader Dim s As String sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OleDbCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() s = "" If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Back to People List Surname: reads querystring (from previous page) displays data for selected record only
34
Mark Dixon 34 Example: Person v2 (Specification) User requirement: Display person’s details from database online –Change surname and save to database
35
Mark Dixon 35 Changing Data SQL –INSERT: inserts a new record INSERT INTO Person (Surname, Age) VALUES ('Smith', 21); –UPDATE: makes changes to specified record UPDATE Person Set Surname = 'Smith', Age = 21 WHERE id = 14; –DELETE: deletes specified record DELETE FROM Person WHERE id = 14
36
Mark Dixon 36 WARNING!! All changes permanent (no undo) WHERE clause is CRITICAL DELETE FROM Person; Will delete ALL records in table
37
Mark Dixon 37 Example: Person.aspx v3 (error) Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Sub Page_Load() Dim sql As String Dim cmd As OleDbCommand Dim r As OleDbDataReader sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OleDbCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Sub btnSave_Click(s As Object, e As EventArgs) Handles btnSave.ServerClick Dim cmd As OleDbCommand Dim sql As String sql = "UPDATE [Person] " + _ " SET [Surname] = '" + txtSurname.Value + "'" + _ " WHERE id = " & Request.QueryString("id") & ";" cmd = New OleDbCommand(sql, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close End Sub Save button executes SQL UPDATE PROBLEM: Page_Load re-reads old surname first
38
Mark Dixon 38 Example: Person.aspx v3b Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Sub Page_LoadComplete(s As Object, e As EventArgs) Dim sql As String Dim cmd As OleDbCommand Dim r As OleDbDataReader sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OleDbCommand(sql, cn) cn.Open() r = cmd.ExecuteReader() If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Sub btnSave_Click(s As Object, e As EventArgs) Handles btnSave.ServerClick Dim cmd As OleDbCommand Dim sql As String sql = "UPDATE [Person] " + _ " SET [Surname] = '" + txtSurname.Value + "'" + _ " WHERE id = " & Request.QueryString("id") & ";" cmd = New OleDbCommand(sql, cn) cn.Open() cmd.ExecuteNonQuery() cn.Close End Sub Save button executes SQL UPDATE Fix: Use Page_LoadComplete
39
Mark Dixon 39 Example: Person.aspx v3c Dim cs As String = "Provider=Microsoft.ACE.OLEDB.12.0;" + _ "Data Source=" + Server.MapPath("People.accdb") + ";" Dim cn As New OleDbConnection(cs) Dim sql As String Sub Page_Load () cn.Open() End Sub Sub btnSave_Click (s As Object, e As EventArgs) Handles btnSave.ServerClick Dim cmd As OleDbCommand sql = "UPDATE [Person] " + _ " SET [Surname] = '" + txtSurname.Value + "'" + _ " WHERE id = " & Request.QueryString("id") & ";" cmd = New OleDbCommand(sql, cn) cmd.ExecuteNonQuery() End Sub Sub Page_LoadComplete (s As Object, e As EventArgs) Dim cmd As OleDbCommand Dim r As OleDbDataReader sql = "SELECT * FROM Person WHERE id=" & Request.QueryString("id") cmd = New OleDbCommand(sql, cn) r = cmd.ExecuteReader() If r.Read() Then txtSurname.Value = r("Surname") End If cn.Close() End Sub Page_Load: first Click events Page_LoadComplete: last
40
Mark Dixon 40 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 and v3) example from the lecture working. Task 3: Modify your code, so that a line of text is displayed confirming that data has been saved. Task 4: Modify your code, so that an add button is included, which allows a new record to be added. Task 5: Modify your code, so that a delete button is included, which allows the current record to be deleted.
41
Mark Dixon 41 Tutorial Exercise: Music Task 1: Create the Music database (from the lecture) with the Track and Artist tables. Task 2: Create a web page to display a list of Artists. Task 4: Change that web page, so that each artist name is a link to another page, which displays all the tracks by that artist. Hint: Use query strings to pass the artist ID between pages.
42
Mark Dixon 42 How To: Database Permissions Generally –Read: works by default –Write: requires permissions Asp.Net pages run as user: –Visual Studio Logged in user (few problems) –IIS ASP.Net Account NETWORKSERVICE (Server 2003) IIS APPPOOL\DefaultAppPool (Windows 7)
43
Mark Dixon 43 How To: Database Permissions 1 In order for ASP to write to a database –Need to give write access account for database file (People.accdb) Right-click on file in File Explorer Click Properties Click Security tab Click Edit button
44
Mark Dixon 44 How To: Database Permissions 2 Click Add button
45
Mark Dixon 45 How To: Database Permissions 3 Click Advanced button
46
Mark Dixon 46 Click Find button Click user Click OK button How To: Database Permissions 4
47
Mark Dixon 47 How To: Database Permissions 5 Select Internet Guest Account Ensure write access is on
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.