Relational Data Model Relational Data...

14
1 Lecture 2: MIS511-FALL- 1516 1 Introduction to Relational Model & Structured Query Language (SQL) Database Resources Management Fall - 1516 Chapter Objectives Basics of Relational Model SQL Basics of SELECT statement 2 MIS511-FALL-1516 Relational Data Model First introduced by E.F Codd. Based on mathematical set theory. Relational data model has 3 main components: Relational Data Structure – Where to store data ? data organization Relational Data Integrity – How to maintain integrity? facilities (constraints) are included to specify business rules to maintain integrity of data as they are being manipulated Data Manipulation – How to manipulate data ? operations (using SQL language) used to manipulate stored data MIS511-FALL-1516 3 Relational Data Structure Main data structure used is a relation. Relation is a two-dimensional grid (table) that holds data about the entity (thing we focus on). SKU_DATA SKU_DATA (SKU, SKU_Description, Department, Buyer) Relational Database is a collection of relations with distinct relation names. MIS511-FALL-1516 4

Transcript of Relational Data Model Relational Data...

Page 1: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

1

Lecture 2:

MIS511-FALL-

15161

Introduction to Relational Model

&

Structured Query Language (SQL)

Database Resources Management

Fall - 1516

Chapter Objectives

• Basics of Relational Model

• SQL

– Basics of SELECT statement

2MIS511-FALL-1516

Relational Data Model

• First introduced by E.F Codd.

• Based on mathematical set theory.

• Relational data model has 3 main components:

– Relational Data Structure – Where to store data ?

• data organization

– Relational Data Integrity – How to maintain integrity?

• facilities (constraints) are included to specify business rules

to maintain integrity of data as they are being manipulated

– Data Manipulation – How to manipulate data ?

• operations (using SQL language) used to manipulate stored

data

MIS511-FALL-1516 3

Relational Data Structure

• Main data structure used is a relation.

– Relation is a two-dimensional grid (table) that holds data

about the entity (thing we focus on).

SKU_DATA

SKU_DATA (SKU, SKU_Description, Department, Buyer)

• Relational Database is a collection of relations with

distinct relation names.

MIS511-FALL-15164

Page 2: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

2

Relational Model Terminology

• Relation is a table with columns and rows.

• Attribute is a named column of a relation.

• Domain is the set of allowable values for one or moreattributes.

• Tuple is a row of a relation.

• Degree is the number of attributes in a relation.– Meta data

• Cardinality is the number of tuples in a relation.– User data

MIS511-FALL-15165

Alternative Terminology

MIS511-FALL-15166

Properties of Relation

• Relation name is distinct from all other relation names in the database.

• Each attribute has a distinct name.

• Values of an attribute are all from the same domain

• Each cell of relation contains exactly one atomic(single) value.

• Each tuple is distinct; there are no duplicate tuples.

• Order of attributes has no significance.

• Order of tuples has no significance, theoretically.

MIS511-FALL-1516 7

Schema

• is a textual representation of the database relations

– defined by its name followed by a set of attribute and

domain name pairs in paranthesis.• A domain is the set of values that can be assigned to an attribute

SKU_DATA (SKU: numeric, SKU_Description: String, Department:String, Buyer:String)

• Relational database schema

– set of relation schemas, each with a distinct name

MIS511-FALL-1516

8

