0% found this document useful (0 votes)
21 views41 pages

4 SQL

The document discusses SQL and relational algebra. It provides an overview of fundamental relational algebra operators and their equivalence to SQL queries. It also covers additional SQL features like views, subqueries, aggregation, and ordering.

Uploaded by

adamshkolnik5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views41 pages

4 SQL

The document discusses SQL and relational algebra. It provides an overview of fundamental relational algebra operators and their equivalence to SQL queries. It also covers additional SQL features like views, subqueries, aggregation, and ordering.

Uploaded by

adamshkolnik5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

SQL

COMP 353/453
Database Programming
Prof. Silva

These slides were prepared by Prof. Silva adapting the slides from Fundamentals of Database Systems (Elmasri and Navathe) and Understanding
Relational Database Query Languages (Dietrich)
Declarative Language
Relational algebra is a procedural language, giving the operations to
retrieve the data from the database.

Relational calculus is a declarative (nonprocedural) query language,


indicating the properties of the data to retrieve from the database.

•Tuple Relational Calculus (TRC)


• variables represent tuples
•Domain Relational Calculus (DRC)
• variables represent domains

SQL is the industry standard query language that is declarative and


its syntax is related to TRC.

2
Basic Query Expressions
Typical SQL query

SELECT A1,A2,...,An
FROM r1,r2,...,rm
WHERE P

is equivalent to the relational algebra expression:

 A ,A ,..., A (  P ( r 1  r 2     r m ))
1 2 n

3
Overview: Fundamental Relational Algebra Operators

Relational
SQL
Algebra

rÈs (select * from r) union (select * from s)

r-s (select * from r) except (select * from s)

rs select * from r, s

sP(r) select * from r where P

pA(r) select A from r


Examples: Fundamental Relational Algebra
Operators
cs_majors È math_majors: select * from cs_majors
union
select * from math_majors

cs_majors - math_majors: select * from cs_majors


except
select * from math_majors

cs_profs  cs_courses: select * from cs_profs, cs_courses

sCLASS='SR'(cs_majors): select * from cs_majors where class = 'SR'

p
math_majors
(cs_majors):
cs_majors
ID,NAME
select id, name from cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
Examples: Additional Relational Algebra Operators

cs_majors Ç math_majors:
select * from cs_majors
intersect
select * from math_majors
cs_profs ⋈NAME=TNAME teaches:
select *
from cs_profs P, teaches T - - note table alias names
where P.name = T.tname
cs_profs ⋈ cs_courses ⋈ teaches:
select P.name, P.office, C.crsid, C.crstitle
from cs_profs P, cs_courses C, teaches T
where P.name = T.tname and
math_majors cs_majors T.tcrsid = C.crsid
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
SQL Expressive Power
• The examples illustrate that SQL is at least as powerful as relational algebra
• An SQL relation is not a set of tuples but a multiset (bag) of tuples because
a relation can have two or more tuples that are identical in all their attribute
values. To force the elimination of duplicates, use distinct in the select clause:
select distinct A1,A2,...,An from r

• Note that union and except return a set of tuples. To allow duplicates, use
union all or except all.
• SQL provides an order by clause to display the query result in sorted order:

select A1,...,An from r where P order by Ai


• SQL provides inherent support for aggregation: avg, min, max, sum, count

SQL has more expressive power than relational algebra.


SQL Standardization
• SQL:1986 – Original SQL standard
• SQL:1992 – SQL2, included joined tables and data definition
• SQL:1999 – Object-relational features
• SQL:2003 – Introduces XML features
• SQL:2006 – Ways that SQL can be used with XML
• SQL:2016 – Adds row pattern matching, JSON
• SQL:2019 – Multidimensional arrays

SQL2 allows for a (natural) join specification in the from clause:


select *
from (cs_profs join teaches on name=tname)
math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
Intermediate Tables: Views and Snapshots
• create view VIEW-NAME as S-F-W
A view is a relation that is not explicitly stored in the database. It is made
visible to a user as a "virtual relation". A view is recomputed for each
query that refers to the VIEW-NAME.
e.g. create view cs_seniors as
select * from cs_majors where class = ‘Senior’

• create snapshot VIEW-NAME as S-F-W


