IS 325 Notes for Monday October 21, 2013. 2 Set Theory Concepts Understanding set theory helps us to: see things in terms of systems organize things into.

Slides:



Advertisements
Similar presentations
พีชคณิตแบบสัมพันธ์ (Relational Algebra) บทที่ 3 อ. ดร. ชุรี เตชะวุฒิ CS (204)321 ระบบฐานข้อมูล 1 (Database System I)
Advertisements

The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
1 CHAPTER 4 RELATIONAL ALGEBRA AND CALCULUS. 2 Introduction - We discuss here two mathematical formalisms which can be used as the basis for stating and.
Relational Model (CB Chapter 4) CPSC 356 Database Ellen Walker Hiram College.
Concepts of Database Management Sixth Edition
Concepts of Database Management Seventh Edition
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
Database Systems Chapter 6 ITM Relational Algebra The basic set of operations for the relational model is the relational algebra. –enable the specification.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Introduction to Oracle9i: SQL1 Basic SQL SELECT Statements.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
CS355 - Theory of Computation Lecture 2: Mathematical Preliminaries.
Concepts of Database Management, Fifth Edition
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Module Title? DBMS E-R Model to Relational Model.
Relational Model Session 6 Course Name: Database System Year : 2012.
1 The Relational Data Model, Relational Constraints, and The Relational Algebra.
Chapter 4 The Relational Model.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Copyright © Curt Hill The Relational Algebra What operations can be done?
Concepts and Terminology Introduction to Database.
THE RELATIONAL DATA MODEL CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
MIS 301 Information Systems in Organizations Dave Salisbury ( )
Database Management COP4540, SCS, FIU Relational Model Chapter 7.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
DBSQL 3-1 Copyright © Genetic Computer School 2009 Chapter 3 Relational Database Model.
Module 3: The Relational Model.  Overview Terminology Relational Data Structure Mathematical Relations Database Relations Relational Keys Relational.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Chapter 2 Adapted from Silberschatz, et al. CHECK SLIDE 16.
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – Relational Model.
CSE314 Database Systems The Relational Algebra and Relational Calculus Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
Chapter 6 The Relational Data Model and the Relational Algebra.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Concepts of Database Management Seventh Edition
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Using Relational Databases and SQL John Hurley Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
Chapter 6 The Relational Algebra Copyright © 2004 Ramez Elmasri and Shamkant Navathe.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Advanced Relational Algebra & SQL (Part1 )
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Presented By: Miss N. Nembhard. Relation Algebra Relational Algebra is : the formal description of how a relational database operates the mathematics.
Query Processing – Implementing Set Operations and Joins Chap. 19.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
CS122 Using Relational Databases and SQL Huiping Guo Department of Computer Science California State University, Los Angeles 2. Single Table Queries.
1 The Relational Data Model David J. Stucki. Relational Model Concepts 2 Fundamental concept: the relation  The Relational Model represents an entire.
Relational Algebra COMP3211 Advanced Databases Nicholas Gibbins
Tarik Booker CS 122. What we will cover… Tables (review) SELECT statement DISTINCT, Calculated Columns FROM Single tables (for now…) WHERE Date clauses,
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Chapter (6) The Relational Algebra and Relational Calculus Objectives
Writing Basic SQL SELECT Statements
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Chapter 2: Intro to Relational Model
CS122 Using Relational Databases and SQL
Chapter 2: Intro to Relational Model
Presentation transcript:

IS 325 Notes for Monday October 21, 2013

2 Set Theory Concepts Understanding set theory helps us to: see things in terms of systems organize things into groups begin to understand logic begin to understand database design and query structure

3 A set is a well defined collection of elements or members. An element or member or instance is an object contained in a set. If every element of Set A is also contained in Set B, then Set A is a subset of Set B. Set A is a proper subset of Set B if Set B has more elements than Set A. The universal set contains all of the elements relevant to a given discussion.

4 SymbolMeaning Upper casedesignates set name Lower casedesignates set elements (normally) { }enclose elements in set  or  is or is not an element of  is a subset of (could be equal sets)  is a proper subset of  is not a subset of  is a superset of (could be equal sets)  is a proper superset of | or : such that (if a condition is true) | |the cardinality of a set

