Sie sind auf Seite 1von 235

Relational Database Management Systems (RDBMS in Energy Management

Systems (EMS)

Review Questions – Set 01

1. What is the role of Database Management system in Energy management?

2. Where does a EMS fit in an electrical network?

3. Why is RDBMS essential in EMS?

4. What is IoT? Explain its architecture.

5. What are the drawbacks of file based data storage systems?

6. Explain the three levels of data abstraction in DBMS?

7. What do you mean by instance and schema? Explain the difference between
these.

8. Explain Data Independence?

9. What is a Data Model?

10. Give an example for Data Definition and Data Manipulation language.

11. What is the role of a Database Administrator?

12. Who are different users interacting with DBMS?

13. Define the "integrity rules" or "integrity constraints".

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

Department of Electrical & Electronics Engineering,


MIT, Manipal University

August 6, 2017

1/30
RDBMS in EMS

2/30
Where does an EMS fit in the network?

3/30
Energy Management Systems

Capability to monitor, control, and optimize the operation of


geographically dispersed transmission and generation assets in
real-time.

I Real-time SCADA applications

I Generation dispatch and control

I Energy scheduling and Accounting

I Transmission security management

I Monitor-Operate-Maintain-Plan

4/30
Applications of RDBMS in Energy Management Systems
(EMS)

Ref : http://db-book.com/

RDBMS - Relational Database Management Systems

RDBMS is the basis for Structured Query Language(SQL), and for


all modern database systems like MS SQL Server, IBM DB2,
Oracle, MySQL, and Microsoft Access.

RDBMS is based on the relational model as introduced by E. F.


Codd.

5/30
Why RDBMS in EMS? I

I EMS generates constantly growing volume of data from


different source (ex:Power plants,substations, sensors, IEDs
etc)

I Different variety of data - Text, Image, audio, video in


structured/unstructured/semi-structured formats - Sensors /
Smart gadgets

I High velocity - data acquisition speed / analysis / processing

I Extracting knowledge or patterns from raw data set

6/30
Why RDBMS in EMS? II

I Data representation - Data models - key for data analysis

I Sensor data - Highly Redundant - Data compression and


Filtering

I Time dependent data - Up to data

I Processing heterogeneous data in limited time

I Scalability and Expandability - Future datasets

7/30
IoT - Internet of Things

IoT - the interconnection via the Internet of computing devices


embedded in everyday objects, enabling them to send and receive
data.

Three layer architecture -


I Sensing / Perception - Data generation
I Network - Data sending
I Application - Data analysis, presentation

8/30
RDBMS - Architecture

9/30
Exercise 1

Let us try to manage student information in a academic institute.

Discuss amongst your friends the strategy to model student data.

10/30
Database Management System (DBMS)

DBMS contains information about a particular enterprise


I Collection of interrelated data

I Set of programs to access the data

I An environment that is both convenient and efficient to use

11/30
Drawbacks of using file systems to store data I

DBMS contains information about a particular enterprise

I Data redundancy and inconsistency - Multiple file formats,


duplication of information in different files

I Difficulty in accessing data -Need to write a new program to


carry out each new task

I Data isolation - Multiple files and formats

I Integrity problems - Integrity constraints become “buried” in


program code rather than being stated explicitly

Hard to add new constraints or change existing ones

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

Example: Transfer of funds from one account to another


should either complete or not happen at all

I Concurrent access by multiple users

Concurrent access needed for performance


Uncontrolled concurrent accesses can lead to inconsistencies
Example: Two people reading a balance (say 100) and
updating it by withdrawing money (say 50 each) at the same
time

I Security problems-Hard to provide user access to some, but


not all, data
13/30
View of Data

14/30
Levels of Abstraction

Physical level: describes how a record (e.g. customer) is stored.

Logical level: describes data stored in database, and the


relationships among the data.

View level: application programs hide details of data types. Views


can also hide information (e.g. salary) for security purposes.

15/30
Is there any problem with this relation?

List out the anomalies(mistake(s)) in this relation:

16/30
Instances and Schemas

Similar to types and variables in programming languages

Schema – the logical structure of the database (e.g., set of


customers and accounts and the relationship between them)

Instance – the actual content of the database at a particular point


in time

17/30
Data Independence

Ability to modify a schema definition in one level without affecting


a schema definition in the other levels.

The interfaces between the various levels and components should


be well defined so that changes in some parts do not seriously
influence others.

Two levels of data independence


I Physical data independence
I Logical data independence

18/30
Data Models

A collection of tools for describing:

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)

I Specification notation for defining the database schema

I DDL compiler generates a set of tables stored in a data


dictionary

I Data dictionary contains metadata (data about data)

I Data storage and definition language – special type of DDL in


which the storage structure and access methods used by the
database system are specified

24/30
Data Manipulation Language (DML)

I Language for accessing and manipulating the data organized


by the appropriate data model - Two classes of languages

I Procedural – user specifies what data is required and how to


get those data

I Nonprocedural – user specifies what data is required without


specifying how to get those data

25/30
Transaction management

I A transaction is a collection of operations that performs a


single logical function in a database application.

I Transaction-management component ensures that the


database remains in a consistent (correct) state despite
system failures (e.g. power failures and operating system
crashes) and transaction failures.

I Concurrency-control manager controls the interaction among


the concurrent transactions, to ensure the consistency of the
database.

26/30
Storage Management

