Database Technology for E-commerce Applications. Three-tier Architecture for Web Applications

Similar documents
COSC344 Database Theory and Applications. Java and SQL. Lecture 12

CS 377 Database Systems SQL Programming. Li Xiong Department of Mathematics and Computer Science Emory University

Database Programming. Week *Some of the slides in this lecture are created by Prof. Ian Horrocks from University of Oxford

The JAVA Way: JDBC and SQLJ

Chapter 9 Java and SQL. Wang Yang wyang@njnet.edu.cn

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

DATABASDESIGN FÖR INGENJÖRER - 1DL124

4. SQL. Contents. Example Database. CUSTOMERS(FName, LName, CAddress, Account) PRODUCTS(Prodname, Category) SUPPLIERS(SName, SAddress, Chain)

Relational Database: Additional Operations on Relations; SQL

IT2305 Database Systems I (Compulsory)

More SQL: Assertions, Views, and Programming Techniques

Why Is This Important? Database Application Development. SQL in Application Code. Overview. SQL in Application Code (Contd.


Services. Relational. Databases & JDBC. Today. Relational. Databases SQL JDBC. Next Time. Services. Relational. Databases & JDBC. Today.

IT2304: Database Systems 1 (DBS 1)

Chapter 13. Introduction to SQL Programming Techniques. Database Programming: Techniques and Issues. SQL Programming. Database applications

What is ODBC? Database Connectivity ODBC, JDBC and SQLJ. ODBC Architecture. More on ODBC. JDBC vs ODBC. What is JDBC?

Course Objectives. Database Applications. External applications. Course Objectives Interfacing. Mixing two worlds. Two approaches

Instant SQL Programming

Brazil + JDBC Juin 2001, douin@cnam.fr

Oracle Database: SQL and PL/SQL Fundamentals

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

1 SQL Data Types and Schemas

CS/CE 2336 Computer Science II

1 File Processing Systems

Oracle Database: SQL and PL/SQL Fundamentals NEW

BCA. Database Management System

CSE 530A Database Management Systems. Introduction. Washington University Fall 2013

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

JDBC (Java / SQL Programming) CS 377: Database Systems

public class ResultSetTable implements TabelModel { ResultSet result; ResultSetMetaData metadata; int num cols;

CS346: Database Programming.

Information Systems SQL. Nikolaj Popov

Oracle Database: SQL and PL/SQL Fundamentals

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

Relational Databases

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

CSC 443 Data Base Management Systems. Basic SQL

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Chapter 4: SQL. Schema Used in Examples

How To Create A Table In Sql (Ahem)

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

Oracle SQL. Course Summary. Duration. Objectives

Supplement IV.D: Tutorial for MS Access. For Introduction to Java Programming By Y. Daniel Liang

DBMS Questions. 3.) For which two constraints are indexes created when the constraint is added?

3. Relational Model and Relational Algebra

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Java and Databases. COMP514 Distributed Information Systems. Java Database Connectivity. Standards and utilities. Java and Databases

CSc 230 Software System Engineering FINAL REPORT. Project Management System. Prof.: Doan Nguyen. Submitted By: Parita Shah Ajinkya Ladkhedkar

A Brief Introduction to MySQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

What is a database? COSC 304 Introduction to Database Systems. Database Introduction. Example Problem. Databases in the Real-World

Oracle Database 10g: Introduction to SQL

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Introduction to Microsoft Jet SQL

Fundamentals of Database Design

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database 10g Express

MySQL for Beginners Ed 3

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

SQL Data Definition. Database Systems Lecture 5 Natasha Alechina

DATABASE MANAGEMENT SYSTEMS. Question Bank:

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

T-SQL STANDARD ELEMENTS

Objectives of SQL. Terminology for Relational Model. Introduction to SQL

SQL and Java. Database Systems Lecture 19 Natasha Alechina

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

2. Basic Relational Data Model

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

Foundations of Information Management

SQL. Short introduction

Database Query 1: SQL Basics

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

Chapter 1: Introduction. Database Management System (DBMS) University Database Example

Database 10g Edition: All possible 10g features, either bundled or available at additional cost.

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

ECS 165A: Introduction to Database Systems

DBMS / Business Intelligence, SQL Server

Databases and BigData

Supplement IV.C: Tutorial for Oracle. For Introduction to Java Programming By Y. Daniel Liang

Introduction to Databases

Chapter 5. SQL: Queries, Constraints, Triggers

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Bridge from Entity Relationship modeling to creating SQL databases, tables, & relations

Databases What the Specification Says

Introduction to Triggers using SQL

Relational databases and SQL

EECS 647: Introduction to Database Systems

SQL Programming. CS145 Lecture Notes #10. Motivation. Oracle PL/SQL. Basics. Example schema:

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

3.GETTING STARTED WITH ORACLE8i

Introduction to SQL and database objects

Financial Data Access with SQL, Excel & VBA

Database Programming with PL/SQL: Learning Objectives

Overview. Database Application Development. SQL in Application Code (Contd.) SQL in Application Code. Embedded SQL. Embedded SQL: Variables

Information Technology NVEQ Level 2 Class X IT207-NQ2012-Database Development (Basic) Student s Handbook

SQL Server An Overview

Chapter 1 JDBC: Databases The Java Way! What Is The JDBC? The JDBC Structure ODBC s Part In The JDBC Summary

Transcription:

Database Technology for E-commerce Applications Three-tier Architecture for Web Applications Client Application Server GUI Web Interface Application Programs Web Pages Presentation layer Human-computer interface Processing layer Application processing: interprets client requests, sends commands to database server, passes back results to clients Database Server Database Management System Data layer Manages permenent data 1

Outline ER conceptual data model Relational data model and SQL Programming databases JDBC HSQL Data Models The primitives used to describe the structure of the information in the database Conceptual and implementation models 2

Entity-Relational Model Proposed by P. Chen, 1976 High-level conceptual data model for database design no implementation details, easier to communicate with nontechnical users more formal than natural language specifications independent of particular implementation Conceptual design in ER model transformed into logical design in an implementation data model, e.g. relational data model Entities and Attributes Entity: a thing whith physical or conceptual existance Attributes: properties describing an entity Domain: set of valid values Entity type: a set of entities with the same attributes Key attribute(s): identify uniquely each entity in the set Key Attr Entity 3

Relationship Types A relationship type among entity types E 1,E 2,,E k : set of associations among entities from these entity types Degree: the number of participating entity types Cardinality Ratios for binary relationships 1:1 1:M M:N Can have attributes Example ER-modelling Name Category ArtNr Article M Date supply Qty N SNr Supplier Name Price Quantity City Address Description 4

Relational Data Model Proposed by Dr. Edgar F. Codd, 1970 Data presented as two-dimentional tables, relations one or more named columns rows(tuples) crossing points of columns and rows contain data values of atomic types Operations apply to relations and produce new relations column 1 <row 2> <row n> column n Translation from ER to Relational Model Articles ArtNr Name Description Category Quantity Price Suppliers SNr Name City Address FK Supply FK ArtNr SNr Qty Date 5

Querying Relational Data Two formalisms procedural relational algebra declarative relational calculus SQL, Structured Query Language Users specify what data to be retrieved SQL query processing module translates queries into efficient execution plans specifying how data is retrieved Increases efficiency of database programming Provides data independence SQL Data Definition Language (DDL) Define/re-define database structure Data Manipulation Language (DML) Updates Queries Additional facilities Views Security, authorization Integrity constraints Transaction constraints 6

Background History SEQUEL (Structures English QUery Language) early 70 s, IBM Research SQL (ANSI 1986), SQL1 or SQL86 SQL2 or SQL92 SQL3 or SQL:1999 SQL:2003 Core specification and optional specialized packages Standard language for all commercial DBMS Each DBMS has features outside standard DDL: Creating Tables Articles ArtNr Name Description Category Quantity Price CREATE TABLE articles ( ArtNr INTEGER, Name VARCHAR(64), Description VARCHAR(256), Category VARCHAR(64), Quantity INTEGER, Price DOUBLE, PRIMARY KEY (ArtNr)); 7

Basic query statement: select from where SELECT A 1, A 2,, A n FROM r 1, r 2,, r m WHERE P; A 1, A 2,, A n list of attribute names to be retrieved r 1, r 2,, r m List of tables required to process the query P Conditional expression identifying the tuples to be retrieved AND, OR, NOT, <, <=, =, >=, > Result of the query is a table The SQL SELECT clause select * from articles; - the star denotes all attributes select Name, Price from articles; - projection on the specified attributes select distinct Category from articles; - duplicate removal select Name, Price, Price*0.25 as VAT from articles; - arithmethic expressions 8

The SQL WHERE clause Selects tuples fulfilling the predicate to be returned select Name, Description, Price from articles where ArtNr=125; select Name, Price from articles where Price>2.5 and Category = Coffee ; Special comparisons Matching string patterns Use LIKE % for any number of arbitrary symbol _ for any symbol select * from articles where Description like %Colombian% ; Approx math equality Use abs(x-x 1 ) < ε: select * from articles where abs(price-2.0) < 0.5; Use BETWEEN: select * from articles where Quantity between 10 and 20; 9

The SQL FROM clause Given two database tables persons Alex John Mike cars Audi x = BMW Mercedes, this SQL query generates all possible persons-cars combinations. select * from persons, cars; Alex John Mike Alex John Mike Alex John Mike Audi Audi Audi BMW BMW BMW Mercedes Mercedes Mercedes Join Relational algebra notation: R C join condition C S C is on the form A R θ A S θ is one of {=, <, >,,, } Several terms can be connected as C 1 C 2 C K. Special cases Equijoin: θ is = Natural join: All identically named attributes in relations R and S have matching values 10

SQL Join For each article find the numbers of its suppliers: select articles.artnr, articles.name, supply.snr from articles, supply where articles.artnr=supply.artnr; select articles.artnr, articles.name, supply.snr from articles JOIN supply on articles.artnr=supply.artnr; SQL Join more examples Show the names and addresses of big suppliers who make supplies with quantity > 1000 select Name, City, Address from suppliers join supply on supply.snr = suppliers.snr where Qty>1000; Show the names and addresses of all suppliers of coffee select suppliers.name, suppliers.city, suppliers.address from articles join supply on articles.artnr = supply.artnr join suppliers on supply.snr = suppliers.snr where articles.category= Coffee ; 11

NULL values Sometimes an attribute is Unknown (date of birth unknown) Unavailable (refuses to list home phone #) Not applicaple (last college degree) Need to represent these cases in a DB! Solution: NULL Comparison of NULL values =,, >, <, LIKE, won t work. NULL is UNDEFINED! SQL check for NULL IS NULL IS NOT NULL JOIN operations Tuples with NULL values in the join columns Not included in result Exception: OUTER JOIN 12

NULL Example Find out articles without specified description select ArtNr, Name from articles where Description is NULL; Aggregate functions Avg average value Min minimum value Max maximum value Sum sum of values Count number of values Average price of all articles select avg(price) from articles; 13

Aggregate functions group by Average price for each category of articles select Category, avg(price) from articles group by Category; For each supplier show his name and how many different articles he supplies select s.name, count(distinct ArtNr) from supplier as s, supply as sup where s.snr = sup.snr group by s.name; Aggregate functions HAVING Show the average price and number of articles for each category with at least 3 articles select Category, avg(price), count(*) from articles group by Category having count(*)>3; Show the categories of articles for which the minimum price is above a threshold 10.0 select Category, min(price) from articles group by Category having min(price)>10.0; 14

SQL Select Summary Clauses: SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY <attribute list> <table list> <condition>] <grouping attributes> <group condition>]] <attribute list>] Views They are views of the underlying tables, not stored tables Defined through queries, i.e. stored queries Reflect changes in the underlying tables Used for frequently posed queries and security Show the average price and number of articles for each category with at least 3 articles create view NamedSupply(Aname,Sname,Qty,Date) as select articles.name,suppliers.name, supply.qty, supply.date from articles join supply on articles.artnr = supply.artnr join suppliers on supply.snr = suppliers.snr; select * from NamedSupply; 15

