Download presentation
Presentation is loading. Please wait.
1
Database: Review Sept. 2009Yangjun Chen ACS-39021 Database Introduction system architecture, Basic concepts, ER-model, Data modeling, B+-tree Hashing Relational algebra, Relational data model SQL: DDL, DML not included
2
Database: Review Sept. 2009Yangjun Chen ACS-39022 Introduction to the database systems What is a database? The main characters of a database The basic database design method The entity-relationship data model for application modeling
3
Database: Review Sept. 2009Yangjun Chen ACS-39023 The main characteristics of the database approach: single repository of data sharable by multiple users concurrency control and transaction concept security and integrity constraints self-describing - system catalogue contains meta data program-data independence some changes to the database are transparent to programs/users multiple views of data - to support individual needs of programs/users
4
Database: Review Sept. 2009Yangjun Chen ACS-39024 Data modeling using ER-model Entity-relationship model -Entity types -strong entities -weak entities -Relationships among entities -Attributes - attribute classification -Constraints -cardinality constraints -participation constraints ER-to-Relation-mapping
5
Database: Review Sept. 2009Yangjun Chen ACS-39025 employee department project dependent ER-model: works for manages works on dependents of controls supervision bdate ssn name lname minit fname sex address salary birthdatenamesex relationship name numberlocation name number location number of employees startdate hours 1 1 1 N supervisorsupervisee N M N 1 M N1 M
6
Database: Review Sept. 2009Yangjun Chen ACS-39026 Database schema, Schema evolution, Database state Working process with a database system Database system architecture Data independence concept Concepts and Architecture
7
Database: Review Sept. 2009Yangjun Chen ACS-39027 Database schema Relation schema Schema evolution Database state Student Name StNo Class Major Smith 17 1 CS Brown 8 2 CS Course CName CNo CrHrs Dept Database 8803 3 CS C 2606 3 CS Section SId CNo Semester Yr Instructor 32 8803 Spring 2000 Smith 25 8803 Winter 2000 Smith 43 2606 Spring 2000 Jones Grades StNo Sid Grade 17 25 A 17 43 B
8
Database: Review Sept. 2009Yangjun Chen ACS-39028 Working process with a database system: Definition record structure data elements names data types constraints etc Construction create database files populate the database with records Manipulation querying updating
9
Database: Review Sept. 2009Yangjun Chen ACS-39029 Database Management System (DBMS) collection of software facilitating the definition, construction and manipulation of databases Users/ actors Request manager Storage manager, Query evaluation Meta data Stored database DBMS
10
Database: Review Sept. 2009Yangjun Chen ACS-390210 Three-schema architecture External view External view Conceptual schema Internal schema Physical storage structures and details Describes the whole database for all users A specific user or groups view of the database
11
Database: Review Sept. 2009Yangjun Chen ACS-390211 external hashing static hashing & dynamic hashing hash function mathematical function that maps a key to a bucket address collisions collision resolution scheme - open addressing - chaining - multiple hashing linear hashing Hashing technique
12
Database: Review Sept. 2009Yangjun Chen ACS-390212 External hashing: the data are on the disk. Static hashing: using a hashing function to map keys to bucket addresses primary area can not be changed collision resolusion scheme: open addressing chaining multiple hashing Dynamic hashing: primary area can be changed linear hashing
13
Database: Review Sept. 2009Yangjun Chen ACS-390213 Linear hashing: 1.What is a phase? 2.When to split a bucket? 3.How to split a bucket? 4.What bucket will be chosen to split next? 5.How do we find a record inserted into a linear hashing file?
14
Database: Review Sept. 2009Yangjun Chen ACS-390214 Linear hashing: initially hash file contains M buckets h i = key mod (2 i M) (i = 0, 1, 2,...) insertion process can be divided into several phases phase 1: insertion using h 0 = key mod M splitting using h 1 = key mod (2 M) splitting rule: overflow of a bucket or if load factor > constant (e.g., 0.70) overflow will be put in the overflow area or redistributed through splitting a bucket splitting buckets from n = 0 to n = M- 1 (after each splitting n is increased by 1. Phase 1 finishes when n = M (in this case, the primary area becomes 2 M buckets long)
15
Database: Review Sept. 2009Yangjun Chen ACS-390215 phase 2: insertion using h 1 = key mod (2 M) splitting using h 2 = key mod (4 M) splitting rule: overflow of a bucket or if load factor > constant (e.g., 0.70) overflow will be put in the overflow area or redistributed through splitting a bucket splitting buckets from n = 0 to n = 2 M- 1 (after each splitting n is increased by 1. Phase 1 finishes when n = 2 M (in this case, the primary area will contain 4 M buckets.) phase 3:... … h 2 = …, h 3 = …,...
16
Database: Review Sept. 2009Yangjun Chen ACS-390216 Linear Hashing including two Phases: - collision resolution strategy: chaining -split rule: load factor > 0.7 -initially M = 4 (M: size of the primary area) -hash functions: h i (key) = key mod 2 i M (i = 0, 1, 2, …) -bucket capacity = 2 Trace the insertion process of the following keys into a linear hashing file: 3, 2, 4, 1, 8, 14, 5, 10, 7, 24, 17, 13, 15.
17
Database: Review Sept. 2009Yangjun Chen ACS-390217 The first phase – phase 0 when inserting the sixth record we would have but the load factor 6/8= 0.75 > 0.70 and so bucket 0 must be split (using h 1 = Key mod 2M): 4848 1 2 14 0 1 2 3 3 0 1 2 3 4 81 2 14 34 n=0 before the split (n is the point to the bucket to be split.) n=1 after the split load factor: 6/10=0.6 no split
18
Database: Review Sept. 2009Yangjun Chen ACS-390218 0 1 2 3 4 81 2 14 34 n=1 load factor: 7/10=0.7 no split insert(5) 8 1515 2 14 34 0 1 2 3 4
19
Database: Review Sept. 2009Yangjun Chen ACS-390219 0 1 2 3 4 8 1515 2 14 34 n=1 load factor: 8/10=0.8 split using h 1. insert(10) 8 1515 2 14 34 10 overflow
20
Database: Review Sept. 2009Yangjun Chen ACS-390220 0 1 2 3 4 5 81 2 14 34 n=2 load factor: 8/12=0.66 no split 10 overflow 5
21
Database: Review Sept. 2009Yangjun Chen ACS-390221 n=2 load factor: 9/12=0.75 split using h 1. 81 2 14 34 10 overflow 5 0 1 2 3 4 5 81 2 14 3737 4 10 overflow 5 insert(7)
22
Database: Review Sept. 2009Yangjun Chen ACS-390222 n=3 load factor: 9/14=0.642 no split. 81 2 10 3737 4514 81 2 10 3737 4514 insert(24)
23
Database: Review Sept. 2009Yangjun Chen ACS-390223 n=3 load factor: 10/14=0.71 split using h 1. 8 24 1 2 10 3737 4514 8 24 1 2 10 345147
24
Database: Review Sept. 2009Yangjun Chen ACS-390224 n=4 8 24 1 2 10 345147 The second phase – phase 1 8 24 1 2 10 345147 n = 0; using h 1 = Key mod 2M to insert and h 2 = Key mod 4M to split. insert(17)
25
Database: Review Sept. 2009Yangjun Chen ACS-390225 n=4 8 24 1 2 10 345147 The second phase – phase 1 8 24 1 2 10 345147 n = 0; using h 1 = Key mod 2M to insert and h 2 = Key mod 4M to split. insert(17)
26
Database: Review Sept. 2009Yangjun Chen ACS-390226 8 24 1 17 2 10 345147 n=0 load factor: 11/16=0.687 no split. 8 24 1 17 2 10 345147 insert(13)
27
Database: Review Sept. 2009Yangjun Chen ACS-390227 8 24 1 17 2 10 34 5 13 147 n=0 load factor: 12/16=0.75 split bucket 0, using h 2. 1 17 2 10 34 5 13 147 8 24
28
Database: Review Sept. 2009Yangjun Chen ACS-390228 n=1 load factor: 13/18=0.722 split bucket 1, using h 2. 1 17 2 10 34 5 13 147 8 24 insert(15) 1 17 2 10 34 5 13 14 7 15 8 24 1 17 2 10 34 5 13 14 7 15 8 24
29
Database: Review Sept. 2009Yangjun Chen ACS-390229 tree - root, internal, leaf, subtree - parent, child, sibling balanced, unbalanced b + -tree - splits on overflow; merge on underflow - in practice it is usually 3 or 4 levels deep search, insert, delete algorithms Multi-level index
30
Database: Review Sept. 2009Yangjun Chen ACS-390230 B + -tree Structure non-leaf node (internal node or a root) (q p internal ) K 1 < K 2 <... < K q-1 (i.e. it’s an ordered set) For any key value, X, in the subtree pointed to by P i K i-1 < X K i for 1 < i < q X K 1 for i = 1 K q-1 < X for i = q Each internal node has at most p internal pointers. Each node except root must have at least p internal /2 pointers. The root, if it has some children, must have at least 2 pointers.
31
Database: Review Sept. 2009Yangjun Chen ACS-390231 B + -tree Structure leaf node (terminal node) K 1 < K 2 <... < K q-1 Pr i points to a record with key value K i, or Pr i points to a page containing a record with key value K i. Maximum of p leaf key/pointer pairs. Each leaf has at least p leaf /2 keys. All leaves are at the same level (balanced). P next points to the next leaf node for key sequencing.
32
Database: Review Sept. 2009Yangjun Chen ACS-390232 A B + -tree 5 37 8 679125813 Records in a file p internal = 3, p leaf = 2.
33
Database: Review Sept. 2009Yangjun Chen ACS-390233 B+-tree insertion: leaf node splitting, internal node splitting Leaf splitting When a leaf splits, a new leaf is allocated the original leaf is the left sibling, the new one is the right sibling key and pointer pairs are redistributed: the left sibling will have smaller keys than the right sibling a 'copy' of the key value which is the largest of the keys in the left sibling is promoted to the parent 33 12 22 3344 48 5512 2244 48 5531 33 22 33 insert 31
34
Database: Review Sept. 2009Yangjun Chen ACS-390234 Internal node splitting If an internal node splits and it is not the root, insert the key and pointer and then determine the middle key a new 'right' sibling is allocated everything to its left stays in the left sibling everything to its right goes into the right sibling the middle key value along with the pointer to the new right sibling is promoted to the parent (the middle key value 'moves' to the parent to become the discriminator between this left and right sibling) 22 33 55 22 26 55 Insert 26 33
35
Database: Review Sept. 2009Yangjun Chen ACS-390235 Internal node splitting When a new root is formed, a key value and two pointers must be placed into it. 26 55 Insert 40 26 55 40
36
Database: Review Sept. 2009Yangjun Chen ACS-390236 Deleting nodes from a B+-tree: 1. When deleting a key from a node A, check whether the number of the remaining keys (or pointers) is p/2 . 2. If it is not the case, redistribute the keys in the left sibling B or in the right sibling C if it is possible. Otherwise, merge A and B or merge A and C. 3.When redistributing or merging, change the key values in the parent node so that the following condition is satisfied: K 1 < K 2 <... < K q-1 (i.e. it is an ordered set) for the key values, X, in the subtree pointed to by P i K i-1 < X <= K i for 1 < i < q X <= K 1 for i = 1 K q-1 < X for i = q
37
Database: Review Sept. 2009Yangjun Chen ACS-390237 A b + -tree 5 37 8 67 91258 13 Records p internal = 3, p leaf = 2.
38
Database: Review Sept. 2009Yangjun Chen ACS-390238 Entry deletion - deletion sequence: 8, 12, 9, 7 5 37 9 67 12 59 13 Deleting 8 causes the node redistribute.
39
Database: Review Sept. 2009Yangjun Chen ACS-390239 Entry deletion - deletion sequence: 8, 12, 9, 7 5 37 67 59 13 12 is removed.
40
Database: Review Sept. 2009Yangjun Chen ACS-390240 Entry deletion - deletion sequence: 8, 12, 9, 7 5 36 657 13 9 is removed.
41
Database: Review Sept. 2009Yangjun Chen ACS-390241 Entry deletion - deletion sequence: 8, 12, 9, 7 5 36 65 13 Deleting 7 makes this pointer no use. Therefore, a merge at the level above the leaf level occurs.
42
Database: Review Sept. 2009Yangjun Chen ACS-390242 Entry deletion - deletion sequence: 8, 12, 9, 7 53 For this merge, 5 will be taken as a key value in A since any key value in B is less than or equal to 5 but any key value in C is larger than 5. 65 13 5 A B C 5 This point becomes useless. The corresponding node should also be removed.
43
Database: Review Sept. 2009Yangjun Chen ACS-390243 Entry deletion - deletion sequence: 8, 12, 9, 7 6513 53 5
44
Database: Review Sept. 2009Yangjun Chen ACS-390244 Data modeling using Relational model Relational algebra Relational Data Model -relation schema, relations -database schema (relational schema), database state -integrity constraints and updating Relational algebra -select, project, join, cartesian product -division -set operations: union, intersection, difference,
45
Database: Review Sept. 2009Yangjun Chen ACS-390245 Integrity Constraints any database will have some number of constraints that must be applied to ensure correct data (valid states) 1. domain constraints a domain is a restriction on the set of valid values domain constraints specify that the value of each attribute A must be an atomic value from the domain dom(A). 2. key constraints a superkey is any combination of attributes that uniquely identify a tuple: t 1 [superkey] t 2 [superkey]. -Example: (in Employee) a key is superkey that has a minimal set of attributes -Example: (in Employee)
46
Database: Review Sept. 2009Yangjun Chen ACS-390246 Integrity Constraints If a relation schema has more than one key, each of them is called a candidate key. one candidate key is chosen as the primary key (PK) foreign key (FK) is defined as follows: i)Consider two relation schemas R 1 and R 2 ; ii) The attributes in FK in R 1 have the same domain(s) as the primary key attributes PK in R 2 ; the attributes FK are said to reference or refer to the relation R 2 ; iii) A value of FK in a tuple t 1 of the current state r(R 1 ) either occurs as a value of PK for some tuple t 2 in the current state r(R 2 ) or is null. In the former case, we have t 1 [FK] = t 2 [PK], and we say that the tuple t 1 references or refers to the tuple t 2. Example: Employee(SSN, …, Dno)Dept(Dno, … ) FK
47
Database: Review Sept. 2009Yangjun Chen ACS-390247 Integrity Constraints 3. entity integrity no part of a PK can be null 4. referential integrity domain of FK must be same as domain of PK FK must be null or have a value that appears as a PK value 5. semantic integrity other rules that the application domain requires: state constraint: gross salary > net income transition constraint: Widowed can only follow Married; salary of an employee cannot decrease
48
Database: Review Sept. 2009Yangjun Chen ACS-390248 Updating and constraints insert Insert the following tuple into EMPLOYEE: When inserting, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity update Update the SALARY of the EMPLOYEE tuple with ssn = ‘999887777’ to 30000. When updating, the integrity constraints should be checked: domain, key, entity, referential, semantic integrity
49
Database: Review Sept. 2009Yangjun Chen ACS-390249 Updating and constraints delete Delete the WORK_ON tuple with Essn = ‘999887777’ and pno = 10. When deleting, the referential constraint will be checked. -The following deletion is not acceptable: Delete the EMPLOYEE tuple with ssn = ‘999887777’ - reject, cascade, modify
50
Database: Review Sept. 2009Yangjun Chen ACS-390250 cascade – a strategy to enforce referential integrity ssn Employee Essn Pno delete Works-on delete
51
Database: Review Sept. 2009Yangjun Chen ACS-390251 cascade – a strategy to enforce referential integrity Employee delete ssn supervisor null Employee delete ssn supervisor null delete not reasonable
52
Database: Review Sept. 2009Yangjun Chen ACS-390252 Modify – a strategy to enforce referential integrity ssn Employee Essn Pno delete Essn Pno null This violates the entity constraint. Works-on
53
Database: Review Sept. 2009Yangjun Chen ACS-390253 Relational Algebra a set of relations a set of operations set operations relation specific select project join division union intersection difference cartesian product
54
Database: Review Sept. 2009Yangjun Chen ACS-390254 Relational algebra Retrieve for each female employee a list of the names of her dependents: FEMALE_EMPS SEX = ‘F’ (EMPLOYEE) ACTUAL_DEPENDENTS EMPNAMES EMPNAMES FNAME,LNAME, SSN (FEMALE_EMPS) RESULT FNAME, LNAME, DEPENDENT_NAME (ACTUAL_DEPENDENTS ) DEPENDENT SSN = ESSN
55
Database: Review Sept. 2009Yangjun Chen ACS-390255 Query: Retrieve the name of employees who work on all the projects that ‘John Smith’ works on. SMITH FNAME = ‘John’ and LNAME = ‘Smith’ (EMPLOYEE) SMITH_PNOs PNO (WORK_ON ESSN = SSN SMITH) SSN_PNO ESSN,PNO (WORK_ON) SSNS(SSN) SSN_PNO : SMITH_PNOs RESULT FNAME, LNAME (SSNS * EMPLOYEE)
56
Database: Review Sept. 2009Yangjun Chen ACS-390256 Division The DIVISION operator can be expressed as a sequence of , , and - operations as follows: Z = {A 1, …, A n, B 1, …, B m }, X = {B 1, …, B m }, Y = Z - X = {A 1, …, A n }, R(Z) S(X) : T 1 Y ( R) T 2 Y ((S T 1 ) - R) T T 1 - T 2 result
57
Database: Review Sept. 2009Yangjun Chen ACS-390257 DDL - creating schemas - modifying schemas DML - select-from-where clause - group by, having, order by - update - view SQL
58
Database: Review Sept. 2009Yangjun Chen ACS-390258 DDL - Examples: Create schema: Create schema COMPANY authorization JSMITH; Create table: Create table EMPLOYEE (FNAMEVARCHAR(15)NOT NULL, MINITCHAR, LNAMEVARCHAR(15)NOT NULL, SSNCHAR(9)NOT NULL, BDATEDATE, ADDRESSVARCHAR(30), SEXCHAR, SALARYDECIMAL(10, 2), SUPERSSNCHAR(9), DNOINTNOT NULL, PRIMARY KEY(SSN), FOREIGN KEY(SUPERSSN) REFERENCES EMPLOYEE(SSN), FOREIGN KEY(DNO) REFERENCES DEPARTMENT(DNUMBER));
59
Database: Review Sept. 2009Yangjun Chen ACS-390259 DDL - Examples: drop schema DROP SCHEMA CAMPANY CASCADE; DROP SCHEMA CAMPANY RESTRICT; drop table DROP TABLE DEPENDENT CASCADE; DROP TABLE DEPENDENT RESTRICT; alter table ALTER TABLE COMPANY.EMPLOYEE ADD JOB VARCHAR(12); ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS CASCADE;
60
Database: Review Sept. 2009Yangjun Chen ACS-390260 DML - select-from-where clause Retrieve a list of employees and the projects they are working on, ordered by department, within each department, ordered alphabetically by last name, first name: SELECTDNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHEREDNUMBER = DNO AND SSN = ESSN AND PNO = PNUMBER ORDER BY DNAME, LNAME, FNAME
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.