A snapshot is a materialized view, which is materialized once at the time
the "create snapshot" command is issued.

math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
More on Querying
SQL supports more complex queries than the basic select-from-
where statement.
• Nested subqueries
• Explicit Sets
• Nested correlated subqueries (Exists/Not Exists)
• Order by
• Aggregation
• Group by
• Having
Nested Subqueries: Set Comparison
Query:
Find computer science majors who are also majoring in math.
Alternative
SQL: select id, name
from cs_majors (select id, name from cs_majors)
INTERSECT
where id in (select id, name from math_majors)
(select id
from math_majors)

Evaluation: (1) nested query


(2) outer query using results of nested query
math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
Nested Subqueries: Equality Comparison
Query:
Find the name of the professor who is currently teaching the
‘Database Management’ course.
Alternative
SQL: select tname
from teaches select tname
from teaches, cs_courses
where tcrsid = where tcrsid = crsid AND
(select crsid crstitle = 'Database Programming'
from cs_courses
where crstitle = 'Database Programming')

To use an equality comparison, the nested query must return a single value.
math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
Explicit Sets Example

Query:
Find upper-level computer science majors

SQL: select id
from cs_majors
where class in ('Junior', 'Senior'); Alternative
select id
from cs_majors
where class = 'Junior’
OR class = 'Senior';

math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
Nested Correlated Subqueries: Exists/Not Exists
A nested correlated subquery is a nested query that references a value of the outer
subquery.
Query: Find the professors who are not teaching any courses.
SQL: select name
from cs_profs P Alternative
where not exists Select name from cs_profs
(select * EXCEPT
from teaches T Select DISTINCT tname from teaches
where T.tname = P.name)
EXISTS: Evaluates to FALSE if the subquery evaluates to empty;
TRUE otherwise.
Evaluation:
For each cs_profs tuple, the nested query selects all teaches tuples whose tname value matches
the cs_profs name; if the result of the exists subquery is empty then that professor is not
teaching any classes, so the professor is included in the result.
math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
ORDER BY
order by ORDER-SPEC-COMMALIST

ORDER-SPEC-COMMALIST ::= {COLUMN-REF | INTEGER|} [asc|desc]


where
COLUMN-REF is an attribute name, and
INTEGER is a column position
asc means ascending (default order if none specified)
desc means descending
Query: Find the courses taught by each professor.
Display the results in alphabetical order by the name of the professor,
with courses displayed in descending order by professor.
SQL: select T.tname, T.tcrsid
from teaches T
order by T.tname asc, T.tcrsid desc
math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
SQL Exercises
dept emp takes course
dnum id empid crsid
dname name crsid cname
mgrid sal date inst
dnum len

a) List the ids of employees in the “Finance” department that earn


more than $75,000.

b) List the names of the employees who took the course with id
‘DB01’ on June 25, 1990.

c) Who is the manager of the employee 'Tabitha Jensen'?


Give the manager name and department name.
Aggregation
To aggregate over the entire table (with column renaming):
select aggregate_operator(Ai) [as nameOfAttribute]
from r
[where P]

e.g., Find the number of courses that are currently being taught.

select count(*) as numberOfCourses


from teaches T

The name of the attribute representing the aggregation is implementation –


dependent. Therefore, it is highly recommended to use the column renaming
feature in SQL to provide a name for the aggregation.
math_majors cs_majors
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
Grouping
To aggregate after grouping:
select Ai, ..., Aj, aggregate_operator(An)
from r
[where P]
group by Ai,...,Aj
Find the number of computer science courses that a computer science
professor is currently teaching:
select tname, count(*) as numberCoursesForCSProf
from teaches
group by tname;
SQL groups the tuples together based on the values of the group by
attributes and performs the aggregation over the group. The attributes in
the select clause and the group by attributes must agree so that the
aggregation
math_majors cs_majorscan return one value for the group.
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
SQL: Aggregation Example
"Find the employee(s) with the maximum salary“
emp(ID, NAME, SAL, SUPERID, DNO)
select id, name, sal
from emp
where sal = (select max(sal)
from emp)

dept emp takes course