Modifying the Database - INSERT INSERT INTO <table> [(column1, column2, )] VALUES ( ); insert into articles values(121, Colombian, good, Coffee,100, 2.25); insert into articles (ArtNr, Name, Category, Price) values(125, Segafredo, Coffee, 6.18); What is the value of Description and Category? Modifying the Database - UPDATE UPDATE <table> SET <column1> = <value1> [, ] WHERE <condition>; Single row: increase the quantity of atricle numbered 121 with 10: UPDATE articles SET Quantity = Quantity - 10 WHERE ArtNr = 121; Multiple rows: increase the price of all black tea by 5%: UPDATE articles SET Price = Price*1.05 WHERE Category = Black tea ; 16

Modifying the Database - DELETE DELETE FROM <table> WHERE <condition>; Single row: delete supplier numbered 10: delete suppliers where SNr = 10; Multiple rows: delete all supplies before 1.1.2006: delete supply where Date< 1-JAN-2006 ; More about SQL Tutorials: http://www.1keydata.com/sql/sql.html http://www.sqlcourse.com/ http://www.sqlcourse2com/ http://www.firstsql.com/tutor.htm (SQL92) http://hsqldb.sourceforge.net/web/hsqldocsfra me.html SQL in HSQLDB 17

Accessing Databases Interactive interfaces Convenient for schema and constraint creation, ad-hoc queries Textual Graphical Application Programs or Database Applications Majority of database interactions Embedded SQL Application Programming Interface, SQL Client Interface Database programming language (Oracle s PL/SQL) Embedded SQL Database statements embedded in host programming language Identified by special prefix Preprocessor or precompiler replaces database statements by function calls 18