5 Sets can be defined two ways: by listing each element (enumeration) A = {4,8,6,2,10} (Note: order isn’t important) by defining the rules for membership A = {x | x is a positive even integer  10} A = {2, 3, 4, 1} 2  A10  A 3  A Ron  A B = {x | x is an even counting number  4} 2  B17  B 4  B“CSI”  B

6 A = {x | x is an English letter A..E} set A contains: A, B, C, D, E B = {x | x is an English letter contained in ADA} set B contains: A, D C = {S, A, N, D } set C contains: A, D, N, S Set Relationships: A  AA  BA  C B  AB  BB  C C  AC  BC  C

7 Two sets are equal if and only if they contain precisely the same elements. Note: The order in which the elements are listed is unimportant. Elements may be repeated in set definitions without increasing the size of the sets. A = {1, 2, 3, 4} B = {1, 4, 2, 3} A  B and B  A; therefore, A = B and B = A A = {1, 2, 3, 4, 4, 3, 2} B = {1, 2, 3, 4} A  B and B  A; therefore, A = B and B = A

8 Cardinality refers to the number of elements in a set. A finite set has a countable number of elements. An infinite set contains elements that are not countable. |A| represents the cardinality of Set A. A = {4, 5, 6, 7, 1, 2, 3, 8, 9} |A| = 9 A = {4, 4, 4, 5, 6, 1}|A| = 4 A = {x | x is an even number } |A|= 1

9 The universal set is the set of all things pertinent to any given model or discussion… designated by the symbol U U = {all students enrolled at SUNYIT} Subsets of U: A = {CS students} B = {IS325} C = {female students} D = {female students with GPA  3.25}

10 Any set that contains no elements is called the empty set. The empty set is a subset of every set including itself. { } or  A = {x | x is a SUNYIT Chicken Hawk} B = {x | x is an English letter  }

11 Set: a well defined collection of items. repetition usually not allowed (else: bag or multiset) order is unimportant (else: sequence) Type: is a set of possible items. Element/member/instance: each item of a particular type for a particular set. In a set, because repetition is usually not allowed, each element/member/instance is unique.

12 Set Operators/Operations The union of two sets is the set that contains elements belonging to either of the two sets. Equivalent to the “or” Boolean operation. Union symbol:  A = {a, b, c, d}B = {c, d, e, f} A  B = {a, b, c, d, e, f}

13 A = {a, b, c, d} B = {c, d, e, f} A  B = {a, b, c, d, e, f} A = {a, b, c, d} B = {x, y, z} A  B = {a, b, c, d, x, y, z}

14 The intersection of two sets is the set of all elements common to both sets. The intersection of disjoint sets is the empty set. Equivalent to the “and” Boolean operation. Intersection symbol:  A = {a, b, c, d}B = {a, b, y, z} A  B = {a, b}

15 A = {a, b, c, d} B = {a, b, x, y} A  B = {a, b} A = {1, 3, 5, 2, 4, 6} B = {7, 8, 10, 11, 9} A  B = 

16 The relative complement or difference of two sets is the set of elements contained in one, but not both, of the sets Similar to the “exclusive or” Boolean operation. Relative complement or difference symbol: — A = {a, b, c, d} and B = {c, d, e, f} A — B = {a, b} B — A = {e, f}

17 A = {a, b, c, d} B = {c, d, e, f } A — B = {a, b} B — A = {e, f}

18 Relational Model Concepts The relational model represents the database as a collection of relations. Think of a relation as a table of values arranged as rows and columns. In the formal relational model terminology, a row is called a tuple, a column is called an attribute, and the table is called a relation. Additional descriptors: file, record, field. We’ll use these terms interchangeably (most everyone does).

19 Tables/Relations are collections (sets) of like things (things that can be well defined)… the table or relation usually is named for the set of like things it contains: Students, Buildings, Courses, Classes, Clients, etc... A relational schema is used to describe a relation, R is called name of this relation A relation schema R, denoted by R(A 1, A 2, …, A n ) is made up of a relation name R and a list of attributes A 1, A 2, …, A n. Each attribute A i is the name of a unique role, characteristic, or trait that pertains specifically to the set of like things for which the relation is named.