I A storage manager is a program module that provides the


interface between the low-level data stored in the database
and the application programs and queries submitted to the
system.

I The storage manager is responsible for the following tasks:


Interaction with the file manager
Efficient storing, retrieving, and updating of data

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

I Application programmers: interact with system through


DML calls.

I Specialized users: write specialized database applications


that do not fit into the traditional data processing framework

I Sophisticated users: form requests in a database query


language.

I Naive users: invoke one of the permanent application


programs that have been written previously

29/30
Overall Database Structure

30/30
Entity-Relationship Model

Review Questions – Set 02

1. What is E-R model? Explain with the help of an example.

2. What is an Entity? What is an Entity type? What is an Entity set?

3. What is a composite attribute? Give examples.

4. What is a single valued attribute? Give examples.

5. What is a multi-valued attribute? Give examples.

6. What do you mean by cardinality? What are different kinds of cardinalities?

7. Give example of following relationships: Many-to-One, One-to-One, One-to-Many,


Many-to-Many.

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

Department of Electrical & Electronics Engineering,


MIT, Manipal University

August 13, 2017

1/26
An Example of ER Model

2/26
RDBMS Design phase

I Characterize data needs for the application

I Choose data model - translate into conceptual schema of the


database

I Logical schema - What attributes to add?

I What relation schemas should we have and how should the


attributes be distributed among the various relation schemas

I Deciding on the physical layout of the database

3/26
DBMS Design Approaches

I Entity Relationship Model - Models an enterprise as a


collection of entities and relationships

I Entity: a “thing” or “object” in the enterprise that is


distinguishable from other objects - Described by a set of
attributes

I Relationship: an association among several entities

I Normalization Theory - Formalize what designs are bad, and


test for them

4/26
Entity Sets – instructor and student

5/26
Relationship Set advisor

Advisor(instructorID,studentID)

6/26
Degree of a Relationship Set

I binary relationship - involve two entity sets (or degree two).


I most relationship sets in a database system are binary.
I Relationships between more than two entity sets are rare.

7/26
Mapping Cardinality Constraints

I Express the number of entities to which another entity can be


associated via a relationship set.

I Most useful in describing binary relationship sets.

8/26
Mapping Cardinalities

9/26
Mapping Cardinalities

10/26
Attribute types

I Simple - Composite

I Single-valued and multivalued attributes

I Derived attributes

I Domain – the set of permitted values for each attribute

11/26
Attribute types

12/26
Derived attribute

Age is derived from DOB (Date of Birth)


Phone is multivalued attribute
Address is composite attribute

13/26
Weak Entity Sets

A weak entity is an entity that cannot be uniquely identified by its


attributes alone.Owner entity and weak entity must participate in
1-to-N relationship, Weak entity in total participation
Double rectangle for weak entity, double diamond for identifying
relationship
Dashed underscore of discriminator

14/26
Relationship Sets with Attributes

15/26
Roles

The function that an entity plays in a relationship is called its role.

They are useful when the meaning of a relationship set needs


clarification.

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

Total participation double line

21/26
E-R Diagram for a University Enterprise

22/26
E-R Diagram notations

23/26
E-R Diagram notations

24/26
Exercise

Suppose you are a new member of the computer support group of


the university library . Your job is to set up the new library
information system using a relational database. Your first step to
do is set up an Entity-Relationship Model that should roughly
represent the following real-world objects:
I Books. But also periodical journals or books that span several
volumes.
I Books are written by authors or edited by editors.
I Books may be published in several editions by a publisher.
Explain your Entity-Relationship Model. Find sensible attributes
for your entities. Mark primary keys and key candidates. What
problems do you encounter? Are there any constraints you can’t
model with the ER Model?

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

¨ Query languages provide support for retrieving


information from a database
¨ Introduced the relational algebra
¤ A procedural query language
¤ Six fundamental operations:
n select, project, set-union, set-difference,
Cartesian product, rename
¤ Several additional operations, built upon the
fundamental operations
n set-intersection, natural join, division, assignment
Extended Operations
3

¨ Relational algebra operations have been extended


in various ways
¤ More generalized
¤ More useful!

¨ Three major extensions:


¤ Generalized projection
¤ Aggregate functions
¤ Additional join operations

¨ All of these appear in SQL standards


Generalized Projection Operation
4

¨ Would like to include computed results into relations


¤ e.g. “Retrieve all credit accounts, computing the current
‘available credit’ for each account.”
¤ Available credit = credit limit – current balance

¨ Project operation is generalized to include computed


results
¤ Can specify functions on attributes, as well as attributes
themselves
¤ Can also assign names to computed values
¤ (Renaming attributes is also allowed, even though this is also
provided by the r operator)
Generalized Projection
5

¨ Written as: P F , F , …, F (E)


1 2 n

¤ Fi are arithmetic expressions


¤ E is an expression that produces a relation

¤ Can also name values: Fi as name

¨ Can use to provide derived attributes


¤ Values are always computed from other attributes stored in
database
¨ Also useful for updating values in database
¤ (more on this later)
Generalized Projection Example
6

¨ “Compute available credit for every credit


account.”
Pcred_id, (limit – balance) as available_credit(credit_acct)

cred_id limit balance cred_id available_credit


C-273 2500 150 C-273 2350
C-291 750 600 C-291 150
C-304 15000 3500 C-304 11500
C-313 300 25 C-313 275
credit_acct
Aggregate Functions
7

