Chapter 2 The relational Model of data. Relational model introduction

Similar documents
CSCI3030U Database Models

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

Databases-1 Lecture-01. Introduction, Relational Algebra

Databases 1. Defining Tables, Constraints

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL DATA DEFINITION LANGUAGE

SQL: Data Definition Language

Introduction to SQL. Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries

Introduction to SQL. Select-From-Where Statements Multirelation Queries Subqueries. Slides are reused by the approval of Jeffrey Ullman s

Chapter 6 The database Language SQL as a tutorial

Chapter 6 The database Language SQL as a tutorial

Introduction to SQL SELECT-FROM-WHERE STATEMENTS SUBQUERIES DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

SQL Continued! Outerjoins, Aggregations, Grouping, Data Modification

Constraints. Local and Global Constraints Triggers

More SQL. Extended Relational Algebra Outerjoins, Grouping/Aggregation Insert/Delete/Update

CS 317/387. A Relation is a Table. Schemas. Towards SQL - Relational Algebra. name manf Winterbrew Pete s Bud Lite Anheuser-Busch Beers

EXTENDED RELATIONAL ALGEBRA OUTERJOINS, GROUPING/AGGREGATION INSERT/DELETE/UPDATE

CS54100: Database Systems

SQL: Data Manipulation Language

Constraints and Triggers

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Why SQL? SQL is a very-high-level language. Database management system figures out best way to execute query

Relational Data Model is quite rigid. powerful, but rigid.

SQL. The Basics Advanced Manipulation Constraints Authorization 1. 1

Views, Indexes, Authorization. Views. Views 8/6/18. Virtual and Materialized Views Speeding Accesses to Data Grant/Revoke Priviledges

Entity-Relationship Model. Purpose of E/R Model

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

From E/R Diagrams to Relations

user specifies what is wanted, not how to find it

Subqueries. Must use a tuple-variable to name tuples of the result

The Relational Model

Database Design and Programming

Beersèname, manfè. Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Relational Algebra and SQL. Basic Operations Algebra of Bags

Design Techniques. 1. Avoid redundancy 2. Limit the use of weak entity sets 3. Don t use an entity set when an attribute will do

Relational Algebra. Algebra of Bags

e e Conceptual design begins with the collection of requirements and results needed from the database (ER Diag.)

Chapter 2 The relational Model of data. Relational algebra

CS54100: Database Systems

Transactions, Views, Indexes. Controlling Concurrent Behavior Virtual and Materialized Views Speeding Accesses to Data

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

Relational Algebra BASIC OPERATIONS DATABASE SYSTEMS AND CONCEPTS, CSCI 3030U, UOIT, COURSE INSTRUCTOR: JAREK SZLICHTA

1 Relational Data Model

A tuple is dangling if it doesn't join with any

Relational Query Languages

CSC 343 Winter SQL: Aggregation, Joins, and Triggers MICHAEL LIUT

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Chapter 10 Advanced topics in relational databases

I Relational Database Modeling how to define

Entity-Relationship Model

The Relational Data Model. Functional Dependencies. Example. Functional Dependencies

Most database operations involve On- Line Transaction Processing (OTLP).

Creating Tables, Defining Constraints. Rose-Hulman Institute of Technology Curt Clifton

I Relational Database Modeling how to define

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

Logical Query Languages. Motivation: 1. Logical rules extend more naturally to. recursive queries than does relational algebra. Used in SQL recursion.

Relational Algebra. Spring 2012 Instructor: Hassan Khosravi

Relational Model. Lecture #6 Autumn, Fall, 2001, LRX

Data Formats and APIs

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

CSE 344 APRIL 16 TH SEMI-STRUCTURED DATA

CSCD43: Database Systems Technology. Lecture 4

On-Line Application Processing

Slides by: Ms. Shree Jaswal

The Relational Model of Data (ii)

Relational Model, Key Constraints

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

XML. Document Type Definitions. Database Systems and Concepts, CSCI 3030U, UOIT, Course Instructor: Jarek Szlichta

Likesèdrinker, beerè. Sellsèbar, beer, priceè. Frequentsèdrinker, barè

