Relational Model, Key Constraints

Similar documents
Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa

Database Modifications and Transactions

Programming and Database Fundamentals for Data Scientists

The Relational Model

Joins, NULL, and Aggregation

Databases - Relations in Databases. (N Spadaccini 2010) Relations in Databases 1 / 16

AC61/AT61 DATABASE MANAGEMENT SYSTEMS DEC 2013

Standard stuff. Class webpage: cs.rhodes.edu/db Textbook: get it somewhere; used is fine. Prerequisite: CS 241 Coursework:

The Relational Model of Data (ii)

Information Systems for Engineers. Exercise 10. ETH Zurich, Fall Semester Hand-out Due

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

Relational Model. CSE462 Database Concepts. Demian Lessa. Department of Computer Science and Engineering State University of New York, Buffalo

SQL queries II. Set operations and joins

Midterm 1 157A Fall /22

Information Systems Engineering. SQL Structured Query Language DDL Data Definition (sub)language

CS 4604: Introduction to Database Management Systems. B. Aditya Prakash Lecture #2: The Relational Model and Relational Algebra

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

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

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

Introduction to Database Systems. Announcements CSE 444. Review: Closure, Key, Superkey. Decomposition: Schema Design using FD

CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E)

Data Definition Language (DDL), Views and Indexes Instructor: Shel Finkelstein

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

CS 377 Database Systems

High-Level Database Models. Spring 2011 Instructor: Hassan Khosravi

Databases. Jörg Endrullis. VU University Amsterdam

CS275 Intro to Databases

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

SQL DATA DEFINITION LANGUAGE

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

SQL DATA DEFINITION LANGUAGE

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

The Relational Data Model. Data Models. Relational vs Semistructured. Structure Operations - Constraints

Information Systems Engineering. SQL Structured Query Language DML Data Manipulation (sub)language

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

Chapter 1: Introduction

CS40 Exam #2 November 14, 2001

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

SQL DATA DEFINITION LANGUAGE

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

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

Full file at

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

SQL: Part II. Introduction to Databases CompSci 316 Fall 2018

Database Technology Introduction. Heiko Paulheim

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 2: Relations and SQL. September 5, Lecturer: Rasmus Pagh

1. Given the name of a movie studio, find the net worth of its president.

Niklas Fors The Relational Data Model 1 / 17

The Basic (Flat) Relational Model. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Chapter 1: Introduction

CMPT 354: Database System I. Lecture 2. Relational Model

Relational data model

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

CS W Introduction to Databases Spring Computer Science Department Columbia University

Chapter 5. Relational Model Concepts 5/2/2008. Chapter Outline. Relational Database Constraints

Chapter 1 SQL and Data

Chapter 5. Relational Model Concepts 9/4/2012. Chapter Outline. The Relational Data Model and Relational Database Constraints

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

CompSci 516 Database Systems. Lecture 2 SQL. Instructor: Sudeepa Roy

Birkbeck. (University of London) BSc/FD EXAMINATION. Department of Computer Science and Information Systems. Database Management (COIY028H6)

CS425 Fall 2016 Boris Glavic Chapter 1: Introduction

Chapter 2 The relational Model of data. Relational model introduction

The Relational Data Model and Relational Database Constraints

SQL: Data Definition Language

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

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

Chapter 7 Constraints and Triggers. Spring 2011 Instructor: Hassan Khosravi

The Relational Model

SQL Constraints and Triggers

Set Operations, Union

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Query Processing & Optimization. CS 377: Database Systems

Basic SQL. Basic SQL. Basic SQL

CS143: Relational Model

Chapter 5. The Relational Data Model and Relational Database Constraints. Slide 5-١. Copyright 2007 Ramez Elmasri and Shamkant B.

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 5-1

Database Systems. Basics of the Relational Data Model

Introduction to Database Systems CSE 414

The Relational Model 2. Week 3

Intermediate SQL ( )

