web programming course, winter 2005 database & sql introduction web programming course...

44
Web programming course, Winte r 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website; available at - http://www.w3schools.com

Upload: gloria-powers

Post on 30-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Database & SQL introduction

Web Programming course

Acknowledgement : this presentation uses examples from the w3c website; available at - http://www.w3schools.com

Page 2: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Outline

Database introductionMotivationHistory Implementation

SQL introduction

Page 3: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Utility of Databases

Data have value independent of use Organized approach to data management

(e.g., data mining) Advantages

Eliminate redundancy in data Share data Archive data Security of data Integrity of data

Page 4: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

DEFINITION

A collection of application programs that perform services to end users.

Each program defines and manages its own data.

File Based Systems

Page 5: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Data Entry& Reports

File handlingRoutines

File Definition

Sales Files

Data Entry& Reports

File handlingRoutines

File Definition

Lease Files

File Based Processing

physical structure and storage of the data files are defined in the program code

Page 6: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Data Dependence Duplication of Data Incompatible file formats

Limitations of File Based Systems

How can these problems be resolved?

Page 7: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

A shared collection of logically

related data designed to meet the

information requirements of an

organisation

The Database Approach

Page 8: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Data Entry& reports

Data Entry& reports

DBMSSales

Leases

ApplicationPrograms

App. Programs

Database

Database Processing

Page 9: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

DEFINITION A software system that enables users to

define, create and maintain the database and which provides controlled access to the database

Database Management System (DBMS)

Page 10: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Allows users to define the database (DDL)

Allows users to insert, update, delete & retrieve data (DML)

Provides controlled accessa security systeman integrity systema concurrency control systema recovery system

Facilities of a DBMS

What does it mean to define a database?

Page 11: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Hardware Software Data Procedures People

Components of a DBMS

Page 12: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Minimal data redundancy Consistency of data Integration of data Improved integrity Consistent security Standards Increased productivity

Advantages

Page 13: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Complexity Additional Hardware Costs Size Performance Experts -Specialised Personnel Potential organisational Conflict Higher impact of failure (centralized

data source failure)

Disadvantages

Page 14: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Database Interfaces

web server &interface prog

relationaldatabase

web browser

dedicated applicationSQL command interface

SQL

SQL SQL

Page 15: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Interaction and Feedback

Transaction: non-decomposable unit of data manipulation example: purchasing an airline ticket on-line typically small and fast for commercial applications may be long and involved in engineering applications

Rollback: if any part of a transaction fails, all completed parts are “rolled back” or undone example: if you haven’t provided your credit card number,

airline ticket purchase on-line transaction fails rollback ensures integrity of database automatically done by DBMS

Page 16: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Relational Database Model Database

Database is a collection of tables (relations) Data are stored in tables

Tables Each table has a name Each table has a set of columns (fields) and rows of data

(records) All operations process a table to produce a new table Each table has a fixed number of columns Each table has an arbitrary number of rows

Based on set theory SQL (Structured Query Language)

DBMS independent language

Why?

Page 17: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Weather Sample TableCity State High Low

Phoenix Arizona 105 90

Tuscon Arizona 101 92

Flagstaff Arizona 88 69

San Diego California 77 60

Albuquerque New Mexico 80 60

Page 18: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Database Columns (Fields)

Columns Each column has a name Columns are accessed by name No standard column ordering Does not make sense to say “the third column” like it

does in a “paper” table or spreadsheet Data in a column belongs to a particular domain

Columns are the “attributes” of the dataset Each value in a column is from the same domain Each value in a column is of the same data type

Page 19: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Database Rows (Records)

Rows Each row entry is either a simple value or empty

("null") Rows are sets of values for the columns (attribute

values) Primary key: a set of columns that uniquely

identifies each row Each row must be unique given the primary key (no

duplicates) Rows are referenced by the primary key Row order cannot be determined by the user (Does

not make sense to say “the fourth row” like it does in a “paper” table or spreadsheet)

Page 20: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Data Types Each row value is an instance of a primitive data type

