Presentation is loading. Please wait.

Presentation is loading. Please wait.

Teacher Workshop Database Design Pearson Education © 2014.

Similar presentations


Presentation on theme: "Teacher Workshop Database Design Pearson Education © 2014."— Presentation transcript:

1 Teacher Workshop Database Design Pearson Education © 2014

2 File-Based System Approach
Image source:

3 Database Approach Image source:

4 Database Shared collection of logically related data (and a description of this data) designed to meet the information needs of an organization System catalog (metadata) provides description of data to enable - Program data independence Logically related data comprises entities, attributes, and relationships of an organization’s information Pearson Education © 2014

5 Database Management System (DBMS)
Pearson Education © 2014

6 Database Approach Data definition language (DDL).
Permits specification of data types, structures and any data constraints. All specifications are stored in the database. Data manipulation language (DML). General enquiry facility (query language) of the data. Pearson Education © 2014

7 Components of DBMS Environment
Pearson Education © 2014

8 History of Database Systems
First-generation Hierarchical and Network Second generation Relational Third generation Object-Relational Object-Oriented Pearson Education © 2014

9 ANSI-SPARC Three-Level Architecture
Pearson Education © 2014 9

10 Differences between Three Levels of ANSI-SPARC Architecture
Pearson Education © 2014 10

11 Data Independence and the ANSI-SPARC Three-Level Architecture
Pearson Education © 2014 11

12 Relational Data Model Pearson Education © 2014 12

13 Network Data Model Pearson Education © 2014 13

14 Hierarchical Data Model
Pearson Education © 2014 14

15 Instances of Branch and Staff Relations
Pearson Education © 2014 15

16 Alternative Terminology for Relational Model
Pearson Education © 2014 16

17 Mathematical Definition of Relation
Consider three sets D1, D2, D3 with Cartesian Product D1 ´ D2 ´ D3; e.g. D1 = {1, 3} D2 = {2, 4} D3 = {5, 6} D1 ´ D2 ´ D3 = {(1,2,5), (1,2,6), (1,4,5), (1,4,6), (3,2,5), (3,2,6), (3,4,5), (3,4,6)} Any subset of these ordered triples is a relation. Pearson Education © 2014 17

18 Relational Keys Primary Key Alternate Keys Foreign Key
Candidate key selected to identify tuples uniquely within relation. Alternate Keys Candidate keys that are not selected to be primary key. Foreign Key Attribute, or set of attributes, within one relation that matches candidate key of some (possibly same) relation. Pearson Education © 2014 18

19 Integrity Constraints
Entity Integrity In a base relation, no attribute of a primary key can be null. Referential Integrity If foreign key exists in a relation, either foreign key value must match a candidate key value of some tuple in its home relation or foreign key value must be wholly null. Pearson Education © 2014 19

20 ER diagram of Branch Has Staff relationship

21 Attributes Attribute Attribute Domain
Property of an entity or a relationship type. Attribute Domain Set of allowable values for one or more attributes.

22 ER diagram of Staff and Branch entities and their attributes

23 Multiplicity of Staff Manages Branch (1:1) relationship

24 Semantic net of Staff Oversees PropertyForRent relationship type

25 Multiplicity of ternary Registers relationship

26 Summary of multiplicity constraints

27 Chapter 17 - Outline Build a logical data model (LDM) from a CDM
Validate LDM to ensure that It accords the data requirements It supports all the transactions Merge local LDMs into a global model Is the global LDM a true and accurate representation of the data requirements?

28 Objectives of SQL Consists of standard English words:
1) CREATE TABLE Staff(staffNo VARCHAR(5), lName VARCHAR(15), salary DECIMAL(7,2)); 2) INSERT INTO Staff VALUES (‘SG16’, ‘Brown’, 8300); 3) SELECT staffNo, lName, salary FROM Staff WHERE salary > 10000; 28 Pearson Education © 2014

29 SELECT Statement FROM Specifies table(s) to be used.
WHERE Filters rows. GROUP BY Forms groups of rows with same column value. HAVING Filters groups subject to some condition. SELECT Specifies which columns are to appear in output. ORDER BY Specifies the order of the output. 29 Pearson Education © 2014

30 Example 6.1 All Columns, All Rows
List full details of all staff. SELECT staffNo, fName, lName, address, position, sex, DOB, salary, branchNo FROM Staff; Can use * as an abbreviation for ‘all columns’: SELECT * 30 Pearson Education © 2014

31 Example 6.1 All Columns, All Rows
31 Pearson Education © 2014

32 Example 6.2 Specific Columns, All Rows
Produce a list of salaries for all staff, showing only staff number, first and last names, and salary. SELECT staffNo, fName, lName, salary FROM Staff; 32 Pearson Education © 2014

33 Example 6.2 Specific Columns, All Rows
33 Pearson Education © 2014

34 Example 6.4 Calculated Fields
Produce list of monthly salaries for all staff, showing staff number, first/last name, and salary. SELECT staffNo, fName, lName, salary/12 FROM Staff; 34 Pearson Education © 2014

