2440: 141 web site administration database management using sql professor: enoch e. damson

22
The University of Akron College of Applied Science & Technology Dept. of Business & Information Technology 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Upload: emil-todd

Post on 28-Dec-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

The University of AkronCollege of Applied Science & Technology

Dept. of Business & Information Technology

2440: 141Web Site Administration

Database Management Using SQLProfessor: Enoch E. Damson

Page 2: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Database ConceptsA database is a collection of data about an entities

For example, a database might contain checking account information in a bank, product items information in an on-line store, students’ records in a college, and so on

Relational databases store information in simple structures called tables

Most commercial databases today are relational databases

Introduction to SQL 2

Page 3: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Database ConceptsAn entity is a distinct object, for example, a person's name, a

product, or an event, to be presented in the tableThe columns in the table contain fields

A field contains a single, specific piece of information within a record. So, a record is a group of related fields

The data in a table is organized into rows and columnsThe rows in the table are called records

A record contains information about a given entity

Introduction to SQL 3

Page 4: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Sample Item Database Table

Introduction to SQL 4

Item Number Item Name Unit Price Inventory

100 Camera $267.99 13

101 Washer $489.56 8

102 TV $189.99 29

Page 5: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Creating TablesDetails required to create database tables include:

1. Table name

The table name identifies the table

A database can consist of many tables

You use the name of a table to reference the table and to manipulate data in that table

2. Fields names

3. Field data typesIntroduction to SQL 5

Page 6: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Data Types

Introduction to SQL 6

Data type Sample data Description

CHAR(length) Newcastle Dr.

For nonnumeric data. Fixed length

VARCHAR(length) Newcastle Dr.

For nonnumeric data. Variable length (listed length indicates maximum)

INTEGER 123456 For whole number data between –231 and +231-1

SMALLINT 31 For whole number data between –215 and +215-1

NUMERIC 2.6E+10 Very large or vary small numbers

DATE 11/16/2001 For date. Implementations vary in different databases

Page 7: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Primary KeyAn integrity constraint that uses a unique identifier to

uniquely identify a record in a table

Introduction to SQL 7

Page 8: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Using Common Fields (Foreign Keys)to Link Two Tables

Introduction to SQL 8

productID

productName

model

Price manufacturerID

100 Washer D1 356.99

1

200 TV S2 255.68

2

manufacturerID name address

phone

1 Weiwei Co. Edward Rd

123456

2 XYZ Co. Central 654321

Page 9: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

An Introduction to SQLThe Structured Query Language (SQL) is a language embedded

in relational DBMSs to process relational databasesSQL can be used as a:

1. Data definition language (DDL): Used to define a table’s column, add or delete columns, and delete

unneeded tables Example: Create table, alter table, drop table, etc

2. Data manipulation language(DML): Used to insert, update, delete, and retrieve data in a table Example: Insert into, update, delete, and select

Introduction to SQL 9

Page 10: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Creating and Dropping TablesCREATE TABLE tableName (field1 dataType, field2 dataType, …) CREATE TABLE tableName

(field1 dataType PRIMARY KEY, field2 dataType, …) DROP TABLE tableName

Introduction to SQL 10

Page 11: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Example: Student TableCREATE TABLE student (

id integer PRIMARY KEY, firstName varchar(15),lastName varchar(15));

Introduction to SQL 11

Page 12: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Inserting Data Into a TableINSERT INTO TABLE tableName VALUES (value1, value2, …)

or

INSERT INTO TABLE tableName (field1, field2, …) VALUES

( value1, value2, …)

Introduction to SQL 12

Page 13: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Updating Table DataUPDATE tableName SET field1=value1, fiedl2=value2, … WHERE conditions

The WHERE clause gives the condition for selecting which rows (records) are to be updated in the table identified as tableName

The SET keyword is followed by the field list to be updated

If the WHERE clause is omitted, all rows in the table are updated

Introduction to SQL 13

Page 14: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Updating Table DataConditions in a WHERE clause are similar to conditional

statements in JSP

Conditions can be constructed with comparison operators and logical operators

You can use the six comparison operators ( =, <> for not equal, <, >, <=, >=) as well as the three logical operators (AND, OR, and NOT) to create compound conditions or to negate a condition

Introduction to SQL 14

Page 15: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Deleting Records from a TableDELETE FROM tableName WHERE conditions

This deletes all rows that satisfy the WHERE clause in the statement

If there is no WHERE clause, then all rows in the table are deleted

Introduction to SQL 15

Page 16: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Retrieving Data

SELECT field1, field2, …FROM tableNameWHERE conditions

The SELECT clause lists the fields retrieved in the query result, separated by commas

The FROM clause lists one or more table names to be used by the query

All fields listed in the SELECT or WHERE clauses must be found in one and only one of the tables listed in the FROM clause

Introduction to SQL 16

Page 17: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Retrieving Data…

The WHERE clause contains conditions for selecting rows from the tables listed in the FROM clause

The data retrieved are from the rows that satisfy the condition (and are therefore selected)

If the WHERE clause is omitted, all rows are selected

Introduction to SQL 17

Page 18: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Wildcard CharactersSpecial symbols that represent any character or

combination of charactersMake it easier to use inexact spelling in a queryThe percent symbol % represents any collection of

characters, including zero charactersThe underscore _ represents any individual character

Introduction to SQL 18

Page 19: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Sorting Retrieved DataSELECT field1, field2, … FROM tableName WHERE

conditions

ORDER BY sort_field1 DESC, sort_field2, …

Sort data retrieved in a particular order

Introduction to SQL 19

Page 20: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Steps to Accessing Databases1. Load the database driver2. Define the connection URL3. Establish the connection4. Create the statement object5. Execute a query or update6. Process the results7. Close the connection

Introduction to SQL 20

Page 21: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Loading a DBMS Driver The DBMS driver acts as the bridge between the server-side

programming environment and the database itself

The driver is a piece of software that knows how to talk to the DBMS

To load a driver, all you need to do is to load the appropriate class

Introduction to SQL 21

Page 22: 2440: 141 Web Site Administration Database Management Using SQL Professor: Enoch E. Damson

Client-side Scripts and Browser DependencyClient-side scripts are executed in the client browser

The browser must provide an appropriate interpreter

All browsers come with built-in engines to support certain client-side scripting languages

Introduction to SQL 22