20 Each attribute A i has an acceptable set or range of values… this acceptable set of values for an attribute is known as the attribute’s domain D (or Dom)in the relation schema R. A domain is a set of values that are indivisible or atomic. Some examples of atomic values: Last Name: the set of names of people Local_phone_numbers: the set of valid seven-digit sequences used by the local phone company Social_security_numbers: the set of valid 9-digit social security numbers issued by US Govt. GPA: a real value between 0 and 4.

21 Some examples of values that are not atomic: Name: can contain valid first and last names plus MI Phone_numbers: valid area code and country code with valid seven digit sequences used by phone a co. Address: includes valid street, city, state, and zip info Each domain has an associated data type (always) and format (usually). The degree of a relation is the number of attributes n of its relation schema. The cardinality of a relation is the number of rows or tuples that are contained in the relation.

22 The degree of a relation is the number of attributes n of its relation schema. It’s possible for attributes to share the same domain within a single relation. However, because attributes must be unique, that specific domain must be capable of fulfilling multiple roles. The cardinality of a relation is the number of rows or tuples that are contained in the relation.

23 Rows represent unique instances or occurrences that are part of the set of like things contained in a table. This means that rows cannot be duplicated. Note: Sets do not usually contain duplicate members (if duplicate members are allowed the set becomes a bag or multiset). Because tables are collections or sets of like things, and because a set (by definition) has no order, the order of rows in a table has no meaning (theoretically). Note: Sets, by definition, have no order (if order is important then a set becomes a sequence).

24 Relational Model - Table Characteristics Each table has a unique name (role). Each column in a single table has a unique name (role). The order of columns is irrelevant. All rows share in a table share the same format/entries. Values under a single column belong to the same domain.

25 Every entry (row/column intersection) is an atomic value. The order of the rows is irrelevant. No two rows are identical.

26 Table Keys Key/Superkey Candidate Key Primary Key Foreign Key Integrity Constraints Entity Integrity Relational Integrity

27 Basic Relational Algebra Operations We need a means of interacting with and manipulating relations. Relational algebra operations enable the us to specify basic retrieval requests and perform manipulations. The result of a relational algebra operation is a new relation, which was formed from one or more relations. A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation.

28 The relational algebra operations are two types: set operations from mathematical set theory (union, intersection, difference, Cartesian product) operations developed specifically for relational databases (selection, projection, join). To perform a union, intersection, or difference operations on two relations, the relations must have union compatible schemas.

29 Two relations have a union compatible schema if they are of the same degree and if the corresponding domains of the corresponding attributes are of the same types. Note: the names of the attributes do not need to be the same. Union, intersection, and difference function as you might expect: union: A  B = {t | t  A  t  B} intersection: A  B = {t | t  A  t  B} difference (minus): A – B = {t | t  A  t  B}

30 The Cartesian product operation can be performed between any two non-empty relations (there is no need for union compatibility). Cartesian product symbol:  A  B means that we want to form a resultant relation by concatenating every row in relation A with every row in relation B. A  B = { x  y | x  A  y  B } (x  y: “the concatenation of rows x and y”)

31 The degree of the relation that results from a Cartesian product operation is the sum of the degrees of the participating relations. The cardinality of the relation that results from a Cartesian product operation is the product of the cardinalities of the two participating relations. Related to the Cartesian product set operation is the natural join or equijoin operation which we commonly refer to as just join.

32 The join operation combines two relations that have at least one common attribute. In general, the join operation combines two relations on their common (shared) attributes. Note: common attributes is a term that describes those attributes that possess the same domain and the same underlying meaning or role… they don’t need to have the same name.

33 The selection operation is used to select a subset of the tuples from a relation that satisfy a selection condition. The selection operation works like a filter that allows the rows/tuples, that satisfy a specific condition or set of conditions, to pass through the filter. Selection symbol:  (the Greek letter sigma) Select from STUDENT those who have Major = CS: General form of selection:

