IT360: Applied Database Systems. Slide Set: #2. Relational Model (Chapter 3 Kroenke) The relational model Relational model terminology

Similar documents
IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

Relational data model

The Relational Data Model. Data Model

Database Management Systems. Chapter 3 Part 1

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

The Relational Model. Chapter 3

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

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

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

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

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

SQL Server 2008 Tutorial 3: Database Creation

The Relational Model of Data (ii)

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

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

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

The Relational Model. Week 2

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

CS275 Intro to Databases

File Processing Approaches

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

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

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

Databases. Jörg Endrullis. VU University Amsterdam

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

Database Systems ( 資料庫系統 )

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

The Relational Model 2. Week 3

Basic SQL. Basic SQL. Basic SQL

Database Applications (15-415)

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

CSC 453 Database Technologies. Tanu Malik DePaul University

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

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

SQL DATA DEFINITION LANGUAGE

Chapter 4. The Relational Model

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

The Relational Data Model and Relational Database Constraints

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

CSC 742 Database Management Systems

ITCS 3160 DATA BASE DESIGN AND IMPLEMENTATION

DBMS. Relational Model. Module Title?

Database Logical Design

SQL Data Definition and Data Manipulation Languages (DDL and DML)

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

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

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

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

The Relational Model

SQL DATA DEFINITION LANGUAGE

Outer Join, More on SQL Constraints

SQL: Data Definition Language

Introduction to Databases

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Database Logical Design

CS 377 Database Systems

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

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

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

The Relational Model Constraints and SQL DDL

CIS 330: Applied Database Systems

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

Introduction to Databases

CS143: Relational Model

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

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

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

SQL DATA DEFINITION LANGUAGE

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

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

The Structured Query Language Get Started

SQL: DDL. John Ortiz Cs.utsa.edu

chapter 8 D ATA INTEGRITY

U1. Data Base Management System (DBMS) Unit -1. MCA 203, Data Base Management System

Database Systems. Course Administration

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

Physical Design of Relational Databases

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

Introduction to Relational Database Management Systems

The Relational Model and Normalization

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Lecture 2 SQL. Announcements. Recap: Lecture 1. Today s topic. Semi-structured Data and XML. XML: an overview 8/30/17. Instructor: Sudeepa Roy

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

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

user specifies what is wanted, not how to find it

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

The Relational Model

Introduction to Database Systems CSE 414

Let s briefly review important EER inheritance concepts

Database Management Systems,

LIKE Condition. % allows you to match any string of any length (including zero length)

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Keys, SQL, and Views CMPSCI 645

CS 2451 Database Systems: Relational Data Model

doc. RNDr. Tomáš Skopal, Ph.D. RNDr. Michal Kopecký, Ph.D.

Introduction to SQL Server. nikos bikakis

ALTER TABLE Statement

Module 3 MySQL Database. Database Management System

Transcription:

IT360: Applied Database Systems Slide Set: #2 Relational Model (Chapter 3 Kroenke 1 Goals Understand: The relational model Relational model terminology Write SQL statements to create tables 2 1

Why Study the Relational Model? Most widely used model. Vendors: IBM, Microsoft, Oracle, Sybase, etc. Competitors: Object-Oriented model ObjectStore, Versant, Ontos A synthesis: object-relational model Informix Universal Server, Oracle, DB2 XML Key-value stores 3 Relational Database A relation is a two-dimensional table Relation schema describes the structure for the table Relation name Column names Column types A relational database is a set of relations 4 2

