Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Web Application Introduction to Data Base.

Similar presentations


Presentation on theme: "1 Introduction to Web Application Introduction to Data Base."— Presentation transcript:

1 1 Introduction to Web Application Introduction to Data Base

2 2 Topics Basic Concepts of Data Base SQL Syntax Transaction MS SQL Server 2000

3 3 Basic Concepts of Data Base What is Data Base What is DBMS? DB Schema Table View Index Primary Key Foreign Key

4 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 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 6 Relational Database Model

7 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 8 Relational Database Overview

9 9

10 10 Relational Database Overview

11 11 Relational Database Overview

12 12 Relational Database Overview

13 13 Relational Database Overview

14 14 Relational Database Overview

15 15 SQL Syntax CREATE DROP INSERT SELECT DELETE UPDATE

16 16 Create Database CREATE DATABASE dbname USE dbname –CREATE DATABASE db04498 –go –USE db04498 –go

17 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 18 Create Table Construct An SQL relation is defined using the create table command: create table name (A 1 D 1, A 2 D 2,..., A n D n, (integrity-constraint 1 ),..., (integrity-constraint k )) –Name is the name of the table –each A i is an attribute name in the schema of relation r –D i is the data type of values in the domain of attribute A i

19 19 Example: CREATE TABLE Authors( authorID int primary key, firstName varchar (32), lastName varchar (64))

20 20 Integrity Constraints in Create Table not null primary key (A 1,..., A n ) 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 21 Foreign Key Example CREATE TABLE Publishers( publisherID int primary key, publisherName varchar (64) ) CREATE TABLE Titles ( isbnchar (16) primary key, titlechar (64), editionNumbervarchar(8), copyrightint, descriptionvarchar(128), publisherIDint references Publishers(publisherID), imageFilevarchar (256), pricefloat)

22 22 Drop Table DROP TABLE name Example –DROP TABLE authors –go

23 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 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 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 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 27 ORDER BY Clause

28 28 ORDER BY Clause

29 29 ORDER BY Clause SELECT * FROM Authors ORDER BY lastName, FirstName

30 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 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 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.publisherID AND A.authorID=AI.authorID AND T.isbn = AI.isbn ORDER BY T.title

33 33 Aggregate Functions These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

34 34 Aggregate Functions (Cont.) Find the average prices in Titles.  Find the number of Authors with different firstName.  Find the number of tuples in the Authors. select avg (price) from Titles select count (*) from Authors select count (distinct firstName) from Authors

35 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 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 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 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 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 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 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 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 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 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 45 Transactions (Cont.) In most database systems, each SQL statement that executes successfully is automatically committed.

46 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 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 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 49 ACID in Transaction Atomicity Consistency Isolation Durability

50 50 MS SQL Server 2000 Enterprise-Level DBMS Microsoft Use.sql to build DB


Download ppt "1 Introduction to Web Application Introduction to Data Base."

Similar presentations


Ads by Google