34 The selection condition is a Boolean expression. The relation resulting from the selection operation has the same attributes as R. The selection condition has two basic forms: or The selection condition is applied to each row in the relation (R). If the condition evaluates to true, the row is selected.

35 Selection is a unary (can be applied to only a one relation). Multiple selection conditions can be combined into a single selection operation via logical operators (AND, OR, NOT).

36 The projection operation works like a filter that allows only desired/specifically list attributes/columns to be passed to a resultant table/relation. Projection symbol: π (the Greek letter pi) Project from STUDENT the attributes Stud_ID, First_name, Last_name, GPA: General form of projection:

37 If the attribute list includes only non-key attributes of R, duplicates will probably occur. Projection removes any duplicate tuples. Binary operators: union, intersection, difference, Cartesian product, join Unary operators: selection, projection

Let's Introduce SQL Broad overview No need to take notes

Candidate Keys A minimal unique identifier (may contain multiple attributes). Example: –Student table  Student_ID or SSN

Primary Keys A candidate key that has been singled out to uniquely identify each record. Difference between candidate key and primary key? A table may have more than one candidate key, but can only have one primary key. Out of all candidate keys, which key do you choose to be the primary key?

Foreign Keys Used to substantiate a relationship. A foreign key is just a candidate key at the other end of the relationship. We usually say a foreign key ‘references’ a primary key. Example: –In Ratings table, MemberID is a foreign key that references the primary key MemberID in the Members table.

Operators Arithmetic –+, -, *, /, % Conditional operators: –, =, =, <>, BETWEEN Logical operators: AND, OR, NOT Other operators: IS NULL

What is an SQL Selection Query? An SQL selection query is a list of SQL clauses, starting with the SELECT clause The SELECT clause is ALWAYS the first clause in a selection query Selection query: –SELECT select_clause_arguments FROM from_clause_arguments WHERE where_clause_arguments GROUP BY group_by_clause_arguments HAVING having_clause_arguments ORDER BY order_by_clause_arguments;

The SELECT Clause In a SELECT clause you can perform simple calculations, even if you don’t select any data from a table Examples: –SELECT 1 + 2; –SELECT (1 + 3)*2 – 7;

SQL Data Types Strings –'Peterson', 'Joe\ 's ', 'Peterson', "Joe 's", "A\\B" Integers –1, 2, 3, etc. Decimals –1.45, 2.83, NULL –NULL, null

SQL Data Types (Cont.) Dates –' ', ' ' –Dates are in 'YEAR-MONTH-DAY' string format Boolean –TRUE –FALSE –NULL

Comments Comments are always ignored Simple Comments –Use double minus signs followed by a space –-- This is a simple comment Bracketed (C-Style) Comments –Place comment inside the asterisks /**/ –/* This is a comment */

The FROM Clause When combined with the FROM clause, the SELECT clause pulls data from the table specified in the FROM clause Syntaxes: –To list specific attributes... SELECT attribute1, attribute2,... FROM tablename; –To list all attributes... use the * character... SELECT * FROM tablename;

SELECT FROM Examples Examples: –Display a list of all movie titles. SELECT Title FROM Movies; –List all attributes for all companies. SELECT * FROM Companies;

Calculated Columns You can use attributes in mathematical expressions of the SELECT clause. Examples: –List the names of all movie-related people along with their height in feet (assuming it is recorded in inches). SELECT FirstName, LastName, Height/12 FROM People;

The WHERE Clause Use the WHERE clause to filter results Syntax: –SELECT attribute1, attribute2,... FROM tablename WHERE boolean_expression; The expression in the WHERE clause is executed once per row. –If expression is true, row is retrieved. –If expression is false or null, row is not retrieved.

WHERE Clause Examples Examples: –List the names of all production companies from Canada. SELECT Name FROM Companies WHERE Country = 'CAN';

Boolean Value Expressions AND OR NOT

