standard query language (sql) - sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf ·...
TRANSCRIPT
Standard Query
Language (SQL)Hamid Zarrabi-Zadeh
Web Programming – Fall 2013
Outline
• Introduction
• Local Storage Options
Cookies
Web Storage
• Standard Query Language (SQL)
Database Commands
Queries
• Summary
2
Introduction
• Any (web) application needs persistence
storage
• There are three general storage strategies:
server-side storage
client-side storage
a hybrid strategy
3
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
4
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
5
Cookies
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
7
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)
8
Set/Get Cookies
• In JavaScript, cookies can be accessed via the
document.cookie pseudo-variable
• Set cookie:
• Read all cookies:
9
var pairs = document.cookie.split(';');
document.cookie = "name=value; expires=date;
path=/; domain=ce.sharif.edu"
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');
10
Web Storage
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
12
Web Storage Example
• Here is an example:
• Note: values are always stored as strings
• Use JSON.stringify() to convert objects to string
before storing, and use JSON.parse() to retrieve
13
if (typeof (Storage) !== 'undefined') {
localStorage.student = 'Ali',
localStorage.grade = 19.5;
}
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
14
Standard Query
Language (SQL)
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, …
16
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
17
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 case-
insensitive, especially on database commands
• Most command-line programs require a trailing
semicolon (;) to terminate a SQL statement
18
SELECT * from table_name;
Database Operations
• Creating a Database
• Using a Database
• Dropping a Database
19
CREATE DATABASE test;
GRANT ALL ON test.* to user(s);
DROP DATABASE test;
USE test;
Table Operations
• Creating a Table
• Dropping a Table
• Cleaning a Table
20
CREATE TABLE users (name VARCHAR(30), userid INT,
groupid INT);
TRUNCATE TABLE users;
DROP TABLE users;
Row Operations
• Inserting a Row
• Updating a Row
• Deleting a Row
21
INSERT INTO users VALUES('Ali', 110, 1);
DELETE FROM users WHERE groupid=4;
UPDATE users SET groupid=4 WHERE groupid=1;
SQL Queries
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
23
SELECT column_name,column_name
FROM table_name;
SELECT * FROM table_name;
SELECT DISTINCT column_name,column_name
FROM table_name;
Conditional Select
• The WHERE clause is used to extract only those
records that fulfill a specified criterion
• Operators:
=, <>, >, >=, <, <=, BETWEEN, LIKE, IN, IS, IS NOT
AND, OR
24
SELECT column_name, column_name
FROM table_name
WHERE column_name operator value;
SELECT name, userid
FROM users
Where userid > 10 AND groupid <= 4
Order Results
• The ORDER BY keyword is used to sort the result-
set by one or more columns
• 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.
25
SELECT column_name, column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
SQL Functions
• SQL has many built-in functions for performing
calculations on data
• Sample aggregate function:
• Sample scalar function:
• Combined:
26
SELECT AVG(column_name) FROM table_name
SELECT UCASE(name), userid
FROM users;
SELECT UCASE(name) FROM users
WHERE grade > (SELECT AVG(grade) FROM users);
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
27
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
28
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
29
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
30