INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014
Announcements PA #1 = DUE Monday 4/14. 11PM PST! Questions? Post on forum! I’ll answer there.
Review 3 tier architecture – Database – Logic – Presentation
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.
2 Types of Databases Relational Database Non-relational databases (NoSQL)
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
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:
Let’s learn MySQL Open WAMP Go to PHPMyAdmin Username = root Password = (none) Backup:
CREATE TABLE Create table Use INNODB engine
INSERT row INSERT INTO VALUES ()
SELECT Retrieve data from table SELECT * FROM Students; // Returns all 4 students
WHERE Add a condition to SELECT statement SELECT * FROM Students WHERE age = 31; // returns Joe Chen
AND/OR WHERE clauses allow boolean operators SELECT * FROM Students WHERE age = 31 OR name='Amy Lin'; // returns Joe Chen & Amy Lin
DELETE Delete row from table DELETE FROM Students WHERE age=31; // Go to browse tab, Joe is deleted, add him back
How do I change a row?
Order by Sort results from SELECT SELECT * FROM Students ORDER BY name;
SELECT expressions Count – SELECT count(*) from Students; Sum – SELECT sum(age) from Students; Avg – SELECT avg(age) from Students;
Group By Group based on columns SELECT age, count(*) FROM Students GROUP BY age;
LIKE String matching allows fuzzy matching SELECT * FROM Students WHERE name like '%Chen';
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
Others Other useful SQL commands I left out – Alter table – Drop table – Update
Best Practices
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
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
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)
Use INNODB engine Other MySQL engines have strange behavior Just use ENGINE=INNODB
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
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?
PDO Use PDO to connect to MySQL Use the ->prepare() to specify parameters database-access-are-you-doing-it-correctly/ database-access-are-you-doing-it-correctly/
Resources
Questions?