¨ Very useful to apply a function to a collection of


values to generate a single result
¨ Most common aggregate functions:
sum sums the values in the collection
avg computes average of values in the collection
count counts number of elements in the collection
min returns minimum value in the collection
max returns maximum value in the collection
¨ Aggregate functions work on multisets, not sets
¤ A value can appear in the input multiple times
Aggregate Function Examples
8

“Find the total amount owed cred_id limit balance

to the credit company.” C-273


C-291
2500
750
150
600
Gsum(balance)(credit_acct) C-304 15000 3500
C-313 300 25
credit_acct
4275

“Find the maximum available credit of any account.”


Gmax(available_credit)(P(limit – balance) as available_credit(credit_acct))
11500
Grouping and Aggregation
9

¨ Sometimes need to compute aggregates on a


per-item basis
puzzle_name
¨ Back to the puzzle database: altekruse
puzzle_list(puzzle_name) soma cube
puzzle box
completed(person_name, puzzle_name) puzzle_list

¨ Examples: person_name puzzle_name


¤ How many puzzles has Alex altekruse
each person completed? Alex soma cube
Bob puzzle box
¤ How many people have Carl altekruse
completed each puzzle? Bob soma cube
Carl puzzle box
Alex puzzle box
Carl soma cube
completed
Grouping and Aggregation (2)
10

puzzle_name person_name puzzle_name


altekruse Alex altekruse
soma cube Alex soma cube
puzzle box Bob puzzle box
puzzle_list Carl altekruse
Bob soma cube
“How many puzzles has each Carl puzzle box
person completed?” Alex puzzle box
Carl soma cube
completed
person_nameGcount(puzzle_name)(completed)
¨ First, input relation completed is grouped by unique values of
person_name
¨ Then, count(puzzle_name) is applied separately to each group
Grouping and Aggregation (3)
11

person_nameGcount(puzzle_name)(completed)

Input relation is grouped by Aggregate function is


person_name applied to each group

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

¨ Sometimes want to compute aggregates over sets of


values, instead of multisets
Example:
¤ Chage puzzle database to include a completed_times
relation, which records multiple solutions of a puzzle
¨ How many puzzles has person_name puzzle_name seconds
Alex altekruse 350
each person completed? Alex soma cube 45
¤ Using completed_times Bob puzzle box 240
Carl altekruse 285
relation this time Bob puzzle box 215
Alex altekruse 290
completed_times
Distinct Values (2)
13

“How many puzzles has each person completed?”


person_name puzzle_name seconds

¨ Each puzzle appears Alex


Alex
altekruse
soma cube
350
45
multiple times now. Bob puzzle box 240
Carl altekruse 285
Bob puzzle box 215
Alex altekruse 290
completed_times

¨ Need to count distinct occurrences of each puzzle’s


name
person_nameGcount-distinct(puzzle_name)(completed_times)
Eliminating Duplicates
14

¨ Can append -distinct to any aggregate function to


specify elimination of duplicates
¤ Usually used with count: count-distinct
¤ Makes no sense with min, max
General Form of Aggregates
15

¨ General form: GF (A ), F (A ), …, F (A ) (E)


G1, G2, …, Gn 1 1 2 2 m m

¤ E evalutes to a relation
¤ Leading Gi are attributes of E to group on

¤ Each Fj is aggregate function applied to attribute Aj of E

¨ First, input relation is divided into groups


¤ If no attributes Gi specified, no grouping is performed
(it’s just one big group)
¨ Then, aggregate functions applied to each group
General Form of Aggregates (2)
16

¨ General form: G , G , …, G GF (A ), F (A ), …, F (A ) (E)


1 2 n 1 1 2 2 m m

¨ Tuples in E are grouped such that:


¤ All tuples in a group have same values for attributes
G1, G2, …, Gn
¤ Tuples in different groups have different values for
G1, G2, …, Gn
¨ Thus, the values {g1, g2, …, gn} in each group
uniquely identify the group
¤ {G1, G2, …, Gn} are a superkey for the result relation
General Form of Aggregates (3)
17

¨ General form: G , G , …, G GF (A ), F (A ), …, F (A ) (E)


1 2 n 1 1 2 2 m m

¨ Tuples in result have the form:


{g1, g2, …, gn, a1, a2, …, am}
¤ gi are values for that particular group
¤ aj is result of applying Fj to the multiset of values of Aj
in that group
¨ Important note: Fj(Aj) attributes are unnamed!
¤ Informally we refer to them as Fj(Aj) in results, but
they have no name.
¤ Specify a name, same as before: Fj(Aj) as attr_name
One More Aggregation Example
18

puzzle_name person_name puzzle_name


altekruse Alex altekruse
soma cube Alex soma cube
puzzle box Bob puzzle box
puzzle_list Carl altekruse
Bob soma cube

“How many people have Carl


Alex
puzzle box
puzzle box
completed each puzzle?” Carl soma cube
completed
puzzle_nameGcount(person_name)(completed)

¨ What if nobody has tried a particular puzzle?


¤ Won’t appear in completed relation
One More Aggregation Example
19

puzzle_name person_name puzzle_name


altekruse Alex altekruse
soma cube Alex soma cube
puzzle box Bob puzzle box
clutch box Carl altekruse
puzzle_list Bob soma cube
Carl puzzle box
¨ New puzzle added to Alex puzzle box
puzzle_list relation Carl soma cube
completed
¤ Would like to see { “clutch box”, 0 } in result…
¤ “clutch box” won’t appear in result!

