LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

Similar documents
Database Systems ( 資料庫系統 )

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

The Relational Model 2. Week 3

The Relational Model. Chapter 3

Data Modeling. Yanlei Diao UMass Amherst. Slides Courtesy of R. Ramakrishnan and J. Gehrke

The Relational Model. Chapter 3. Database Management Systems, R. Ramakrishnan and J. Gehrke 1

The Relational Model. Why Study the Relational Model? Relational Database: Definitions

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Database Systems. Course Administration

The Relational Model. Outline. Why Study the Relational Model? Faloutsos SCS object-relational model

The Relational Model. Roadmap. Relational Database: Definitions. Why Study the Relational Model? Relational database: a set of relations

Relational Databases BORROWED WITH MINOR ADAPTATION FROM PROF. CHRISTOS FALOUTSOS, CMU /615

High-Level Database Models (ii)

Introduction to Data Management. Lecture #4 (E-R à Relational Design)

From ER to Relational Model. Book Chapter 3 (part 2 )

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

High Level Database Models

Database Management Systems. Chapter 3 Part 2

The Relational Model (ii)

Database Applications (15-415)

CIS 330: Applied Database Systems. ER to Relational Relational Algebra

The Relational Model

Why Study the Relational Model? The Relational Model. Relational Database: Definitions. The SQL Query Language. Relational Query Languages

ER to Relational Mapping. ER Model: Overview

Database Applications (15-415)

The Relational Model

Introduction to Data Management. Lecture #5 (E-R Relational, Cont.)

Integrity constraints, relationships. CS634 Lecture 2

The Entity-Relationship Model

CIS 330: Applied Database Systems

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

The Entity-Relationship Model. Overview of Database Design

Overview of db design Requirement analysis Data to be stored Applications to be built Operations (most frequent) subject to performance requirement

Database Applications (15-415)

CSE 530A. ER Model to Relational Schema. Washington University Fall 2013

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

Database Applications (15-415)

Introduction to Data Management. Lecture #6 E-Rà Relational Mapping (Cont.)

MIS Database Systems Entity-Relationship Model.

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

CSC 261/461 Database Systems Lecture 10

OVERVIEW OF DATABASE DEVELOPMENT

Introduction to Data Management. Lecture #5 Relational Model (Cont.) & E-Rà Relational Mapping

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

Comp 5311 Database Management Systems. 4b. Structured Query Language 3

Introduction to Database Design

Introduction to Data Management. Lecture #3 (E-R Design, Cont d.)

The Relational Model. Week 2

Introduction to Database Design

COMP Instructor: Dimitris Papadias WWW page:

CSE 530A. ER Model. Washington University Fall 2013

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

ER Model Overview. The Entity-Relationship Model. Database Design Process. ER Model Basics

Translating an ER Diagram to a Relational Schema

Introduction to Database Design. Dr. Kanda Runapongsa Dept of Computer Engineering Khon Kaen University

Introduction to Database Design

The Entity-Relationship Model

SQL DDL. CS3 Database Systems Weeks 4-5 SQL DDL Database design. Key Constraints. Inclusion Constraints

Conceptual Design. The Entity-Relationship (ER) Model

CIS 330: Web-driven Web Applications. Lecture 2: Introduction to ER Modeling

Database Management Systems. Session 2. Instructor: Vinnie Costa

Conceptual Design. The Entity-Relationship (ER) Model

Database Design & Deployment

Database Design. ER Model. Overview. Introduction to Database Design. UVic C SC 370. Database design can be divided in six major steps:

CS425 Midterm Exam Summer C 2012

CS/INFO 330 Entity-Relationship Modeling. Announcements. Goals of This Lecture. Mirek Riedewald

Database Systems. Lecture2:E-R model. Juan Huo( 霍娟 )

The Entity-Relationship Model. Overview of Database Design. ER Model Basics. (Ramakrishnan&Gehrke, Chapter 2)

CS 377 Database Systems

Overview. Introduction to Database Design. ER Model. Database Design

Part I: Structured Data

The Entity-Relationship (ER) Model

DATABASE MANAGEMENT SYSTEMS

DBMS. Relational Model. Module Title?

Contents. Database. Information Policy. C03. Entity Relationship Model WKU-IP-C03 Database / Entity Relationship Model

The Relational Model

The Entity-Relationship Model

The Relational Data Model. Data Model

CSC 261/461 Database Systems Lecture 8. Spring 2018

