+ All Categories
Home > Documents > Database Design and MySQL

Database Design and MySQL

Date post: 12-Feb-2016
Category:
Upload: yuma
View: 30 times
Download: 0 times
Share this document with a friend
Description:
Database Design and MySQL. Session 4 INFM 718N Web-Enabled Databases. Agenda. Database design MySQL Project teams: next steps (if we have time) Programming. (PC). Interface Design. (IE, Firefox). Client-side Programming. (JavaScript). Interaction Design. Interchange Language. - PowerPoint PPT Presentation
55
Database Design and MySQL Session 4 INFM 718N Web-Enabled Databases
Transcript
Page 1: Database Design and MySQL

Database Design and MySQL

Session 4INFM 718N

Web-Enabled Databases

Page 2: Database Design and MySQL

Agenda

• Database design

• MySQL

• Project teams: next steps

• (if we have time) Programming

Page 3: Database Design and MySQL

Database

Server-side Programming

Interchange Language

Client-side Programming

Web Browser

Client Hardware

Server Hardware (PC, Unix)

(MySQL)

(PHP)

(HTML, XML)

(JavaScript)

(IE, Firefox)

(PC)

Bus

ines

sru

les

Inte

ract

ion

Des

ign

Inte

rfac

eD

esig

n

• Relational normalization• Structured programming• Software patterns• Object-oriented design• Functional decomposition

Page 4: Database Design and MySQL

Databases• Database

– Collection of data, organized to support access– Models some aspects of reality

• DataBase Management System (DBMS)– Software to create and access databases

• Relational Algebra– Mathematical theory that supports optimization

Page 5: Database Design and MySQL

Database “Programming”• Structured Query Language (SQL)

– Consistent, unambiguous interface to any DBMS– Simple command structure:

• e.g., SELECT last-name FROM students WHERE dept=“CLIS”– Useful standard for inter-process communications

• Visual programming (e.g., Microsoft Access)– Unambiguous, and easier to learn than SQL

• Natural language (e.g., interactive voice response system)– Improves ease of use, but with potential for ambiguity and error

• e.g., Show me the last names of students in CLIS

Page 6: Database Design and MySQL

Getting Started

• What questions must you answer?• What data is needed to generate the answers?

– Entities• Attributes of those entities

– Relationships• Nature of those relationships

• How will the user interact with the system?– Relating the question to the available data– Expressing the answer in a useful form

Page 7: Database Design and MySQL

An E-R Example

student team

implement-role

member-of

project

creates

manage-role

php-project ajax-project

d

1

1

M

M

1

1

human

client needs1 M

Page 8: Database Design and MySQL

E-R Diagrams• Entities

– Types • Subtypes (disjoint / overlapping), aggregation

– Attributes• Mandatory / optional

– Identifier• Relationships

– Cardinality– Existence– Degree

Page 9: Database Design and MySQL

Making Tables from E-R Diagrams

• Pick a primary key for each entity• Build the tables

– One per entity– Plus one per M:M relationship– Choose terse but memorable table and field names

• Check for parsimonious representation– Relational “normalization”– Redundant storage of computable values

• Implement using a DBMS

Page 10: Database Design and MySQL

Table-Oriented Lingo• Field An “atomic” unit of data

– number, string, true/false, …

• Record A collection of related fields

• Table A collection of related records– Each record is one row in the table– Each field is one column in the table

• Primary Key The field that identifies a record– Values of a primary key must be unique

• Database A collection of tables

Page 11: Database Design and MySQL

Relational Lingo• Tables represent “relations”

– Course, course description– Name, email address, department

• Named fields represent “attributes”

• Each row in the table is called a “tuple”– The order of the rows is not important

• Queries specify desired conditions– The DBMS then finds data that satisfies them

Page 12: Database Design and MySQL

Visualizing Tables

primary key

Page 13: Database Design and MySQL

Key Lingo

• “Primary Key” uniquely identifies a record– e.g. student ID in the student table

• “Compound” primary key– Synthesize a primary key with a combination of fields– e.g., Student ID + Course ID in the enrollment table

• “Foreign Key” is primary key in the other table– Note: it need not be unique in this table

Page 14: Database Design and MySQL

Goals of “Normalization”• Save space

– Save each fact only once

• More rapid updates– Every fact only needs to be updated once

• More rapid search– Finding something once is good enough

• Avoid inconsistency– Changing data once changes it everywhere

Page 15: Database Design and MySQL

Normalization• 1NF: Single-valued indivisible (atomic) attributes