35 Example 6.5 Comparison Search Condition
List all staff with a salary greater than 10,000. SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary > 10000; 35 Pearson Education © 2014

36 Example 6.6 Compound Comparison Search Condition
List addresses of all branch offices in London or Glasgow. SELECT * FROM Branch WHERE city = ‘London’ OR city = ‘Glasgow’; 36 Pearson Education © 2014

37 Example 6.7 Range Search Condition
List all staff with a salary between 20,000 and 30,000. SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary BETWEEN AND 30000; BETWEEN test includes the endpoints of range. 37 Pearson Education © 2014

38 Example 6.8 Set Membership
List all managers and supervisors. SELECT staffNo, fName, lName, position FROM Staff WHERE position IN (‘Manager’, ‘Supervisor’); 38 Pearson Education © 2014

39 Example 6.9 Pattern Matching
Find all owners with the string ‘Glasgow’ in their address. SELECT ownerNo, fName, lName, address, telNo FROM PrivateOwner WHERE address LIKE ‘%Glasgow%’; 39 Pearson Education © 2014

40 Example 6.10 NULL Search Condition
List details of all viewings on property PG4 where a comment has not been supplied. There are 2 viewings for property PG4, one with and one without a comment. Have to test for null explicitly using special keyword IS NULL: SELECT clientNo, viewDate FROM Viewing WHERE propertyNo = ‘PG4’ AND comment IS NULL; 40 Pearson Education © 2014

41 Example 6.11 Single Column Ordering
List salaries for all staff, arranged in descending order of salary. SELECT staffNo, fName, lName, salary FROM Staff ORDER BY salary DESC; 41 Pearson Education © 2014

42 SELECT Statement - Aggregates
ISO standard defines five aggregate functions: COUNT returns number of values in specified column. SUM returns sum of values in specified column. AVG returns average of values in specified column. MIN returns smallest value in specified column. MAX returns largest value in specified column. 42 Pearson Education © 2014

43 Example 6.16 Use of MIN, MAX, AVG
Find minimum, maximum, and average staff salary. SELECT MIN(salary) AS myMin, MAX(salary) AS myMax, AVG(salary) AS myAvg FROM Staff; Pearson Education © 2014

44 Example Use of GROUP BY Find number of staff in each branch and their total salaries. SELECT branchNo, COUNT(staffNo) AS myCount, SUM(salary) AS mySum FROM Staff GROUP BY branchNo ORDER BY branchNo; 44 Pearson Education © 2014

45 Example 6.19 Subquery with Equality
List staff who work in branch at ‘163 Main St’. SELECT staffNo, fName, lName, position FROM Staff WHERE branchNo = (SELECT branchNo FROM Branch WHERE street = ‘163 Main St’); 45 Pearson Education © 2014

46 Example 6.21 Nested subquery: use of IN
List properties handled by staff at ‘163 Main St’. SELECT propertyNo, street, city, postcode, type, rooms, rent FROM PropertyForRent WHERE staffNo IN (SELECT staffNo FROM Staff WHERE branchNo = (SELECT branchNo FROM Branch WHERE street = ‘163 Main St’)); 46 Pearson Education © 2014

47 Example Simple Join List names of all clients who have viewed a property along with any comment supplied. SELECT c.clientNo, fName, lName, propertyNo, comment FROM Client c, Viewing v WHERE c.clientNo = v.clientNo; 47 Pearson Education © 2014

48 Example 6.26 Three Table Join
For each branch, list staff who manage properties, including city in which branch is located and properties they manage. SELECT b.branchNo, b.city, s.staffNo, fName, lName, propertyNo FROM Branch b, Staff s, PropertyForRent p WHERE b.branchNo = s.branchNo AND s.staffNo = p.staffNo ORDER BY b.branchNo, s.staffNo, propertyNo; 48 Pearson Education © 2014

49 Example 6.26 Three Table Join
Alternative formulation for FROM and WHERE: FROM (Branch b JOIN Staff s USING branchNo) AS bs JOIN PropertyForRent p USING staffNo 49 Pearson Education © 2014

50 Example 6.35 INSERT … VALUES
Insert a new row into Staff table supplying data for all columns. INSERT INTO Staff VALUES (‘SG16’, ‘Alan’, ‘Brown’, ‘Assistant’, ‘M’, Date‘ ’, 8300, ‘B003’); 50 Pearson Education © 2014

51 Example 6.38/39 UPDATE All Rows
Give all staff a 3% pay increase. UPDATE Staff SET salary = salary*1.03; Give all Managers a 5% pay increase. SET salary = salary*1.05 WHERE position = ‘Manager’; 51 Pearson Education © 2014

52 Example 6.41/42 DELETE Specific Rows
Delete all viewings that relate to property PG4. DELETE FROM Viewing WHERE propertyNo = ‘PG4’; Delete all records from the Viewing table. DELETE FROM Viewing; 52 Pearson Education © 2014


Download ppt "Teacher Workshop Database Design Pearson Education © 2014."

Similar presentations


Ads by Google