Standard Query Language (SQL)

Standard Query
Language (SQL)
Hamid Zarrabi-Zadeh
Web Programming – Fall 2013
2
Outline
• Introduction
• Local Storage Options
 Cookies
 Web Storage
• Standard Query Language (SQL)
 Database Commands
 Queries
• Summary
3
Introduction
• Any (web) application needs persistence
storage
• There are three general storage strategies:
 server-side storage
 client-side storage
 a hybrid strategy
4
Client-Side Storage
• Client-side data is stored locally within the user's
browser
• A web page can only access data stored by
itself
• For a long time, cookies were the only option to
store data locally
• HTML5 introduced several new web storage
options
5
Server-Side Storage
• Server-side data is usually stored within a file or a
database system
• For large data, database systems are preferable
over plain files
• Database Management Systems (DBMSs)
provide an efficient way to store and retrieve
data
Cookies
7
Cookies
• A cookie is a piece of information stored on a
user's browser
• Each time the browser requests a page, it also
sends the related cookies to the server
• The most common use of cookies is to identify a
particular user amongst a set of users
8
Cookies Structure
• Each cookie has:
•
a name
•
a value (a 4000 character string)
•
expiration date (optional)
•
path and domain (optional)
• if no expiration date is specified, the cookie is
considered as a session cookie
• Session cookies are deleted when the browser
session ends (the browser is closed by the user)
9
Set/Get Cookies
• In JavaScript, cookies can be accessed via the
document.cookie pseudo-variable
• Set cookie:
document.cookie = "name=value; expires=date;
path=/; domain=ce.sharif.edu"
• Read all cookies:
var pairs = document.cookie.split(';');
10
jQuery.Cookie
• A simple jQuery plugin for reading, writing and
deleting cookies
• Read cookie
 $.cookie(); // all cookies as a dictionary
 $.cookie('the_cookie');
• Set cookie
 $.cookie('the_cookie', 'the_value');
 $.cookie('the_cookie', 'the_value', {expires: 7, path: '/'});
• Remove cookie
 $.removeCookie('the_cookie');
Web Storage
12
Web Storage
• HTML5 introduces new local storages, which are
more secure and faster than cookies
 they are not included with every server request
 they can store large amounts of data efficiently
• Two new objects for storing data
 localStorage – stores data with no expiration date
 sessionStorage – stores data for one session
13
Web Storage Example
• Here is an example:
if (typeof (Storage) !== 'undefined') {
localStorage.student = 'Ali',
localStorage.grade = 19.5;
}
• Note: values are always stored as strings
• Use JSON.stringify() to convert objects to string
before storing, and use JSON.parse() to retrieve
14
Local Databases
• HTML5 has two standard interfaces for supporting
local databases:
 Web SQL Database
 Indexed Database
• Web SQL defines a rational database that can
be queried using SQL
• IndexedDB is a NoSQL database which uses
object stores rather than the typical rational
database implementation
Standard Query
Language (SQL)
16
What is SQL?
• SQL is a standard language for accessing and
manipulating rational databases
• SQL is an ANSI (American National Standards
Institute) standard, though there are slightly
different versions of the SQL language
• Examples of SQL database systems are
 Oracle
 MS SQL Server
 IBM DB 2
 MySQL, SQLight, …
17
Rational Databases
• Components:
 databases
 tables
 rows
 fields (corresponds to table columns)
• Operations
 create/drop databases or tables
 insert/update/delete table rows
 query/fetch a set of rows
18
SQL Commands
• Most database systems provide a command-line
tool for issue SQL commands or queries
• There are also some GUI tools built upon
command-line tools providing a more
comfortable user interface
• Most databases are configured to be caseinsensitive, especially on database commands
• Most command-line programs require a trailing
semicolon (;) to terminate a SQL statement
SELECT * from table_name;
19
Database Operations
• Creating a Database
CREATE DATABASE test;
GRANT ALL ON test.* to user(s);
• Using a Database
USE test;
• Dropping a Database
DROP DATABASE test;
20
Table Operations
• Creating a Table
CREATE TABLE users (name VARCHAR(30), userid INT,
groupid INT);
• Dropping a Table
DROP TABLE users;
• Cleaning a Table
TRUNCATE TABLE users;
21
Row Operations
• Inserting a Row
INSERT INTO users VALUES('Ali', 110, 1);
• Updating a Row
UPDATE users SET groupid=4 WHERE groupid=1;
• Deleting a Row
DELETE FROM users WHERE groupid=4;
SQL Queries
23
SQL Select
• The SELECT statement is used to select data from
a database
• The result is stored in a result table, called the
result-set
SELECT column_name,column_name
FROM table_name;
SELECT * FROM table_name;
SELECT DISTINCT column_name,column_name
FROM table_name;
24
Conditional Select
• The WHERE clause is used to extract only those
records that fulfill a specified criterion
SELECT column_name, column_name
FROM table_name
WHERE column_name operator value;
• Operators:
 =, <>, >, >=, <, <=, BETWEEN, LIKE, IN, IS, IS NOT
 AND, OR
SELECT name, userid
FROM users
Where userid > 10 AND groupid <= 4
25
Order Results
• The ORDER BY keyword is used to sort the resultset by one or more columns
SELECT column_name, column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
• The ORDER BY keyword sorts the records in
ascending order by default.
• To sort the records in a descending order, you
can use the DESC keyword.
26
SQL Functions
• SQL has many built-in functions for performing
calculations on data
• Sample aggregate function:
SELECT AVG(column_name) FROM table_name
• Sample scalar function:
SELECT UCASE(name), userid
FROM users;
• Combined:
SELECT UCASE(name) FROM users
WHERE grade > (SELECT AVG(grade) FROM users);
27
Aggregate Functions
• SQL aggregate functions return a single value,
calculated from values in a column
• Useful aggregate functions:
 AVG() - Returns the average value
 COUNT() - Returns the number of rows
 FIRST() - Returns the first value
 LAST() - Returns the last value
 MAX() - Returns the largest value
 MIN() - Returns the smallest value
 SUM() - Returns the sum
28
Scalar Functions
• SQL scalar functions return a single value, based
on the input value.
• Useful scalar functions:
 UCASE() - Converts a field to upper case
 LCASE() - Converts a field to lower case
 MID() - Extract characters from a text field
 LEN() - Returns the length of a text field
 ROUND() - Rounds to the number of decimals specified
 NOW() - Returns the current system date and time
 FORMAT() - Formats how a field is to be displayed
29
Summary
• There are three general options for storing data
on client side:
 Cookies
 Web Storage
 Web SQL / Indexed DB
• Most server-side storage systems use rational
databases to store data
• SQL is a standard language for accessing and
manipulating rational databases
30
References
• W3Schools
 http://www.w3schools.com/sql
• Core Python Applications Programming
 By Wesley J. Chun
• Internet Programming by Pat Morin
 http://cg.scs.carleton.ca/~morin/teaching/2405