Keys, Referential Integrity and PHP One to Many on the Web.

Slides:



Advertisements
Similar presentations
DB glossary (focus on typical SQL RDBMS, not XQuery or SPARQL)
Advertisements

Widhy Hayuhardhika NP, S.Kom. Overview of database structure Connecting to MySQL database Selecting the database to use Using the require_once statement.
Designing Tables in Microsoft Access By Ed Lance.
ERWin Template Overview By: Dave Wentzel. Agenda u Overview of Templates/Macros u Template editor u Available templates u Independent column browser u.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
The Collections Keeper A collections management system Brian J. Mullen.
Maintenance Modifying the data –Add records –Delete records –Update records Modifying the design –Add fields into tables –Remove fields from a table –Change.
Introduction to Databases CIS 5.2. Where would you find info about yourself stored in a computer? College Physician’s office Library Grocery Store Dentist’s.
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2010 All Rights Reserved. 1.
DAT702.  Standard Query Language  Ability to access and manipulate databases ◦ Retrieve data ◦ Insert, delete, update records ◦ Create and set permissions.
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
With Microsoft Access 2007 Volume 1© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access 2007 Volume 1 Chapter.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
DAY 14: ACCESS CHAPTER 1 Tazin Afrin October 03,
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Management Information Systems MS Access MS Access is an application software that facilitates us to create Database Management Systems (DBMS)
SQL Server 7.0 Maintaining Referential Integrity.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Relationships and Advanced Query Concepts Using Multiple Tables Please use speaker notes for additional information!
Kirkwood Center for Continuing Education Introduction to PHP and MySQL By Fred McClurg, Copyright © 2015, Fred McClurg, All Rights.
Database Design. Referential Integrity : data in a table that links to data in another table must always work in such a way that following the link will.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
Access Project 3 Notes. Introduction Maintaining the Database  Modifying the data to keep it up-to-date Restructure the Database  To change the database.
Creating PHPs to Insert, Update, and Delete Data CS 320.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
© 2002 by Prentice Hall 1 Database Processing with Microsoft Access David M. Kroenke Database Concepts 1e Appendix A.
IST 220 Introduction to Databases Course Wrap-up.
Data Driven Designs 99% of enterprise applications operate on database data or at least interface databases. Most common DBMS are Microsoft SQL Server,
Database Processing with Microsoft Access Appendix DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Chapter 9 Logical Database Design : Mapping ER Model To Tables.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
NMD202 Web Scripting Week5. What we will cover today PHP & MySQL Displaying Dynamic Pages Exercises Modifying Data PHP Exercises Assignment 1.
Microsoft Office 2013: In Practice Chapter 2 Using Design View, Data Validation, and Relationships Copyright © 2014 by The McGraw-Hill Companies, Inc.
Microsoft FrontPage 2003 Illustrated Complete Integrating a Database with a Web Site.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Session 1 Module 1: Introduction to Data Integrity
CP476 Internet Computing Perl CGI and MySql 1 Relational Databases –A database is a collection of data organized to allow relatively easy access for retrievals,
Chapter 3: Relational Databases
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Tables & Relationships
CHAPTER 7 DATABASE ACCESS THROUGH WEB
© 2016, Mike Murach & Associates, Inc.
CSIS 115 Database Design and Applications for Business
CIS 155 Table Relationship
ISC440: Web Programming 2 Server-side Scripting PHP 3
CS122 Using Relational Databases and SQL
Relational Database Design
CS122 Using Relational Databases and SQL
Microsoft Access Validation Rules, Table Relationships And
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Presentation transcript:

Keys, Referential Integrity and PHP One to Many on the Web

Music Database ArtistAlbum Track PK: artist_id PK: (artist_id, album_id) FK: artist_id PK: track_id FK: artist_id, album_id 1New Order 2Nick Cave 3Miles Davis 13 Retro 1 2 Substance 3 2 In a Silent Way 0 Do You Love Me Elegia 1 1

Composite Primary Key Implications AUTOINCREMENT works great with single column primary key. Not so with composite keys. Query may require two joins (transition table) Update, Delete may require two joins (transition table)

Music Database ArtistAlbum Track PK: artist_id PK: album_id FK: artist_id PK: track_id FK: artist_id, album_id 1New Order 2Nick Cave 3Miles Davis 11 Retro 1 2 Substance 3 3 In a Silent Way 0 Do You Love Me Elegia 1 1

Referential Integrity Review Referential Integrity rule: When one table has a foreign key to another table, the concept of referential integrity states that you may not add a record to the table that contains the foreign key unless there is a corresponding record in the linked table. – It also includes the techniques known as cascading update and cascading delete, which ensure that changes made to the linked table are reflected in the primary table.

Referential Integrity and SQL Parent table (no foreign key) – Insert: No impact – Delete: Must prevent orphan foreign key – Modify (primary key not changed ): No impact – Modify (primary key change): Update all children (cascade update) Child table (foreign key) – Insert: foreign key must be valid and not null – Delete: no impact – Update: foreign key must be valid and not null

Review of PHP with One Table No foreign key (parent table) Insert, Modify, Delete and Query from Web page.

Referential Integrity and SQL Parent table (no foreign key) – Insert: No impact – Delete: Must prevent orphan foreign key – Modify (primary key not changed ): No impact – Modify (primary key change): Update all children (cascade update)

