ERM to SQL. Abstraction Layers Conceptual –What data is held An Image and its meta-data Entity-Relationship model (ERM) Logical –How data is organised.

Slides:



Advertisements
Similar presentations
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
Advertisements

BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Bogdan Shishedjiev SQL1 SQL Reminder of SQL. Bogdan Shishedjiev SQL 2 Subsets of language Data definition language (DDL) –Domain definition –Schema definition.
SQL zSeQueL zSQL zSQL ybetter support for Algebraic operations zSQL Post-Relational yrow and column types, stored procedures, triggers,
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
Conceptual Modelling Entity Relationship Model Overview Entities, Attributes and Relationship modelling Generating a Relational Database for an EAR model.
Relational Database. Converting Model to target implementation QSEE will produce 3 types of schema –XML document DTD XML schema –SQL Relational Database.
Data Management Design
RELATIONSHIP  THE WAY TABLES ARE RELATED  A TABLE MUST PARTICIPATE IN AT LEAST ONE RELATIONSHIP  IN A BINARY RELATIONSHIP TWO ENTITIES PARTICIPATE 
Data Modelling. EAR model This modelling language allows a very small vocabulary: Just as English has nouns, verbs, adjectives, pronouns.., EAR models.
Physical, Logical, Conceptual DSA Lecture
Physical, Logical, Conceptual DSA Lecture
Lecture 4: Introduction to PHP 3 PHP & MySQL
Logical Layer The Relational Data Model + SQL. Abstraction Layers zConceptual yWhat data is held xAn Image and its meta-data xEntity-Relationship model.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
OO - Lecture 4 Tutorial Review Associations Inheritance of Functions Polymorphism.
Working with SQL and PL/SQL/ Session 1 / 1 of 27 SQL Server Architecture.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
Using ER/Studio.
Oracle Data Definition Language (DDL)
Databases From A to Boyce Codd. What is a database? It depends on your point of view. For Manovich, a database is a means of structuring information in.
Web Application Development. Define ER model in QSEE Generate SQL Create Database mySQL Write Script to use TableEditor class Process to create A simple.
Web Application Development. Tools to create a simple web- editable database QSEE MySQL (or PHPMyAdmin) PHP TableEditor.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
Irwin/McGraw-Hill Copyright © 2000 The McGraw-Hill Companies. All Rights reserved Whitten Bentley DittmanSYSTEMS ANALYSIS AND DESIGN METHODS5th Edition.
Database Technical Session By: Prof. Adarsh Patel.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen M:N Relationships & Bridge Classes These.
Eurotrace Hands-On The Eurotrace File System. 2 The Eurotrace file system Under MS ACCESS EUROTRACE generates several different files when you create.
Lecture 8 Database Theory & Practice (2) : The Relational Data Model UFCEKG-20-2 Data, Schemas & Applications.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
Lecture2: Database Environment Prepared by L. Nouf Almujally 1 Ref. Chapter2 Lecture2.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
DataBase Management System What is DBMS Purpose of DBMS Data Abstraction Data Definition Language Data Manipulation Language Data Models Data Keys Relationships.
Copyright 2006 Prentice-Hall, Inc. Essentials of Systems Analysis and Design Third Edition Joseph S. Valacich Joey F. George Jeffrey A. Hoffer Chapter.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
GLOBEX INFOTEK Copyright © 2013 Dr. Emelda Ntinglet-DavisSYSTEMS ANALYSIS AND DESIGN METHODSINTRODUCTORY SESSION EFFECTIVE DATABASE DESIGN for BEGINNERS.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Chapter 3: Relational Databases
11-1 © Prentice Hall, 2004 Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
Creating E/R Diagrams with SQL Server Management Studio, Writing SQL Queries D0ncho Minkov Telerik School Academy schoolacademy.telerik.com Technical Trainer.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
CompSci 280 S Introduction to Software Development
Fundamentals of DBMS Notes-1.
Relational Normalization Theory
Including Constraints
Chapter 4 Relational Databases
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Teaching slides Chapter 8.
Lecture 16 : The Relational Data Model
Lecture 16 : The Relational Data Model
Presentation transcript:

ERM to SQL

