SQL 101 for Web Developers 14 November 2012. What is a database and why have one? Tables, relationships, normalization SQL – What SQL is and isn’t – CRUD:

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

CSE 190: Internet E-Commerce Lecture 10: Data Tier.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
CSC 2720 Building Web Applications Database and SQL.
1ISM - © 2010 Houman Younessi Lecture 3 Convener: Houman Younessi Information Systems Spring 2011.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Introduction to SQL Steve Perry
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Web Services Week 8 Aims: –Using web services as front ends to databases Objectives: –Review of relational databases –Connecting to and querying databases.
Concepts of Database Management Seventh Edition Chapter 4 Keys and Relationship.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
SQL pepper. Why SQL File I/O is a great deal of code Optimal file organization and indexing is critical and a great deal of code and theory implementation.
Database: SQL and MySQL
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
Database Fred Durao What is a database? A database is any organized collection of data. Some examples of databases you may encounter in.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 What is database 2? What is normalization? What is SQL? What is transaction?
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
CS 101 – Access notes Databases (Microsoft Access) 4 parts of a database database design –Try to understand the ideas behind database design, not just.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
SQL Basic. What is SQL? SQL (pronounced "ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Module Review Basic SQL commands: Create Database, Create Table, Insert and Select 2. Connect an SQL Database to PHP 3. Execute SQL Commands in.
CS453: Databases and State in Web Applications (Part 2) Prof. Tom Horton.
CHAPTER 10 PHP MySQL Database
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
Concepts of Database Management Seventh Edition Chapter 4 Keys and Relationship.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
2006­02-08 | Database Normalization | © MySQL AB 2006 | 1 An Introduction to Database Normalization Mike Hillyer – MySQL AB.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
 MySQL  DDL ◦ Create ◦ Alter  DML ◦ Insert ◦ Select ◦ Update ◦ Delete  DDL(again) ◦ Drop ◦ Truncate.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Chapter 12 Introducing Databases. Objectives What a database is and which databases are typically used with ASP.NET pages What SQL is, how it looks, and.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 5 Introduction to SQL.
Rob Gleasure robgleasure.com
CS320 Web and Internet Programming SQL and MySQL
 2012 Pearson Education, Inc. All rights reserved.
Principles of Software Development
ISC440: Web Programming 2 Server-side Scripting PHP 3
Structured Query Language (SQL) William Klingelsmith
LINQ to DATABASE-2.
Database systems Lecture 3 – SQL + CRUD
Rob Gleasure robgleasure.com
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
CS3220 Web and Internet Programming SQL and MySQL
Rob Gleasure robgleasure.com
PHP and MySQL.
Presentation transcript:

SQL 101 for Web Developers 14 November 2012

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

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

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 Rees ENG 101Spring Beth Hardy EEB 102Fall Tom Rees EEB 102Fall

Student records – one table namephonecoursetermgrade Tom Rees ENG 101Spring Beth Hardy EEB 102Fall Tom Rees EEB 102Fall fnamelnamephonecourse_ prefix course_ num termyeargrade TomRees ENG101Spring BethHardy EEB102Fall TomRees EEB102Fall

Normalized Data

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 address) – one-to-many (one student – many enrollment records) – many-to-many (many students to many sections)

Table relationships Many to many: books and authors Author table: authorID name Book table: bookID, title, ISBN Book_Author table: Book_AuthorIDbookIDauthorID

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

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

SELECT * FROM student_records; (will get entire contents of table) SELECT fname FROM student_records; | fname | | Tom | | Beth | | Tom | fnamelnamephonecourse_ prefix course_ num termyeargrade TomRees ENG101Spring BethHardy EEB102Fall TomRees EEB102Fall

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 TomRees ENG101Spring BethHardy EEB102Fall TomRees EEB102Fall

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');

SELECT fname, lname FROM student_records ORDER BY lname; | fname | lname | | Tom | Rees | | Beth | Hardy | SELECT DISTINCT lname FROM student_records; | lname | | Rees | | Hardy |

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?

Joins

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

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.

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 | | | Jon | Stephens | | 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.

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 | | | Jon | Stephens | | | 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.

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.

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.)

What didn’t we cover? Database optimization and performance Security issues like sql injection Data integrity and constraints Data types Views Triggers Transactions PDOs

What else didn’t we cover? Logic Math expressions String manipulation Parameterized queries Database design Different database technologies

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.

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.

I like “Learn SQL the hard way” (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.