dnum id empid crsid
dname name crsid cname
mgrid sal date inst
dnum len
SQL Exercise: Aggregation

dept emp takes course


dnum id empid crsid
dname name crsid cname
mgrid sal date inst
dnum len

Find the average salaries of employees by department.


Give the department number (DNUM) and average salary.
Having Clause
• can only appear in conjunction with a GROUP BY
• specifies a condition on the grouping attributes
• only groups that satisfy the having condition are included in the answer

Query: For each professor teaching more than one course, display the name of
the professor and the number of courses the professor is teaching.
SQL: select T.tname, count(*) as numberOfCoursesForProf
from teaches T
group by T.tname
having count(*) > 1
Evaluation: (1) where - condition on individual tuples
(2) group by - groups by grouping attributes
math_majors cs_majors(3) having - conditions on the group selected
id id cs_profs teaches cs_course
name name name tname crsid
class class office tcrsid crstitle
age age
SQL Arithmetic Expressions
The select statement can include arithmetic expressions using +, -, *, /
Consider a scenario of a student worker who is hired at an hourly rate, and
reports hours weekly:
studentWorker(sID, deptCode, hourlyRate)
hoursReported(payDate, sID, hours)

Query: Compute weekly gross for all students ordered by payDate & sID:
SQL: select payDate, sID, hours * hourlyRate as wklyGross
from studentWorker natural join hoursReported
order by payDate, sID
Query: Compute year to date gross for all students ordered by sID
SQL: select sID, sum(hours * hourlyRate) as ytdGross
from studentWorker natural join hoursReported
group by sID
order by sID
22
SQL Arithmetic Expressions Exercise
Using the EmpDeptCourseTakes enterprise, write a query to compute the
yearly commitment in funds for salary and benefits by department,
assuming that the cost of benefits for an employee is 35% of the salary.

dept emp takes course


dnum id empid crsid
dname name crsid cname
mgrid sal date inst
dnum len

23
SQL Data Definition Language
• The industry-standard query language, SQL, also has a
component for defining data, called the data definition
language (DDL).

• The standard specifies statements for creating a table, and


the constraints on the attributes of the table.
Create Table

create table TABLENAME


(COLNAME COLTYPE [ATTRIBUTE-CONSTRAINT],
...,
TABLE-CONSTRAINT-LIST, ...);

Where:
ATTRIBUTE-CONSTRAINT represents constraints on the
attribute, and
TABLE-CONSTRAINT-LIST represents constraints on the table
Attribute Constraints - Simple

Simple attribute constraints include:


• primary key
• not null
• unique
Attribute Constraints - Simple Examples

create table cs_profs


( name varchar(24) primary key,
office varchar(6) not null);

create table cs_courses


( crsid varchar(6) primary key,
crstitle varchar(20) not null);
Data Definition
Create Table: Attribute Constraints

ATTRIBUTE-CONSTRAINT ::= [constraint CONSTRAINT-NAME]


not null | primary key | unique |
CHECK-CONSTRAINT | COLUMN-REFERENCE

CHECK-CONSTRAINT ::= check (BOOLEAN-VALUED-EXPRESSION)

COLUMN-REFERENCE ::= references REFERENCED-TABLE-NAME


[(REFERENCED-COLUMN-NAME)]
[on update ACTION] [on delete ACTION]

ACTION ::= no action | cascade | set null | set default


Attribute Constraints: Check Example

create table cs_majors


( id varchar(3) primary key,
name varchar(24) not null,
class varchar(10)
check (class in ('Freshmen', 'Sophomore',
'Junior', 'Senior', 'Graduate' ) )
);
References Action

The action specifies what to do on the update/delete of the


referenced primary key.
• no action: This default option prevents an update/delete if
referential integrity will be violated by the operation.
• cascade: updates the foreign key/deletes the tuple for all
matching rows in the referencing table.
• set null: updates the foreign key to null
This action cannot be used with the not null constraint on the
foreign key.
• default: updates the foreign key to the default value
Attribute Constraints: Column Reference Example

create table teaches


(name varchar(24)
references cs_profs(name)
on update cascade,
crsid varchar(6)
references cs_courses(crsid)
on update cascade,
primary key (name, crsid)
);
Data Definition
Create Table: Table Constraints