Abstraction Layers Conceptual –What data is held An Image and its meta-data Entity-Relationship model (ERM) Logical –How data is organised in storage Block and Directory structure Tables, keys Physical –How data is stored in bits JPEG as a stream of bytes A Database as files and records stored in a DBMS-specific format Abstraction Realisation (Refinement Reification) (Reverse Engineering) (Engineering, Model-Driven development

QSEE

Case Tool - QSEEQSEE is a free download for individual use is available in the lab (older version  ) enables a wide range of diagram types to be created –UML –Data flow diagrams … –Entity-relationship diagram With generation of SQL for creating a database (with some fixes  - use Perl script to clean up)

ER Model to RDBMS QSEE will produce 2 types of schema –XML DTD XML schema –SQL Relational Database with different targets: ORACLE MySQL Microsoft SQL server SQL 2 standard

RDBMS target Each entity becomes a table Each attribute becomes a column of a table Each relationship become common fields and values in two tables. –Two cases One-many Many-many

Primary Key An attribute which has a different value for every entity instance – ‘Unique’. May be a combination of several attributes – Composite Primary Key. Index on this attribute created by the database May need to be created if no attribute is unique and stable – Auto-increment. Primary keys are the glue of a relational database.

One-many relationship Each one-many relationship is implemented by adding the primary key of the entity on the one side (the parent) as new columns on the many (the child) side This added column is known as a ‘foreign key’ –So Image gets a new column called ‘memberid’ a foreign key constraint defines the relationship When an image is added to the database, the appropriate value of memberid for the creator is added to the image record (a common value)

Relationship in RDBMS M1 P4P5P1 M2 P3 P9 P7 M3 pkM1 pkM2null pkM2 MemberPhoto

Generated SQL DDL CREATE TABLE Member( memberidINTEGER NOT NULL, name VARCHAR(20), PRIMARY KEY (memberid) ); CREATE TABLE Photo( photoidINTEGER NOT NULL, image64MEDIUMBLOB, memberidINTEGER NOT NULL, PRIMARY KEY (photoid), FOREIGN KEY (memberid) references Member(memberid) );

Using a foreign key To find all the Photos belonging to member M2 Select * from photo where memberid=‘M2’ To find the creator of photo P1 Select member.name from photo natural join member where photoid=‘P1’

Foreign Key integrity What happens when you add a Photo –The memberid on the Photo record must reference an existing Member What happens when a member is deleted –Don’t allow it if she has any photos : ‘RESTRICT’ –Delete all her photos as well : ‘CASCADE’ –Set the memberid to NULL ALTER TABLE Photo ADD INDEX (memberid), ADD CONSTRAINT fk1_Photo_to_Member FOREIGN KEY(memberid) REFERENCES Member(memberid) ON DELETE RESTRICT ON UPDATE RESTRICT;

Many-many relationship

G1 P4P5P1 G5 P3P7 G2 pkG1 pkP1 pkG2 pkP1 pkG1 pkP4 pkG5 pkP4 pkG2 pkP5 pkG1 pkP7 GroupPhoto GroupPhoto GroupPhoto

Adding a link table CREATE TABLE Tag( tagNameVARCHAR(40) NOT NULL, PRIMARY KEY (tagName) ); CREATE TABLE Photo_Tag( photoidINTEGER NOT NULL, tagNameVARCHAR(40) NOT NULL, PRIMARY KEY (photoid,tagName), INDEX(photoid), FOREIGN KEY(photoid) REFERENCES Photo(photoid) ON DELETE RESTRICT ON UPDATE RESTRICT, INDEX(tagName), FOREIGN KEY(tagName) REFERENCES Tag(tagName) ON DELETE RESTRICT ON UPDATE RESTRICT );

QSEE tasks Handles the addition of foreign keys Creates link tables for many-many relationships So let QSEE do the work –If a primary key changes, the database definition will be updated automatically –Integrity Constraints generated for you

‘Weak entities’ Entities whose existence depends on the existence of one or more ‘parent’ entities’ –Typically this is a one-many relationship –If the parent is deleted, so are the children –The primary key of the parent becomes a column in the child (foreign key) AND this column is part of the primary of the CHILD –Denoted in the ERM by a bar across the relationship

Emp-dept is a bit more complicated

SQL DDL for emp-dept -- Create a Database table to represent the "Emp" entity. CREATE TABLE Emp( empnoINTEGER NOT NULL, enameVARCHAR(12), jobVARCHAR(12), hiredateDATE, salDECIMAL(8,2), commDECIMAL(8,2), deptnoINTEGER NOT NULL, mgr_empnoINTEGER NOT NULL, -- Specify the PRIMARY KEY constraint for table "Emp". -- This indicates which attribute(s) uniquely identify each row of data. CONSTRAINTpk_Emp PRIMARY KEY (empno) ); ALTER TABLE Emp ADD CONSTRAINT fk2_Emp_to_Emp FOREIGN KEY(mgr_empno) REFERENCES Emp(empno) ON DELETE RESTRICT ON UPDATE RESTRICT;

EmpnoEname 7369SMITH 7499ALLEN 7521WARD 7566JONES 7654MARTIN 7698BLAKE 7782CLARK 7788SCOTT 7839KING 7844TURNER 7876ADAMS 7900JAMES 7902FORD 7934MILLER Select Mgr from Emp where Sal between 2000 and 3000 Select Empno, Ename from Emp Find the names of the managers of employees who earn between 2000 and 3000 E M EmpnoMgrDeptno

Find the names of the managers of employees who earn between 2000 and 3000 Select Ename From Emp E, Emp M Where E.mgr=M.empno and E.sal between 2000 and 3000 Mgr Empno Ename KING Distinct(Ename)

Target is XML

Target XML Here the names of columns and entities are included in the data. The relationship between member and images is containment – all the images for a member occur immediately after the details of the member herself Same ER model but different implementations in different technologies.

Tutorial Generate a database for the KML folder/placemark/style Clean up the generated code –Remove Use statement Comments starting ---- –Use a text editor or if possible the Perl script Use PhpMyAdmin to create this database in your mySQL account