Download presentation
Presentation is loading. Please wait.
Published byMonica Lauren Fisher Modified over 9 years ago
1
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014
2
Announcements PA #1 = DUE Monday 4/14. 11PM PST! Questions? Post on forum! I’ll answer there.
3
Review 3 tier architecture – Database – Logic – Presentation
4
Disclaimer There are a lot of other database classes (240, 340, etc) and this is a deep topic with a lot of expertise. Here is my “programmer’s guide to databases”, basically everything you need to know until you work on the database itself.
5
2 Types of Databases Relational Database Non-relational databases (NoSQL)
6
Relational Database Recognize relationships between stored items For example: – Teacher Table has a teacher ID – Class Table has a teacher ID saying who’s teaching that class, this ID should be a foreign key relationship to the teacher ID in the teacher table – This is a relationship MySQL, Oracle database, Microsoft SQL Server
7
Non-relational Database No constraints, relationship, it’s a simpler model Think of it as a Key Value Pair Much easier to scale! Relatively new (2000’s) MongoDB, Cassandra, Redis Read more here: http://www.mongodb.com/learn/nosql
8
Let’s learn MySQL Open WAMP Go to PHPMyAdmin Username = root Password = (none) Backup: http://www.compileonline.com/execute_sql_online.phphttp://www.compileonline.com/execute_sql_online.php
9
CREATE TABLE Create table Use INNODB engine
10
INSERT row INSERT INTO VALUES ()
11
SELECT Retrieve data from table SELECT * FROM Students; // Returns all 4 students
12
WHERE Add a condition to SELECT statement SELECT * FROM Students WHERE age = 31; // returns Joe Chen
13
AND/OR WHERE clauses allow boolean operators SELECT * FROM Students WHERE age = 31 OR name='Amy Lin'; // returns Joe Chen & Amy Lin
14
DELETE Delete row from table DELETE FROM Students WHERE age=31; // Go to browse tab, Joe is deleted, add him back
15
How do I change a row?
16
Order by Sort results from SELECT SELECT * FROM Students ORDER BY name;
17
SELECT expressions Count – SELECT count(*) from Students; Sum – SELECT sum(age) from Students; Avg – SELECT avg(age) from Students;
18
Group By Group based on columns SELECT age, count(*) FROM Students GROUP BY age;
19
LIKE String matching allows fuzzy matching SELECT * FROM Students WHERE name like '%Chen';
20
JOINS Joins two or more tables via a relationship Create a table for Clubs {name, studentId} Basketball = Kenneth/Patrick Kayak = Patrick/Amy How do I get name of each student in the club? Ex: get all names of students in basketball club
21
Others Other useful SQL commands I left out – Alter table – Drop table – Update
22
Best Practices
23
CREATE TABLE sql file Keep a text file with all your create table queries Do NOT create tables manually It’s common to need to setup multiple machines (test environment, production environment, etc) Just run entire file => entire database is setup
24
Import Data Do not write code to do this This is a solved problem Your code is probably going to be buggy Use the functionality in phpMyAdmin/other database management tools
25
Index/Keys Always define a primary key If you’ll search by that column, use INDEX If that column is unique and you’ll search by that column, use UNIQUE INDEX HUGE for performance If you don’t have this, anything over 1000 rows will be super slow O(n) vs. O(1)
26
Use INNODB engine Other MySQL engines have strange behavior Just use ENGINE=INNODB
27
Work Load Whenever possible… Off load work to your compute instance Instead of SELECT SUBSTRING(…), do the substring command in EC2 Instead of sorting in the database, sort n the compute instance EC2 = easier to scale. No data replication problems
28
MySQL Create: – Teacher {id, name, class} – Class {id, name, teacherId, studentId} How do I get all students of a particular class? How do I get the name and #students each teacher is teaching?
29
PDO Use PDO to connect to MySQL Use the ->prepare() to specify parameters http://net.tutsplus.com/tutorials/php/php- database-access-are-you-doing-it-correctly/ http://net.tutsplus.com/tutorials/php/php- database-access-are-you-doing-it-correctly/
30
Resources http://dev.mysql.com/doc/refman/5.0/en/
31
Questions?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.