"> ">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Databases for Web applications Making a table of table information. Reprise on database design. SQL. Classwork/Homework: Projects! Postings.

Similar presentations


Presentation on theme: "Creating Databases for Web applications Making a table of table information. Reprise on database design. SQL. Classwork/Homework: Projects! Postings."— Presentation transcript:

1 Creating Databases for Web applications Making a table of table information. Reprise on database design. SQL. Classwork/Homework: Projects! Postings.

2 <?php require("opendbo.php"); $query="show tables"; $rs=mysqli_query($link, $query); ?> Table names <?php $i = 0; while ($row=mysqli_fetch_array($rs)){ print(" "); $tablenames[$i] = $row[0]; $i++; print($row[0]); print(" "); } print(" ");

3 for ($j=0;$j<$i;$j++) { $query = "describe ".$tablenames[$j]; print (" ". $tablenames[$j]. " table \n "); print (" Field Type Null Key \n "); $rt=mysqli_query($link,$query); while ($fi=mysqli_fetch_array($rt)) { print (" ". $fi['Field']. " \n "); print (" ".$fi['Type']. " \n "); print (" ".$fi['Null']. " \n "); print (" ".$fi['Key']. " \n "); print (" "); } print (" "); } ?>

4 my query on table info http://socialsoftware.purchase.edu/jeanine.m eyer/showtableinfo.php

5 Challenge What if you have a large set of queries for the user –one of my earliest assignments at a hospital: the abominable abdominal report –?

6 Table of queries If you have a large set of fixed SQL queries, you may make a new table: iddescriptiontext 1final diagnosis when presenting signs of appendicitis Select final.diagnosis from final, initial where initial.temp > 100 AND initial.pain = 'left' AND final.caseno = initial.caseno 2initial potential ulcer cases Select * from initial where initial.pain = 'sharp' AND initial.temp < 100 ….

7 Present to user Pick selection: description final diagnosis when presenting signs of appendicitis initial potential ulcer cases Don't show the user the messy SQL

8 Produce responses Make the query the SQL corresponding to the user's choice. Display recordset in a table –Now, need generalized code that creates headings for tables and extracts names of fields 'on the fly' based on information in recordset. php: –mysqli_fetch_field –mysqli_fetch_array

9 <?php require("opendbo.php"); $query="Select * from customers"; $result=mysqli_query($link, $query); $fieldnames= Array(); print (" "); $nf = mysqli_num_fields($result); for ($i=0; $i<$nf;$i++) { $fieldobj= mysqli_fetch_field($result); $fieldnames[$i]=$fieldobj->name; print (" ".$fieldnames[$i]." "); } print (" \n"); while ($row=mysqli_fetch_array($result)) { print (" "); for ($i=0; $i<$nf; $i++) { print (" ".$row[$fieldnames[$i]]." "); } print(" "); } mysqli_close($link); ?> first for loop to set up headers Second for loop, in while loop, to extract field data.

10

11 Note http://socialsoftware.purchase.edu/jeanine. meyer/showCurrentCustomers.phphttp://socialsoftware.purchase.edu/jeanine. meyer/showCurrentCustomers.php If you do [ever] show field names to your customers/clients/players/…, –you need to make them longer/better/etc. –Make sure database doesn't have blanks, etc.

12 MySQL Rights Possible to limit rights to specific users. –specific table or whole database –SELECT, INSERT, DELETE, UPDATE, DROP, CREATE, ALTER NOTE: I don't know if your rights include right to use GRANT! Limiting rights is to prevent unintentional errors as much as / more than not trusting certain people. Your php code is main line of protection.

13 SQL question Generate list of customers who had orders made in the summer of 2013. Tables needed: –customers, orders –JOIN … ON condition the natural one: when id fields match WHERE condition? –order date 'in' summer. Use BETWEEN Any grouping (aka aggregation)? –No GROUP BY. Instead, use DISTINCT