SKU_DATA (SKU, SKU_Description, Department, Buyer

ORDER_ITEM (OrderNumber, SKU, Quantity, Price, ExtendedPrice)

RETAIL_ORDER (OrderNumber, StoreNumber, StoreZip, OrderMonth, OrderYear, OrderTotal)

Page 3: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

3

Data Integrity

• Three types of data integrity constraints

– Domain Constraints

– Entity Constraints

– Referential Constraints

MIS511-FALL-15169

Domain Constraints

• The domain constraint states that the values of

an attribute must be from the same domain.

– A domain is the set of values that can be assigned to

an attribute.• Description values are limited to String of length 20

• Department values are limited to – Water Sports, Cycling, Climbing, Camping...

• STU_NAME values are limited to String

• STU_DOB values are limited to Date

• STU_GPA values are limited to Numeric– the range 0–4, inclusive, the domain is [0,4],

MIS511-FALL-151610

Relational Keys

• A key consists of one or more attributes that determine other attributes

– We need to specify one or more attributes (relational keys) that uniquely identify each tuple in a relation.

– Primary Key : It is a set of one or more attributes that has been selected to identify unique tuples

• Minimum set

• Composite Key : A key that has more than one attribute

MIS511-FALL-151611

Relational Keys• Foreign Key

– An attribute or a set of attributes for a relation that serves as a link to the primary key of some other (sometimes the same) relation

• whose values match the primary key values in the other relation.

MIS511-FALL-151612

Page 4: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

4

Entity/Referencial Integrity Constraints

• Entity Integrity

– ensure that every relation of a relational data model has

a primary key

• the data values for the attributes of the primary key cannot be

NULL.

– A null is no value at all. It does not mean a zero or a space.

• Referential Integrity

– ensure that the foreign keys values of a relation must

match the primary key value of some relation;

• otherwise, the value of a foreign key must be NULL.

• Many DBMSs enforce integrity constraints automatically.

MIS511-FALL-151613

Entity/Referencial Integrity

•PRODUCT

•VENDOR

•EMPLOYEE

MIS511-Fall-1516 14

Data Manipulation

• A way to access and manipulate the data in the

relations.

• Example of a manipulation language :

– Structured Query Language (SQL)

UPDATE SKU_DATA

SET Department = ‘Sport’

WHERE SKU=‘301000’;

MIS511-FALL-151615

Data Manipulation : SQL

• Main language for relational DBMSs.

– data sublanguage• creating and processing database data and metadata

• not a full featured programming language

– non-procedural• specify what information you require, rather than how to get it

• can be used by range of users

– relatively easy to learn• essentially free-format, vocabulary less than 100 words

• consists of standard English words: SELECT, INSERT, UPDATE...

– An ISO standard now exists for SQL, • the formal and defacto standard language for relational db.

• several different dialects (Oracle, Microsoft SQL Server, MySQL, IBM’s DB2, Microsoft Access)

MIS511-FALL-1516 16

Page 5: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

5

History

• In 1974, D. Chamberlin (IBM San Jose Laboratory) defined languagecalled ‘Structured English Query Language’ (SEQUEL).

• A revised version, SEQUEL/2, was defined in 1976 but name wassubsequently changed to SQL for legal reasons

• In late 70s, ORACLE appeared and was probably first commercial RDBMSbased on SQL.

• In 1987, ANSI and ISO published an initial standard for SQL.

• In 1989, ISO published an addendum that defined an ‘IntegrityEnhancement Feature’.

• In 1992, first major revision to ISO standard occurred, referred to as SQL2or SQL/92.

• In 1999, SQL:1999 was released with support for object-oriented datamanagement.

• In late 2003, SQL:2003 was released.

• Most recent version is SQL:2008

MIS511-FALL-1516 17

SQL : DDL DCL and DML

• SQL statements can be divided into three categories:

• Data definition language (DDL) statements– Used for creating tables, relationships, and other structures.

• Data Control Language (DCL) statements

– Statements to specify transaction control, semantic integrity (triggers and assertions), authorization and management of privileges

– Statements for specifying the physical storage parameters such as file structures and access paths (indexes)

– Both Covered later.

• Data manipulation language (DML) statements.– Used for queries and data modification

– Covered NOW

MIS511-FALL-151618

Cape Codd Outdoor Sports

• Cape Codd Outdoor

Sports is a fictitious

company based on an

actual outdoor retail

equipment vendor.

• Three tables are used:

– RETAIL_ORDER,

– ORDER_ITEM, &

– SKU_DATA,

• (SKU = Stock Keeping Unit).

MIS511-FALL-1516

© 2012 PPH

19

Retail Sales Tables

MIS511-FALL-1516

© 2012 PPH(student_version)

20

Cape Codd Outdoor Sports

Page 6: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

6

MS Access and its SQL

MIS511-Fall-1516 21

The SQL SELECT Statement

• Used to list contents of table

• Syntax:

SELECT columnList

FROM relationList

• columnList represents one or more attributes, separated by commas

• Asterisk can be used as wildcard character to list all attributes

• All SQL statements end with a semi-colon (;)

MIS511-FALL-1516

© 2012 PPH

22

Specific Columns on one Relation

MIS511-FALL-1516

© 2012 PPH(student_version)

23

List the departments and buyers

Specifying Column Order

MIS511-FALL-1516

© 2012 PPH(student_version)

24

List the buyers and departments

Page 7: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

7

The DISTINCT Keyword

MIS511-FALL-1516

© 2012 PPH(student_version)

25

List the buyers and departments

(elimiate duplicates)

Selecting All Columns: Asterisk (*) Keyword

MIS511-FALL-1516

© 2012 PPH(student_version)

26

List all stock’s data (SKU, SKU_Desc., Dept., buyer)

Sorting the Results: ORDER BY

MIS511-FALL-1516

© 2012 PPH(student_version)

27

List orders sorted by order number

NOTE: the actual relation contents are unaffected by the ORDER BY

SELECT columnList

FROM relationList

[ORDER BY columnlist [ASC | DESC] ] |

Multi-level Sorting : ORDER BY

MIS511-FALL-1516

© 2012 PPH(student_version)

28

List orders sorted by order number & (then) price

Page 8: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

8

Sort Order: Ascending & Descending

MIS511-FALL-1516

© 2012 PPH(student_version)

29

List orders sorted by price descending& order number

NOTE: The default sort order is ASC – does not have to be specified

Specific Rows from One Table : WHERE

MIS511-FALL-1516

© 2012 PPH(student_version)30

List stock data in “Water Sports” Dept.

NOTE: SQL wants a plain ASCII single quote: ' NOT ‘ !most SQL implementations yield case-sensitive match

SELECT columnList

FROM relationList

[WHERE conditionList]

[ORDER BY columnList [ASC | DESC] ]

Specific Columns and Rows from one Table

MIS511-FALL-1516

© 2012 PPH(student_version)

31

List only the desc. and buyers in the “Climbing” Dept.

Criteria / Condition

• Expression using one of the operators,

– a column name on one side and a value on the other

• Department = 'Water Sports'

– column names on both sides:

• Price <= ExtendedPrice

• Relational operators : =, <>, >, >=, <, <=

• Logical operators : AND, OR, NOT

– chain expressions together with logical operators.

• AND : row must meet all of the conditions

• OR : a row needs to meet only one of the conditions

• NOT : inverts the result of expression

MIS511-FALL-1516

© 2012 PPH(student_version)32

Page 9: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

9

WHERE Clause Options: AND

MIS511-FALL-1516

© 2012 PPH(student_version)

33

List stock data in WS depart. for Nancy

WHERE Clause Options: OR

MIS511-FALL-1516

© 2006 PPH(student_version)

34

List stock data in either “Camping” or “Climbing” depts.

WHERE Clause Options: IN

MIS511-FALL-1516

© 2012 PPH(student_version)

35

List stock data for Nancy, Cindy and Jerry

Used to check whether an attribute

value matches any value within a

value list

WHERE Clause Options: NOT IN

MIS511-FALL-1516

© 2012 PPH(student_version)

36

List stock data for someone other than NM, CL, JM

Page 10: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

10

WHERE Clause Options: Range with BETWEEN

MIS511-FALL-1516

© 2012 PPH(student_version)

37

List order data for

ExtendedPrice btw 100 and 200 Used to check whether an attribute

value is within a range

WHERE Clause Options: Range with Math Symbols

MIS511-FALL-1516

© 2012 PPH(student_version)

38

WHERE Clause Options : IS NULL

MIS511-FALL-1516

© 2012 PPH(student_version)

39

List items with

no Department info

Used to check whether an attribute

value is null

WHERE Clause Options : LIKE and Wildcards

• Used to check whether an attribute value

matches a given string pattern

– in conjunction with wildcards to find patterns within

string attributes

• SQL 92 Standard (SQL Server, Oracle, etc.):

• _ = Exactly one character

• % = Any set of one or more characters

– MS Access (based on MS DOS)

• ? = Exactly one character

• * = Any set of one or more characters

MIS511-FALL-1516

© 2012 PPH(student_version)

40

Page 11: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

11

WHERE Clause Options:LIKE and Wildcards (Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

41

List stock data for any buyer starting with “Pete”

WHERE Clause Options:LIKE and Wildcards (Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

42

List stock data that contains “Tent” in its description

WHERE Clause Options:LIKE and Wildcards (Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

43

List stock data that contains 2 in 3rd position

SQL Built-in Functions

• There are five SQL Built-in Functions:

– COUNT returns number of values in specified column.

– SUM returns sum of values in specified column.

– AVG returns average of values in specified column.

– MIN returns smallest value in specified column.

– MAX returns largest value in specified column.

• Each operates on a single column of a table and returns a single value.

• COUNT, MIN, and MAX apply to numeric and non-numeric fields, but SUM and AVGmay be used on numeric fields only.

• Apart from COUNT(*), each function eliminates nulls first and operates only onremaining non-null values.

MIS511-FALL-1516

© 2012 PPH(student_version)

44

Page 12: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

12

SQL Built-in Functions(Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

45

Find the sum of total orders for order 3000

An alias is an alternative name

given to a column or relation in

any SQL

SQL Built-in Functions(Cont’d)

SELECT OrderNumber,SUM(ExtendedPrice)

FROM ORDER_ITEM

MIS511-FALL-1516

© 2012 PPH(student_version)

46

SELECT OrderNumber

FROM ORDER_ITEM

WHERE Price > AVG(Price);

SQL Built-in Functions (Cont’d)

SELECT SUM (ExtendedPrice) AS OrderItemSum,

AVG (ExtendedPrice) AS OrderItemAvg,

MIN (ExtendedPrice) AS OrderItemMin,

MAX (ExtendedPrice) AS OrderItemMax

FROM ORDER_ITEM;

MIS511-FALL-1516

© 2012 PPH(student_version)

47

SQL Built-in Functions (Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

48

Find the number of orders

Page 13: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

13

SQL Built-in Functions (Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

49

Find the number of departments (distinct)

String Functions in SELECT

MIS511-FALL-1516

© 2012 PPH(student_version)

50

List the Buyer and departments into a single

colum named Sponsor

String Functions in SELECT (Cont’d)

MIS511-FALL-1516

© 2012 PPH(student_version)

51

List the Buyers and Departments into a single

column named Sponsor

Arithmetic Operations in SELECT

MIS511-FALL-1516

© 2012 PPH(student_version)

52

Compute and compare the Extended Price

Page 14: Relational Data Model Relational Data Structuremisprivate.boun.edu.tr/darcan/mis511/slides_1516/2... · 2015. 10. 5. · 2 Relational Model Terminology • Relation is a table with

14

Arithmetic in SELECT

MIS511-FALL-1516

© 2012 PPH(student_version)53

• Compute and compare sum of the Extended

Price