Integer Real (e.g., number, currency Character (e.g., text, hyperlink, yes/no) Date/Time

No complex types in standard DBMS (matrix, drawing) Object oriented databases may allow objects and

structures Non existent value is “null”

Page 21: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Domain types char(n): fixed length char string varchar(n): variable-length char string int or integer smallint numeric(p,d): fixed-point number of given precision real, double precision float(n): floats with a given precision date: containing year,month, and date time: in hours, minutes, and seconds Null value is part of each domain

Page 22: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

SQL

Structured Query Language Communicate with databases Used to created and edit databases. Also used to create queries, forms, and

reports

Page 23: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

DDL and DML SQL consists of two types of statements

SQL Data Definition Language (DDL) permits database tables to be created or deleted

inserts new data into a database table CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table

SQL Data Manipulation Language (DML) SELECT - extracts data from a database table UPDATE - updates data in a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into a database table

Page 24: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Define new domains and tablesCREATE DOMAIN personDom CHAR(20);

CREATE TABLE emp (ename personDom, dno int default 0, sal real);

Page 25: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

“Select” clause 1 Specify attributes to project onto

Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

SELECT *FROM EmpWHERE Emp.Sal < 60K;

use ‘*’ to denote all attributes:

SELECT columns FROM table WHERE condition ;

Page 26: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

“Select” clause 2

“SELECT” does not automatically eliminate duplicates.

Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Select dnoFrom Emp;

Use keyword distinct to explicitly remove duplicates

Select distinct dnoFrom Emp;

Page 27: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

“FROM” clause

Specify relations

E1: Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

Dept(dno, dname, mgr)dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

E2: Emp (ename, dno, sal)eName Dno Sal

Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

SELECT columns FROM table WHERE condition ;

SELECT E1.enameFROM Emp as E1, Dept, Emp as E2WHERE E1.dno = Dept.dno AND Dept.mgr = E2.ename AND E1.sal > E2.sal;

Renaming relations: Use “as” to define “tuple variables,” to disambiguate multiple references to the same relation

?Who makes

more moneythan his boss?

Page 28: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

“WHERE” clause Specify optional conditions

“Employees who work for Sally and have a salary < 90K”

SELECT enameFROM Emp, DeptWHERE Emp.dno=Dept.dno AND

D.mgr = ‘Lisa’ AND sal < 90000;

eName Dno Sal Jack 111 50K Alice 111 90K Lisa 222 80K Tom 333 70K Mary 333 60K

dno dname Mgr 111 Sells Alice 222 Toys Lisa 333 Electronics Mary

Emp (ename, dno, sal) Dept(dno, dname, mgr)

Lisa

Page 29: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Conditions Used In Where Clause

= equals

> greater than

< less than

>= greater than or equal to

<= less than or equal to

<> not equal to

Page 30: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Like

Used to make complex searching easy. If you are trying to find all people’s names which begin with E for example:

SELECT firstname FROM employee

WHERE firstname LIKE 'E%';

Page 31: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Ordering output tuplesOrder the tuples by dno. Within each dept, order salaries from highest to lowest. For salary ties, use alphabetical order on the name.

SELECT * FROM Emporder by dno, sal, ename;

ename dno sal location

John 123 10000 NY

Roger 123 12000 NY

Mary 245 69460 London

Susan 323 12300 London

Joe 124 46000 Paris

Page 32: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Set Operations Union: .

Intersect: .

Except: -

(select mgr from D where dname=‘toy’)union(select mgr from D where dname = ‘sells’);

(select mgr from D where dname=‘toy’)intersect(select mgr from D where dname = ‘sales’);

(select mgr from D where dname=‘toy’)except(select mgr from D where dname = ‘sells’);

“Find names of people who are managers of either the toy or the sales department.”

“Find names of people who are managers of both the toy and the sales departments.”

“Find names of people who are managers of the toy but of the sales department.”

Page 33: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Conserving Duplicates The UNION, INTERSECT, and EXCEPT operators use the set

semantics, not bag semantics. To keep duplicates, use “ALL” after the operators:

UNION ALL, INTERSECT ALL, EXCEPT ALL

Student (ssno, name)

Ssno Name 111 Tom 222 Jack 444 Mary

TA (ssno, name)

Ssno Name 111 Tom 222 Jack 555 Alice

111 111 222 222 444 555

Result

(SELECT ssno FROM student)UNION(SELECT ssno FROM ta);

111 222 444 555

ALL

Page 34: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Aggregation functions MIN, MAX, SUM, COUNT, AVG

input: collection of numbers/strings (depending on operation)

output: relation with a single attribute with a single row

“What is the minimum, maximum,

average salary of employees in the toy department”

SELECT MIN(sal), MAX(sal), AVG(sal)FROM Emp, DeptWHERE Emp.dno = Dept.dno and D.dname = ’Toy’;

Page 35: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

JOIN

Sometimes we have to select data from two or more tables to make our result complete. We have to perform a join

Tables in a database can be related to each other with keys. A primary key is a column with a unique value for each row. The purpose is to bind data together, across tables, without repeating all of the data in every table.

Page 36: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

INNER JOIN

The INNER JOIN returns all rows from both tables where there is a match.

Employee_ID Name

01 Hansen, Ola

02 Svendson, Tove

03Svendson, Stephen

04 Pettersen, Kari

Orders:

Prod_IDProduct Employee_ID

234 Printer 01

657 Table 03

865 Chair 03

Employees:

SELECT Employees.Name, Orders.Product FROM Employees INNER JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID

Result

Name Product

Hansen, Ola Printer

Svendson, Stephen Table

Svendson, Stephen Chair

If there are rows in Employees that do not have matches in Orders, those rows will not be listed

Page 37: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Left\Right Join The LEFT JOIN returns all the rows from the first table (Employees), even if

there are no matches in the second table (Orders).

The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table (Employees).

SELECT Employees.Name, Orders.Product FROM Employees LEFT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID

SELECT Employees.Name, Orders.Product FROM Employees RIGHT JOIN Orders ON Employees.Employee_ID=Orders.Employee_ID

NameProduct

Hansen, Ola Printer

Svendson, Tove  

Svendson, Stephen Table

Svendson, Stephen Chair

Pettersen, Kari  

Name Product

Hansen, Ola Printer

Svendson, Stephen Table

Svendson, Stephen Chair

Page 38: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

INSERT INTO

The INSERT INTO statement is used to insert new rows into a table

INSERT INTO table_name VALUES (value1, value2,....)

INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....)