Relation Example EMPLOYEE(EmployeeNumber:integer, FirstName:string, LastName:string, Department:string, Email:string, Phone:integer 5 Relation All entries in a column are of the same kind Each column has a unique name Cells of the table hold a single value The order of the columns is not important The order of the rows is not important No two rows may be identical 6 3

Tables That Are Not Relations 7 Alternative Terminology Although not all tables are relations, the terms table and relation are normally used interchangeably The following sets of terms are used interchangeably: 8 4

Create a Table CREATE TABLE statement is used for creating relations/tables Each column is described with three parts: column name data type optional constraints 9 Specify Data Types Choose the most specific data type possible!!! Generic Data Types: CHAR(n VARCHAR(n DATE TIME MONEY INTEGER DECIMAL CREATE TABLE EMPLOYEE ( EmployeeNumber integer, EmployeeName char(50, Phone char(15, Email char(50, HireDate date, ReviewDate date 10 5

Specify Null Status Null status: whether or not the value of the column can be NULL CREATE TABLE EMPLOYEE ( EmployeeNumber integer NOT NULL, EmployeeName char (50 NOT NULL, Phone char (15 NULL, Email char(50 NULL, HireDate date NOT NULL, ReviewDate date NULL 11 Specify Default Values Default value - value supplied by the DBMS, if no value is specified when a row is inserted Syntax/support CREATE TABLE EMPLOYEE ( depends on DBMS EmployeeNumber integer NOT NULL, EmployeeName char (50 NOT NULL, Phone char (15 NULL, Email char(50 NULL, HireDate date NOT NULL DEFAULT (getdate(, ReviewDate date NULL 12 6

Specify Other Data Constraints Data constraints are limitations on data values CREATE TABLE EMPLOYEE ( Name for EmployeeNumber integer NOT NULL, constraint EmployeeName char (50 NOT NULL, Phone char (15 NULL, Email char(50 NULL, HireDate date NOT NULL DEFAULT (getdate(, ReviewDate date NULL, CONSTRAINT Check_Email CHECK (Email LIKE %@gmail.com 13 Integrity Constraints (IC IC: condition that must be true for any instance of the database Domain constraints Key constraints Foreign Key constraints ICs are specified when schema is defined ICs are checked when relations are modified A legal instance of a relation is one that satisfies all specified ICs DBMS should not allow illegal instances 14 7

Keys A key is a combination of one or more columns that is used to identify rows in a relation A composite key is a key that consists of two or more columns A set of columns is a key for a relation if : 1. No two distinct rows can have same values in all key columns, and 2. This is not true for any subset of the key Part 2 false? A superkey 15 Candidate and Primary Keys A candidate key is a key A primary key is a candidate key selected as the primary means of identifying rows in a relation: There is one and only one primary key per relation The primary key may be a composite key The ideal primary key is short, numeric and never changes 16 8

Surrogate Keys A surrogate key is an artificial column added to a relation to serve as a primary key: DBMS supplied Short, numeric and never changes an ideal primary key! Has artificial values that are meaningless to users Remember Access (ID auto number 17 Specify Primary Key CREATE TABLE EMPLOYEE ( EmployeeNumber integer NOT NULL, EmployeeName char (50 NOT NULL, Phone char (15 NULL, Email char(50 NULL, HireDate date NOT NULL DEFAULT (getdate(, ReviewDate date NULL, CONSTRAINT Check_Email CHECK (Email LIKE %@gmail.com, CONSTRAINT PK_Employee PRIMARY KEY (EmployeeNumber 18 9

Specify Alternate Keys Alternate keys: alternate identifiers of unique rows in a table CREATE TABLE EMPLOYEE ( EmployeeNumber integer NOT NULL, EmployeeName char (50 NOT NULL, Phone char (15 NULL, Email char(50 NULL, HireDate date NOT NULL DEFAULT (getdate(, ReviewDate date NULL, CONSTRAINT Check_Email CHECK (Email LIKE %@gmail.com, CONSTRAINT PK_Employee PRIMARY KEY (EmployeeNumber, CONSTRAINT AK_Email UNIQUE (Email, CONSTRAINT AK_ENamePhone UNIQUE (EmployeeName, Phone 19 ICE: Is This a Relation? Why? A X C A John Ryan MD jr@gmail.com Bob Smith MD, VA, NY Alice Brown CA bsm@gmail.com Jane Doe WA jd@yahoo.com John Ryan MD jr@gmail.com 5 4 5 4 20 10

ICE: Find possible PK, AK X Y Z W John Ryan MD jr@gmail.com Bob Smith MD bsm@gmail.com Alice Brown CA John Doe WA jd@yahoo.com 21 Foreign Keys and Referential Integrity Constraints A foreign key is the primary key of one relation that is placed in another relation to form a link between the relations A referential integrity constraint: the values of the foreign key must exist as primary key values in the corresponding relation No dangling references 22 11

Foreign Keys CREATE TABLE LOCKER( LockerNumber integer PRIMARY KEY, LockerRoom integer, LockerSize integer CREATE TABLE CLUB_MEMBER( MemberNumber integer PRIMARY KEY MemberName char(50, Phone char(15, Email char(50, LockerNumber integer NULL, CONSTRAINT FK_Locker FOREIGN KEY (LockerNumber REFERENCES LOCKER(LockerNumber, CONSTRAINT Unique_Locker UNIQUE(LockerNumber 23 Enforcing Referential Integrity What if a new Member row is added/updated that references a nonexistent locker? Reject it! What if a Locker row is deleted? Options: Also delete all Member rows that refer to it. Disallow deletion of Locker row that is referred. Set LockerNumber in Member to default value Set LockerNumber in Member to null Similar if primary key of Locker row is 24 updated 12

Referential Integrity in SQL/92 SQL/92 supports all 4 options on deletes and updates. Default is NO ACTION (delete/update is rejected CASCADE (delete/update all rows that refer to deleted/updated row SET NULL / SET DEFAULT CREATE TABLE CLUB_MEMBER( MemberNumber integer PRIMARY KEY MemberName char(50, Phone char(15, Email char(50, LockerNumber integer NULL, CONSTRAINT FK_Locker FOREIGN KEY (LockerNumber REFERENCES LOCKER(LockerNumber ON DELETE SET NULL ON UPDATE CASCADE, CONSTRAINT Unique_Locker UNIQUE(LockerNumber 25 FOREIGN KEY Constraints DEPARTMENTS STUDENTS DepartmentName: char(18 Phone: char(18 Building: char(18 Room: integer D:SN U:C Majors I:SN U:SN StudentNumber: integer StudentLastName: char(18 StudentFirstName: char(18 Email: varchar(50 PhoneNumber: char(18 DepartmentName: char(18 (FK DepartmentName Phone Building Room Mathematics 410-293-4573 Michelson Hall 308 History 410-293-2255 Sampson Hall 120 Computer Science 410-293-6800 Michelson Hall 340 Student Student Number LastName Student FirstName 190 Smith John jsmith@usna.edu 410-431-3456 Email PhoneNumber MajorDepartmentName 673 Doe Jane jdoe@usna.edu Computer Science 312 Doe Bob bred@usna.edu 443-451-7865 Mathematics CREATE TABLE Departments (DepartmentName char(18, Phone char(18 NOT NULL, Building char(18, Room integer, PRIMARY KEY (DepartmentName 26 13

Summary Relational Model 2-D tables Relational schema: structure of table Constraints Domain Key Candidate, Primary, Alternate, Surrogate Foreign key Referential integrity constraint 27 SQL: Creating Tables CREATE TABLE table_name( column_name1 column_type1 [constraints1],, [[CONSTRAINT constraint_name] table_constraint] Table constraints: NULL/NOT NULL PRIMARY KEY (columns UNIQUE (columns CHECK (conditions FOREIGN KEY (local_columns REFERENCES foreign_table (foreign_columns [ON DELETE action_d ON UPDATE action_u] Specify surrogate key in SQL Server: column_name int_type IDENTITY (seed, increment Specify surrogate key in MySQL: column_name int_type AUTO_INCREMENT 28 14