TABLE-CONSTRAINT-LIST ::= [constraint CONSTRAINT_NAME]


PRIMARY-KEY-CONSTRAINT | UNIQUE-CONSTRAINT |
REFERENTIAL-CONSTRAINT | CHECK-CONSTRAINT
PRIMARY-KEY-CONSTRAINT ::=
primary key (COLUMN-NAME [{,COLUMN-NAME }...])
UNIQUE-CONSTRAINT ::= unique (COLUMN-NAME [{,COLUMN-NAME}...])
REFERENTIAL-CONSTRAINT ::=
foreign key (REFERENCING-COLUMN-NAME
[{, REFERENCING-COLUMN-NAME }...]) REFERENCES-SPECIFICATION
REFERENCES-SPECIFICATION ::= references REFERENCED-TABLE-NAME
[(REFERENCED-COLUMN-NAME [{, REFERENCED-COLUMN-NAME }...])]
[on update ACTION] [on delete ACTION]
ACTION ::= no action | cascade | set null | set default
Data Definition: Create Table

create table teaches


(tname varchar(24),
tcrsid varchar(6),
primary key (tname, tcrsid),
foreign key (tname) references cs_profs(name)
on update cascade,
foreign key (tcrsid) references cs_courses(crsid)
on update cascade);
Data Manipulation: Insert
The insert into statement provides a mechanism to insert tuples into the
named table according to the specified ATTRIBUTE-LIST and SOURCE:

insert into TABLENAME [ (ATTRIBUTE-LIST) ]


SOURCE

where SOURCE is either


• a list of explicit values, or
e.g. insert into cs_courses
values ('COMP453', …)
• the specification of a select statement
e.g. insert into cs_courses
select … from … where …
Data Manipulation: Delete
The delete statement removes tuples from the table specified. The
optional where clause restricts the delete operation to the rows
of the table that satisfy the DELETE-CONDITION. If the where
clause is not specified, then all rows of the table are deleted:

delete from TABLENAME


[where DELETE-CONDITION]

e.g. delete from cs_majors


where class = 'Senior'
Data Manipulation: Update

The update statement modifies the columns given in the set


clause to the values determined by the expressions specified.
The optional where clause can restrict the rows of the table to
be updated:

update TABLENAME
set COLUMN-NAME = VALUE-EXPRESSION, …
[where UPDATE-CONDITION]

e.g. update cs_majors


set class = 'Senior'
where class = 'Junior'
Security & Authorization
protects the database from malicious access
Security mechanisms:
• Discretionary security mechanisms
• Used to grant privileges to users
• Mandatory security mechanisms
• Classify data and users into various security classes
• Implement security policy
• Role-based security

Responsibility of DBA
• account creation to restrict access to the DB
• privilege granting/revocation
• security level assignment
Discretionary Privileges: Account Level

• CREATE SCHEMA - create a database schema


• CREATE TABLE - create a base table
• CREATE VIEW - create a virtual table
• ALTER - to add or remove attributes from relations
• DROP - to delete relations or views
• MODIFY - to insert, delete, or update tuples
• SELECT - retrieve tuples by select
Discretionary Privileges: Relation Level

• SELECT – an authorized database user must have the select


table privilege to access any column of a table
To restrict a user’s access to only some columns of a table, define a view on
that table to project out the desired attributes and grant the user the select
privilege on the view.
• INSERT - can restrict to certain attributes
• UPDATE - can restrict to certain attributes
• DELETE
• REFERENCE – can restrict to certain attributes; allows the
column of a table to be referenced in the specification of a
constraint; e.g., referential integrity
Granting Privileges
grant PRIVILEGE-LIST
on TABLE-LIST
to USER-LIST
[with grant option]

“with grant option” specifies that the users on the USER-LIST


will be able to grant these privileges to other users

e.g. grant delete on cs_majors to cs_dept;


grant select on teaches to
faculty with grant option; grant update on
students to cs_dept;
Revoking Privileges
revoke PRIVILEGE-LIST
on TABLE-LIST
from USER-LIST

Note that the system must automatically revoke privileges


granted by “users with grant option”.

e.g. revoke select on teaches from csTA;

You might also like