AEM I Test
AEM I Test
AEM I Test
Systems (EMS)
7. What do you mean by instance and schema? Explain the difference between
these.
10. Give an example for Data Definition and Data Manipulation language.
14. Draw a neat sketch of overall DBMS structure and label the parts.
Page 1 of 1
Applications of RDBMS in Energy Management
Systems (EMS)
Shailesh K R
August 6, 2017
1/30
RDBMS in EMS
2/30
Where does an EMS fit in the network?
3/30
Energy Management Systems
I Monitor-Operate-Maintain-Plan
4/30
Applications of RDBMS in Energy Management Systems
(EMS)
Ref : http://db-book.com/
5/30
Why RDBMS in EMS? I
6/30
Why RDBMS in EMS? II
7/30
IoT - Internet of Things
8/30
RDBMS - Architecture
9/30
Exercise 1
10/30
Database Management System (DBMS)
11/30
Drawbacks of using file systems to store data I
12/30
Drawbacks of using file systems to store data II
I Atomicity of updates - Failures may leave database in an
inconsistent state with partial updates carried out
14/30
Levels of Abstraction
15/30
Is there any problem with this relation?
16/30
Instances and Schemas
17/30
Data Independence
18/30
Data Models
I Data
I Data relationships
I Data semantics
I Data constraints
19/30
Object-based logical models
I Entity-relationship model
I Object-oriented model
I Semantic model
I Functional model
20/30
Record-based logical models
I Relational model
I Network model
I Hierarchical model
21/30
Entity-Relationship Model
Example: ER model
22/30
Relational Model
23/30
Data Definition Language (DDL)
24/30
Data Manipulation Language (DML)
25/30
Transaction management
26/30
Storage Management
27/30
Database Administrator
I Schema definition
I Storage structure and access method definition
I Schema and physical organization modification
I Granting user authority to access the database
I Specifying integrity constraints
I Acting as liaison with users
I Monitoring performance and responding to changes in
requirements
28/30
Database Users
29/30
Overall Database Structure
30/30
Entity-Relationship Model
8. What is the difference between the strong entity set and weak entity set? How is
a weak entity set represented in a E-R model?
9. Consider the following set of requirements for a university database that is used
to keep track of students’ transcripts.
a. For each student, the university maintains student name, student number,
social security number, current address and phone, permanent address
and phone, birthdate, gender, class (freshman, sophomore, ..., graduate),
major department, minor department (if any) and current degree
program (B.A., B.S., . . ., Ph.D.). Some user applications need to refer to the
city, state, and zip code of the student’s current address, and to the
student’s last name. Both social security number and student number
have unique values for each student. Each student must have exactly one
major department. Each student can have at most one minor department.
b. Each department is described by a name, department code, office phone,
and college. Both name and code have unique values for each department.
c. Each course has a course name, description, course number, credit hours,
level, and offering department. The value of course number is unique for
each course.
d. Each section is associated with an instructor, semester, year, course, and
section number. The section number distinguishes different sections of
the same course that are taught during the same semester/year. The
Page 1 of 2
possible values for the section number are 1, 2, 3, . . .; up to the number of
sections taught during each semester.
e. A grade report has a student name, section, letter grade, and numeric
grade (0, 1, 2, 3, 4 for F, D, C, B, A, respectively).
Draw an ER diagram that captures all the above requirements. Specify key
attribute(s) of each entity set. F or each relationship set, specify structural
constraints and participation constraints. Write down any other assumptions
that are not given, but are used by you to make the ER diagram complete.
Solution : http://web.cs.iastate.edu/~cs461/homeworks/hw1-sol.pdf
Page 2 of 2
Database Design - Introduction to the Entity
Relationship Diagram/ ER Model
Shailesh K R
1/26
An Example of ER Model
2/26
RDBMS Design phase
3/26
DBMS Design Approaches
4/26
Entity Sets – instructor and student
5/26
Relationship Set advisor
Advisor(instructorID,studentID)
6/26
Degree of a Relationship Set
7/26
Mapping Cardinality Constraints
8/26
Mapping Cardinalities
9/26
Mapping Cardinalities
10/26
Attribute types
I Simple - Composite
I Derived attributes
11/26
Attribute types
12/26
Derived attribute
13/26
Weak Entity Sets
14/26
Relationship Sets with Attributes
15/26
Roles
16/26
Cardinality Constraints
One-to-One relationship
17/26
One-to-Many Relationship
One-to-Many relationship
18/26
Many-to-one Relationship
Many-to-One relationship
19/26
Many-to-many Relationship
Many-to-many relationship
20/26
Total and Partial Participation
21/26
E-R Diagram for a University Enterprise
22/26
E-R Diagram notations
23/26
E-R Diagram notations
24/26
Exercise
25/26
Solution - One out of many
possible solutions
26/26
RELATIONAL ALGEBRA II
CS121: Introduction to Relational Database Systems
Fall 2016 – Lecture 3
Last Lecture
2
person_nameGcount(puzzle_name)(completed)
person_name puzzle_name
Alex altekruse
Alex soma cube person_name
Alex puzzle box Alex 3
Bob puzzle box Bob 2
Bob soma cube Carl 3
Carl altekruse
Carl puzzle box
Carl soma cube
Distinct Values
12
¤ E evalutes to a relation
¤ Leading Gi are attributes of E to group on
r s r s r s r s
attr1 attr2 attr3 attr1 attr2 attr3 attr1 attr2 attr3 attr1 attr2 attr3
b r2 s2 a r1 null b r2 s2 a r1 null
c r3 s3 b r2 s2 c r3 s3 b r2 s2
c r3 s3 d null s4 c r3 s3
d null s4
Effects of null Values
23
¨ and
true Ù unknown = unknown
false Ù unknown = false
unknown Ù unknown = unknown
¨ or
true Ú unknown = true
false Ú unknown = unknown
unknown Ú unknown = unknown
¨ not
¬ unknown = unknown
Relational Operations
25
¨ Project: P(E)
¤ Project operation must eliminate duplicates
¤ null value is treated like any other value
¤ Duplicate tuples containing null values are also eliminated
¨ Union, Intersection, and Difference
¤ null values are treated like any other value
¤ Set union, intersection, difference computed as expected
¨ These choices are somewhat arbitrary
¤ null means “value is unknown or missing”…
¤ …but in these cases, two null values are considered equal.
¤ Technically, two null values aren’t the same. (oh well)
Grouping and Aggregation
27
¨ In grouping phase:
¤ null is treated like any other value
¤ If two tuples have same values (including null) on the
grouping attributes, they end up in same group
¨ In aggregation phase:
¤ null values are removed from the input multiset before the
aggregate function is applied!
n Slightly different from arithmetic behavior; it keeps one null value
from wiping out an aggregate computation.
¤ If the aggregate function gets an empty multiset for input,
the result is null…
n …except for count! In that case, count returns 0.
Generalized Projection, Outer Joins
28
¨ Outer joins:
¤ Behave just like natural join operation, except for
padding missing values with null
Back to Our Puzzle!
person_name puzzle_name
29
Alex altekruse
Alex soma cube
“How many people Bob puzzle box
have completed puzzle_name Carl altekruse
altekruse Bob soma cube
each puzzle?” soma cube Carl puzzle box
puzzle box Alex puzzle box
clutch box Carl soma cube
puzzle_list completed
puzzle_nameGcount(person_name)(puzzle_list completed)
¨ Remember: r is a relation-variable
¤ Assignment operator assigns a new relation-value to r
¤ Hence, RHS expression may need to include existing
version of r, to avoid losing unchanged tuples
Inserting New Tuples
32
¨ Example: altekruse
soma cube
Get rid of the “soma cube” puzzle. puzzle box
puzzle_list
person_name puzzle_name
Problem: Alex altekruse
n completed relation references Alex soma cube
the puzzle_list relation Bob puzzle box
Carl altekruse
n To respect referential integrity Bob soma cube
constraints, should delete from Carl puzzle box
completed first. Alex puzzle box
Carl soma cube
completed
Deleting Tuples (2)
36
Another example:
“Add 5% interest to accounts with a balance less than
$10,000, and 6% interest to accounts with a balance
of $10,000 or more.”
account ¬ Pacct_id,branch_name,(balance*1.05)(sbalance<10000(account)) È
Pacct_id,branch_name,(balance*1.06)(sbalance≥10000(account))
1
Relational Algebra
A relation is a set of attributes with values for
each attribute such that:
Each attribute value must be a single value only
(atomic).
All values for a given attribute must be of the
same type (or domain).
Each attribute name must be unique.
The order of attributes is insignificant
No two rows (tuples) in a relation can be identical.
The order of the rows (tuples) is insignificant.
2
Relational Algebra
Relational Algebra is a collection of
operations on Relations.
3
Relational Algebra
Two main collections of relational operators:
4
Set Theoretic Operations
5
Union: RUS
6
Difference: R - S
Bill Smith 22
Mary Keen 23
Tony Jones 32
7
Intersection: R∩S
Sally Green 28
8
Union Compatible Relations
9
Union Compatible Relations
10
Additional properties
11
Cartesian Product: R×S
R S
First Last Age
Dinner Dessert
Bill Smith 22
Steak Ice Cream
Mary Keen 23
Lobster Cheesecake
Tony Jones 32
12
Cartesian Product: R×S
R×S:
13
Relational Algebra
Two main collections of relational operators:
14
Selection Operator
15
Selection Operator
16
Selection Operator
Logical operators: ∧ T F ∨ T F
¬ T F
T T F T T T
F T
F F F F T F
17
Selection Examples
18
Selection Examples
19
Selection Examples
20
Selection Examples
21
Selection Examples
22
Projection Operator
23
Projection Operator
24
Projection Examples
25
Combining Selection and Projection
26
Combining Selection and Projection
27
Aggregate Functions
28
Aggregate Functions
29
Aggregate Functions Examples
30
Aggregate Functions Examples
31
Aggregate Functions Examples
COUNT(name)
2
32
Aggregate Functions Examples
SUM(salary)
85000
33
Join Operation
34
Join Operation
35
Join Examples
36
Join Examples
37
Join Examples
38
Join Examples
39
Join Examples
depart.dept) DEPART
40
Natural Join
42
Outer Join
43
Left Outer Join
R S
(Left) outer join is a join in which tuples from R that
do not have matching values in common columns of
S are also included in result relation.
44
Outer Join Examples
45
Left Outer Join
46
Right Outer Join
47
Full Outer Join
48
Relational algebra and SQL
SELECT statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]
49
SELECT Statement
50
Relational algebra and SQL
Projection
Example: The table E (for EMPLOYEE)
nr name salary
1 John 100
5 Sarah 300
7 Tom 100
51
Relational algebra and SQL - Projection
nr salary
select nr, 1 100
salary from E 5 300 nr, salary(E)
7 100
52
Relational algebra and SQL - Selection
select * from
nr name salary
E where salary
< 200 and 7 Tom 100 salary < 200 and nr >= 7(E)
nr >= 7
53
Combination of projection and selection
name salary
John 100
select name,
Tom 100
salary from name, salary ( salary
E where < 200(E))
salary < 200
54
Cartesian Product
55
Cartesian Product
Relational
SQL Result
algebra
enr ename edept dnr dname
1 Bill A A Marketing
1 Bill A B Sales
1 Bill A C Legal
select 2 Sarah C A Marketing
*from E, E×D
2 Sarah C B Sales
D
2 Sarah C C Legal
3 John A A Marketing
3 John A B Sales
3 John A C Legal
56
Join ("inner join")
Relational
SQL Result
algebra
57
Aggregate functions
58
Sum
select sum
sum(salary) Fsum(salary)(E)
500
from E
Count:
Duplicates are not eliminated.
Null values are ignored.
59
Count
select count
count(salary) 3 Fcount(salary)(E)
from E
select count
count(distinct 2 Fcount(salary)(πsalary(E))
salary)from E
60
Aggregate Functions
dept sum
select A 200
sum(salary) C 300
from E group deptFsum(salary)(E)
by dept
61
Aggregate Functions
Relational
SQL Result
algebra
E group by count(*)(E)
dept
62
Outer join
63
Outer join
64
Outer join
Relational
SQL Result
algebra
65
Outer Join
66
Outer Join
= dnr) group by
dnr, dname
67
Relational Model
8. List and explain aggregate functions used in relational algebra with suitable
examples for each.
10. What are the unary operations in Relational Algebra? Explain with suitable
examples.
11. Differentiate between Cartesian product and natural join operations used in
relational algebra.
12. Explain the terms super key and candidate key with an example.
13. What is a primary key? Define foreign key? How does it play a role in the join
operation?
Page 1 of 2
14. Consider the following relational schema
Employee (empno,name,office,age)
Books(isbn,title,authors,publisher)
Loan(empno, isbn,date)
Write the following queries in relational algebra.
a. Find the names of employees who have borrowed a book Published by
McGraw-Hill.
b. Find the names of employees who have borrowed all books Published by
McGraw-Hill.
c. Find the names of employees who have borrowed more than five different
books published by McGraw-Hill.
d. For each publisher, find the names of employees who have borrowed
more than five books of that publisher.
Page 2 of 2
Relational Model
Shailesh K R
1/31
Example of a Relation
2/31
Attribute Types
3/31
Relation Schema and Instance
4/31
Relations are Unordered
I Order of tuples is irrelevant (tuples may be stored in an
arbitrary order)
I Example: instructor relation with unordered tuples
5/31
Keys
6/31
Relational algebra
Relational database systems are expected to be equipped with a
query language that can assist its users to query the database
instances.
8/31
Project Operation –
selection of columns (Attributes)
9/31
Union of two relations
10/31
Set difference of two relations
11/31
Set intersection of two relations
12/31
Joining two relations - Cartesian-product
13/31
Cartesian-product – naming issue
14/31
Renaming a Table
15/31
Composition of Operations
16/31
Joining two relations – Natural Join
17/31
Natural Join Example
18/31
Notes about Relational Languages
19/31
Summary of Relational Algebra Operators
20/31
Summary of Relational Algebra Operators
21/31
Summary of Relational Algebra Operators
22/31
Relational Algebra -Joins
23/31
Aggregate functions - Example
24/31
Relational Algebra -Notations
25/31
Example queries
26/31
Example queries I
I List the year and title of each book - πYear ,Title (BOOKS)
27/31
Example queries II
I List the name of students who are older than 30 and who are
not studying CS-
πStName (σAge>30 (STUDENTS)) −
πStName (σMajor =‘CS 0 (STUDENTS))
28/31
Questions 2
29/31
Questions 2
30/31
Questions 2
31/31
SQL – Structured Query Langauge
11. Discuss the various type of join operations? Why are these joins required?
Page 1 of 1
Structured Query Language (SQL)
Shailesh K R
1/32
Structured Query Language (SQL)
2/32
Data Definition Language
3/32
Example of a relation
4/32
Domain Types in SQL
I char(n). Fixed length character string, with user-specified
length n.
I varchar(n). Variable length character strings, with
user-specified maximum length n.
I int. Integer (a finite subset of the integers that is
machine-dependent).
I smallint. Small integer (a machine-dependent subset of the
integer domain type).
I numeric(p,d). Fixed point number, with user-specified
precision of p digits, with d digits to the right of decimal
point. (ex., numeric(3,1), allows 44.5 to be stores exactly, but
not 444.5 or 0.32)
I real, double precision. Floating point and double-precision
floating point numbers, with machine-dependent precision.
I float(n). Floating point number, with user-specified precision
of at least n digits.
5/32
Create Table Construct
6/32
Integrity Constraints (ICs)
7/32
Primary Key Constraints
8/32
Foreign Keys, Referential Integrity
9/32
Enforcing Referential Integrity
10/32
Integrity Constraints in Create Table
11/32
Example
12/32
Logical DB Design: ER to Relational
13/32
Logical DB Design: ER to Relational
14/32
Example
15/32
Inserting, Updating and Deleting Tuples
16/32
The SELECT Clause
17/32
The SELECT Clause
18/32
The SELECT Clause
19/32
SELECT - WHERE
20/32
The LIKE operator
21/32
Eliminating Duplicates
22/32
Ordering the Results
23/32
Joins in SQL
24/32
Joins
25/32
Joins
26/32
Joins
27/32
Disambiguating Attributes
28/32
Set Operations
29/32
Aggregate Functions – Group By
30/32
Aggregate Functions
31/32
Aggregate Functions – Having Clause
32/32