¨ Joining the two tables doesn’t help either


¤ Natural join won’t produce any rows with “clutch box”
Outer Joins
20

¨ Natural join requires that both left and right tables


have a matching tuple
r s = PR È S(sr.A =s.A
1 1
(r ´ s))
Ù r.A2=s.A2 Ù … Ù r.An=s.An

¨ Outer join is an extension of join operation


¤ Designed to handle missing information
¨ Missing information is represented by null values in
the result
¤ null = unknown or unspecified value
Forms of Outer Join
21

¨ Left outer join: r s


¤ If a tuple tr Î r doesn’t match any tuple in s,
result contains { tr, null, …, null }
¤ If a tuple ts Î s doesn’t match any tuple in r, it’s
excluded
¨ Right outer join: r s
¤ If a tuple tr Î r doesn’t match any tuple in s, it’s
excluded
¤ If a tuple ts Î s doesn’t match any tuple in r,
result contains { null, …, null, ts }
Forms of Outer Join (2)
22

¨ Full outer join: r s


¤ Includes tuples from r that don’t match s,
as well as tuples from s that don’t match r
¨ Summary:
r= attr1 attr2 s= attr1 attr3
a r1 b s2
b r2 c s3
c r3 d s4

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

¨ Introducing null values affects everything!


¤ null means “unknown” or “nonexistent”
¨ Must specify effect on results when null is present
¤ These choices are somewhat arbitrary…
¤ (Read your database user’s manual! J)
¨ Arithmetic operations (+, –, *, /) involving null always
evaluate to null (e.g. 5 + null = null)
¨ Comparison operations involving null evaluate to
unknown
¤ unknown is a third truth-value
¤ Note: Yes, even null = null evaluates to unknown.
Boolean Operators and unknown
24

¨ 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

¨ For each relational operation, need to specify


behavior with respect to null and unknown
¨ Select: sP(E)
¤ If P evaluates to unknown for a tuple, that tuple is excluded
from result (i.e. definition of s doesn’t change)
¨ Natural join: r s
¤ Includes a Cartesian product, then a select
¤ If a common attribute has a null value, tuples are excluded
from join result
¤ Why?
n null = (anything) evaluates to unknown
Project and Set-Operations
26

¨ 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

¨ Generalized Projection operation:


¤ A combination of simple projection and arithmetic
operations
¤ Easy to figure out from previous rules

¨ 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

¨ Use an outer join to include all puzzle_name person_name


puzzles, not just solved ones altekruse Alex
soma cube Alex
puzzle_list completed puzzle box Bob
altekruse Carl
soma cube Bob
puzzle box Carl
puzzle box Alex
soma cube Carl
clutch box null
Counting the Solutions
30

¨ Now, use grouping and aggregation


¤ Group on puzzle name
¤ Count up the people!

puzzle_nameGcount(person_name)(puzzle_list completed)

puzzle_name person_name puzzle_name person_name puzzle_name


altekruse Alex altekruse Alex altekruse 2
soma cube Alex altekruse Carl soma cube 3
puzzle box Bob soma cube Alex puzzle box 3
altekruse Carl soma cube Bob clutch box 0
soma cube Bob soma cube Carl
puzzle box Carl
puzzle box Bob
puzzle box Alex
puzzle box Carl
soma cube Carl
puzzle box Alex
clutch box null
clutch box null
Database Modification
31

¨ Often need to modify data in a database


¨ Can use assignment operator ¬ for this
¨ Operations:
¤ r¬rÈE Insert new tuples into a relation
¤r¬r–E Delete tuples from a relation
¤ r ¬ P(r) Update tuples already in the relation

¨ 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

¨ Inserting tuples simply involves a union:


r¬rÈE
¤ E has to have correct arity

¨ Can specify actual tuples to insert:


completed ¬ completed È constant
{ (“Bob”, “altekruse”), (“Carl”, “clutch box”) } relation

¤ Adds two new tuples to completed relation

¨ Can specify constant relations as a set of values


¤ Each tuple is enclosed with parentheses
¤ Entire set of tuples enclosed with curly-braces
Inserting New Tuples (2)
33

¨ Can also insert tuples generated from an expression


¨ Example:
“Dave is joining the puzzle club. He has done every
puzzle that Bob has done.”
¤ Find out puzzles that Bob has completed, then construct
new tuples to add to completed
Inserting New Tuples (3)
34

¨ How to construct new tuples with name “Dave” and


each of Bob’s puzzles?
¤ Could use a Cartesian product:
{ (“Dave”) } ´ Ppuzzle_name(sperson_name=“Bob”(completed))
¤ Or, use generalized projection with a constant:
P“Dave” as person_name, puzzle_name(sperson_name=“Bob”(completed))
¨ Add new tuples to completed relation:
completed ¬ completed È
P“Dave” as person_name, puzzle_name(sperson_name=“Bob”(completed))
Deleting Tuples
35

¨ Deleting tuples uses the – operation:


r¬r–E puzzle_name

¨ 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

¨ completed references puzzle_list


¤ puzzle_name is a key
¤ completed shouldn’t have any values for puzzle_name that
don’t appear in puzzle_list
¤ Delete tuples from completed first.
¤ Then delete tuples from puzzle_list.

