Relational Query Languages
Relational Query Languages
LANGUAGES
■ Relational database systems are equipped with a query language that can assist its users to
query the database instances.
■ Query languages: Allow manipulation and retrieval of data from a database.
■ Relational model supports simple and powerful QLs:
– Strong formal foundation based on logic.
– Allows for much optimization
■ Query Languages != programming languages!
– QLs not expected to be “Turing complete”( no complete instruction set)
– QLs not intended to be used for complex calculations.
– QLs support easy, efficient access to large data sets.
■ There are two kinds of mathematical query languages that form the basis for the “real”
languages (e.g. SQL) and for implementation
– Relational Algebra: More operational(procedural), very useful for representing
execution plans.
– Relational Calculus: Lets users describe what they want, rather than how to
compute it. (Non-operational, declarative.)
SQL- Structured Query Language
■ SQL is a standard language for storing, manipulating
and retrieving data in databases.
■ You use SQL in: MySQL, SQL Server, MS Access,
Oracle, Sybase, Informix, Postgres, and other database
systems.
■ SQL is an ANSI (American National Standards
Institute) standard
What Can SQL do?
■ retrieve data from a database
■ insert records in a database
■ update records in a database
■ delete records from a database
■ create new databases
■ create new tables in a database
■ create stored procedures in a database
■ create views in a database
■ set permissions on tables, procedures, and views
RELATIONAL
ALGEBRA
What is Relational Algebra (RA)
■ An algebra is a formal structure consisting of sets and operations
on those sets.
■ Not used as a query language in actual DBMSs. (SQL instead.)
■ Relational algebra is a procedural query language,
■ Operands of this algebra are relations (input and output)
– relations are sets of tuples
■ It uses operators to perform queries. An operator can be either
unary or binary.
■ Relational algebra is performed recursively on a relation and
intermediate results are also considered relations.
Basic Operators
■ Intersection, ; r ∩ s = r – (r – s)
■ join, (natural, equi-join, theta join, semi-join)
■ division, ÷
■ renaming: Not essential, but (very!) useful.
Selection ()
■ SQL
SELECT * FROM S2 WHERE rating >8
■ RA
σ rating>8(S2)
Projection ( )
A1,…,An (σc(R))
■ Firstly select rows based on a condition from R to form a new relation R1 (intermediate results)
– R1σc(R)
■ Secondly project the selected attributes from R1 to form a new relation R2
– R2 A1,…,An (R1)
Retrieve all pet names and their ratings
whose rating is greater than 8
■ SQL ■ RA
RA
Employee (σDepartment= ‘sales’(r1))
Given two relations r1 and r2. Use SQL
scripts and relational algebra to answer
the following queries.
b) Find the head of the ‘production’
department
SQL
SELECT Head
FROM r2
WHERE Department =
‘production’;
Head
RA
Mori
HeadσDepartment= ‘production’(r2))
Given two relations r1 and r2. Use SQL
scripts and relational algebra to answer the
following queries.
c) Who is the head of the employee named
‘Smith’
SQL
SELECT head
FROM r1
INNER JOIN r2
Head
ON r1.Department = r2. Department
where Employee = “Smith"; Brown
RA
Head (r1 ⋈ r2)) OR
Head (r1 (Employee=’Smith’) r2)
JOIN
MySQL
Different Types of SQL JOINs
■ (INNER) JOIN
■ LEFT (OUTER) JOIN
■ RIGHT (OUTER) JOIN
■ FULL (OUTER) JOIN
INNER JOIN
S1
sid sname rating
22 Dustin 7
31 Lubber 8
58 Rusty 10
R1
age sid bid day