14 SQL question SELECT DISTINCT cname FROM customers as c JOIN orders as o ON c.cid = o.cid WHERE o.date BETWEEN ‘2013- 06-01’ AND ‘2013-08-31’

15 SQL question What products were ordered by customers during Spring semester? Strategy: –decide on tables and ON conditions –determine dates and set up WHERE condition using BETWEEN –use DISTINCT

16 Left Join, etc. Searching for what is there and what isn't there. –Left Join –NOT EXISTS. You can create (on the fly) a new table and check if it has any rows. –See http://explainextended.com/2009/09/18/not-in- vs-not-exists-vs-left-join-is-null-mysql/ http://explainextended.com/2009/09/18/not-in- vs-not-exists-vs-left-join-is-null-mysql/

17 php: checking for value Using isset($_GET['guess']) is better than using $guess = @$_GET['guess']; if ($guess != null) { }

18 php: strpos Check if string is present in another string $n = strpos($answer,"SUNY"); if ($n>-1) { } Alternative to regular expression

19 php: explode Breaks up string into an array if $listofstuff is "orange,apple,peach" $list = explode(",",$listofstuff); produces $list = ["orange","apple","peach"];

20 Database administrator technical: working with software, including maintaining security, performance. Keeping aware of changes in products, new products. administrative: working with technical staff AND other staff specific business: knowing and keeping up-to-date with knowledge of this business.

21 Internationalization & Localization How to make the php MySQL application work in different places character sets (includes symbols, encodings for symbols, collations (ordering rules) error messages (MySQL). Presumably your php code does error handling, also. time zone way to express dates See SET and SHOW commands

22 Reprise: Data base design Not obvious! Need to determine the information and the location of information in one or more tables. For each piece of information, what is the data type (representation) –different types of numbers –different types of character strings –MAY choose to incorporate media

23 Reprise: Database table design Consider orders by customer what information goes with the order and what information goes with the customer You do not want information associated with a customer to be repeated in every order! –wasteful of space –if (when) information changes, want to change in just one place

24 Maintenance of database Change fields in records Delete records Need to maintain referential integrity –Meaning that if a field in a table represents a foreign key (a reference to a record in another or this table), the referenced record exists!

25 Referential integrity Can handle in middleware (php) or MySQL or both. MySQL –CREATE statements specify field as FOREIGN KEY. Add clause saying what to do upon deletion ON UPDATE CASCADE ON UPDATE SET NULL ON UPDATE RESTRICT -- if there is a reference, an error will be generated. This is the default

26 Database Transaction refers to a sequence of operations on the database that are to be treated as a unit. Either they all go through or none go through. php MySQL has commands to implement transactions. –Turn off the 'auto-commit feature' –check each query if any problem, use mysqli_rollback() –when all okay, use mysqli_commit()

27 Project presentation Prepare and printout 1 pager: project name, members of team, abstract (1 paragraph summary), screen shot Show (corrected/improved/finalized) design diagrams –DFD and ERD Demonstrate Show storyboard –how project is implemented in html and php scripts Show and explain tricky parts of code.

28 Like SQL feature: stripped down regular expression –Wild card: % stands for 0, 1 or more letters SELECT * FROM states WHERE sname LIKE ‘North%’

29 Two forms of INSERT INSERT INTO customers VALUE (v1, v2,…vn) Where order of the values v’s is the order of the fields when the table was created INSERT INTO customers (fname, lname, zipcode) VALUES (‘Groucho’,’Marks’,10000)

30 QUIZ Take W3Schools SQL quiz http://www.w3schools.com/quiztest/quiztes t.asp?qtest=SQLhttp://www.w3schools.com/quiztest/quiztes t.asp?qtest=SQL Or put sql quiz into Google.

31 Classwork/Homework Keep working on projects Postings (and respond to postings) on databases and related technologies in the news.


Download ppt "Creating Databases for Web applications Making a table of table information. Reprise on database design. SQL. Classwork/Homework: Projects! Postings."

Similar presentations


Ads by Google