creating databases for web applications sql. xml. linked lists. nosql. homework: keep working on...

29
Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects.

Upload: reynold-eaton

Post on 19-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Creating Databases for Web applications

SQL. XML. Linked Lists. NoSQL.Homework: Keep working on projects. Post constructive feedback on other

projects.

Page 2: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

SQL UNION

• Assume a database with table for full-time faculty and another table for part-time faculty. – Reasons not to do this: some overlap of

fields.– Reasons to do this: most processing involves

just one or the other. Why have optional fields? Why waste space?

• SQL UNION can be used when application calls for information from both tables.

Page 3: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

SELECT UNION• SELECT column_name(s) FROM

table_name1UNION SELECT column_name(s) FROM table_name2

• SELECT fname, start_date FROM faculty WHERE start_date < 2005 UNION adjname, start_date FROM adjuncts WHERE start_date < 2005– NOTE: the start_date expression may be

wrong…

Page 4: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

SELECT …. BETWEEN

• Can combine two tests using the BETWEEN operator

• SELECT department, COUNT(*) as c FROM students GROUP BY department HAVING c BETWEEN 5 AND 10

• SELECT department, COUNT(*) as c FROM students GROUP BY department HAVING c >= 5 AND c<=10.

Page 5: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

SELECT … BETWEEN

• Assume table with fields that hold dates.

• SELECT * FROM orders WHERE orderdate BETWEEN '7/20/08' AND '8/05/08'; – Need to research date and date/time

'arithmetic'

Page 6: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Classwork

Consider customers (cid, cname, zipcode), products (pid, pname, price), orders (oid,cid,date), orderedproducts (opid, oid, pid, quantity) example.

• Generate list of zipcodes with total number of orders.

• Generate list of product names, with total ordered for each zipcode

• Generate list of customers who had orders made in the summer of 2013.

• ?

Page 7: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Hints• Decide on tables needed

– Some fields in some tables, maybe even all the fields in some tables, may not be part of the result. They are needed to make up the JOINed table to obtain the results.

– Generally, how tables are JOINed, the ON conditions is fixed• Decide on WHERE conditions . These are conditions on

individual records.• Decide if any GROUP operations: any aggregations

– NOTE: DISTINCT also does an aggregation

• Decide if any HAVING conditions: conditions on aggregated results

• Decide on any functions, e.g., COUNT(*)

Page 8: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

XML• Template for a structured form of data, namely tree:

– nodes and child nodes– attributes– text content

• Note: some call this semi or partially structured– some elements may NOT have all attributes or child nodes

• Developers create and agree on structure• Intended for use by different applications• JavaScript, php, other languages have built-in methods.

• Human readable• Not particularly compact, nor speedy in terms of processing.

– A [long] text field is read in and interpreted.• Generally, a copy of the XML file is generated and sent to be used

by an application.

Page 9: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

XML example<book isbn="978-1-4302-4032-7"><type value="nonfiction">

<subtype value="computing"/><level value="professional"/>

</type><title>HTML5 and JavaScript Projects</title><publisher>

<pname>Apress</pname><website>www.apress.com</website>

</publisher></book>

I made all this up. There probably exist more than one XML schema for books.

Page 10: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

More…

• Many technologies/standards around XML, including ways to specify definitions, do queries, etc.

• Posting opportunity….

Page 11: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Question?

• What are uses for XML?

Page 12: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Class work

• Define XML for corporate 'family' tree– a company can have subsidiaries– decide on the information

• in attributes• in element contents• in child elements

Page 13: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Linked lists Example

[You may have worked on this in Data Structures.]

Multiple ways. This is one way.

•Information is parent and children

•Data contained in one big array of arrays

•Each component array is

[name, index first child, index next sibling]

•Use -1 to indicate no child or no next sibling.

Page 14: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

My family• ignore in-laws….• one parent for each child0 Esther 1 -11 Anne 3 22 Jeanine 4 -13 David -1 54 Daniel 7 65 Tom 9 86 Aviva -1 -17 Annika -1 -1