Functional Dependencies

SQL Overview. CSCE 315, Fall 2017 Project 1, Part 3. Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch

Programming and Database Fundamentals for Data Scientists

Introduction to Databases CSE 414. Lecture 2: Data Models

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

Warehousing. Data Mining

JSON - Overview JSon Terminology

Grouping Operator. Applying γ L (R) Recall: Outerjoin. Example: Grouping/Aggregation. γ A,B,AVG(C)->X (R) =??

ER Model. CSC 343 Winter 2018 MICHAEL LIUT

Running Example Tables name location

Recap and Schedule. Till Now: Today: Ø Query Languages: Relational Algebra; SQL. Ø Datalog A logical query language. Ø SQL Recursion.

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

COMP 430 Intro. to Database Systems

CS143: Relational Model

Contains slides made by Naci Akkøk, Pål Halvorsen, Arthur M. Keller, Vera Goebel

Query Languages for XML

Databases 1. SQL/PSM and Oracle PL/SQL

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

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

Keep relation as the fundamental. Compare with ëobject-oriented DBMS," which. and tacks on relations as one of many types.

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

Chapter 9 SQL in a server environment

E/R Diagrams! Converting E/R Diagrams to Relations!

CSIE30600 Database Systems Basic SQL 2. Outline

Basic SQL. Basic SQL. Basic SQL

Keep relation as the fundamental. Compare with ëobject-oriented DBMS," which. and tacks on relations as one of many types.

The data structures of the relational model Attributes and domains Relation schemas and database schemas

Design Theory for Relational Databases

Transcription:

Chapter 2 The relational Model of data Relational model introduction 1

Contents What is a data model? Basics of the relational model Next : How to define? How to query? Constraints on relations 2

What is a Data Model? A data model is a notation for describing data or information. It consists of three parts: Structure of the data: mathematical representation of data Operations on data. Constraints. 3

Two important data models The relational model ( and object-relational model): relational model = tables The semistructured-data model semistructured model = trees/graphs XML and its related standards. 4

A relation is a Table Attributes (column headers) Tuples (rows) name Winterbrew Bud Lite Each attribute has a domain, an element type. manf Pete s Anheuser-busch Relation name Beers 5

Schemas ( 模式 ) Relation schema = relation name and attribute list. Optionally: types of attributes. Example: Beers(name, manf) or Beers(name: string, manf: string) Database = collection of relations. Database schema = set of all relation schemas in the database. 6

Relation Instances ( 关系实例 ) is current set of rows for a relation schema. Example: beer relation Name Winterblue Budlit Manf. Peters A.B. Dynamic changing 7

Key of Relations There are many constraints on relations Key constraints is one of them For example: Beer(name, manf) If name is a key, do not allow two tuples to have the same name. Each object should be distinguished in the world 8

Why Relations? Very simple model. Often matches how we think about data. Abstract model that underlies SQL, the most important database language today. 9

a Running Example Beers(name, manf) Bars(name, addr, license) Drinkers(name, addr, phone) Likes(drinker, beer) Sells(bar, beer, price) Frequents(drinker, bar) Underline = key (tuples cannot have the same value in all key attributes). 10

Database Schemas in SQL SQL is primarily a query language, for getting information from a database. SQL also includes a data-definition component for describing database schemas. 11

Creating (Declaring) a Relation Simplest form is: CREATE TABLE <name> ( <list of elements> ); To delete a relation: DROP TABLE <name>; 12

Creating (Declaring) a Relation (cont.) To modify schemas ALTER TABLE <name> ADD <new attribute> ALTER TABLE <name> DROP <attribute>

Three kinds of table Stored relations: tables, a relation that exists in the database, can be modified or queried. real, stored. Views: relations defined by a computation. virutal, not really exists. Temporary tables: constructed by the SQL processor when it performs. thrown away, not stored.

Elements of Table Declarations Most basic element: an attribute and its type. The most common types are: INT or INTEGER (synonyms). REAL or FLOAT (synonyms). CHAR(n ) = fixed-length string of n characters. VARCHAR(n ) = variable-length string of up to n characters. 15