– Split “Doug Oard” to two attributes as (“Doug”, “Oard”)– Model M:M implement-role relationship with a table

• 2NF: Attributes depend on complete primary key– (id, impl-role, name)->(id, name)+(id, impl-role)

• 3NF: Attributes depend directly on primary key– (id, addr, city, state, zip)->(id, addr, zip)+(zip, city, state)

• 4NF: Divide independent M:M tables– (id, role, courses) -> (id, role) + (id, courses)

• 5NF: Don’t enumerate derivable combinations

Page 16: Database Design and MySQL

Normalized Table Structure

• Persons: id, fname, lname, userid, password• Contacts: id, ctype, cstring• Ctlabels: ctype, string• Students: id, team, mrole• Iroles: id, irole• Rlabels: role, string• Projects: team, client, pstring

Page 17: Database Design and MySQL

Referential Integrity

• “Foreign key” values must exist in another table– If not, those records cannot be joined

• Checked when data added to this table– MySQL “Error 150”

• Triggers when data deleted/changed in other table– Specify SET NULL, RESTRICT or CASCADE

Page 18: Database Design and MySQL

Getting started with MySQL

• “root” creates database, grants permissions– By you on WAMP (mysql –u root –p)– By Charles Goldman on OTAL– CREATE DATABASE team1;– GRANT SELECT, INSERT, UPDATE, DELETE, INDEX, ALTER, CREATE, DROP ON team1.* TO

‘foo’@’localhost’ IDENTIFIED BY ‘bar’;– FLUSH PRIVILEGES;

• Start mysql– Start->Run->cmd for WAMP, ssh for OTAL– mysql –u foo –p bar [you can cd to your playspace first, but you don’t need to]

• Connect to your database– USE team1;

Page 19: Database Design and MySQL

Some Useful MySQL Commands• Looking around

– SHOW DATABASES;– SHOW TABLES;– DESCRIBE tablename;– SELECT * FROM tablename;

• Optimization– SHOW TABLE STATUS \G;

• OPTIMIZE TABLE tablename;– EXPLAIN <SQLquery>;

• ALTER TABLE tablename ADD INDEX fieldname;

Page 20: Database Design and MySQL

Creating TablesCREATE TABLE contacts ( ckey MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, id MEDIUMINT UNSIGNED NOT NULL, ctype SMALLINT UNSIGNED NOT NULL, cstring VARCHAR(40) NOT NULL, FOREIGN KEY (id) REFERENCES persons(id) ON DELETE CASCADE, FOREIGN KEY (ctype) REFERENCES ctlabels(ctype) ON DELETE RESTRICT, PRIMARY KEY (ckey)) ENGINE=INNODB;

To delete: DROP TABLE contacts;

Page 21: Database Design and MySQL

Populating TablesINSERT INTO ctlabels (string) VALUES ('primary email'), ('alternate email'), ('home phone'), ('cell phone'), ('work phone'), ('AOL IM'), ('Yahoo Chat'), ('MSN Messenger'), (‘other’);

To empty a table: DELETE FROM ctlabels;

Page 22: Database Design and MySQL

The SQL SELECT Command

• SELECT (“projection”) chooses columns– Based on their label

• WHERE (“restriction”) chooses rows– Based on their contents

• e.g. department ID = “HIST”

• These can be specified together– SELECT Student ID, Dept WHERE Dept = “History”

Page 23: Database Design and MySQL

WHERE Clause

• Each SELECT contains a single WHERE

• Numeric comparison <, >, =, <>, …

• e.g., grade<80

• Boolean operations – e.g., Name = “John” AND Dept <> “HIST”

Page 24: Database Design and MySQL

A Denormalized “Flat File”

Student ID Last Name First Name Department IDDepartmentCourse ID Course description Grades email1 Arrows John EE EE lbsc690 Information Technology 90 jarrows@wam1 Arrows John EE Elec Engin ee750 Communication 95 ja_2002@yahoo2 Peters Kathy HIST HIST lbsc690 Informatino Technology 95 kpeters2@wam2 Peters Kathy HIST history hist405 American History 80 kpeters2@wma3 Smith Chris HIST history hist405 American History 90 smith2002@glue4 Smith John CLIS Info Sci lbsc690 Information Technology 98 js03@wam

Page 25: Database Design and MySQL

A Normalized Relational Database

Department ID DepartmentEE Electronic EngineeringHIST HistoryCLIS Information Stuides

