Download presentation
Presentation is loading. Please wait.
Published byMoris Skinner Modified over 9 years ago
1
SQL 101 for Web Developers 14 November 2012
2
What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD: four basic operations – Select queries step by step – Using JOINs in SELECT queries Next steps and resources
3
What is a database and why? A database can be as simple as a single, excel- like table For your website, a database brings you the power of dynamic data Different technologies are used (e.g., MySQL, SQL Server, Access, Oracle, Sybase, DB2, SQLite) but most website-connected dbs are relational databases
4
Student records – one table A database is a whole spreadsheet file. A table is a tab/sheet in the spreadsheet, with each one being given a name. A column is a column in both. A row is a row in both. namephonecoursetermgrade Tom Rees111-2222ENG 101Spring 20093.5 Beth Hardy333-4444EEB 102Fall 20093.0 Tom Rees111-2222EEB 102Fall 20103.5
5
Student records – one table namephonecoursetermgrade Tom Rees111-2222ENG 101Spring 20093.5 Beth Hardy333-4444EEB 102Fall 20093.0 Tom Rees111-2222EEB 102Fall 20103.5 fnamelnamephonecourse_ prefix course_ num termyeargrade TomRees111-2222ENG101Spring20093.5 BethHardy333-4444EEB102Fall20093.0 TomRees111-2222EEB102Fall20103.5
6
Normalized Data
8
Table relationships Primary keys – unique identifier for each record in one table Primary keys – also used to build relationships among tables – one-to-one (studentID to email address) – one-to-many (one student – many enrollment records) – many-to-many (many students to many sections)
9
Table relationships Many to many: books and authors Author table: authorID name Book table: bookID, title, ISBN Book_Author table: Book_AuthorIDbookIDauthorID 136 239 347 459
10
SQL statements SQL is a standard language for accessing databases. It is not a programming language or a scripting language. Four basic types of queries: “CRUD” stands for – create – read – update – delete
11
SQL statements Four basic types: “CRUD” stands for – (create) CREATE (table) INSERT INTO (table) VALUES – (read) SELECT column_names FROM table_name – (update) UPDATE – (delete) DELETE rows, DROP tables
12
SELECT * FROM student_records; (will get entire contents of table) SELECT fname FROM student_records; +---------+ | fname | +---------+ | Tom | | Beth | | Tom | +---------+ fnamelnamephonecourse_ prefix course_ num termyeargrade TomRees111-2222ENG101Spring20093.5 BethHardy333-4444EEB102Fall20093.0 TomRees111-2222EEB102Fall20103.5
13
SELECT grade FROM student_records WHERE lname = 'Rees'; +---------+ | grade | +---------+ | 3.5 | +---------+ The WHERE statement lets you filter records Lots of operators in addition to = fnamelnamephonecourse_ prefix course_ num termyeargrade TomRees111-2222ENG101Spring20093.5 BethHardy333-4444EEB102Fall20093.0 TomRees111-2222EEB102Fall20103.5
14
SELECT * FROM student_records WHERE lname = 'Rees' AND term = 'Fall'; SELECT * FROM student_records WHERE lname = 'Rees' AND (term = 'Fall' OR term = 'Spring'); SELECT * FROM student_records WHERE lname LIKE 'R%'; SELECT * FROM student_records WHERE lname IN ('Rees', 'Hardy');
15
SELECT fname, lname FROM student_records ORDER BY lname; +---------+---------+ | fname | lname | +---------+---------+ | Tom | Rees | | Beth | Hardy | +---------+---------+ SELECT DISTINCT lname FROM student_records; +---------+ | lname | +---------+ | Rees | | Hardy | +---------+
16
Why you must be familiar with your data: When you get results back from a query, how do you know you got the right results?
17
Joins
18
JOIN (Inner Join): Return rows when there is at least one match in both tables LEFT (Outer) JOIN: Return all rows from the left table, even if there are no matches in the right table RIGHT (Outer) JOIN: Return all rows from the right table, even if there are no matches in the left table FULL (Outer) JOIN: Return rows when there is a match in one of the tables
19
Joins The syntax used determines which table will be fully represented. A row of NULL values is substituted when a matching row is not present.
20
table “Author” contains names of authors, with the primary key “Author_ID” table “Book_Author” contains ISBN numbers, keyed with the primary key “Book_Author_ID” and linked to the Author table by the foreign key “Author_ID.” SELECT First_Name, Last_Name, ISBN FROM Author INNER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; +------------+-----------+------------+ | First_Name | Last_Name | ISBN | +------------+-----------+------------+ | Chad | Russell | 1590593324 | | Jon | Stephens | 1590593324 | +------------+-----------+------------+ This INNER JOIN only gets rows where there is a match. If an author doesn’t have a book, and a book doesn’t have an author, neither type of record shows up.
21
When we need at least one row in the result set for every row in a given table, regardless of matching rows, we use an OUTER JOIN query. SELECT First_Name, Last_Name, ISBN FROM Author LEFT OUTER JOIN Book_Author ON Author.Author_ID = Book_Author.Author_ID; +------------+-----------+------------+ | First_Name | Last_Name | ISBN | +------------+-----------+------------+ | Chad | Russell | 1590593324 | | Jon | Stephens | 1590593324 | | Mike | Hillyer | NULL | +------------+-----------+------------+ This query will NOT return rows for books that don’t have an author – just authors that don’t have a book.
23
SELECT s.first_name, s.last_name, c.course_prefix, c.course_number FROM students s, courses c, enrollment e, sections sc WHERE s.student_id = e.student_id AND e.section_id = sc.section_id AND c.course_id = sc.course_id; Although not explicitly named as such, this is an INNER JOIN.
24
How do you grab the data output from a query? Depends on your database type and your programming or scripting language (for example, you can use PHP functions to read the result set one row at a time and put the results into an array.)
25
What didn’t we cover? Database optimization and performance Security issues like sql injection Data integrity and constraints Data types Views Triggers Transactions PDOs
26
What else didn’t we cover? Logic Math expressions String manipulation Parameterized queries Database design Different database technologies
27
Why do I need to know any SQL when my CMS does it all for me? It helps to know your data Sometimes the fastest way to fix a problem is with a straight database query You can check up on the queries written by your CMS You can’t always use a CMS – sometimes you need to write a custom web app. You might need to migrate data from your department’s Access database into another db.
28
What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD: four basic operations – Select queries step by step – Using JOINs in SELECT queries Next steps and resources: depends on the technologies you are using. There are a lot of good online tutorials, and of course books.
29
I like “Learn SQL the hard way” http://sql.learncodethehardway.org/book/ (coaches you through creating a SQLite db on your local computer, entering data into it, and then running queries on it) Why is it “the hard way”? Enforces precision, attention to detail, and persistence by requiring you to type each exercise (no copy-paste!) and make it run.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.