Or specify the columns for which you want to insert data:

Page 39: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Insert Into Example

LastNameFirstName

Address City

Pettersen Kari Sto 20 Stav

INSERT INTO Persons  VALUES ('Hetland', 'Camilla', 'Hagabakka 24', 'Sandnes')

LastNameFirstName

Address City

Pettersen Kari Sto 20 Stav

Hetland Camilla Haga 24 Sandnes

Page 40: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

UPDATE

The UPDATE statement is used to modify the data in a table.

UPDATE table_name SET column_name = new_value WHERE column_name = some_value

Page 41: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Person:

LastNameFirstNam

eAddress City

Nilsen Fred Kirk 56 Stav

Rasmussen   Sto 67  

LastNameFirstName Address City

Nilsen Fred Kirk56 Stav

Rasmussen Nina Sto 67  

UPDATE Person

SET FirstName = 'Nina'

WHERE LastName = 'Rasmussen'

UPDATE Person SET Address = 'Stien 12', City = 'Stav' WHERE LastName = 'Rasmussen'

LastNameFirstName Address City

Nilsen Fred Kirk56 Stav

Rasmussen Nina Stien 12  Stav

Page 42: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

DELETE The DELETE statement is used to delete

rows in a table.DELETE FROM table_name WHERE column_name = some_value

LastNameFirstName Address City

Nilsen Fred Kirk 56 Stav

DELETE FROM Person WHERE LastName = 'Rasmussen'

LastNameFirstName Address City

Nilsen Fred Kirk56 Stav

Rasmussen Nina Stien 12  Stav

Page 43: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Summary

Why uses databases instead of file systems, what are the pro-cons, when to use each alternative?

What is a relational database? How can you define its semantics?

What is SQL? What are DDL and DML? What statements are related to each SQL rule

Page 44: Web programming course, Winter 2005 Database & SQL introduction Web Programming course Acknowledgement : this presentation uses examples from the w3c website;

Web programming course, Winter 2005

Questions?