Part I: Structured Data

Similar documents
Informatics 1: Data & Analysis

Informatics 1: Data & Analysis

Informatics 1: Data & Analysis

Informatics 1: Data & Analysis

Informatics 1: Data & Analysis

Informatics 1: Data & Analysis

Part I: Structured Data

The Relational Model

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

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

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

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

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

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

Database Management Systems. Chapter 3 Part 2

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

The Relational Model. Chapter 3

ER to Relational Mapping

The Entity-Relationship Model

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

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

High-Level Database Models (ii)

Database Applications (15-415)

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

Introduction to Databases

The Relational Model

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

Informatics 1: Data & Analysis

The Relational Model 2. Week 3

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

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

CS143: Relational Model

High Level Database Models

Database Systems ( 資料庫系統 )

Chapter 9: Relational DB Design byer/eer to Relational Mapping Relational Database Design Using ER-to- Relational Mapping Mapping EER Model

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

The Entity-Relationship Model. Overview of Database Design

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

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

Database Applications (15-415)

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

From ER Diagrams to the Relational Model. Rose-Hulman Institute of Technology Curt Clifton

Translating an ER Diagram to a Relational Schema

SQL DATA DEFINITION LANGUAGE

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

The Relational Model

SQL DATA DEFINITION LANGUAGE

How to translate ER Model to Relational Model

Relational Database Systems Part 01. Karine Reis Ferreira

The Relational Data Model. Data Model

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

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

DATABASE MANAGEMENT SYSTEMS. UNIT I Introduction to Database Systems

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Chapter 1 SQL and Data

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Relational Model (ii)

Informatics 1: Data & Analysis

Database Systems. Course Administration

In This Lecture. The Relational Model. The Relational Model. Relational Data Structure. Unnamed and named tuples. New thing:scheme (and attributes)

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Relational Model: History

Introduction to Data Management CSE 344. Lecture 2: Data Models

Database Technology Introduction. Heiko Paulheim

Announcements. Using Electronics in Class. Review. Staff Instructor: Alvin Cheung Office hour on Wednesdays, 1-2pm. Class Overview

Informatics 1: Data & Analysis

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

CONCEPTUAL DESIGN: ER TO RELATIONAL TO SQL

SQL DATA DEFINITION LANGUAGE

COSC344 Database Theory and Applications. σ a= c (P) Lecture 3 The Relational Data. Model. π A, COSC344 Lecture 3 1

Review for Exam 1 CS474 (Norton)

The Relational Model. Week 2

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

The Relational Model of Data (ii)

Let s briefly review important EER inheritance concepts

Relational Model Concepts

Administrivia. The Relational Model. Review. Review. Review. Some useful terms

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

Database Management

8) A top-to-bottom relationship among the items in a database is established by a

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

The Entity-Relationship Model (ER Model) - Part 2

CS W Introduction to Databases Spring Computer Science Department Columbia University

DATABASE MANAGEMENT SYSTEMS

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

CMPT 354 Database Systems I

DATABASE MANAGEMENT SYSTEM SHORT QUESTIONS. QUESTION 1: What is database?

Introduction to Databases CS348

SQL: Data Definition Language

Data and Knowledge Management Dr. Rick Jerz

Introduction to Databases CSE 414. Lecture 2: Data Models

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

CMPT 354: Database System I. Lecture 3. SQL Basics

Overview of Data Management

OVERVIEW OF DATABASE DEVELOPMENT

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Relational Databases Fall 2017 Lecture 7

CSCC43H: Introduction to Databases

Relational data model

CS530 Database Architecture Models. Database Model. Prof. Ian HORROCKS. Dr. Robert STEVENS. and Design The Relational

Transcription:

Inf1-DA 2011 2012 I: 24 / 117 Part I Structured Data Data Representation: I.1 The entity-relationship (ER) data model I.2 The relational model Data Manipulation: I.3 Relational algebra I.4 Tuple relational calculus I.5 The SQL query language Related reading: Chapter 3 of [DMS], 3.1,3.2,3.4,3.5

Inf1-DA 2011 2012 I: 25 / 117 History of relational model The relational model was introduced in 1970 by Edgar F. Codd, a British computer scientist working at IBM s Almaden Research Center in San Jose, California. IBM was initially slow to exploit the idea, but by the mid 1970 s IBM was at the forefront of the commercial development of relational database systems with its System R project, which included the development and first implementation of SQL. (Codd was sidelined from this project!) Around the same time, the relational model was developed and implemented at UC Berkeley (the Ingres project) Nowadays relational databases are a multi-billion pound industry. A major reason for the success of the relational model is its simplicity Codd received the 1981 Turing Award for his pioneering work on relational databases

