Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data.

Slides:



Advertisements
Similar presentations
Mark Dixon Page 1 05 – Database Design: Sub-forms.
Advertisements

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, SoCCE SOFT 131Page 1 24 – Web applications: Writing data to Databases using ASP.
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, 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.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
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?
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
ACCESS CHAPTER 1. OBJECTIVES Tables Queries Forms Reports Primary and Foreign Keys Relationship.
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 1Advanced Tables, Relationships, Queries, and Forms Chapter 3Advanced Query Techniques.
Mark Dixon 1 20 – Persistent data storage: relational databases and ADO.
Mark Dixon Page 1 5 – Persistent data storage: relational databases.
ASP.NET Programming with C# and SQL Server First Edition
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.
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.
CSCI 6962: Server-side Design and Programming Introduction to Active Server Pages.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 9 1 Microsoft Office Access 2003 Tutorial 9 – Using Action Queries, and Defining Table Relationships.
SYST Web Technologies SYST Web Technologies Databases & MySQL.
Mark Dixon Page 1 21 – Persistent data storage: relational databases and MySQL.
Mark Dixon 1 05 – JSP Databases: Multiple Tables.
Mark Dixon 1 06 – JSP Databases: Multiple Tables.
ISM 4212 Lab Creating DB Tables 02 copyright Lars Paul Linden 2007.
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.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
Mark Dixon Page 1 Web-Application Development Workshop.
Tutorial 9 Using Action Queries and Advanced Table Relationships.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 2 BACKNEXTEND 2-1 LINKS TO OBJECTIVES Creating Related Tables Creating Related Tables Determining.
Mark Dixon Page 1 21 – Web applications: Writing data to Databases using ASP.
Microsoft Access 2013 ®® Tutorial 10 Automating Tasks with Macros.
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.
M Dixon 1 Web-Application Development Workshop. M Dixon 2 Session Aims & Objectives Aims –to introduce the main concepts involved in creating web-applications.
O FFICE M ANAGEMENT T OOL - II B BA -V I TH. Abdus Salam2 Week-7 Introduction to Query Introduction to Query Querying from Multiple Tables Querying from.
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Web applications: Databases & ASP.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Databases: Structured Query Language.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Mark Dixon, SoCCE SOFT 131Page 1 14 – Databases: Multiple Tables.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
® Microsoft Access 2010 Tutorial 9 Using Action Queries and Advanced Table Relationships.
18 – Databases: Structured Query Language
Objectives Create an action query to create a table
17 – Persistent data storage: relational databases and ADO
19 – Databases: Multiple Tables
Tutorial 9 Using Action Queries and Advanced Table Relationships
Presentation transcript:

Mark Dixon 1 21 – Databases: Multiple Tables and Writing Data

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

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

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 + " "   

Mark Dixon 5 Advice Don’t –put anything on desktop –Especially database

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

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

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)

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

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

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

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

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 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS

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 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS

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 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS

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 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

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 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

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 Artist IDArtist NameCountry 1Black SabbathUK 2AerosmithUS 3Alien Ant FarmUS 4DisturbedUS

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

Mark Dixon 20 Question: Keys NamePopulationLand MassContID UK Spain Germany Egypt Kenya China Country Continent IDName 1Europe 2Africa 3Asia Name a Primary Key Name a Foreign Key ID in the Continent table ContID in the Country table

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

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

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

Mark Dixon 24 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 25 SQL: Joining tables SELECT * FROM Person, Hobby; Two tables Cartesian set (all record combinations):

Mark Dixon 26 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 27 SQL: Joining tables IDSurname 1Dixon Smith 2 SELECT ID, Surname FROM Person, Hobby WHERE Person.ID = Hobby.PersonID;

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

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

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

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

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

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

Mark Dixon 34 Example: Person v2 (Specification) User requirement: Display person’s details from database online –Change surname and save to database

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

Mark Dixon 36 WARNING!! All changes permanent (no undo) WHERE clause is CRITICAL DELETE FROM Person; Will delete ALL records in table

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

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

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

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.

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.

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)

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

Mark Dixon 44 How To: Database Permissions 2 Click Add button

Mark Dixon 45 How To: Database Permissions 3 Click Advanced button

Mark Dixon 46 Click Find button Click user Click OK button How To: Database Permissions 4

Mark Dixon 47 How To: Database Permissions 5 Select Internet Guest Account Ensure write access is on