Download presentation
Presentation is loading. Please wait.
1
Lecture 2 Relational Database
Reason of RDB's popularity: simplicity, easy to use, properties of rob, tables for entity and their relation Concepts, relational database
2
Outline Relational Data Model Schema and Instance
Mathematica Fundament select: s project: Õ union: È set difference: – Cartesian product: x rename: r
3
Relational Data Model R
4
Example of a Relation attributes (or columns) tuples (or rows)
Relations, attributes, tuples, ids, the semantics of a row as well as a column, take the table as an example A tuple is a sequence of values Similar to instructors, departments….
5
Relationship between two entities
Related tuples(tables)
6
Attribute Types The set of allowed values for each attribute is called the domain of the attribute Attribute values are (normally) required to be atomic; that is, indivisible The special value null is a member of every domain. Indicated that the value is “unknown” or does not exist The null value causes complications in the definition of many operations domain: Permitted values for a column Atomic: depends the real application, for example, Address Null: causes a lot of difficulties from operations tables, should be eliminated
7
Relation Schema and Instance
A1, A2, …, An are attributes R = (A1, A2, …, An ) is a relation schema Example: instructor = (ID, name, dept_name, salary) Formally, given sets D1, D2, …. Dn a relation r is a subset of D1 x D2 x … x Dn Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai Î Di Database Schema: the logical design of the database and consists of a list of attributes and their corresponding domains ,does not generally change database instance, which is a snapshot of the data in the database at a given instant in time, change with time Relation between tuples The current values (relation instance) of a relation are specified by a table An element t of r is a tuple, represented by a row in a table
8
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in an arbitrary order) Example: instructor relation with unordered tuples Tuples is in set, the properties of a set the order in which the tuples appear in a relation is irrelevant, since a relation is a set of tuples. Any two relations, if they have the same schema and same tuples, they are the same as long as they have the same set of tuples whatever their order is.
9
Keys Let K Í R K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation r(R) Example: {ID} and {ID,name} are both superkeys of instructor. Superkey K is a candidate key if K is minimal Example: {ID} is a candidate key for Instructor One of the candidate keys is selected to be the primary key which one? Foreign key constraint: Value in one relation must appear in another referencing relation referenced relation example – dept_name in instructor is a foreign key from instructor referencing department referential integrity constraint; Tuples in the same tables should be distinguishable. A superkey is a set of one or more attributes that, taken collectively, allow us to identify uniquely a tuple in the relation, maybe have extraneous attributes (ID, name) vs. Id candidate keys: minimal superkeys primary key to denote a candidate key that is chosen by the database designer as the principal means of identifying tuples within a relation attribute values are never, or very rarely, changed Primary key attributes are also underlined , no duplication, unique
10
Schema Diagram for University Database
schema diagrams depicted, A database schema, along with primary key and foreign key dependencies, can be depicted by schema diagrams The parts of a table The underlined attributes are the PK The arrow from… to… E-R model
11
Expression of relation schema
12
Mathematica Fundament:
Relational Algebra
13
Relational Query Languages
Procedural vs .non-procedural, or declarative “Pure” languages: Relational algebra Tuple relational calculus Domain relational calculus The above 3 pure languages are equivalent in computing power We will concentrate in this chapter on relational algebra Not turning-machine equivalent consists of 6 basic operations In a procedural language, the user instructs the system to perform a sequence of operations on the database to compute the desired result. In a nonprocedural language, the user describes the desired information without giving a specific procedure for obtaining that information There are a number of “pure” query languages: The relational algebra is procedural(fundamental techniques for extracting data from the database ),whereas the tuple relational calculus and domain relational calculus are nonprocedural.
14
Relational Algebra Procedural language Six basic operators
select: s project: Õ union: È set difference: – Cartesian product: x rename: r The operators take one or two relations as inputs and produce a new relation as a result.
15
Select Operation Notation: s p(r) p is called the selection predicate
Defined as: sp(r) = {t | t Î r and p(t)} Where p is a formula in propositional calculus consisting of terms connected by : Ù (and), Ú (or), Ø (not) Each term is one of: <attribute> op <attribute> or <constant> where op is one of: =, ¹, >, ³. <. £
16
Select Operation – selection of rows (tuples)
Relation r sA=B ^ D > 5 (r)
17
Example of Selection s dept_name=“Physics”(instructor)
18
Project Operation Notation:
where A1, A2 are attribute names and r is a relation name. The result is defined as the relation of k columns obtained by erasing the columns that are not listed Duplicate rows removed from result, since relations are sets
19
Example of Selecting columns (Attributes)
Relation r: ÕA,C (r) To eliminate the dept_name attribute of instructor ÕID, name, salary (instructor)
20
Union Operation Notation: r È s Defined as:
r È s = {t | t Î r or t Î s} For r È s to be valid. r, s must have the same arity (same number of attributes) The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s)
21
Example of Union Operation
Relations r, s: r È s:
22
Example of Union Operation
Example: to find all courses taught in the Fall 2009 semester, or in the Spring 2010 semester, or in both Õcourse_id (s semester=“Fall” Λ year=2009 (section)) È Õcourse_id (s semester=“Spring” Λ year=2010 (section))
23
Set Difference Operation
Notation r – s Defined as: r – s = {t | t Î r and t Ï s} Set differences must be taken between compatible relations. r and s must have the same arity attribute domains of r and s must be compatible
24
Example of Set difference of two relations
Relations r, s: r – s:
25
Example of Set difference of two relations
Example: to find all courses taught in the Fall 2009 semester, but not in the Spring 2010 semester Õcourse_id (s semester=“Fall” Λ year=2009 (section)) − Õcourse_id (s semester=“Spring” Λ year=2010 (section))
26
Set-Intersection Operation
Notation: r Ç s Defined as: r Ç s = { t | t Î r and t Î s } Assume: r, s have the same arity attributes of r and s are compatible Note: r Ç s = r – (r – s)
27
Example of Set intersection of two relations
Relation r, s: r Ç s Note: r Ç s = r – (r – s)
28
Cartesian-Product Operation
Notation r x s Defined as: r x s = {t q | t Î r and q Î s} Assume that attributes of r(R) and s(S) are disjoint. (That is, R Ç S = Æ). If attributes of r(R) and s(S) are not disjoint, then renaming must be used.
29
Example of Cartesian-Product Operation
Relations r, s: r x s:
30
Cartesian-product – naming issue
Relations r, s: B r.B s.B r x s:
31
Rename Operation Allows us to name, and therefore to refer to, the results of relational- algebra expressions. Allows us to refer to a relation by more than one name. Example: r x (E) returns the expression E under the name X If a relational-algebra expression E has arity n, then returns the result of expression E under the name X, and with the attributes renamed to A1 , A2 , …., An .
32
Renaming a Table Allows us to refer to a relation, (say E) by more than one name r x (E) returns the expression E under the name X Relations r r.A r.B s.A s.B r x r s (r) α β 1 2 α β 1 2
33
Composition of Operations
Can build expressions using multiple operations Example: sA=C (r x s) r x s sA=C (r x s)
34
Natural Join Let r and s be relations on schemas R and S respectively. Then, the “natural join” of relations R and S is a relation on schema R È S obtained as follows: Consider each pair of tuples tr from r and ts from s. If tr and ts have the same value on each of the attributes in R Ç S, add a tuple t to the result, where t has the same value as tr on r t has the same value as ts on s
35
Natural Join Example Relations r, s: Natural Join r s Õ A, r.B, C, r.D, E (s r.B = s.B ˄ r.D = s.D (r x s)))
36
Notes about Relational Languages
Each Query input is a table (or set of tables) Each query output is a table. All data in the output table appears in one of the input tables Relational Algebra is not Turning complete Can we compute: SUM AVG MAX MIN
37
Summary of Relational Algebra Operators
Symbol (Name) Example of Use (Selection) σ salary > = (instructor) σ Return rows of the input relation that satisfy the predicate. Π (Projection) Π ID, salary (instructor) Output specified attributes from all rows of the input relation. Remove duplicate tuples from the output. x (Cartesian Product) instructor x department Output pairs of rows from the two input relations that have the same value on all attributes that have the same name. ∪ (Union) Π name (instructor) ∪ Π name (student) Output the union of tuples from the two input relations. - (Set Difference) Π name (instructor) -- Π name (student) Output the set difference of tuples from the two input relations. ⋈ (Natural Join) instructor ⋈ department Output pairs of rows from the two input relations that have the same value on all attributes that have the same name.
38
Quiz Sno Sname Ssex Sage Sdept
Course Student Cno Cname Cpqo Ccredit database 2 math information system 4 Operating System 5 Data structure 6 Data Processing 7 Pascal Sno Sname Ssex Sage Sdept Yong Li male 20 CS Chen liu female IS 95003 Ming Wang female MA 95004 li Zhang male IS 1.Give the names and departments of all students Sno Cno Grade SC 2.Give the students whose department is ‘IS’ 3.Give the student number who study the course ‘1’ 4.Give the student names who study the course ‘1’ or ‘3’ 5. Give the students’ name, name and the grades of the courses they took
39
• Table • Relation • Tuple
• Attribute • Domain • Atomicdomain Nullvalue Database schema Database instance Relation schema Relation instance Keys Superkey ◦ Candidate key Primary key Foreign key Referencing relation Referenced relation Referential integrity constraint Schema diagram • Querylanguage ◦ Procedural language Nonprocedural language Operations on relations Selection of tuples Selection of attributes ◦ Natural join Cartesian product Set operations Relational algebra
40
What have you learned from this lecture?
Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.