Course ID Course Descriptionlbsc690 Information Technologyee750 Communicationhist405 American History

Student ID Course ID Grades1 lbsc690 901 ee750 952 lbsc690 952 hist405 803 hist405 904 lbsc690 98

Student ID Last Name First Name Department ID email1 Arrows John EE jarrows@wam2 Peters Kathy HIST kpeters2@wam3 Smith Chris HIST smith2002@glue4 Smith John CLIS js03@wam

Student Table

Department Table Course Table

Enrollment Table

Page 26: Database Design and MySQL

Example of Join

Student ID Last Name First Name Department ID email1 Arrows John EE jarrows@wam2 Peters Kathy HIST kpeters2@wam3 Smith Chris HIST smith2002@glue4 Smith John CLIS js03@wam

Student TableDepartment ID DepartmentEE Electronic EngineeringHIST HistoryCLIS Information Stuides

Department Table

Student ID Last Name First Name Department IDDepartment email1 Arrows John EE Electronic Engineering jarrows@wam2 Peters Kathy HIST History kpeters2@wam3 Smith Chris HIST History smith2002@glue4 Smith John CLIS Information Stuides js03@wam

“Joined” Table

Page 27: Database Design and MySQL

Project

Student ID Last Name First Name Department IDDepartment email1 Arrows John EE Electronic Engineering jarrows@wam2 Peters Kathy HIST History kpeters2@wam3 Smith Chris HIST History smith2002@glue4 Smith John CLIS Information Stuides js03@wam

New Table

Student ID Department1 Electronic Engineering2 History3 History4 Information Stuides

SELECT Student ID, Department

Page 28: Database Design and MySQL

Restrict

Student ID Last Name First Name Department IDDepartment email2 Peters Kathy HIST History kpeters2@wam3 Smith Chris HIST History smith2002@glue

Student ID Last Name First Name Department IDDepartment email1 Arrows John EE Electronic Engineering jarrows@wam2 Peters Kathy HIST History kpeters2@wam3 Smith Chris HIST History smith2002@glue4 Smith John CLIS Information Stuides js03@wam

New Table

WHERE Department ID = “HIST”

Page 29: Database Design and MySQL

What are Requirements?

• Attributes– Appearance– Concepts (represented by data)

• Behavior– What it does– How you control it– How you observe the results

Page 30: Database Design and MySQL

Who Sets the Requirements?

• People who need the task done (customers)

• People that will operate the system (users)

• People who use the system’s outputs

• People who provide the system’s inputs

• Whoever pays for it (requirements commissioner)

Page 31: Database Design and MySQL

The Requirements Interview• Focus the discussion on the task

– Look for entities that are mentioned• Discuss the system’s most important effects

– Displays, reports, data storage– Learn where the system’s inputs come from– People, stored data, devices, …

• Note any data that is mentioned– Try to understand the structure of the data

• Shoot for the big picture, not every detail

Page 32: Database Design and MySQL

First Things First

• Functionality

• Content

• Usability

• Security/Stability

Page 33: Database Design and MySQL

Language Learning

• Learn some words

• Put those words together in simple ways

• Examine to broaden your understanding

• Create to deepen your mastery

• Repeat until fluent

Page 34: Database Design and MySQL

Thinking About PHP

• Local vs. Web-server-based display

• HTML as an indirect display mechanism

• “View Source” for debugging

• Procedural perspective (vs. object-oriented)

Page 35: Database Design and MySQL

Arrays in PHP• A set of key-element pairs

$days = array(“Jan”->31, “Feb”=>28, …);$months = explode(“/”, “Jan/Feb/Mar/…/Dec”);$_POST

• Each element is accessed by the key– {$days[“Jan”]}– $months[0];

• Arrays and loops work naturally together

Page 36: Database Design and MySQL

Thinking about Arrays

• Naturally encodes an order among elements– $days = rksort($days);

• Natural data structure to use with a loop– Do the same thing to different data

• PHP unifies arrays and hashtables– Elements may be different types

Page 37: Database Design and MySQL

Functions in PHP

• Declarationfunction multiply($a, $b=3){return $a*$b;}

• Invoking a method$b = multiply($b, 7);

• All variables in a function have only local scope• Unless declared as global in the function

Page 38: Database Design and MySQL

Why Modularity?

• Limit complexity– Extent– Interaction– Abstraction

• Minimize duplication

Page 39: Database Design and MySQL

Using PHP with (X)HTML Forms<form action=“formResponseDemo.php”, method=“post”>

