1 Introduction to Web Application Introduction to Data Base
1
Introduction to Web Application
Introduction to Data Base
2
Topics• Basic Concepts of Data Base
• SQL Syntax
• Transaction
• MS SQL Server 2000
3
Basic Concepts of Data Base
• What is Data Base
• What is DBMS?
• DB Schema
• Table
• View
• Index
• Primary Key
• Foreign Key
4
Introduction to Data Base?
• Database– Integrated collection of data– Database management system (DBMS)
• Store and organize data consistent with database’s format
• Relational database– SQL (Structured Query Language)
» Queries
» Manipulate data
5
Relational Database Model
• Composed of tables
• Row– Number column– Primary key
• Reference data in the table
• A column or set of columns in table contains unique data
6
Relational Database Model
number name department salary location
23603 Jones 413 1100 New Jersey
24568 Kerwin 413 2000 New Jersey
34589 Larson 642 1800 Los Angeles
35761 Myers 611 1400 Orlando
47132 Neumann 413 9000 New Jersey
78321 Stephens 611 8500 Orlando
Row
ColumnPrimary key
7
Relational Database Overview• Primary key uniquely identifies each row
– Rule of Entity Integrity
• Composite primary key
• Lines connecting tables– Relationships
• One-to-many relationship
• Foreign key– Join multiple tables– Rule of Referential Integrity
8
Relational Database Overview
Field Description authorID Author’s ID number in the database. In the Books.mdb
database, this Integer column is defined as auto-increment. For each new row inserted in this table, the database increments the authorID value, ensuring that each row has a unique authorID. This column represents the table’s primary key.
firstName Author’s first name (a String). lastName Author’s last name (a String). Fig. 22.3 Authors table from Books.mdb.
9
Relational Database Overview
authorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 3 Tem Nieto 4 Kate Steinbuhler 5 Sean Santry 6 Ted Lin 7 Praveen Sadhu 8 David McPhie 9 Cheryl Yaeger 10 Marina Zlatkina 11 Ben Wiedermann 12 Jonathan Liperi Fig. 22.4 Data from the Authors table of
Books.mdb.
10
Relational Database Overview
Field Description publisherID The publisher’s ID number in the database. This
auto-incremented Integer is the table’s primary key.
publisherName The name of the publisher (a String). Fig. 22.5 Publishers table from Books.mdb.
publisherID publisherName 1 Prentice Hall 2 Prentice Hall PTG Fig. 22.6 Data from the Publishers table of Books.mdb.
Field Description authorID The author’s ID number, which allows the database
to associate each book with a specific author. The integer ID number in this column must also appear in the Authors table.
isbn The ISBN number for a book (a String). Fig. 22.7 AuthorISBN table from Books.mdb.
11
Relational Database Overview
authorID isbn authorID isbn 1 0130895725 2 0139163050 1 0132261197 2 013028419x 1 0130895717 2 0130161438 1 0135289106 2 0130856118 1 0139163050 2 0130125075 1 013028419x 2 0138993947 1 0130161438 2 0130852473 1 0130856118 2 0130829277 1 0130125075 2 0134569555 1 0138993947 2 0130829293 1 0130852473 2 0130284173 1 0130829277 2 0130284181 1 0134569555 2 0130895601 1 0130829293 3 013028419x 1 0130284173 3 0130161438 1 0130284181 3 0130856118 1 0130895601 3 0134569555 2 0130895725 3 0130829293 2 0132261197 3 0130284173 2 0130895717 3 0130284181 2 0135289106 4 0130895601 Fig. 22.8 Data from AuthorISBN table in Books.mdb.
12
Relational Database Overview
Field Description isbn ISBN number of the book (a String). title Title of the book (a String). editionNumber Edition number of the book (a String). copyright Copyright year of the book (an Integer). description Description of the book (a String). publisherID Publisher’s ID number (an Integer). This value
must correspond to an ID number in the Publishers table.
imageFile Name of the file containing the book’s cover image (a String).
price Suggested retail price of the book (a real number). [Note: The prices shown in this database are for example purposes only.]
Fig. 22.9 Titles table from Books.mdb.
13
Relational Database Overview
isbn title edition-Number
publisherID copy-right
price
0130923613 Python How to Program 1 1 2002 $69.95 0130622214 C# How to Program 1 1 2002 $69.95 0130341517 Java How to Program 4 1 2002 $69.95 0130649341 The Complete Java Training
Course 4 2 2002 $109.95
0130895601 Advanced Java 2 Platform How to Program
1 1 2002 $69.95
0130308978 Internet and World Wide Web How to Program
2 1 2002 $69.95
0130293636 Visual Basic .NET How to Program
2 1 2002 $69.95
0130895636 The Complete C++ Training Course
3 2 2001 $109.95
0130895512 The Complete e-Business & e-Commerce Programming Training Course
1 2 2001 $109.95
Fig. 22.10 Portion of the data from the Titles table of Books.mdb.
14
Relational Database Overview
15
SQL Syntax
• CREATE
• DROP
• INSERT
• SELECT
• DELETE
• UPDATE
16
Create Database
• CREATE DATABASE dbname
• USE dbname– CREATE DATABASE db04498– go– USE db04498– go
17
Drop Database
• DROP DATABASE dbname;– USE master– go– if exists (select * from sysdatabases where
name='db04498')• DROP DATABASE db04498
– go– CREATE DATABASE db04498– go
18
Create Table Construct
• An SQL relation is defined using the create table command:
create table name (A1 D1, A2 D2, ..., An Dn,(integrity-constraint1),...,(integrity-constraintk))
– Name is the name of the table
– each Ai is an attribute name in the schema of relation r
– Di is the data type of values in the domain of attribute Ai
19
• Example:
CREATE TABLE Authors(
authorID int primary key,
firstName varchar (32),
lastName varchar (64))
20
Integrity Constraints in Create Table
• not null
• primary key (A1, ..., An)
• check (P), where P is a predicate
Example:
CREATE TABLE Authors( authorID int,firstName varchar (32),lastName varchar (64),primary key (authorID),check (authorID >= 1))
21
Foreign Key• Example• CREATE TABLE Publishers(• publisherID int primary key,• publisherName varchar (64)• )
• CREATE TABLE Titles (• isbn char (16) primary key,• title char (64),• editionNumber varchar(8), • copyright int,• description varchar(128),• publisherID int• references Publishers(publisherID),• imageFile varchar (256),• price float)
22
Drop Table
• DROP TABLE name
• Example– DROP TABLE authors– go
23
Insert
• The INSERT Command– INSERT INTO table_name (col_name1, …
col_namen) VALUES (value1, …, valuen)– The correspondence between column names
and values is positional
• Example– INSERT INTO Authors VALUES (1, 'Harvey',
'Deitel')
24
SELECT• The SELECT Command
– Used to specify queries– Three clauses:
• SELECT, FROM, and WHERE
– General form:• SELECT column names FROM table names
WHERE condition
• Example– SELECT * FROM Authors– SELECT authorID, lastName FROM Author
WHERE authorID = 1
25
WHERE Clause • Specify selection criteria for query
– SELECT columnName1, columnName2, … FROM tableName WHERE criteria•SELECT title, editionNumber, copyright FROM Titles WHERE copyright > 1999
– LIKE• Pattern matching
– Asterisk ( * )
» SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘D*’
– Question mark ( ? )
» SELECT authorID, firstName, lastName FROM Authors WHERE lastName LIKE ‘?I*’
26
ORDER BY Clause • Arranged in ascending or descending order
– SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC•SELECT authorID, firstName, lastName FROM Authors ORDER BY lastName ASC
– SELECT columnName1, columnName2, … FROM tableName ORDER BY column DESC•SELECT authorID, firstName, lastName FROM Authors ORDER BY lastName DESC
27
ORDER BY Clause
authorID firstName lastName 2 Paul Deitel 1 Harvey Deitel 6 Ted Lin 12 Jonathan Liperi 8 David McPhie 3 Tem Nieto 7 Praveen Sadhu 5 Sean Santry 4 Kate Steinbuhler 11 Ben Wiedermann 9 Cheryl Yaeger 10 Marina Zlatkina Fig. 22.17 Authors from table Authors in ascending order by lastName.
28
ORDER BY Clause
authorID firstName lastName 10 Marina Zlatkina 9 Cheryl Yaeger 11 Ben Wiedermann 4 Kate Steinbuhler 5 Sean Santry 7 Praveen Sadhu 3 Tem Nieto 8 David McPhie 12 Jonathan Liperi 6 Ted Lin 2 Paul Deitel 1 Harvey Deitel Fig. 22.18 Authors from table Authors in descending order by
lastName.
29
ORDER BY ClauseauthorID firstName lastName 1 Harvey Deitel 2 Paul Deitel 6 Ted Lin 12 Jonathan Liperi 8 David McPhie 3 Tem Nieto 7 Praveen Sadhu 5 Sean Santry 4 Kate Steinbuhler 11 Ben Wiedermann 9 Cheryl Yaeger 10 Marina Zlatkina Fig. 22.19 Authors from table Authors in ascending order by
lastName and by firstName.
• SELECT * FROM Authors
• ORDER BY lastName, FirstName
30
Merging Data from Multiple Tables
• Normalize databases– Ensure database does not store data redundantly
– SELECT columnName1, columnName2, … FROM table1 t1, table2 t2
WHERE t1.c1 = t2.c2
31
Example
SELECT T.title, T.isbn, T.copyright, P.publishername
FROM Titles T, Publishers P
WHERE T.publisherID = P.publisherID
ORDER BY T.title
32
Example
SELECT T.title, T.isbn, A.firstName, A.lastName, T.copyright, P.publishername
FROM Titles T, Publishers P, Authors A, AuthorISBN AI
WHERE T.publisherID = P.publisherIDAND A.authorID=AI.authorIDAND T.isbn = AI.isbnORDER BY T.title
33
Aggregate Functions
• These functions operate on the multiset of values of a column of a relation, and return a value
avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values
34
Aggregate Functions (Cont.)
• Find the average prices in Titles.
Find the number of Authors with different firstName.Find the number of Authors with different firstName.
Find the Find the numbernumber of tuples in the Authors. of tuples in the Authors.
select avg (price)from Titles
select count (*)from Authors
select count (distinct firstName)from Authors
35
Aggregate Functions (Cont.)
• Test MAX, MIN and SUM.
select MAX (price)from Titles
select MIN (price)from Titles
select SUM (price)from Titles
36
Update
• The UPDATE Command– To change one or more values of a row in a
table– UPDATE table_name
SET col_name1 = value1, … col_namen = valuen
WHERE col_name = value– The WHERE clause is the primary key of
the row to be updated
• Example– UPDATE Authors SET firstName='han'
WHERE authorID=1
37
Delete• The DELETE Command
• To delete one or more tuples in table
• DELETE FROM table_name
• WHERE col_name = value …
• The WHERE clause could specify more than one row of the table
• Example– DELETE FROM Authors Where authorID=‘1’
38
Transactions
• A transaction is a sequence of queries and update statements executed as a single unit– Transactions are started implicitly and terminated
by one of• commit work: makes all updates of the transaction
permanent in the database
• rollback work: undoes all updates performed by the transaction.
39
Transactions
• Motivating example– Transfer of money from one account to another
involves two steps:• Deduct from one account and credit to another
– If one steps succeeds and the other fails, database is in an inconsistent state
– Therefore, either both steps should succeed or neither should
40
Serialize Transaction
T1:
FIND A;
RA:=A-100;
UPD A;
FIND B;
RB:=B+100;
UPD B;
T2:
FIND A;
temp = A*0.1;
RA := A-temp;
UPD A;
FIND B;
RB:=B+temp;
UPD B;
41
Error running
T1:
FIND A;
RA:=A-100;
UPD A;
FIND B;
RB:=B+100;
UPD B;
T2:
FIND A;
temp = A*0.1;
RA := A-temp;
UPD A;
FIND B;
RB:=B+temp;
UPD B;
42
Serialize Transaction
T1:
FIND A;
A:=A-100;
UPD A;
FIND B;
B:=B+100;
UPD B;
T2:
FIND A;
temp = A*0.1;
A := A-temp;
UPD A;
FIND B;
B:=B+temp;
UPD B;
43
Error running
T1:
FIND A;
RA:=A-100;
UPD A;
FIND B;
RB:=B+100;
UPD B;
T2:
FIND A;
temp = A*0.1;
RA := A-temp;
UPD A;
FIND B;
RB:=B+temp;
UPD B;
44
Transactions (cont.)
• If any step of a transaction fails, all work done by the transaction can be undone by rollback work.
• Rollback of incomplete transactions is done automatically, in case of system failures
45
Transactions (Cont.)
• In most database systems, each SQL statement that executes successfully is automatically committed.
46
Begin Transaction
• Example– USE db04498– go
– delete from authors where authorid=13– go
– begin transaction– insert into authors values (13, 'han', 'weili')
– select * from authors where authorid = 13– go
47
Commit• USE db04498• go• delete from authors where authorid=13• go
• begin transaction• insert into authors values (13, 'han', 'weili')• go
• rollback• go• select * from authors where authorid = 13• go
48
Rollback• USE db04498• go
• delete from authors where authorid=13• go
• begin transaction• insert into authors values (13, 'han', 'weili')• go
• commit• go• select * from authors where authorid = 13• go
49
ACID in Transaction
• Atomicity
• Consistency
• Isolation
• Durability
50
MS SQL Server 2000
• Enterprise-Level DBMS
• Microsoft
• Use .sql to build DB