Exam I Computer Science 420 Dr. St. John Lehman College City University of New York 12 March 2002

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Relational Model Concepts

The Relational Data Model. Data Model

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

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

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

Chapter 2 Introduction to Relational Models

CSE 344 MAY 14 TH ENTITIES

BASIC SQL CHAPTER 4 (6/E) CHAPTER 8 (5/E)

High Level Database Models

Introduction to Data Management. Lecture #17 (SQL, the Never Ending Story )

The Relational Model. Chapter 3

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

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

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Database Management Systems. Chapter 3 Part 1

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Transcription:

Relational Model, Key Constraints PDBM 6.1 Dr. Chris Mayfield Department of Computer Science James Madison University Jan 23, 2019

What is a data model? Notation for describing data or information Structure of the data Conceptual vs physical Operations on the data Limited set of queries / updates Constraints on the data Limitations of what data can be Important data models Relational / object-relational Semistructured (e.g., XML) Jan 23, 2019 Relational Model, Key Constraints 2 of 23

Relational model title year length genre Gone With the Wind 1939 231 drama Star Wars 1977 124 sci-fi Wayne s World 1992 95 comedy Structure: Table (like an ArrayList of objects) Columns define role played by different pieces of data Operations: Relational Algebra (select, project, join) Constraints: Genre must be action, comedy, drama,... No two movies can have same title and year Jan 23, 2019 Relational Model, Key Constraints 3 of 23

Semistructured model (XML) Extensible Markup Language < movies > < movie title =" Gone With the Wind " > <year >1939 </ year > < length >231 </ length > <genre >drama </ genre > </ movie >... </ movies > Structure: Tree (or graph) Tags define role played by different pieces of data Operations: Traversals in the implied tree Constraints: Limitations on data types (per tag) Which tags can be top-level / nested Jan 23, 2019 Relational Model, Key Constraints 4 of 23

Semistructured model (JSON) JavaScript Object Notation {" movies ": [ " movie ": { " title ": " Gone With the Wind ", " year ": 1939, " length ": 231, " genre ": " drama " }... ]} Arrays in [] s values (string, number, object, array, true, false, null) Objects in {} s key:value pairs (keys must be strings) The Fat-Free Alternative to XML http://www.json.org/xml.html Jan 23, 2019 Relational Model, Key Constraints 5 of 23

The Relational Model Serving databases since 1969

Terminology title year length genre Gone With the Wind 1939 231 drama Star Wars 1977 124 sci-fi Wayne s World 1992 95 comedy Attributes: title, year, length, genre Schema: Movie(title, year, length, genre) Database schema = set of schemas of relations DBMS schema = collection of tables, views, etc Tuples: rows of the relation Jan 23, 2019 Relational Model, Key Constraints 7 of 23

Domains title year length genre Gone With the Wind 1939 231 drama Star Wars 1977 124 sci-fi Wayne s World 1992 95 comedy Domain = set of values for an attribute i.e., its data type Movie(title: string, year: integer, length: integer, genre: string) NULL is a special value Unknown / undefined / empty /... Jan 23, 2019 Relational Model, Key Constraints 8 of 23

Who invented all this? Edgar Frank Ted Codd (1923 2003) A Relational Model of Data for Large Shared Data Banks Communications of the ACM 13 (6): 377 387, June 1970 Jan 23, 2019 Relational Model, Key Constraints 9 of 23

Group exercise #1 Design the schema for a zoo database Relations animal building cage keeper species Consider What attributes needed for each relation? How will you join the relations together? Jan 23, 2019 Relational Model, Key Constraints 10 of 23

Primary/Foreign Key Constraints

Keys of relations Unique key Attribute (or set of attributes) such that No pair of tuples have the same value(s) Useful for identifying / referencing tuples What are the keys in: Student Course Grade Hermione Grainger Potions A- Draco Malfoy Potions B Harry Potter Potions A Ron Weasley Potions C Key attributes are often underlined or bolded Jan 23, 2019 Relational Model, Key Constraints 12 of 23