Inf1-DA 2011 2012 I: 26 / 117 Building blocks The basic construct is a relation. It consists of a schema and an instance The schema can be thought of as the format of the relation A relation instance is also known as a table A schema is a set of fields, which are (, domain) pairs fields may be referred to as attributes, or colus domains are referred to as types The rows of a table are called tuples (or records) and they are value assignments from the specified domain for the fields of the table The arity of a relation is its number of colus (fields) The cardinality of a table is its number of rows (tuples)

Inf1-DA 2011 2012 I: 27 / 117 Example Fields (a.k.a. attributes, colus) Schema Tuples { (a.k.a. records, rows) age email s0456782 John 18 john@inf s0412375 Mary 18 mary@inf s0378435 Helen 20 helen@phys s0189034 Peter 22 peter@math

Inf1-DA 2011 2012 I: 28 / 117 SQL: The Structured Query Language SQL is the standard language for interacting with relational database management systems Substantial parts of SQL are declarative: code states what should be done, not necessarily how to do it. When actually querying a large database, database systems take advantage of this to plan, rearrange, and optimize the execution of queries. Procedural parts of SQL do contain imperative code to make changes to the database. While SQL is an international standard (ISO 9075), individual implementations have notable idiosyncrasies, and code may not be entirely portable.

Inf1-DA 2011 2012 I: 29 / 117 Data definition in SQL A special subset of SQL called the Data Definition Language (DDL) is used to declare table schemata Relations are called tables in SQL It is a typed language For simplicity, we will assume there are only three types: (i) integer for integer numbers, (ii) real for real numbers (floating point), and (iii) char(n) for a string of maximum length n There is also a special null value, which we see briefly later.

Inf1-DA 2011 2012 I: 30 / 117 General form of a DDL statement create table table ( attribute attribute type [, attribute attribute type ]* integrity constraints ) Example 1 create table ( char(8), char(20), age integer, email char(15), primary key () )

Inf1-DA 2011 2012 I: 31 / 117 The example defines the table. The last line implements a primary key constraint, it declares to be the chosen primary key for. This constraint requires that the table contains at most one row with any given value. This is enforced by the system. Any attempt to insert a new row with an value that already exists in some other row of the table will fail. create table ( char(8), char(20), age integer, email char(15), primary key () )

Inf1-DA 2011 2012 I: 32 / 117 General form of a DDL statement create table table ( attribute attribute type [, attribute attribute type ]* integrity constraints ) Example 2 create table Takes ( char(8), code char(20), mark integer, primary key (, code), foreign key () references, foreign key (code) references Courses )

Inf1-DA 2011 2012 I: 33 / 117 In this case, the primary key is a composite key using a pair of fields. The foreign key constraints given here enforce two further properties: Whenever a tuple is inserted, the value for the field must be a value that appears in the primary key colu of the table Similarly, the value for the code field must be a value that appears in the primary key colu of the Courses table create table Takes ( char(8), code char(20), mark integer, primary key (, code), foreign key () references, foreign key (code) references Courses )

Inf1-DA 2011 2012 I: 34 / 117 Key constraints example code mark s0456782 inf1 80 s0412375 geo1 78 s0412375 inf1 56 s0189034 math1 62 Referencing relation Primary key Foreign key age email s0456782 John 18 john@inf s0412375 Mary 18 mary@inf s0378435 Helen 20 helen@phys s0189034 Peter 22 peter@math code year inf1 Informatics 1 1 math1 Mathematics 1 1 geo1 Geology 1 1 dbs Database Systems 3 adbs Advanced Databases 4 Referenced relations

Inf1-DA 2011 2012 I: 35 / 117 Summary We have seen two forms of constraint: primary key (declaration) foreign key (declaration) references table Primary key constraints declare primary keys. Foreign key constraints link colus of one table to the primary key colus of another table. Both are declared by the user, but enforced by the system itself. (Attempting to enter a tuple that violates the constraint results in failure.) N.B. In the ER model, was an entity set and Takes a relationship. In the relational model, both are (necessarily!) implemented as tables.

Inf1-DA 2011 2012 I: 36 / 117 Translating an ER diagram to a relational schema Given an ER diagram, we can look for a relational schema that closely approximates the ER design. The translation is approximate because it is not always feasible to capture all the constraints in the ER design within the relational schema. (In SQL, certain types of constraint, for example, are inefficient to enforce, and so usually not implemented.) There is more than one approach to translating an ER diagram to a relational schema. Different translations amount to making different implementation choices for the ER diagram. It is possible to make these translations complete (work for any diagram) and automatic (in a push-button graphical tool); but in this course we shall just consider a few examples illustrating some of the main ideas.

Inf1-DA 2011 2012 I: 37 / 117 Mapping entity sets age email Algorithm Create a table for the entity set. Make each attribute of the entity set a field of the table, with an appropriate type. Declare the field or fields comprising the primary key

Inf1-DA 2011 2012 I: 38 / 117 Mapping entity sets age email create table ( char(8), char(20), age integer, email char(15), primary key () )

Inf1-DA 2011 2012 I: 39 / 117 Mapping relationship sets (no key constraints) mark email code year Takes Courses Algorithm Create a table for the relationship set. Add all primary keys of the participating entity sets as fields of the table. Add a field for each attribute of the relationship. Declare a primary key using all key fields from the entity sets. Declare foreign key constraints for all these fields from the entity sets.

Inf1-DA 2011 2012 I: 40 / 117 Mapping relationship sets (no key constraints) mark email code year Takes Courses create table Takes ( char(8), code char(20), mark integer, primary key (, code), foreign key () references, foreign key (code) references Courses )

Inf1-DA 2011 2012 I: 41 / 117 Mapping relationship sets (with key constraints) Directed_By DoS Algorithm Create a table for the relationship set. Add all primary keys of the participating entity sets as fields of the table. Add a field for each attribute of the relationship. Declare a primary key using the key fields from the source entity set only. Declare foreign key constraints for all the fields from the source and target entity sets.

Inf1-DA 2011 2012 I: 42 / 117 Mapping relationship sets (with key constraints) Directed_By DoS create table Directed By ( char(8), staff id char(8), primary key (), foreign key () references, foreign key (staff id) references DoS ) Note that this has captured the key constraint, but not the participation constraint. That requires an alternative approach, and a further kind of declaration.

Inf1-DA 2011 2012 I: 43 / 117 Mapping relationship sets (with key constraints, 2nd method) Directed_By DoS Algorithm Create a table for the source and target entity sets as usual. Add every primary key field of the target as a field in the source. Declare these fields as foreign keys. Because the Directed By relation is many-to-one, we don t in fact need a whole table for the relation itself. However, this does slightly pollute the source entity table.

Inf1-DA 2011 2012 I: 44 / 117 Mapping relationship sets (with key constraints, 2nd method) Directed_By DoS create table ( char(8), char(20), age integer, email char(15), dos id char(8), primary key (), foreign key (dos id) references DoS ) Note that this has still not included the participation constraint on in Directed By, but we are now nearer to doing so...

Inf1-DA 2011 2012 I: 45 / 117 Null values In SQL, a field can have the special value null A null value means that a field is either unknown/missing/unavailable, or undefined/makes no sense here. Some implementations do not allow the null value to appear in primary key fields. They may allow nulls to appear in foreign key fields. Null values can be disallowed from specific fields with a not null declaration. In certain circumstances, by disallowing null, we can enforce a participation constraint.

Inf1-DA 2011 2012 I: 46 / 117 Mapping relationship sets with key+participation constraints Directed_By DoS Algorithm Create a table for the source and target entity sets as usual. Add every primary key field of the target as a field in the source. Declare these fields as not null. Declare these fields as foreign keys.

Inf1-DA 2011 2012 I: 47 / 117 Mapping relationship sets with key+participation constraints Directed_By DoS create table ( char(8), char(20), age integer, email char(15), dos id char(8) not null, primary key (), foreign key (dos id) references DoS )

Inf1-DA 2011 2012 I: 48 / 117 Mapping weak entity sets and identifying relationships number capacity address Rooms Is_Located _In Buildings Algorithm Create a table for the weak entity set. Make each attribute of the weak entity set a field of the table. Add fields for the primary key attributes of the identifying owner. Declare a foreign key constraint on these identifying owner fields. Instruct the system to automatically delete any tuples in the table for which there are no owners

Inf1-DA 2011 2012 I: 49 / 117 Mapping weak entity sets and identifying relationships number capacity address Rooms Is_Located _In Buildings create table Rooms ( number char(8), capacity integer, building char(20), primary key (number,building ), foreign key (building ) references Buildings on delete cascade )

Inf1-DA 2011 2012 I: 50 / 117 Mapping hierarchical entities email Full-Time ISA Part-Time Part-Time Fraction Declare a table as usual for the superclass of the hierarchy. For each subclass declare another table using superclass s primary key and the subclass s extra attributes. Declare the primary key from the superclass as the primary key of the subclass, and with a foreign key constraint.

Inf1-DA 2011 2012 I: 51 / 117 Mapping hierarchical entities email Full-Time ISA Part-Time Part-Time Fraction create table PT ( char(8), pt frac integer, primary key (), foreign key () references )