Top Banner
Chapter 8 Databases
31

Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Jan 04, 2016

Download

Documents

Bertha Hamilton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Chapter 8Databases

Page 2: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Objectives

• Explain the structure of a relational database

• Use SQL for creating, maintaining, and accessing relational databases

• Use Java/JDBC for accessing relational databases

• Explain and apply basic principles of good database design

Page 3: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

RDBMS

• A Relational Database Management System (RDBMS) provides data storage and access for web applications

Page 4: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Relation

• relation is a mathematical term that refers to an ordered set of values drawn from different domainsEx: a relation on numbers x letters x symbols

(55, A, #)

Page 5: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Database Structure

• A database includes one or more tables– Each table represents one type of entity

• Example: Tables in a Library Database

Patron

Book

Loan(transaction)

Recording

Page 6: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Database Structure

• Each table field represents an entity attribute• Each table row represents one entity

Year Make Model Color

1973 Volkswagen Jetta Red

1992 Ford Aerostar Blue

2004 Chevrolet Suburban Black

Car table:

field

row

Page 7: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Structured Query Language

• SQL is a standard language for creating and maintaining relational databases

• SQL statement types:– data definition: create databases and tables– data manipulation: add, modify, delete data– data control: set access permissions

Page 8: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Basic SQL Statements

• Data definition– CREATE, DROP

• Data manipulation– SELECT, INSERT, UPDATE, DELETE

• Data control– GRANT, REVOKE

Page 9: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

CREATE

• Create a database or a table

CREATE DATABASE ehsl

CREATE ehsl.player (playerNr int PRIMARY KEY,name VARCHAR(30),isCurrent BOOLEAN NOT NULL)

Page 10: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Basic SQL Data Types

• INTEGER• DECIMAL(T, R)

– T=total digits, R=right digits (after '.')• FLOAT

• CHAR(N) N characters• VARCHAR(N) up to N characters

• BOOLEAN

• DATE• TIME

Page 11: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

DROP

• DROP can be used to delete an entire database or a table

DROP ehsl

DROP ehsl.player

Page 12: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

SELECT

• SELECT retrieves data from a database

SELECT field-list FROM database.table

WHERE condition

ORDER BY field-list

– field-list is a comma-separated list of fields from the named table (* means "all fields")

– condition is a Boolean condition using field names and/or constants

Page 13: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

SELECT

• Example

SELECT * FROM ehsl.player

SELECT playerNr, name FROM ehsl.player

WHERE isCurrent=TRUE

SELECT playerNr, name, status FROM ehsl.player

WHERE playerNr >= 90001

ORDER BY status, name

Page 14: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

INSERT

• INSERT adds a new row to a table

INSERT INTO ehsl.player VALUES (23752, 'Jane Doe', TRUE)

Page 15: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

UPDATE

• UPDATE changes one or more rows

UPDATE database.tableSET field-assignment-listWHERE condition

UPDATE ehsl.player

SET isCurrent=TRUEWHERE playerNr=33256

Page 16: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

DELETE

• DELETE removes one or more rows from a table

DELETE FROM database.tableWHERE condition

DELETE FROM ehsl.playerWHERE playerNr=33523

Page 17: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Warning

• UPDATE and DELETE without a WHERE clause will affect all rows!

UPDATE ehsl.playerSET isCurrent=true

DELETE FROM ehsl.player

Change all rows!

Delete all rows!

Page 18: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

GRANT

• GRANT can be used to give access permissions to users

GRANT ALL PRIVILEGES ON database.tableTO user-name

– user-name is formatted as user@host, for example 'jonesac'@'localhost'

Page 19: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

REVOKE

• REVOKE can be used to eliminate access permissions

REVOKE ALL PRIVILEGES on database.tableFROM user-name

REVOKE ALL PRIVILEGES on ehsl.playerFROM 'jonesac'@'localhost'

Page 20: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Create User

• The CREATE command can also be used to create new users

CREATE USER user-nameIDENTIFIED BY password

CREATE USER 'jonesac'@'localhost'IDENTIFIEC BY 'abc123#'

Page 21: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

JDBC

• Java Database Connectivity (JDBC) is a Java API that allows Java programs to interact with relational database management systems

• Interaction also requires a database driver, which translates JDBC commands to procedure calls on the RDBMS

RDBMS

Driver

JDBC

ApplicationProgram

Page 22: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

JDBC – Load Driver

• The first step is to load the database driver– Usually provided by the RDBMS vendor

String driverClassName = "com.mysql.jdbc.Driver";

try {

Class.forName(driverClassName);

}

catch (ClassNotFoundException cnfe) {

….

}

Page 23: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

JDBC – Execute Query (1/2)

• To execute an SQL Query:

String query = "...";Vector<String> colNames = new Vector<String>, result = new Vector<String>;try { Connection con = DriverManager.getConnection( dbUrl, dbUserId, dbPassword); Statement st = con.createStatement(); ResultSet rs = st.executeQuery(query); ResultSetMetaData md = rs.getMetaData();

Page 24: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

JDBC – Execute Query (2/2)

• To execute an SQL Query:// get column namesfor (int i = 1; i <= md.getColumnCount(); i++) { colNames.addColumnName(md.getColumnName(i));}// get field valueswhile (rs.next()) { for (int i = 1; i<=md.getColumnCount(); i++) { result.addFieldValue(rs.getString(i)); }}con.close();} catch (SQLException s) { ...}

access the next row of the table

access the next field of the row

Page 25: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

JDBC – Execute Command

• To execute an SQL command:

int result = 0;String command = "...";try { con = DriverManager.getConnection( dbUrl, dbUserId, dbPassword); stmt = con.createStatement(); result = stmt.executeUpdate(command); con.close();} catch (SQLException s) { ...} result = number of rows affected

(inserted, modified, or deleted)

Page 26: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

JDBC Design

• An effective design for database access:– JSP: user interface presentation– Java Servlet: application logic– Java Bean: database access

(JDBC)

JSPcreate user interface

Java Servletprocess / prepare

data

Java Beanaccess RDBMS

using JDBC

RDBMS

dependency

Page 27: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Database Design Principles

1. Each field should contain a single value

2. Repeated fields with empty values should be made a separate table

3. Each table should represent only one entity

Page 28: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Example: Registration

• WRONG:

• Better:

multiple valued field

empty fields

Page 29: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Example: Registration

• Better still:

multiple entities:class registration /class name

Page 30: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Example: Registration

• RIGHT:

Page 31: Chapter 8 Databases. Objectives Explain the structure of a relational database Use SQL for creating, maintaining, and accessing relational databases Use.

Review

• Relational Database / RDBMS• SQL

– Data Definition– Data Manipulation– Data Control

• JDBC– Database Driver– Query Execution– Command Execution

• Database Design Principles