SYLLABUS ADMIN DATABASE SYSTEMS I WEEK 2 THE ENTITY-RELATIONSHIP MODEL. Assignment #2 changed. A2Q1 moved to A3Q1

Introduction to Data Management. Lecture #3 (Conceptual DB Design) Instructor: Chen Li

The Entity-Relationship Model. Steps in Database Design

The Relational Model of Data (ii)

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

COMP 244. ER-Diagram Notations. Entity-Relationship Diagrams DATABASE CONCEPTS & APPLICATIONS. Database Concepts & Applications 1.

CS W Introduction to Databases Spring Computer Science Department Columbia University

Relational data model

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Database Management Systems. Chapter 3 Part 1

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Relational Model. CS 377: Database Systems

Database Design CENG 351

CSIT5300: Advanced Database Systems

Introduction to Databases

The Entity-Relationship (ER) Model 2

01/01/2017. Chapter 5: The Relational Data Model and Relational Database Constraints: Outline. Chapter 5: Relational Database Constraints

Database Management Systems. Chapter 2 Part 2

The Relational Model. Database Management Systems

COMP 244 DATABASE CONCEPTS & APPLICATIONS

Review. The Relational Model. Glossary. Review. Data Models. Why Study the Relational Model? Why use a DBMS? OS provides RAM and disk

Transcription:

LAB 3 Notes The Relational Model Chapter 3 In the previous lab we discussed the Conceptual Database Design Phase and the ER Diagram. Today we will mainly discuss how to convert an ER model into the Relational model of a specific database. Outline ) Introduction discuss briefly the relational model 2) Logical Database Design (Steps involved ER model -> Relational model). 3) A glance at Views 4) Putting it all together An example using Postgres ) Introduction Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row, cols)) DDL (Data Definition Language) language for creating, manipulating and querying data in a relational DBMS. Domain, Primary key, Foreign Key constraints are fundamental part of the relational model. 2) Logical Database Design [ ER schema -> relational database schema ] ER schema is convenient for initial high-level description of the database design There is a standardized procedure to translate the ER diagram -> Relational schema (in fact software does it automatically) A relation schema can be thought of as the basic information describing a table or relation. This includes a set of column names, the data types associated with each column, and the name associated with the entire table. Formally:{ < f : d,, f n : d n > d DOM,, d n DOM n } For example, a relation schema for the relation called Students could be expressed using the following representation: Students(sid: string, name: string, login: string, age: integer, gpa: real) There are five fields or columns, with names and types as shown above. Domain is synonymous with data type. Attributes can be thought of as columns in a table. Therefore, an attribute domain refers to the data type associated with a column. A relation instance is a set of tuples (also known as rows or records) that each conform to the schema of the relation.

The relation cardinality is the number of tuples in the relation. The relation degree is the number of fields (or columns) in the relation. Entity Example: (, name, age); name age We use the Data Definition Language (syntax) of a DBMS to describe the generated Tables // notation used in book, (ingres, ansi) - CREATE TABLE ( CHAR() NOT NULL, name CHAR(30) NOT NULL, age Integer, - ALTER TABLE ADD Column surname:char(40) NOT NULL; // Adding to a relation - INSERT INTO (, name, age) VALUES ( 34342, Smith, 8); //Update tuple - UPDATE SET name= John where name= smith ; //delete tuples - DELETE WHERE name= John //Drop Table - DROP TABLE s;