• Note: other orders would be valid

Page 15: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Your family

• Leave out a parent and in-laws

• OR come up with alternative– One possibility is to include all half/step

siblings or– Another possibility is to add fields for siblings

from the father versus siblings from the mother

– OR ???

Page 16: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Contrast with Relational Database

• Tables, records, fields with some fields indicating relationships, i.e., pointing to records using primary key as the foreign key

• Commercial products and open source products (MySQL) store data efficiently, perform operations quickly, provide many functions.

• DBMS also holds the data itself. One copy of information is maintained and accessed by the different applications.

Page 17: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Relational Database family

• Two tables

• People and relationships

• Relationships have fields for two people PLUS label for relationship.

• Allow multiple relationships.

Page 18: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

My family: people table• Esther• Anne• Jeanine• Daniel• Aviva• David• Tommy• Debbie• Annika• Liam• Grant• Allison• …

Page 19: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

My family: relationships

1. r_id id for Esther id for Jeanine mother

2. r_id id for Esther id for Anne mother

3. ….

Page 20: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Your family

• Write out at least 5 family members and at least 5 relationships…

Page 21: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

NoSQL

• Alternatives to SQL

• MongoDB

• http://www.mongodb.org/display/DOCS/SQL+to+Mongo+Mapping+Chart

Page 22: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Key-value pairs

• The data is a set of key-value pairs– The input values passed from a form to a php

file– Associative array

• Options to set up map using Google Maps API

• They may or may not be unique, that is, a specific key may have more than one value

Page 23: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Parallel structures

• Recall simple states capitals quiz

• May have more than 2 arrays of corresponding data

Page 24: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

So….• You may come into a situation with this question

already resolved. Data exists in some form already OR your teacher says use this…

OR you need to decide• Parallel structures? Key-value? Does the tree

structure suit the data? Or would tables be better?• Is the place of the data important or sending copies

around okay, even appropriate– read-only most of the time?

• ?

Page 25: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

XML in a database

• A field in the table is suitable for XML data

• Can use a long text datatype called BLOB

• MySql stores and returns the text.

Page 26: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

MySQL XML functions These use a special language called XPATH to indicate

how to interpret the XML. • EXTRACTVALUE

– SELECT EXTRACTVALUE(fieldname,XPATH expression) FROM tablename

• SELECT ExtractValue(xml_text,'/person[@id="3"]/firstname') as fname FROM my_table WHERE row_id=10

– The resultset reflects the results!

• UPDATEXML– UPDATEXML(fieldname,XPATH expression indicating

replacement in places where there are matches, replacement text)

• UPDATE my_table SET xml_text = UpdateXML(xml_text,'//age','<age>30</age>') WHERE row_id=10

• These can be used in direct MySQL coding including coding to be saved as STORED PROCEDURE.

Page 27: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

SQL injection

• Technique by which malicious user enters input that can change result of SQL statement.

• Say a piece of text, dtext, is taken to be the id field for all records to be deleted from the database. The assumed usage is that it is just one id.

– What if dtext was 50 thenDROP FROM customers WHERE id=dtext would drop record with id 50

– What if dtext was 0 or true thenDROP FROM customers where id=50 or TRUE would drop everything

• There are techniques to prevent these (such as input=number) but they can be more complicated.

Page 28: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Extra credit opportunities

NOT just a link. Read and report on what you learn.• expressions involving DATE, TIMESTAMP, etc.• stored procedures in general• MySql standalone coding• XML in MySQL• NoSQL or MongoDB• Hadoop• SQL injection

• Ajax• Cloud computing• ???

Page 29: Creating Databases for Web applications SQL. XML. Linked Lists. NoSQL. Homework: Keep working on projects. Post constructive feedback on other projects

Homework

• Keep working on final projects.

• Post on topics mentioned (SQL injection, NoSQL, etc.)

• Post constructive feedback on other projects to their posting on Proposals forum.