Example from vdoe Jan 23, 2019 Relational Model, Key Constraints 13 of 23

PRIMARY KEY syntax Single attribute: CREATE TABLE MovieStar ( name char(30) PRIMARY KEY, address varchar(255), gender char(1) NOT NULL, birthdate date ); Multiple attributes: CREATE TABLE Movies ( title char(100), year integer, length integer NOT NULL, genre char(10) NOT NULL, studioname char(30), producerc# integer, PRIMARY KEY (title, year) ); -- tuple syntax Jan 23, 2019 Relational Model, Key Constraints 14 of 23

Foreign keys Referenced attributes must be a PRIMARY KEY or UNIQUE CREATE TABLE Studio ( name char(30) PRIMARY KEY, address varchar(255), presc# integer REFERENCES MovieExec(cert#) ); Alternatively / for multiple attributes: CREATE TABLE Studio ( name char(30) PRIMARY KEY, address varchar(255), presc# integer, FOREIGN KEY (presc#) REFERENCES MovieExec(cert#) ); Why should we do this at the database level? What happens if referring attributes are NULL? Jan 23, 2019 Relational Model, Key Constraints 15 of 23

Referential integrity Example: Studio.presC# must be a MovieExec.cert# What happens when you... try to insert a new Studio tuple? try to update a Studio s presc#? try to delete a MovieExec tuple? try to update a MovieExec s cert#? DBMS policies Default: reject violating modifications CASCADE: mimic actions at foreign keys SET NULL: replace all foreign key values Jan 23, 2019 Relational Model, Key Constraints 16 of 23

Enforcement policies CREATE TABLE Studio ( name char(30) PRIMARY KEY, address varchar(255), presc# integer REFERENCES MovieExec(cert#) ON DELETE SET NULL ON UPDATE CASCADE ); Why do we SET NULL for deletions? Why do we CASCADE for updates? What do we call tuples with invalid foreign key values? How is this related to foreign-key joins? Jan 23, 2019 Relational Model, Key Constraints 17 of 23

Group exercise #2 Declare keys for your zoo database Relations animal building cage keeper species Consider What are the PRIMARY KEYS? What are the FOREIGN KEYS? Jan 23, 2019 Relational Model, Key Constraints 18 of 23

Attribute and Tuple Constraints

Attribute constraints May be declared on one or more attributes NOT NULL These attributes never have NULL values Use this constraint whenever possible! UNIQUE No two tuples may have the same values (except NULL) PRIMARY KEY = UNIQUE + NOT NULL Each relation may only have one Every relation should have one! Jan 23, 2019 Relational Model, Key Constraints 20 of 23

Attribute-based CHECK constraints New requirement: all certs must be 6+ digits CREATE TABLE Studio ( name char(30) PRIMARY KEY, address varchar(255), presc# integer CHECK (presc# >= 100000) ); Only checked when presc# is inserted/updated What s the difference between a data type and a domain? CREATE TABLE MovieStar ( name char(30) PRIMARY KEY, address varchar(255), gender char(1) CHECK (gender IN ('F', 'M', 'O')) ); Only checked when gender is inserted/updated Jan 23, 2019 Relational Model, Key Constraints 21 of 23

CHECK FOREIGN KEY What s wrong with this relation? CREATE TABLE Studio ( name char(30) PRIMARY KEY, address varchar(255), presc# integer CHECK (presc# IN (SELECT cert# FROM MovieExec)) ); Only checked when presc# is inserted/updated NULL values will also cause this CHECK to fail Is this a bug or a feature? Jan 23, 2019 Relational Model, Key Constraints 22 of 23

Group exercise #3 Constraints for your zoo database Relations animal building cage keeper species Consider Attribute CHECK constraints? Tuple-based CHECK constraints? Jan 23, 2019 Relational Model, Key Constraints 23 of 23