SQL Client Interfaces Library of functions for database calls available to the host language Actual database statements are parameters in function calls Types: 1. Vendor-specific client interfaces 2. Standard client interfaces for DBMSindependent application ODBC, SQL/CLI JDBC Impedance Mismatch Difference between database model and programming language model Differences in data types require a binding for each host programming language SQL query result is a table, or set of tuples, and require binding to programming language data structure. Cursor iterates over tuples in a query result. 19

Typical Interaction Sequence Establish a connection with a database specifying database URL, login account and password Submit queries, updates and other SQL statements Process the results Close the connection JDBC Architecture 20

JDBC Basic Classes import java.sql.*; D. Ince, Developing distributed and e-commerce applications, Figure 5.2, p. 113 Establishing Connections 1. Load JDBC driver Class.forName("org.hsqldb.jdbcDriver"); 2. Make connection Connection con= DriverManager.getConnection( url, // Database URL "mylogin", // login name to the DBMS mypassword"); // password for the DBMS Connection con= DriverManager.getConnection( "jdbc:hsqldb:file:testdb", "sa", ""); 21

Send SQL Statements 1. Create JDBC Statement object Statement stmt = con.createstatement(); 2. Execute SQL statements stmt.executeupdate(<update String>) for DDL, DML- updates statements stmt.executequery(<query String>) for queries stmt.execute() general method for multiple result sets 3. Close statement stmt.close(); Creating Tables SQL DDL statement Statement stmt = con.createstatement(); stmt.executeupdate("create TABLE articles ( ArtNr INTEGER, Name VARCHAR(64), Description VARCHAR(256), Category VARCHAR(64), Quantity INTEGER, Price DOUBLE, UNIQUE(ArtNr))"); 22

Entering Data Statement stmt = con.createstatement(); stmt.executeupdate( INSERT INTO articles + VALUES(1, Colombian, good, Coffee,100, 2.25) ); SQL Queries Statement stmt = con.createstatement(); ResultSet rs = stmt.executequery( SELECT ArtNr, Name, Price + FROM Articles ); 23

Retrieving Results next method moves a cursor to the next row getxxx methods retrieve a column value given column name or number in the result set. xxx is Java type of retrieved value Example: while(rs.next()){ int nr = rs.getint( ArtNr"); String nm = rs.getstring("name"); double pr = rs.getdouble("price"); System.out.println(nr + " " + nm + " " + pr); } Updating Data Statement stmt = con.createstatement(); stmt.executeupdate( UPDATE articles + SET Quantity = Quantity - 10 + WHERE ArtNr = 1 ); public void updatearticles(int nr, int qty) throws SQLException { Statement stmt = con.createstatement(); String updstring = "UPDATE articles + "SET Quantity = Quantity - " + qty + " WHERE ArtNr = " + nr; stmt.executeupdate(updstring); stmt.close(); } 24

Prepared Statements Sent to DBMS and precompiled Typically parameterized SQL statements String query= SELECT ArtNr, Name, Price + FROM Articles WHERE Category =? ); PreparedStatement pstmt = con.preparestatement(query); String cname; while(...){ // read category into cname pstmt.setstring(1,cname); ResultSet rs = pstmt.executequery(); while(rs.next()){ // print results } } Using Metadata ResultSetMetaData: getcolumncount() - number of columns in result set getcolumnname(i) - name of column #i getcolumntypename(i) - type of column #i in DBMS gettablename(i) ResultSetMetaData meta = rs.getmetadata(); int colmax = meta.getcolumncount(); for(int k = 1; k <= colmax; k++) { System.out.println(meta.getColumnName(k) + of type + meta.getcolumntypename(k)); } 25

Retrieving Query Results Using Metadata ResultSet rs = stmt.executequery(" "); ResultSetMetaData meta = rs.getmetadata(); int colmax = meta.getcolumncount(); int i; String s; while (rs.next()) { for (i = 1; i <= colmax; ++i) { s = rs.getstring(i); System.out.print(s + " "); } System.out.println( ); } HSQLDB Relational database engine in Java Supports subset of SQL:92 + enhancements from SQL:99 and 2003 Integrated with Open Office Components: RDBMS JDBC driver Database Manager Query Tool 26

HSQLDB cont. Database engine runs in both Server modes In-process(embedded, standalone) mode Database files mydb.properties (settings) mydb.script (schema and tables) mydb.log (logs changes) mydb.data (cashed tables) mydb.backup Tables MEMORY held entirely in memory, recreated from.script when the database is opened CACHED partially held in memory, allow processing of large tables HSQL Database Manager java -cp../lib/hsqldb.jar org.hsqldb.util.databasemanager 27

SQL and Java Data Types SQL Type INTEGER VARCHAR CHAR DATE TIME DECIMAL REAL BIGINT SMALLINT Java Type int String String java.sql.date java.sql.time java.math.bigdecimal double long short 28

Using HSQLDB Use in-process mode Create the DB with a Java program Set the permissions of the DB directory and files to be accessible for writing for Tomcat 29