Example: Create Table CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL ); 16

SQL Values Integers reals Strings requires single quotes. Two single quotes = real quote, e.g., Joe s Bar. Bit strings of fixed or varying length, BIT(n) means bit string of length n Any value can be NULL. Boolean: true, false, unknown 17

Dates and Times in SQL The form of a date value is: DATE yyyy-mm-dd Example: DATE 2007-09-30 for Sept. 30, 2007. The form of a time value is: TIME hh:mm:ss Example: TIME 15:30:02.5 = two and a half seconds after 3:30PM. 18

Declaring Keys An attribute or list of attributes may be declared PRIMARY KEY or UNIQUE. Meaning: no two tuples of the relation may agree in all the attribute(s) on the list. PRIMARY KEY or UNIQUE attributes can be declared when creating a table. 19

Declaring Single-Attribute Keys Place PRIMARY KEY or UNIQUE after the type in the declaration of the attribute. Example: CREATE TABLE Beers ( ); name manf CHAR(20) UNIQUE, CHAR(20) 20

Declaring Multiattribute Keys A key declaration can also be another element in the list of elements of a CREATE TABLE statement. This form is essential if the key consists of more than one attribute. May be used even for one-attribute keys. 21

Example: Multiattribute Key The bar and beer together are the key for Sells: CREATE TABLE Sells ( bar CHAR(20), beer VARCHAR(20), price REAL, PRIMARY KEY (bar, beer) ); 22

PRIMARY KEY vs. UNIQUE In a table declaration: 1. PRIMARY KEY : only one PRIMARY KEY, No attribute of a PRIMARY KEY can ever be NULL in any tuple. 2. UNIQUE: several UNIQUE attributes, may have NULL s values. 23

Other Attributes Properties NOT NULL = every tuple must have a real value for this attribute. i.e. the value for this attribute may never be NULL. DEFAULT value = says that if there is no specific value known for this attribute s component in some tuple, use the stated <value>. 24

Example: Default Values CREATE TABLE Drinkers ( name CHAR(30) PRIMARY KEY, addr CHAR(50) DEFAULT 123 Sesame St., phone CHAR(16) ); 25

Effect of Defaults insert the fact that Sally is a drinker, but we know neither her address nor her phone. INSERT INTO Drinkers(name) VALUES( Sally ); 26

Effect of Defaults (cont.) What tuple appears in Drinkers? name addr phone Sally 123 Sesame St NULL If we had declared phone NOT NULL, this insertion would have been rejected. 27

Semistructured Data Based on trees. Motivation: flexible representation of data. sharing of documents among systems and databases. 28

Graphs of Semistructured Data Nodes = objects. Labels on arcs (like attribute names). Atomic values at leaf nodes (nodes with no arcs out). Flexibility: no restriction on Labels out of a node. Number of successors with a given label. 29

Example: Data Graph name Joe s bar servedat addr Maple beer name Bud manf A.B. root The bar object for Joe s Bar manf beer name M lob year 1995 prize The beer object for Bud Notice a new kind of data. award Gold 30

JavaScript Object Notation (JSON) Standard for serializing data objects Human-readable, useful for data interchange Useful for representing and storing semistructured data 31

JSON example { Beers : [ { name : Bud, manf : A.B., price : 13}, { name : Mobel, manf : A.B., Prize : { year : 1995, award : gold } ] } Basic constructs (recursive) Base values number, string, boolean, Objects { } sets of label-value pairs Arrays [ ] lists of values 32

Relational Model versus JSON Relational JSON Structure Tables Nested sets, array schema Fixed in advance Flexible, self descripting Queries Simple expressive language Not widely used Ordering none arrays Implementation Native system NOSQL system 33

XML versus JSON XML JSON Verbosity More Less Complexity More Less Validity DTD, XSD, widely used JSON scheme, not widely used Prog. Interface mismatch More direct Querying Xpath,Xquery Json Path, Json Query 34

Summarization Relational model, XML model, JSON notations A data model consists of three parts: Data structure Operations on the data? Constraints? Next: Relational algebra: operations & constraints. Relational algebra: the core of the SQL. 35