AND Syntax: boolean_arg1 AND boolean_arg2 Returns: TRUE, FALSE, or NULL Description: Returns TRUE if both arguments are TRUE Examples: –SELECT TRUE AND TRUE; -- TRUE –SELECT TRUE AND FALSE; -- FALSE –SELECT FALSE AND FALSE; -- FALSE

OR Syntax: boolean_arg1 OR boolean_arg2 Returns: TRUE, FALSE, or NULL Description: Returns TRUE if any argument is TRUE Examples: –SELECT TRUE OR TRUE; -- TRUE –SELECT TRUE OR FALSE; -- TRUE –SELECT FALSE OR FALSE; -- FALSE

NOT Syntax: NOT boolean_arg Returns: TRUE, FALSE, or NULL Description: Returns TRUE if argument is FALSE, FALSE if argument is TRUE, and NULL otherwise. Examples: –SELECT NOT TRUE; -- FALSE –SELECT NOT FALSE; -- TRUE –SELECT NOT NULL; -- NULL

AND, OR, and NOT Examples Examples: –Display all members from the United States or Australia. SELECT Username FROM Members WHERE Country = 'USA' OR Country = 'AUS'; –Display all male members from the United States. SELECT Username FROM Members WHERE Country = 'USA' AND Gender = 'M'; –Display all members who are not Pro Members (Boolean). SELECT Username FROM Members WHERE NOT ProMember;

Mixing ANDs and ORs Be careful of parentheses!!! –Display all male members from the United States or Australia (correct). SELECT Username FROM Members WHERE (Country = 'USA' OR Country = 'AUS') AND Gender = 'M'; –Display all male members from the United States or Australia (wrong). SELECT Username FROM Members WHERE Country = 'USA' OR Country = 'AUS' AND Gender = 'M';

BETWEEN Like saying attribute >= x AND attribute <= y Note that comparison includes boundaries Example: –List the titles of all movies with a runtime between one to two hours. SELECT Title, Runtime/60 FROM Movies WHERE Runtime/60 BETWEEN 1 AND 2;

IS NULL Do not use conditional operators to compare NULL NULL is a special keyword, not a specific value Why? Consider: –SELECT (NULL = NULL); -- NULL –SELECT (NULL IS NULL); -- TRUE To determine if something is NULL –Use the IS NULL keyword To determine if something is not NULL –Use the IS NOT NULL keyword

IS NULL Examples Examples: –List the first and last names of all movie-related people whose heights are known. SELECT FirstName, LastName FROM People WHERE Height IS NOT NULL; –List the first and last names of all movie-related people whose heights are unknown. SELECT FirstName, LastName FROM People WHERE Height IS NULL;

LIKE Predicate Used for pattern matching strings –%  placeholder for zero or more characters –_  placeholder for any single character Syntax: –StringExpression LIKE PatternConstant –Returns TRUE, FALSE or NULL Examples: –'% sql %' means any number of characters followed by a space, then sql followed by a space, then any number of characters.

LIKE Example Example: –List all member usernames and their s for only those members that come from a.com domain. SELECT FirstName, LastName, FROM Members WHERE LIKE '%.com';

NOT LIKE Negation of LIKE Ignores comparing NULL strings Example: –List all member usernames and their s for only those members that do not come from a.com domain. SELECT FirstName, LastName, FROM Members WHERE NOT LIKE '%.com';

The ORDER BY Clause To sort the result of a selection query, use the ORDER BY clause Syntax: –SELECT A 1, A 2,... FROM tablename ORDER BY F 1 [ASC | DESC], F 2 [ASC | DESC],... Example: –List all movie titles in descending order. SELECT Title FROM Movies ORDER BY Title DESC;

Ordering By Multiple Attributes Note that the ORDER BY clause can take multiple attribute names. Example: –List the first and last names of all movie-related people along with their gender. Sort by gender in descending order then by first name in ascending order. SELECT FirstName, LastName, Gender FROM People ORDER BY Gender DESC, FirstName ASC;

DISTINCT Use when you want to remove duplicate results Example: –Display a list of unique countries that all members come from (wrong answer). SELECT Country FROM Members; –Display a list of unique countries that all members come from (correct answer). SELECT DISTINCT Country FROM Members;