completed ¬ completed – spuzzle_name=“soma cube”(completed)


puzzle_list ¬ puzzle_list – spuzzle_name=“soma cube”(puzzle_list)
Of course, could also write:
completed ¬ spuzzle_name≠“soma cube”(completed)
Deleting Tuples (3)
37

¨ In the relational model, we have to think about


foreign key constraints ourselves…
¨ Relational database systems take care of these
things for us, automatically.
¤ Will explore the various capabilities and options in a
few weeks
Updating Tuples
38

¨ General form uses generalized projection:


r ¬ P F , F , …, F (r)
1 2 n
acct_id branch_name balance
¨ Updates all tuples in r A-301 New York 350
A-307 Seattle 275
A-318 Los Angeles 550
A-319 New York 80
¨ Example: A-322 Los Angeles 275
account
“Add 5% interest to all bank account balances.”
account ¬ Pacct_id, branch_name, (balance*1.05)(account)
¤ Note: Must include unchanged attributes too
¤ Otherwise you will change the schema of r
Updating Some Tuples
39

¨ Updating only some tuples is more verbose


¤ Relation-variable is set to the entire result of the evaluation
¤ Must include both updated tuples, and non-updated tuples,
in result
¨ Example:
“Add 5% interest to accounts with a balance less than
$10,000.”
account ¬ Pacct_id, branch_name, (balance*1.05)(sbalance<10000(account)) È
sbalance≥10000(account)
Updating Some Tuples (2)
40

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

¨ Don’t forget to include any non-updated tuples in


your update operations!
Relational Algebra Summary
41

¨ Very expressive query language for retrieving


information from a relational database
¤ Simple selection, projection
¤ Computing correlations between relations using joins
¤ Grouping and aggregation operations
¨ Can also specify changes to the contents of a
relation-variable
¤ Inserts, deletes, updates
¨ The relational algebra is a procedural query
language
¤ State a sequence of operations for computing a result
Relational Algebra Summary (2)
42

¨ Benefit of relational algebra is that it can be formally


specified and reasoned about
¨ Drawback is that it is very verbose!
¨ Database systems usually provide much simpler query
languages
¤ Most popular by far is SQL, the Structured Query Language
¨ However, many databases use relational algebra-like
operations internally!
¤ Great for representing execution plans, due to its
procedural nature
Next Time
43

¨ Transition from relational algebra to SQL


¨ Start working with “real” databases J
Relational Algebra

 The Relational Model consists of the


elements: relations, which are made up
of attributes.

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.

 Relations are operands and the result of an


operation is another relation.

3
Relational Algebra
 Two main collections of relational operators:

 Set theory operations:


 Union, Intersection, Difference and Cartesian product.

 Specific Relational Operations:


 Selection, Projection, Join, Division.

4
Set Theoretic Operations

Consider the following relations R and S.


R S
First Last Age First Last Age
Bill Smith 22
Forrest Gump 36
Sally Green 28
Sally Green 28
Mary Keen 23
Tony Jones 32 DonJuan DeMarco 27

5
Union: RUS

 Result: Relation with tuples from R and S


with duplicates removed.
First Last Age
Bill Smith 22
Sally Green 28
Mary Keen 23
Tony Jones 32
Forrest Gump 36
DonJuan DeMarco 27

6
Difference: R - S

 Result: Relation with tuples from R but not


from S

First Last Age

Bill Smith 22

Mary Keen 23

Tony Jones 32

7
Intersection: R∩S

 Result: Relation with tuples that appear in


both R and S.

First Last Age

Sally Green 28

8
Union Compatible Relations

 Attributes of relations need not be identical to


perform union, intersection and difference
operations.
 However, they must have the same number
of attributes or arity and the domains for
corresponding attributes must be identical.

9
Union Compatible Relations

 Domain is the datatype and size of an


attribute.
 The degree of relation R is the number of
attributes it contains.
 Definition: Two relations R and S are union
compatible if and only if they have the same
degree and the domains of the corresponding
attributes are the same.

10
Additional properties

 Union, Intersection and difference operators


may only be applied to Union Compatible
relations.

 Union and Intersection are commutative


operations

 Difference operation is NOT commutative.

11
Cartesian Product: R×S

 Produce all combinations of tuples from two


relations.

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:

First Last Age Dinner Dessert


Bill Smith 22 Steak Ice Cream
Bill Smith 22 Lobster Cheesecake
Mary Keen 23 Steak Ice Cream
Mary Keen 23 Lobster Cheesecake
Tony Jones 32 Steak Ice Cream
Tony Jones 32 Lobster Cheesecake

13
Relational Algebra
 Two main collections of relational operators:

 Set theory operations:


 Union, Intersection, Difference and Cartesian product.

 Specific Relational Operations:


 Selection, Projection, Join, Division.

14
Selection Operator

 Selection and Projection are unary operators.

 The selection operator is sigma: σ

 The selection operation acts like a filter on a


relation by returning only a certain number of
tuples.

15
Selection Operator

 The resulting relation will have the same


degree as the original relation.

 The resulting relation may have fewer tuples


than the original relation.

 The tuples to be returned are dependent on a


condition that is part of the selection operator.

16
Selection Operator

 σC(R) Returns only those tuples in R that


satisfy condition C
 A condition C can be made up of any
combination of comparison or logical
operators that operate on the attributes of R.
 Comparison operators:

 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

 Assume the following relation EMP has the


