Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 IS 325 Notes for Monday October 21, 2013

2 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 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 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 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 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 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 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 11..13} |A|= 1

9 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 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  712341345}

11 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 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 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 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 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 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 17 A = {a, b, c, d} B = {c, d, e, f } A — B = {a, b} B — A = {e, f}

18 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 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 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 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 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 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 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 25 Every entry (row/column intersection) is an atomic value. The order of the rows is irrelevant. No two rows are identical.

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

27 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 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 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 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 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 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 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 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 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 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 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

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

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

40 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?

41 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.

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

43 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;

44 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;

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

46 SQL Data Types (Cont.) Dates –'2005-02-14', '1973-08-13' –Dates are in 'YEAR-MONTH-DAY' string format Boolean –TRUE –FALSE –NULL

47 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 */

48 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;

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

50 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;

51 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.

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

53 Boolean Value Expressions AND OR NOT

54 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

55 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

56 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

57 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;

58 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';

59 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;

60 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

61 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;

62 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.

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

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

65 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;

66 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;

67 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;


Download ppt "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."

Similar presentations


Ads by Google