Teacher Workshop Database Design Pearson Education © 2014.

Slides:



Advertisements
Similar presentations
CSC271 Database Systems Lecture # 11.
Advertisements

Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
CSC271 Database Systems Lecture # 13. Summary: Previous Lecture  Grouping through GROUP BY clause  Restricted groupings  Subqueries  Multi-Table queries.
1 Minggu 4, Pertemuan 8 SQL: Data Manipulation (Cont.) Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Lesson II The Relational Model © Pearson Education Limited 1995, 2005.
Chapter 6 SQL: Data Manipulation. 2 Objectives of SQL u Database language should allow user to: –create database and relation structures –perform insertion,
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
1 Minggu 4, Pertemuan 7 SQL: Data Manipulation Matakuliah: T0206-Sistem Basisdata Tahun: 2005 Versi: 1.0/0.0.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
LOGICAL DATABASE DESIGN
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Agenda TMA01 M876 Block 3 – Using SQL Structured Query Language - SQL A non-procedural language to –Create database and relation structures. –Perform.
Data Manipulation Using MySQL tMyn1 Data Manipulation Using MySQL Ideally, a database language should allow a user to: –Create the database and relation.
SQL: Overview SQL Overview © Pearson Education Limited 1995, 2005.
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
10/15/2012ISC239 Isabelle Bichindaritz1 SQL Queries.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
Chapter 7 SQL: Data Manipulation Chapter#6 in the text book Pearson Education © 2009.
Chapter 6 SQL: Data Manipulation (Advanced Commands) Pearson Education © 2009.
Chapter 9 Methodology - Logical Database Design Chapter 16 in Textbook.
Chapter 7 Introduction to SQL(Wk11_12) © Pearson Education Limited 1995, 2005.
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
SQL Data Manipulation II Chapter 5 CIS 458 Sungchul Hong.
Chapter 5 SQL Data Manipulation Language Chapter 5 in Textbook.
Chapter 5 SQL: Data Manipulation © Pearson Education Limited 1995, 2005.
Bayu Adhi Tama, ST., MTI. Introduction Relational algebra and relational calculus are formal languages associated with the relational.
Advanced Database Systems
1 Pertemuan > > Matakuliah: >/ > Tahun: > Versi: >
1 E-R Model (II) Keys To identify records in a table Candidate Key Primary Key Alternate Key Composite Key.
Chapter 7 SQL: Data Manipulation Chapter #6 in the textbook Pearson Education © 2009.
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2009.
IST 210 SQL Todd Bacastow IST 210: Organization of Data.
Chapter 8 Introduction to SQL
Chapter 5 Relational Algebra Pearson Education © 2014.
CSC271 Database Systems Lecture # 8. Summary: Previous Lecture  Relation algebra and operations  Selection (Restriction), projection  Union, set difference,
SQL : Data Manipulation Pertemuan 09 s/d 12 Matakuliah: M0564 /Pengantar Sistem Basis Data Tahun : 2008.
Modelling Methodologies Chapter 16, 17, 18. Modeling Methodologies2 Database Design Physical DB design Logical DB design Conceptual DB design Hardware.
IST 210 More SQL Todd Bacastow IST 210: Organization of Data.
Chapter 6 SQL – Data Manipulation Pearson Education © 2014.
CSCI 6315 Applied Database Systems Review for Midterm Exam I Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Chapter 5 Relational Algebra and Relational Calculus Pearson Education © 2014.
SQL: Data Manipulation. Objectives Describe the purpose and importance of SQL. Demonstrate how to retrieve data from database using SELECT and: ▫Use compound.
SQL: Additional Notes. 2 Example 5.3 Use of DISTINCT List the property numbers of all properties that have been viewed. SELECT propertyNo FROM Viewing;
Chapter 6 SQL: Data Manipulation Pearson Education © 2009.
Lecture 11 SQL. Agenda SQL Select statement WHERE clause BindingSource filtering.
Chapter 11 SQL: Data Manipulation
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
Tahun : <<2007>> Versi : <<2/1>>
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Data Manipulation Language
CS311 Database Management system
SQL – Data Manipulation
Relational Algebra and Relational Calculus
Introduction to SQL Chapter 3.
The Relational Database Model
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Chapter Name SQL: Data Manipulation Chapter #6 in the textbook
Conceptual Database Design
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation
SQL – Data Manipulation
Chapter Name SQL: Data Manipulation
Chapter Name SQL: Data Manipulation Transparencies JOINS
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation Transparencies
Chapter Name SQL: Data Manipulation
Presentation transcript:

Teacher Workshop Database Design Pearson Education © 2014

File-Based System Approach Image source: http://afis.ucc.ie/gkiely/IS2206/DDBMS_Diagrams.htm

Database Approach Image source: http://afis.ucc.ie/gkiely/IS2206/DDBMS_Diagrams.htm

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

Database Management System (DBMS) Pearson Education © 2014

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

Components of DBMS Environment Pearson Education © 2014

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

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

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

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

Relational Data Model Pearson Education © 2014 12

Network Data Model Pearson Education © 2014 13

Hierarchical Data Model Pearson Education © 2014 14

Instances of Branch and Staff Relations Pearson Education © 2014 15

Alternative Terminology for Relational Model Pearson Education © 2014 16

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

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

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

ER diagram of Branch Has Staff relationship

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

ER diagram of Staff and Branch entities and their attributes

Multiplicity of Staff Manages Branch (1:1) relationship

Semantic net of Staff Oversees PropertyForRent relationship type

Multiplicity of ternary Registers relationship

Summary of multiplicity constraints

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?

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

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

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

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

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

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

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

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

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

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 20000 AND 30000; BETWEEN test includes the endpoints of range. 37 Pearson Education © 2014

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

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

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

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

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

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

Example 6.17 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

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

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

Example 6.24 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

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

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

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‘1957-05-25’, 8300, ‘B003’); 50 Pearson Education © 2014

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

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