following tuples:
Name Office Dept Rank
Smith 400 CS Assistant
Jones 220 Econ Adjunct
Green 160 Econ Assistant
Brown 420 CS Associate
Smith 500 Fin Associate

18
Selection Examples

• Select only those Employees in the CS


department:
σ Dept = 'CS' (EMP)
Result:

Name Office Dept Rank


Smith 400 CS Assistant
Brown 420 CS Associate

19
Selection Examples

 Select only those Employees with last name


Smith who are assistant professors:
σ Name = 'Smith' ∧Rank = 'Assistant' (EMP)
Result:
Name Office Dept Rank

Smith 400 CS Assistant

20
Selection Examples

 Select only those Employees who are either


Assistant Professors or in the Economics
department:
σ Rank = 'Assistant' ∨ Dept = 'Econ' (EMP)
Result:
Name Office Dept Rank
Smith 400 CS Assistant
Jones 220 Econ Adjunct
Green 160 Econ Assistant

21
Selection Examples

 Select only those Employees who are not in


the CS department or Adjuncts:
σ ¬ (Rank = 'Adjunct' ∨ Dept = 'CS') (EMP)
Result:
Name Office Dept Rank
Green 160 Econ Assistant
Smith 500 Fin Associate

22
Projection Operator

 Projection is also a Unary operator.


 The Projection operator is pi: π
 Projection limits the attributes that will be
returned from the original relation.
 The general syntax is: π attributes R
Where attributes is the list of attributes to be
displayed and R is the relation.

23
Projection Operator

 The resulting relation will have the same


number of tuples as the original relation
(unless there are duplicate tuples produced).

 The degree of the resulting relation may be


equal to or less than that of the original
relation.

24
Projection Examples

• Project only the names and departments of


the employees:
π name, dept (EMP)
Results:
Name Dept
Smith CS
Jones Econ
Green Econ
Brown CS
Smith Fin

25
Combining Selection and Projection

 The selection and projection operators can


be combined to perform both operations.
 Show the names of all employees working in
the CS department:
π name σ ( Dept = 'CS' (EMP) )
Results:
Name
Smith
Brown

26
Combining Selection and Projection

 Show the name and rank of those Employees


who are not in the CS department or
Adjuncts:
π name, rank σ(¬ (Rank = 'Adjunct'∨ Dept = 'CS') (EMP) )
Results:
Name Rank
Green Assistant
Smith Associate

27
Aggregate Functions

 We can also apply Aggregate functions to


attributes and tuples:
 SUM
 MINIMUM
 MAXIMUM
 AVERAGE, MEAN, MEDIAN
 COUNT

28
Aggregate Functions

 Assume the relation EMP has the following


tuples:
Name Office Dept Salary
Smith 400 CS 45000
Jones 220 Econ 35000
Green 160 Econ 50000
Brown 420 CS 65000
Smith 500 Fin 60000

29
Aggregate Functions Examples

 Find the minimum Salary: F MIN (salary) (EMP)


Results:
MIN(salary)
35000

30
Aggregate Functions Examples

 Find the average Salary: F AVG (salary) (EMP)


Results:
AVG(salary)
51000

31
Aggregate Functions Examples

 Count the number of employees in the CS


department: F COUNT (name) σ ( Dept = 'CS' (EMP) )
Results:

COUNT(name)
2

32
Aggregate Functions Examples

 Find the total payroll for the Economics


department: F SUM (salary) σ (Dept = 'Econ' (EMP) )
Results:

SUM(salary)
85000

33
Join Operation

 Join operations bring together two relations