PHP with Two Tables: No Foreign Key (parent table) Insert: No considerations Delete: Cannot leave orphan foreign key – Prohibit deletions – Delete all children that belong to parent*** – Set all children foreign key to NULL (no parent) Update: Cannot leave orphan foreign key – Prohibit change to primary key in parent*** – Update all children with updated foreign key

Cascade Delete (Child then Parent) if(isset($_GET['deleteartist'])) { $sql = 'DELETE FROM album WHERE artist_id = :artist_id'; $s=$pdo->prepare($sql); $s->bindValue(':artist_id', $_POST['artist_id']); $s->execute(); $sql='DELETE FROM artist WHERE artist_id = :artist_id'; $s=$pdo->prepare($sql); $s->bindValue(':artist_id', $_POST['artist_id']); $s->execute(); header('Location:.'); exit(); } First DELETE children Then DELETE parent

Referential Integrity and SQL Child table (foreign key) – Add: foreign key must be valid and not null – Delete: no impact – Update: foreign key must be valid and not null

PHP with Two Tables: Foreign key table (child table) Insert: Input new data, select valid foreign key – Drop down box – Check boxes Delete: No considerations-just delete child Update (no foreign key change): No considerations-just query and update child Update (foreign key change): select valid foreign key – Drop down box – Check boxes

DELETE From a Child Table (index.php) No Impact if (isset($_POST['action']) and $_POST['action'] == 'Delete') { include $_SERVER['DOCUMENT_ROOT']. '/connect/db.inc.php'; // Delete the joke $sql = 'DELETE FROM album WHERE album_id = :album_id'; $s = $pdo->prepare($sql); $s->bindValue(':album_id', $_POST['album_id']); $s->execute(); header('Location:.'); exit(); }

INSERT into Child Table Form to collect new data – Create and populate Drop Down box for foreign key Controller (index.php) 2 parts: Part 1: -SQL to collect foreign keys for Drop Down Box on form -load form (without values) Part 2: -SQL to post INSERT with form data * requires foreign key (AUTOINCREMENT handles primary key)

SQL to collect foreign keys for Drop Down (index.php) $result = $pdo->query('SELECT artist_id, artist_name FROM artist'); foreach ($result as $row) { $artists[] = array( 'artist_id' => $row['artist_id'], 'artist_name' => $row['artist_name'] ); }

load form (without values) index.php include 'form.html.php';

Create and populate Drop Down box for foreign key (form.html.php) Type the album name here: Artist: Select one ">

SQL to post INSERT with form data index.php $sql = 'INSERT INTO album SET album_name = :album_name, artist_id = :artist_id'; $s = $pdo->prepare($sql); $s->bindValue (':album_name', $_POST['album_name']); //form.html.php’s album_name $s->bindValue (':artist_id', $_POST['artist']); //form.html.php’s artist $s->execute();

* requires foreign key (AUTOINCREMENT handles primary key) Form.html.php Index.php $s->bindValue(':artist_id', $_POST['artist']); //form.html.php’s artist

Query Child Table include $_SERVER['DOCUMENT_ROOT']. '/connect/db.inc.php'; // The basic SELECT statement $select = 'SELECT album_id, album_name'; $from = ' FROM album'; $where = ' WHERE TRUE'; $placeholders = array();

Allow Users to Choose Criteria if ($_GET['artist'] != '') // Search by author { $where.= " AND artist_id = :artist_id"; $placeholders[':artist_id'] = $_GET['artist']; } if ($_GET['album_name'] != '') // search text { $where.= " AND album_name LIKE :album_name"; $placeholders[':album_name'] = '%'. $_GET['album_name']. '%'; } $sql = $select. $from. $where; $s = $pdo->prepare($sql); $s->execute( $placeholders);

Store Query Result for Form foreach ($s as $row) { $albums[] = array('album_id' => $row['album_id'], 'album_name' => $row['album_name']); } include 'albums.html.php';

Search Result Form (albums.html.php) Search Results Album Name Options <input type="hidden" name="album_id" value=" ">

UPDATE into Child Table Query: find row to update (done) Form: collect new data – Create and populate Drop Down box for foreign key Controller (index.php) 2 parts: Part 1: -SQL to collect foreign keys for Drop Down box on form -load form (with selected record) Part 2: -SQL to post UPDATE with form data * requires primary key of child and foreign key

Create and Populate Drop Down Artist: Select one ">

load form (with selected record) Collect all data for a child to display on form (index.php) $sql = 'SELECT album_id, album_name, artist_id FROM album WHERE album_id = :album_id'; $s = $pdo->prepare($sql); $s->bindValue(':album_id', $_POST['album_id']); $s->execute(); Where did album_id come from? albums.php.html (used for search results) ">

Load form (with selected record) Store album info from query in variables (index.php) $row = $s->fetch(); $album_name = $row['album_name']; $artist_id = $row['artist_id']; $album_id = $row['album_id']; Then load into form (form.html.php) Type the album name here:

SQL to post UPDATE with form data index.php $sql = 'UPDATE album SET album_name = :album_name, artist_id = :artist_id WHERE album_id = :album_id'; $s = $pdo->prepare($sql); $s->bindValue(':album_id', $_POST['album_id']); $s->bindValue (':album_name', $_POST['album_name']); //form.html.php’s album_name $s->bindValue (':artist_id', $_POST['artist']); //form.html.php’s artist $s->execute();

*requires primary key of child and foreign key $s->bindValue(':album_id',$_POST['album_id']); //primary key $s->bindValue(':artist_id', $_POST['artist']); //foreign key