CS143: Relational Model

Similar documents
The Relational Model. Suan Lee

CS425 Fall 2016 Boris Glavic Chapter 1: Introduction

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

DATABASE MANAGEMENT SYSTEMS. UNIT I Introduction to Database Systems

Database Technology Introduction. Heiko Paulheim

Chapter 1: Introduction

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

DBMS. Relational Model. Module Title?

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

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

The Relational Model Constraints and SQL DDL

Database Systems CSE 303. Lecture 02

Database Systems CSE 303. Lecture 02

Chapter 1: Introduction

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

CSC 453 Database Technologies. Tanu Malik DePaul University

CS 2451 Database Systems: Relational Data Model

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

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

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

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

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Today's Party. Example Database. Faloutsos/Pavlo CMU /615

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

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

The Relational Data Model. Data Model

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

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

Part I: Structured Data

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

Lecture 1: Relational Databases

COMP 430 Intro. to Database Systems

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

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

Today Learning outcomes LO2

The SQL data-definition language (DDL) allows defining :

پوهنتون کابل پوهنځی كمپيوترساینس

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

CS317 File and Database Systems

Chapter 3: Introduction to SQL

Introduction to Databases CSE 414. Lecture 2: Data Models

Database Applications (15-415)

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

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

Lecture 2: Introduction to SQL

EECS 647: Introduction to Database Systems

SQL DATA DEFINITION LANGUAGE

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Introduction. Example Databases

The Relational Model of Data (ii)

Data! CS 133: Databases. Goals for Today. So, what is a database? What is a database anyway? From the textbook:

Informatics 1: Data & Analysis

SQL DATA DEFINITION LANGUAGE

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

A practical introduction to database design

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

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

The Relational Model

Database Management Systems MIT Introduction By S. Sabraz Nawaz

Announcements. Relational Model & Algebra. Example. Relational data model. Example. Schema versus instance. Lecture notes

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

Ø Last Thursday: String manipulation & Regular Expressions Ø guest lecture from the amazing Sam Lau Ø reviewed in section and in future labs & HWs

After completing this unit, you should be able to: Define the terms

Who, where, when. Database Management Systems (LIX022B05) Literature. Evaluation. Lab Sessions. About this course. After this course...

CS 377 Database Systems

CGS 3066: Spring 2017 SQL Reference

CS317 File and Database Systems

Relational Database Systems Part 01. Karine Reis Ferreira

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

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Introduction. Who wants to study databases?

CS275 Intro to Databases

Chapter 3: Introduction to SQL

Basic operators: selection, projection, cross product, union, difference,

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Outline. Database Management Systems (DBMS) Database Management and Organization. IT420: Database Management and Organization

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

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

MTAT Introduction to Databases

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

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

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

Lecture 3: SQL Part II

Data Definition and Data Manipulation. Lecture 5: SQL s Data Definition Language CS1106/CS5021/CS6503 Introduction to Relational Databases

Lecture 5: SQL s Data Definition Language

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

CS145 Introduction. About CS145 Relational Model, Schemas, SQL Semistructured Model, XML

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

SQL (and MySQL) Useful things I have learnt, borrowed and stolen

Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Database Principle. Zhuo Wang Spring

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

SQL: Data Definition Language

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Basic SQL. Basic SQL. Basic SQL

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

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

Transcription:

CS143: Relational Model Book Chapters (4th) Chapters 1.3-5, 3.1, 4.11 (5th) Chapters 1.3-7, 2.1, 3.1-2, 4.1 (6th) Chapters 1.3-6, 2.105, 3.1-2, 4.5 Things to Learn Data model Relational model Database construction steps Data Model The way we model/conceptualize/visualize/represent data Need some representation to manage data in a computer Many different ways to model data Example (Airline flight): Graph model Node: city Edge: flight between cities Label on edge: flight time, etc. Example (Company hierarchy): Tree model CEO Presidents Vice presidents Department heads... Many others, like object model, XML model, network model, etc. Models to learn in the class: Relational and E/R model Example to Use in the Class School information Student(sid, name, age, GPA, address,... ) Class(dept, cnum, sec, title, instructor,... ) Enroll(sid, dept, cnum, sec)... 1

Relational Model Example: Student(sid, name, address, age, GPA) Student sid name addr age GPA 301 John 183 Westwood 19 2.1 303 Elaine 301 Wilshire 17 3.9 401 James 183 Westwood 17 3.5 208 Esther 421 Wilshire 20 3.1 All data is represented as relations (= tables) Each relation has a set of attributes (= columns) Each relation contains of a set of tuples (= rows) Each attribute has a domain (= type) Only atomic types Similar to Excel spreadsheet History of Relational Model By far, the most significant invention in the history of DBMS E.F. Codd, 1970 Completely revolutionized the field ($9B market in 2001 according to Gartner) Before it, network and hierarchical model: difficult to use and pose queries Turing Award, 1981 Extremely simple Used by practically all commercial DBMS these days An argument for simplicity Initial research prototypes IBM System R IBM DB2 Berkeley Postgres PostgresSQL IBM and Oracle: first commercial RDB vendor Still dominate the market 2