Step 2: Relationships Constraints found in a relationship Key Constraint determined by the At-Most question Participation Constraint determined by the At-Least question 2a) WITHOUT PARTICIPATION CONSTRAINT M:N Relationships The relationship becomes a relation itself EMP_DEPT(,did) Also called an Entity-Relationship M works N Department did CREATE TABLE ( CHAR() NOT NULL, CREATE TABLE Department (did INTEGER NOT NULL, Primary Key(did)); CREATE TABLE Emp_Dept ( CHAR() NOT NULL, did INTEGER NOT NULL, Primary Key(, did), FOREIGN KEY () REFERENCES (), FOREIGN KEY (did) REFERENCES Department(did)); Generally: A A a a 2 a 3 B B b b 2 b 3 C a b :M Relationships Suppose that an manages N Departments but any department is managed only by at-most person. The key of the one Entity moves to the direction of M. (), Department(did, ) primary key foreign key

manages M Department did CREATE TABLE ( CHAR() NOT NULL, CREATE TABLE Department (did INTEGER NOT NULL, CHAR() Primary Key(did), FOREIGN KEY () REFERENCES ()); Generally: B A a a 2 a 3 b A B b b 2 b 3 : Relationships Suppose an employee works on at-most single-person project works Project Move the key to either direction BUT remember that you want to avoid nulls E.g. (, pid) Project(pid) May create too many nulls because each employee is not said to be working on ATleast project The participation constraint that will be described in a while will make it clear to which direction we should move the key () Project(pid, ) on the other hand has no nulls since every project belongs to some employee. 2b) WITH PARTICIPATION Constraints Participation Constraint The At-Least question (bold line) This will give you the complete picture since any ER should capture key and participation constraints

i) FIRST of all the participation constraint makes it clear in which direction to move the key in the case of a : relationship Again : Relationships Suppose an employee works on 0: projects and each project is worked on by : employees works Project Project PID EID EID Name Now we automatically know that the key moves to the direction of the bold line. (so we avoid nulls). What would happen if the participation constraint was from both sides the key goes to either direction ii) Enforcing referential integrity Suppose you are given the following example We already know what is happening with the keys. (Dept gets the key) manages M Department did ON [DELETE UPDATE] of the refereed tuple the DBMS should { CASCADE, SET NULL, RESTRICT=NO ACTION=default, SET DEFAULT} with the current relation a) RESTRICT=No ACTION=default CREATE TABLE Department (did INTEGER, CHAR() NOT NULL Primary Key() FOREIGN KEY () REFERENCES () ON DELETE NO ACTION; Means: ON DELETE of the refereed tuple in relation EMPLOYEE the DBMS should prohibit the delete of the given employee

2 3 Department did 2 2 3 3 If we want to delete employee i.e. Delete FROM where = ; System gives Illegal Action Violates Referential Integrity Constraint We can change the manager did= to did=2 and then try again e.g. UPDATE Department D SET D.=2 WHERE D.=; b) CASCADE CREATE TABLE Department (did INTEGER, CHAR() NOT NULL Primary Key() FOREIGN KEY () REFERENCES () ON DELETE CASCADE; Means: ON DELETE of the refereed tuple in relation EMPLOYEE the DBMS should cascade the delete. That means that the Department record should be deleted as well.!!!! c) SET NULL CREATE TABLE Department (did INTEGER, CHAR() NOT NULL Primary Key() FOREIGN KEY () REFERENCES () ON DELETE SET NULL; ILLEGAL since can t be NULL MIGHT violate PARTICIPATION constraint d) SET DEFAULT CREATE TABLE Department (did INTEGER, CHAR() NOT NULL DEFAULT 0 ; Primary Key() FOREIGN KEY () REFERENCES () ON DELETE SET DEFAULT; Means: ON DELETE of the refereed tuple in relation EMPLOYEE the DBMS should set in Department to the default value (i.e. 0)!!!!

Step 3: Weak Entities has> M dependent dname age CREATE TABLE Dependent ( dname CHAR() NOT NULL, CHAR() NOT NULL, age Integer, Primary Key(, dname) FOREIGN KEY () REFERENCES () ON DELETE CASCADE; // deletes dependent Step 4 : ISA Hierarchies Ssn, name, lot EMPLOYEE Hourly_wages, hours_worked ISA contractid Hourly_Emps Contract_Emps Inherit the key & create 3 relations. In order to find e.g. name of hourly_employees we need to combine EMPLOYEE, HOURLY_EMPS EMPLOYEE SSN Name Lot HOURLY EMPLOYEE H_SSN H_wages Hours CONTRECT EMPLOYEE C_SSN C_ID 3) VIEWS ) Useful for hiding information reducing degree CREATE TABLE ( CHAR() NOT NULL, Name CHAR(30) NOT NULL, PIN INTEGER NOT NULL,

CREATE VIEW Secure_(, name) AS SELECT E., E.name FROM E; 2) Useful for shrinking information reducing cardinality CREATE TABLE ( CHAR() NOT NULL, Name CHAR(30) NOT NULL, BIRTHCITY char(30) NOT NULL, CREATE VIEW Chicago_s(, name) AS SELECT E., E.name FROM E WHERE E. BIRTHCITY= Chicago ; Attention needs to be given on updating VIEWS, It is generally a bad idea but you can still do it (updatable views) MORE EXAMPLES GIVEN IN LAB AND OFFICE HOURS Putting it all together Consider the following ER diagram from last week: Construct a corresponding relational schema and database in Postgres. In additional, create a view of reports with letter grades equal to A.