standard query language (sql) - sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf ·...

30
Standard Query Language (SQL) Hamid Zarrabi-Zadeh Web Programming – Fall 2013

Upload: donga

Post on 12-Mar-2018

231 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

Standard Query

Language (SQL)Hamid Zarrabi-Zadeh

Web Programming – Fall 2013

Page 2: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

Outline

• Introduction

• Local Storage Options

Cookies

Web Storage

• Standard Query Language (SQL)

Database Commands

Queries

• Summary

2

Page 3: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

Introduction

• Any (web) application needs persistence

storage

• There are three general storage strategies:

server-side storage

client-side storage

a hybrid strategy

3

Page 4: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 5: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 6: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

Cookies

Page 7: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 8: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 9: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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"

Page 10: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 11: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

Web Storage

Page 12: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 13: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

}

Page 14: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 15: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

Standard Query

Language (SQL)

Page 16: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 17: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 18: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

Page 19: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

Page 20: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

Page 21: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

Page 22: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

SQL Queries

Page 23: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

Page 24: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 25: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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;

Page 26: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 27: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 28: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 29: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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

Page 30: Standard Query Language (SQL) - Sharifce.sharif.edu/~zarrabi/courses/2013/ce419/notes/sql.pdf · Standard Query Language (SQL) ... jQuery.Cookie • A simple jQuery plugin for reading,

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