Concepts and Terminology Schema The structure of relations in database: relation name, attribute name, domain (optional). Example: Student(sid, name, address, GPA, age) Course(dept: char(2), cnum: int, sec: int, unit: int, title: char(100)) char(2): string of length 2 Instance (= Data) Actual contents (tuples) of relation (explain using the table example) Keys Schema Type, Instance Value Schema Class, Instance Instance A set of attributes that are known to be unique in the relation Student(sid, name, address, GPA, age) Multiple keys possible Q: When do we need keys? How can they be used? Name Scope Names of relation: Unique across relations Names of attributes: Unique in a table, same name in different tables OK Set semantics No duplicate tuples (different in SQL. More discussion later) Q: Can a relation with no duplicates have no keys? Tuple order does not matter Authors of a paper: Need explicit ordering Orders of attributes do not matter 3

Null value Common interpretation Do not know Do not want to say Not applicable Example: Student(id, dept, name, GPA) before first quarter? Complications from Null Example: Student(id, dept, name, age, GPA) Q 1 : Find students whose age 20. Susan s age is Null. Susan in the result? Q 2 : Find students whose age < 20. Susan in the result? Q 3 : Find students whose age 20 or age < 20 and. Susan in the result? But Q 3 = Q 1 Q 2? Something is wrong. Relational algebra, SQL: 3-valued logic Every condition is evaluated as True, False or Unknown Various (arbitrary) rules to deal with anomalous situation More discussion later Unfortunately, Nulls are very common in DB. Significant complexity in DBMS implementation Steps in Database Construction Flow chart diagram 1. Domain Analysis: Understand application-domain semantics being captured E/R diagram discussed in the latter half of the course 2. Database design: Design tables to capture the information Relational design theory (funtional dependency, normal form, etc) discussed in the latter half of the course 3. Table creation: using Database Definition Language DDL: A language to define relations and their characteristics: Schema, integrity constraints, indexes,... 4. Load: typically bulk-load. insert tuple possible 5. Query and update: using Data Manipulation Language DML: A language to query and update relations 4

SQL and DDL, Load, DML What is SQL? Structured Query Language The standard language for interacting with all commericial RDBMS The history of SQL standard SQL89 (Ansi SQL): first standard SQL92 (SQL2): the main standard. several hundred pages SQL3 (SQL99): no vendor supports it all! 1600 pages SQL4 (SQL03): bug-fix release SQL5 (SQL06): extensions for XML SQL6 (SQL08): merge, truncate, diagnostics We will mainly use the standard SQL92 in class. Individual product uses slight variations of the standard. Some class query may not run on them. SQL has many components DDL: Schema definition, constraints, indexes,... DML: data retrieval, modification,... Transactions, Authorization,... We learn schema definition part today. Basic SQL Types Basic SQL types (commonly used subset) String Char(n) padded fixed length Varchar(n) variable length Number Integer 32bit Decimal(5,2) 999.99 Real, Double 32bit, 64bit Datetime Date 2002-01-15 Time 13:50:00 Timestamp 2002-01-15 13:50:00 Schema definition (table creation) 5

CREATE TABLE Course ( dept CHAR(2) NOT NULL, cnum INTEGER NOT NULL, sec INTEGER NOT NULL, unit INTEGER, instructor VARCHAR(30), title VARCHAR(30), PRIMARY KEY(dept, cnum, sec) ) No Null in primary key Course(dept, cnum, sec, unit, instructor, title) Course(dept, cnum, sec, unit, instructor, title) CREATE TABLE Course ( dept CHAR(2) NOT NULL DEFAULT CS, cnum INTEGER NOT NULL, sec INTEGER NOT NULL, unit INTEGER, instructor VARCHAR(30), title VARCHAR(30) DEFAULT, PRIMARY KEY(dept, cnum, sec), UNIQUE(dept, cnum, instructor), UNIQUE(dept, sec, title) ) One primary key per table Unique for other keys Primary key, unique are enforced through index (more discussion later) SQL92: No Null in primary key. Null OK for unique (DB2: No Null for unique). DEFAULT for default values SQL for dropping a table DROP TABLE Course Loading data Vendor specific Oracle, MySQL LOAD DATA INFILE <datafile> INTO TABLE Course DB2 IMPORT FROM <datafile> OF DEL INSERT INTO Course Bulk-load from a comma separated file 6

Querying and update: Data Manipulation Language (DML) DML: A language to query and update relations Relational query languages Relational algebra (formal), SQL (practical) What is a relational query? Input: relation Output: relation Input relation query Output relation More details later Things to remember Data model Schema Instance Relational model relation, attribute, tuple, domain key null value set semantics Database construction steps 1. Domain analysis 2. Database design: E/R model, database design theory 3. Table creation: DDL 4. Load 5. Query and update: DML 7