email: <input type=“text”, name=“email”, value=“<?php echo $email ?>”, size=30 /><input type=“radio”, name=“sure”, value=“yes” /> Yes<input type=“radio”, name=“sure”, value=“no” /> No<input type=“submit”, name=“submit”, value=“Submit” /><input type=“hidden”, name=“submitted”, value=“TRUE” />

</form>

if (isset($_POST[“submitted”])) {echo “Your email address is $email.”;

} else {echo “Error: page reached without proper form submission!”;

}

Page 40: Database Design and MySQL

Sources of Complexity

• Syntax– Learn to read past the syntax to see the ideas– Copy working examples to get the same effect

• Interaction of data and control structures– Structured programming

• Modularity

Page 41: Database Design and MySQL

Some Things to Pay Attention ToSyntax• How layout helps reading• How variables are named• How strings are used• How input is obtained• How output is created

Structured Programming• How things are nested• How arrays are used

Modular Programming• Functional decomposition• How functions are invoked• How arguments work• How scope is managed• How errors are handled• How results are passed

Page 42: Database Design and MySQL

Programming Skills Hierarchy

• Reusing code [run the book’s programs]

• Understanding patterns [read the book]

• Applying patterns [modify programs]

• Coding without patterns [programming]

• Recognizing new patterns

Page 43: Database Design and MySQL

Best Practices

• Design before you build

• Focus your learning

• Program defensively

• Limit complexity

• Debug syntax from the top down

Page 44: Database Design and MySQL

Rapid Prototyping + Waterfall

UpdateRequirements

ChooseFunctionality

BuildPrototype

InitialRequirements

WriteSpecification

CreateSoftware

WriteTest Plan

Page 45: Database Design and MySQL

Focus Your Learning

• Find examples that work– Tutorials, articles, examples

• Cut them down to focus on what you need– Easiest to learn with throwaway programs

• Once it works, include it in your program– If it fails, you have a working example to look at

Page 46: Database Design and MySQL

Defensive Programming

• Goal of software is to create desired output

• Programs transform input into output– Some inputs may yield undesired output

• Methods should enforce input assumptions– Guards against the user and the programmer!

• Everything should be done inside methods

Page 47: Database Design and MySQL

Limiting Complexity

• Single errors are usually easy to fix– So avoid introducing multiple errors

• Start with something that works– Start with an existing program if possible– If starting from scratch, start small

• Add one new feature– Preferably isolated in its own method

Page 48: Database Design and MySQL

Types of Errors• Syntax errors

– Detected at compile time

• Run time exceptions– Cause system-detected failures at run time

• Logic errors– Cause unanticipated behavior (detected by you!)

• Design errors– Fail to meet the need (detected by stakeholders)

Page 49: Database Design and MySQL

Debugging Syntax Errors• Focus on the first error message

– Fix one thing at a time

• The line number is where it was detected– It may have been caused much earlier

• Understand the cause of “warnings”– They may give a clue about later errors

• If all else fails, comment out large code regions– If it compiles, the error is in the commented part

Page 50: Database Design and MySQL

Run Time Exceptions

• Occur when you try to do the impossible– Use a null variable, divide by zero, …

• The cause is almost never where the error is– Why is the variable null?

• Exceptions often indicate a logic error– Find why it happened, not just a quick fix!

Page 51: Database Design and MySQL

Debugging Run-Time Exceptions• Run the program to get a stack trace

– Where was this function called from?

• Print variable values before the failure

• Reason backwards to find the cause– Why do they have these values?

• If necessary, print some values further back

Page 52: Database Design and MySQL

Logic Errors

• Evidenced by inappropriate behavior

• Can’t be automatically detected– “Inappropriate” is subjective

• Sometimes very hard to detect– Sometimes dependent on user behavior– Sometimes (apparently) random

• Cause can be hard to pin down

Page 53: Database Design and MySQL

Debugging Logic Errors• First, look where the bad data was created

• If that fails, print variables at key locations– if (DEBUG) echo “\$foobar = $foobar”;

• Examine output for unexpected patterns

• Once found, proceed as for run time errors– define (“DEBUG”, FALSE); to clean the output

Page 54: Database Design and MySQL

Three Big Ideas

• Functional decomposition– Outside-in design

• High-level languages– Structured programming, object-oriented design

• Patterns– Design patterns, standard algorithms, code reuse

Page 55: Database Design and MySQL

One-Minute Paper

What was the muddiest point in today’s class?

• Be brief!• No names!


Recommended