and combine their attributes and tuples in a
specific fashion.

 The generic join operator (called the Theta


Join is:

 It takes as arguments the attributes from the


two relations that are to be joined.

34
Join Operation

• For example assume we have the EMP relation as


above and a separate DEPART relation with (Dept,
MainOffice, Phone) :
EMP EMP.Dept = DEPART.Dept DEPART

 The join condition can be


 When the join condition operator is = then we call
this an Equijoin
 Note that the attributes in common are repeated.

35
Join Examples

 Assume we have the EMP relation from


above and the following DEPART relation:

Name Office Dept Salary Dept MainOffice Phone


Smith 400 CS 45000 CS 404 555-1212
Jones 220 Econ 35000 Econ 200 555-1234
Green 160 Econ 50000 Fin 501 555-4321
Brown 420 CS 65000 Hist 100 555-9876
Smith 500 Fin 60000

36
Join Examples

 Find all information on every employee


including their department info:
EMP emp.Dept = depart.Dept DEPART

37
Join Examples

 EMP emp.Dept = depart.Dept DEPART

Name Office EMP.Dept Salary DEPART.Dept MainOffice Phone


Smith 400 CS 45000 CS 404 555-1212
Jones 220 Econ 35000 Econ 200 555-1234
Green 160 Econ 50000 Econ 200 555-1234
Brown 420 CS 65000 CS 404 555-1212
Smith 500 Fin 60000 Fin 501 555-4321

38
Join Examples

 Find all information on every employee


including their department info, where the
employee works in an office numbered less
than the department main office:
EMP (emp.office < depart.mainoffice) ∧ (emp.dept =
depart.dept) DEPART

39
Join Examples

 EMP (emp.office < depart.mainoffice) ∧ (emp.dept =

depart.dept) DEPART

Name Office EMP.Dept Salary DEPART.Dept MainOffice Phone


Smith 400 CS 45000 CS 404 555-1212
Green 160 Econ 50000 Econ 200 555-1234
Smith 500 Fin 60000 Fin 501 555-4321

40
Natural Join

 Notice in the generic (Theta) join operation,


any attributes in common (such as dept
above) are repeated.
 The Natural Join operation removes these
duplicate attributes.
 The natural join operator is: *
 We can also assume using * that the join
condition will be = on the two attributes in
common.
41
Natural Join Example

Example: EMP * DEPART


Results:
Name Office Dept Salary MainOffice Phone
Smith 400 CS 45000 404 555-1212
Jones 220 Econ 35000 200 555-1234
Green 160 Econ 50000 200 555-1234
Brown 420 CS 65000 404 555-1212
Smith 500 Fin 60000 501 555-4321

42
Outer Join

 Often in joining two relations, a tuple in one relation


does not have a matching tuple in the other relation:
there is no matching value in the join attributes.
 To display rows in the result that do not have
matching values in the join column, use Outer join.
 Types of outer joins:
 Left Outer Join
 Right Outer Join
 Full 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.

 Missing values in the second relation are set to null.

 The advantage of an Outer join is that information is


preserved, that is, the Outer join preserves tuples
that would have been lost by other types of tuples.

44
Outer Join Examples

Assume we have two relations:


PEOPLE: MENU:

Name Age Food Food Day


Alice 21 Hamburger Pizza Monday
Bill 24 Pizza Hamburger Tuesday
Carl 23 Beer Chicken Wednesday
Dina 19 Shrimp Pasta Thursday
Tacos Friday

45
Left Outer Join

 PEOPLE people.food = menu.food MENU

Name Age people.Food menu.Food Day


Alice 21 Hamburger Hamburger Tuesday
Bill 24 Pizza Pizza Monday
Carl 23 Beer NULL NULL
Dina 19 Shrimp NULL NULL

46
Right Outer Join

 PEOPLE people.food = menu.food MENU

Name Age people.Food menu.Food Day


Bill 24 Pizza Pizza Monday
Alice 21 Hamburger Hamburger Tuesday
NULL NULL NULL Chicken Wednesday
NULL NULL NULL Pasta Thursday
NULL NULL NULL Tacos Friday

47
Full Outer Join

 PEOPLE people.food = menu.food MENU


Name Age people.Food menu.Food Day
Alice 21 Hamburger Hamburger Tuesday
Bill 24 Pizza Pizza Monday
Carl 23 Beer NULL NULL
Dina 19 Shrimp NULL NULL
NULL NULL NULL Chicken Wednesday
NULL NULL NULL Pasta Thursday
NULL NULL NULL Tacos Friday

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

FROM Specifies table(s) to be used.


WHERE Filters rows.
GROUP BY Forms groups of rows with same
column value.
HAVING Filters groups subject to some
condition.
SELECT Specifies which columns are to
appear in output.
ORDER BY Specifies the order of the output.

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

SQL Result Relational algebra


salary
select distinct 100
salary from E salary(E)
300

nr salary
select nr, 1 100
salary from E 5 300 nr, salary(E)

7 100

52
Relational algebra and SQL - Selection

SQL Result Relational algebra

select * from nr name salary


E where salary 1 John 100 salary < 200(E)
< 200 7 Tom 100

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

SQL Result Relational algebra

name salary
John 100
select name,
Tom 100
salary from name, salary ( salary
E where < 200(E))
salary < 200

54
Cartesian Product

table E (for EMPLOYEE) table D (for DEPARTMENT)

enr ename edept dnr dname


1 Bill A A Marketing
2 Sarah C B Sales
3 John A C Legal

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

enr ename dept dnr dname


1 Bill A A Marketing
select * σdept = dnr (E × D)
2 Sarah C C Legal
from E, D 3 or, using the
John A A Marketing equivalent join
where
dept = dnr operation
E dept = dnr D

57
Aggregate functions

 Table E (for EMPLOYEE)

nr name salary dept


1 John 100 A
5 Sarah 300 C
7 Tom 100 A
12 Anne null C

58
Sum

SQL Result Relational algebra

select sum
sum(salary) Fsum(salary)(E)
500
from E

 Count:
 Duplicates are not eliminated.
 Null values are ignored.

59
Count

SQL Result Relational algebra

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

 We can calculate aggregates "grouped by"


something:
SQL Result Relational algebra

dept sum
select A 200
sum(salary) C 300
from E group deptFsum(salary)(E)

by dept

61
Aggregate Functions

 Several aggregates simultaneously:

Relational
SQL Result
algebra

dept sum count


select
sum(salary), A 200 2
count(*)from C 300 1 deptFsum(salary),

E group by count(*)(E)

dept

62
Outer join

 Example: Table E (for EMPLOYEE); table D


(for DEPARTMENT)

enr ename dept dnr dname


1 Bill A A Marketing
2 Sarah B B Sales
3 John A C Legal

 List each employee together with the


department he or she works at.

63
Outer join

 What if we want to know the number of


employees at each department?

64
Outer join

Relational
SQL Result
algebra

enr ename dept dnr dname

select * 1 Bill A A Marketing


from (E 2 Sarah B B Sales
right outer E edept = dnr D
3 John A A Marketing
join D on
dept = dnr) null null null C Legal

65
Outer Join

SQL Result Relational algebra

dnr dname count


select dnr, A Marketing 2
dname, B Sales 1
count(*)from C Legal 1
(E right outer dnr, dnameFcount(*)(E dept =
dnr D)
join D on dept
= dnr)group by
dnr, dname

66
Outer Join

SQL Result Relational algebra

dnr dname count


select dnr,
dname, A Marketing 2
count(enr)from B Sales 1
(E right outer dnr, dnameFcount(enr)(E
C Legal 0 D)
join D on edept dept = dnr

= dnr) group by
dnr, dname

67
Relational Model

Review Questions – Set 03

1. What is a Relation Schema and a Relation?

2. What is an attribute? Define domain, tuple, and relation.

3. What is degree of a Relation?

4. What is Relationship, Relationship set, and Relationship type?

5. What is degree of Relationship type?

6. Discuss the importance of entity integrity and referential integrity constraints.

7. Write in detail about different types of constraints that can be specified on a


relation.

8. List and explain aggregate functions used in relational algebra with suitable
examples for each.

9. Consider the following tables:


Employee (Emp_no, Name, Emp_city)
Company (Emp_no, Company_name, Salary)
Write a relational algebra queries:
a. To display Employee name and company name.
b. To display employee name, employee city , company name and salary of
all the employees whose salary >10000
c. To display all the employees working in ‘Infosys’ company.

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.

15. Consider the following schema:


Suppliers (sid, sname, address)
Parts (pid, pname, color)
Catalog (sid, pid, cost)
Write the relational algebraic queries for the following:
a. Find the sids of suppliers who supply some red or green part
b. Find the sids of suppliers who supply every red or green part
c. Find the pids of parts supplied by at least two different suppliers.

Page 2 of 2
Relational Model

Shailesh K R

Department of Electrical & Electronics Engineering,


MIT, Manipal University

August 21, 2017

1/31
Example of a Relation

2/31
Attribute Types

I The set of allowed values for each attribute is called the


domain of the attribute

I Attribute values are (normally) required to be atomic; that is,


indivisible

I The special value null is a member of every domain. Indicated


that the value is “unknown”

I The null value causes complications in the definition of many


operations

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.

Relational algebra is a procedural query language, which takes


instances of relations as input and yields instances of relations as
output.

It uses operators to perform queries. An operator can be either


unary or binary.

They accept relations as their input and yield relations as their


output.

Relational algebra is performed recursively on a relation and


intermediate results are also considered relations.
7/31
Select Operation –
selection of rows (tuples)

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)

I List all students with the books they can borrow -


STUDENTS x BOOKS

I List all information about students whose major is CS. -


σMajor =‘CS 0 (STUDENTS)

I List all books published by McGraw-Hill before 1990-


σPublisher =‘McGraw −Hill 0 ANDYear <1990 (BOOKS)

27/31
Example queries II

I List the name of those authors who are living in Davis-


πAName (σAddresslike‘%DAVIS%0 (AUTHORS))

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

I Rename AName in the relation AUTHORS to Name -


ρAUTHORS(Name,Adress) (AUTHORS)

28/31
Questions 2

29/31
Questions 2

30/31
Questions 2

31/31
SQL – Structured Query Langauge

Review Questions – Set 04

1. What is SDL (Storage Definition Language)?

2. What is Data Storage - Definition Language?

3. What is DDL, DCL, and DML (Data Manipulation Language)?

4. What is VDL (View Definition Language)?

5. What is the purpose of group by clause in the SELECT statement?

6. Explain the aggregate functions used with SQL.

7. What are views? How they are created?

8. What do you mean by integrity constraints?

9. Differentiate between SQL commands DROP TABLE and DROP VIEW.

10. What is the difference between WHERE and Having Clause?

11. Discuss the various type of join operations? Why are these joins required?

12. Consider the following tables:


Employee (Emp_no, Name, Emp_city)
Company (Emp_no, Company_name, Salary)
i. Write a SQL query to display Employee name and company name.
ii. Write a SQL query to display employee name, employee city, company name
and salary of all the employees whose salary >10000
iii. Write a query to display all the employees working in ‘XYZ’ company.

13. Write SQL commands to perform the following.


i. To create a table STUDENT with fields Register number, St-name, Address,
course section, total marks.
ii. To insert values to that table interactively
iii. To create a view with fields register name and st-name.
iv. To change the total marks to 35 if the marks lie in between 25 and 34.
v. To delete tuples from the relation if the total marks is less than 35.

Page 1 of 1
Structured Query Language (SQL)

Shailesh K R

Department of Electrical & Electronics Engineering,


MIT, Manipal University

August 30, 2017

1/32
Structured Query Language (SQL)

I SQL is used to communicate with a relational database


management system.

I SQL statements are used to perform tasks such as update


data on a database, or retrieve data from a database.

I Standard SQL commands such as SELECT, INSERT,


DELETE, CREATE, DROP, ALTER can be used to
accomplish almost everything that one needs to do with a
database.

2/32
Data Definition Language

The SQL data-definition language (DDL) allows the specification


of information about relations, including:
I The schema for each relation.
I The domain of values associated with each attribute.
I Integrity constraints

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

INSERT INTO Students (sid, name, login, age, gpa) VALUES


(53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

DELETE FROM Students S WHERE S.name = ‘Smith’

UPDATE STUDENTS set GPA = 6.23, AGE=21 where SID =


53688;

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

Das könnte Ihnen auch gefallen