CS计算机代考程序代写 SQL data structure database chain Chapter 4: Intermediate SQL

Chapter 4: Intermediate SQL

2

● Join Expressions

● Views

● Transactions

● Integrity Constraints

● SQL Data Types and Schemas

● Authorization

Joined Relations

3

● Join operations take two relations and return as a result another

relation.

● A join operation is a Cartesian product which requires that tuples in

the two relations match (under some condition). It also specifies the

attributes that are present in the result of the join

● The join operations are typically used as subquery expressions in

the FROM clause

● Three types of joins:

○ Natural join

○ Inner join

○ Outer join

Natural Join

4

● Natural join matches tuples with the same values for all common

attributes, and retains only one copy of each common column.

Examples:

● List the names of instructors along with the course ID of the

courses that they taught:

● Same query in SQL with “natural join” construct:

SELECT name, course_id
FROM student, takes
WHERE student.ID = takes.ID;

SELECT name, course_id
FROM student
NATURAL JOIN takes;

IF the two of the above queries are same what are the benefits
of using “JOIN”?

Multiple Relations

6

A FROM clause can have multiple relations combined using natural join:

SELECT A1, A2, … An
FROM r1 NATURAL JOIN r2 … NATURAL JOIN rn
WHERE P;

“student”Table

SELECT * FROM student;

7

“takes”Table

SELECT *
FROM takes;

8

Natural Join of “student” and “takes”

SELECT *
FROM student
NATURAL JOIN takes;

9

Common Errors with Natural Join?

Unrelated attributes with same name can get equated incorrectly.

Classwork Poll Question-1:

Which one of these two queries is correct for listing the

names of all students along with the titles of courses that

they have taken:

SELECT name, title
FROM student NATURAL JOIN takes, course
WHERE takes.course_id = course.course_id;

SELECT name, title
FROM student NATURAL JOIN takes NATURAL JOIN course;

a.

b.

Common Errors with Natural Join?

Consider the following SQL query that seeks to find a list of titles of all

courses taught in Spring 2017 along with the name of the instructor.

Select name, title from

instructor natural join teaches natural join section natural join course

where semester = ‘Spring’ and year = 2017;

What is wrong with this query?

Although the query is syntactically correct, it does not compute the

expected answer because dept name is an attribute of both course and

instructor.

As a result of the natural join, results are shown only when an instructor

teaches a course in her or his own department.

Class Discussion: Common Errors with Natural Join

10

Natural Join with UsingClause

● To avoid the danger of equating attributes erroneously, we can use

the “USING” construct that allows us to specify exactly which

columns should be equated.

● Example:

SELECT name, title
FROM (student NATURAL JOIN takes) JOIN course USING (course_id);

11

‘ON’ Join Condition

● The ON condition allows a general predicate over the relations being

joined.

● This predicate is written like a WHERE clause predicate except for

the use of the keyword ON.

● Example:

The ON condition above specifies that a tuple from student matches a

tuple from takes if their ID values are equal.

● Equivalent to:

SELECT *
FROM student JOIN takes ON student.ID = takes.ID;

SELECT *
FROM student, takes
WHERE student.ID = takes.ID;

Natural Join and Loss of Information

● Given relations r, s:

● Natural Join: A B C D E
3 1 5 7 9

4 2 6 8 10

A B C

3 1 5

4 2 6

B D E

1 7 9

2 8 10

r s

16

In this case, natural join/inner join = left join = right join = full outer join.
No loss of information Why?

Natural Join and Loss of Information

● Given relations r, s:

● Natural Join:
A B C D E

3 1 5 7 9

A B C

3 1 5

4 0 6

B D E

1 7 9

11 8 10

r s

17

There is some information loss in this case. What is that loss?
What if one or both of that table information is important,
and we want to avoid this information loss

Natural Join and Loss of Information

● Given relations r, s:

● r Natural left outer join s:

A B C

3 1 5

4 0 6

B D E

1 7 9

11 8 10

r s

18

A B C D E

3 1 5 7 9

4 0 6 – –

Is there any information loss? Which relation?

Natural Join and Loss of Information

● Given relations r, s:

● r Natural right outer join s:

A B C

3 1 5

4 0 6

B D E

1 7 9

11 8 10

r s

19

A B C D E

3 1 5 7 9

– 11 – 8 10

Is there any information loss? Which relation?

Natural Join and Loss of Information

● Given relations r, s:

● r Natural full outer join s:

A B C

3 1 5

4 0 6

B D E

1 7 9

11 8 10

r s

20

A B C D E

3 1 5 7 9

4 0 6 – –

– 11 – 8 10

Is there any information loss? Which relation?

12

Outer Join

● An extension of the join operation that avoids loss of information.

● Computes the join and then adds tuples form one relation that does

not match tuples in the other relation to the result of the join.

● Uses NULL values.

● Three forms of outer join:

○ left outer join

○ right outer join

○ Full outer join

13

Outer Join (cont.)

● Relation ‘course’

● Relation ‘prereq’

● Note that:

○ ‘course’ and ‘prereq’ are modified from the default uni db

○ ‘course’ is missing data for CS-347

○ ‘prereq’ is missing data for CS-315

Left Outer Join

Result:

In relational algebra:

course ⟕prereq

SELECT *
FROM course NATURAL LEFT OUTER JOIN prereq;

23

Right Outer Join

Result:

In relational algebra:

course ⟖prereq

SELECT *
FROM course NATURAL RIGHT OUTER JOIN prereq;

24

Full Outer Join

Result:

In relational algebra:

course ⟗ prereq

SELECT *
FROM course NATURAL FULL OUTER JOIN prereq;

25

Joined Types andConditions

26

● Join operations take two relations and return as a result another

relation.

● These additional operations are typically used as subquery

expressions in the FROM clause

● Join conditions define which tuples in the two relations match, and

what attributes are present in the result of the join.

● Join types define how tuples in each relation that do not match any

tuple in the other relation (based on the join condition) are treated.

Join Types

Inner join

Left outer join

Right outer join

Full outer join

Join Conditions

NATURAL

ON

USING

Joined Relations – Examples

Also:

SELECT *
FROM course NATURAL RIGHT OUTER JOIN prereq;

SELECT *
FROM course FULL OUTER JOIN prereq USING (course_id);

27

Joined Relations – Examples (cont.)

What is the difference between the above, and a natural join?

SELECT *
FROM course INNER JOIN prereq
ON course.course_id = prereq.course_id;

SELECT *
FROM course LEFT OUTER JOIN prereq
ON course.course_id = prereq.course_id;

28

Joined Relations – Examples (cont.)

Also:

SELECT *
FROM course NATURAL RIGHT OUTER JOIN prereq;

SELECT *
FROM course FULL OUTER JOIN prereq USING (course_id);

29

Views

● In some cases, it is not desirable for all users to see the entire

logical model (that is, all the actual relations stored in the

database.)

● Consider a person who needs to know an instructor’s name and

department, but not the salary. This person should see a relation

described, in SQL, by:

● A view provides a mechanism to hide certain data from the view of

certain users.

● Any relation that is not of the conceptual model but is made visible

21 to a user as a “virtual relation” is called a view.

SELECT ID, name, dept_name
FROM instructor;

View Definition

31

● A view is defined using the create view statement which has the

form

create view v as < query expression >

where is any legal SQL expression. The view

name is represented by v.

● Once a view is defined, the view name can be used to refer to the

virtual relation that the view generates.

● View definition is not the same as creating a new relation by

evaluating the query expression

○ Rather, a view definition causes the saving of an expression; the

expression is substituted into queries using the view.

Class Work: Poll Question-2

Which of the following is not true about use of a database view?

A – It restricts data access.

B – It makes queries easy.

C – It provides data independence.

D – It prevents different views of same data.

Examples of “view”

33

● A view of instructors without their salary:

● Find all instructors in the Biology department:

● Create a view of department salary totals:

CREATE VIEW faculty AS
SELECT ID, name, dept_name
FROM instructor;

SELECT name
FROM faculty
WHERE dept_name = ‘Biology’;

CREATE VIEW departments_total_salary (dept_name, total_salary) AS
SELECT dept_name, SUM(salary)
FROM instructor
GROUP BY dept_name;

Views Defined Using Other Views

34

● One view may be used in the expression defining another view

● A view relation v
1
is said to depend directly on a view relation v

2
if v

2

is used in the expression defining v
1

● A view relation v
1
is said to depend on view relation v

2
if either v

1

depends directly to v
2
or there is a path of dependencies from v

1
to

v
2

● A view relation v is said to be recursive if it depends on itself.

Views Defined Using OtherViews (cont.)

35

CREATE VIEW physics_fall_2009 AS
SELECT course.course_id, sec_id, building, room_number
FROM course, section
WHERE course.course_id = section.course_id
AND course.dept_name = ‘Physics’
AND section.semester = ‘Fall’
AND section.year = 2009;

CREATE VIEW physics_fall_2009_watson AS
SELECT course_id, room_number
FROM physics_fall_2009
WHERE building = ‘Watson’;

View Expansion

36

● A way to define the meaning of views defined in terms of other

views.

● Let view v
1
be defined by an expression e

1
that may itself contain

uses of view relations.

● View expansion of an expression repeats the following replacement

step:

repeat

Find any view relation v
i
in e

1

Replace the view relation v
i
by the expression defining vi

until no more view relations are present in e
1

● As long as the view definitions are not recursive, this loop will

terminate

27

View Expansion (cont.)

● Expand the view:

● To:

CREATE VIEW physics_fall_2009_watson AS
SELECT course_id, room_number
FROM (SELECT course.course_id, building, room_number

FROM course, section
WHERE course.course_id = section.course_id

AND course.dept_name = ‘Physics’
AND section.semester = ‘Fall’
AND section.year = 2009) AS physics_fall_2009

WHERE building = ‘Watson’;

CREATE VIEW physics_fall_2009_watson AS
SELECT course_id, room_number
FROM physics_fall_2009
WHERE building = ‘Watson’;

Materialized Views

38

● Certain database systems allow view relations to be physically

stored.

○ Physical copy created when the view is defined.

○ Such views are called materialized views

● If relations used in the query are updated, the materialized view

result becomes out of date

○ Need to maintain the view, by updating the view whenever the

underlying relations are updated.

● PostgreSQL supports this with CREATE MATERIALIZED VIEW …

Update of a View

39

● Add a new tuple to faculty view which we defined earlier:

● This insertion must be represented by the insertion into the

instructor relation

○ Must have a value for salary.

● Two approaches:

○ Reject the insert

○ Insert the tuple (‘30765’, ‘Green’, ‘Music’, NULL) into the

instructor relation

INSERT INTO faculty
VALUES (‘30765’, ‘Green’, ‘Music’);

Some Updates cannot beTranslated Uniquely

40

Issues:

● which department, if multiple departments in Taylor?

● what if no department is in Taylor?

CREATE VIEW instructor_info AS
SELECT ID, name, building
FROM instructor, department
WHERE instructor.dept_name = department.dept_name;

INSERT INTO instructor_info
VALUES (‘69987’, ‘White’, ‘Taylor’);

And Some Updates not atAll

41

What happens if we insert (‘25566’, ‘Brown’, ‘Biology’, 100000) into

history_instructors?

CREATE VIEW history_instructors AS
SELECT *
FROM instructor
WHERE dept_name = ‘History’;

View Updates in SQL

42

● Most SQL implementations allow updates only on simple views

○ The FROM clause has only one database relation.

○ The SELECT clause contains only attribute names of the relation,

and does not have any expressions, aggregates, or DISTINCT

specification.

○ Any attribute not listed in the SELECT clause can be set to NULL

○ The query does not have a GROUP BY or HAVINGclause.

Transactions

43

● A transaction consists of a sequence of query and/or update

statements and is a “unit” of work

● The SQL standard specifies that a transaction begins implicitly when

an SQL statement is executed.

● The transaction must end with one of the following statements:

○ Commit work: The updates performed by the transaction

become permanent in the database.

○ Rollback work: All the updates performed by the SQL

statements in the transaction are undone.

● Atomic transaction

○ either fully executed or rolled back as if it never occurred

● Isolation from concurrent transactions

https://www.postgresqltutorial.com/postgresql-transaction/

Integrity Constraints

44

● Integrity constraints guard against accidental damage to the

database, by ensuring that authorized changes to the database do

not result in a loss of data consistency. A few examples:

○ A checking account must have a balance greater than

$10,000.00

○ A salary of a bank employee must be at least $4.00 an hour

○ A customer must have a (non-null) phone number

Integrity Constraints on a Single Relation

45

● NOTNULL

● PRIMARY KEY

● UNIQUE

● CHECK (P), where P is a predicate

Not Null and Unique Constraints

46

● NOTNULL

○ Declare name and budget to be NOT NULL

name varchar(20) NOTNULL

budget numeric(12,2) NOTNULL

● UNIQUE (A
1
, A

2
, …, A

m
)

○ The unique specification states that the attributes A , A , … A
1 2 m

form a candidate key.

○ Candidate keys are permitted to be null (in contrast to primary

keys).

The CHECK clause

47

● CHECK (P) where P is apredicate

Example: ensure that semester is one of fall, winter, spring or

summer.

CREATE TABLE section (
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
PRIMARY KEY (course_id, sec_id, semester, year),
CHECK (semester IN (‘Fall’, ‘Winter’, ‘Spring’, ‘Summer’))

);

Referential Integrity

48

● Ensures that a value that appears in one relation for a given set of

attributes also appears for a certain set of attributes in another

relation.

○ Example: If “Biology” is a department name appearing in one of

the tuples in the instructor relation, then there exists a tuple in

the department relation for “Biology”.

● Let A be a set of attributes. Let R and S be two relations that contain

attributes A and where A is the primary key of S.

● A is said to be a foreign key of R if for any values of A appearing in R

these values also appear in S.

Referential Integrity (cont.)

49

● Foreign keys can be specified as part of the CREATE TABLE

statement

● By default, a foreign key references the primary key attributes of the

referenced table.

● SQL allows a list of attributes of the referenced relation to be

specified explicitly.

FOREIGN KEY (dept_name) REFERENCES department;

FOREIGN KEY (dept_name) REFERENCES department (dept_name);

Cascading Actions in Referential Integrity

50

● When a referential-integrity constraint is violated, the normal

procedure is to reject the action that caused the violation.

● An alternative, in case of delete or update is to cascade

● alternative actions to cascade: set null, set default, no action

CREATE TABLE course (
…,
dept_name varchar(4),
PRIMARY KEY (…),
FOREIGN KEY (dept_name) REFERENCES department (dept_name)
ON DELETE CASCADE
ON UPDATE CASCADE

);

41

Integrity Constraint Violation During Transactions

● Consider:

● How to insert a tuple without causing constraint violation?

○ insert father and mother of a person before inserting person

○ OR, set father and mother to NULL initially, update after

inserting all persons (not possible if father and mother

attributes declared to be NOTNULL)

○ OR defer constraint checking

CREATE TABLE person (
id char(10),
name char(40),
mother char(10),
father char(10),
PRIMARY KEY (id),
FOREIGN KEY (mother) REFERENCES person,
FOREIGN KEY (father) REFERENCES person

);

Complex CheckClauses

52

● The predicate in the CHECK clause can be an arbitrary predicate that

can include a subquery.

● The check condition states that the time_slot_id in each tuple in the

section table is actually the identifier of a time slot in the time_slot

relation

● The condition has to be checked not only when a tuple is inserted or

modified in section, but also when the relation time_slot changes

CHECK (time_slot_id IN (SELECT time_slot_id FROM time_slot))

Type Conversion and Formatting Functions

53

● We can use an expression of the form

CAST (e AS t)

to convert an expression e to the type t

● Example:

● COALESCE function

SELECT CAST (id AS numeric(5)) AS inst_id
FROM instructor
ORDER BY inst_id;

SELECT id, COALESCE (salary, 0) AS salary
FROM instructor;

Built-in Data Types inSQL

54

● date: Dates, containing a (4 digit) year, month and date

○ Example: date ‘2005-7-27’

● time: Time of day, in hours, minutes and seconds.

○ Example: time ‘09:00:30’ time ‘09:00:30.75’

● timestamp: date plus time of day

○ Example: timestamp ‘2005-7-27 09:00:30.75’

● interval: period of time

○ Example: interval ‘1’ day

○ Subtracting a date/time/timestamp value from another gives an

interval value

○ Interval values can be added to date/time/timestamp values

Large-ObjectTypes

55

● Large objects (photos, videos, CAD files, etc.) are stored as a large

object:

○ blob: binary large object –object is a large collection of

uninterpreted binary data (whose interpretation is left to an

application outside of the database system)

○ clob: character large object –object is a large collection of

character data

○ When a query returns a large object, a pointer is returned rather

than the large object itself.

● In PostgreSQL: bytea

User-DefinedTypes

56

● create type construct in SQL creates user-defined types

● Use:

● In PostgreSQL it is often used to create a composite type used in

the stored procedures as the return data type.

CREATE TYPE Dollars AS numeric(12,2) FINAL

CREATE TABLE department (
dept_name varchar(20),
building varchar(15),
budget Dollars

);

Domains

57

● create domain construct in SQL-92 creates user-defined domain

types

● Types and domains are similar. Domains can have constraints, such

as not null, specified on them.

CREATE DOMAIN person_name char(20) NOT NULL

CREATE DOMAIN degree_level varchar(10)
CONSTRAINT degree_level_test
CHECK (VALUE IN (‘Bachelors’, ‘Masters’, ‘Doctorate’))

DefaultValues

58

SQL allows a default value to be specified for an attribute as illustrated

by the following create table statement:

CREATE TABLE student (
id varchar(5),
name varchar(20) NOT NULL,
dept_name varchar(20),
tot_cred numeric(3,0) DEFAULT 0,
PRIMARY KEY (id)

);

Index Creation

59

● Indices are data structures used to speed up access to records with

specified values for index attributes

○ e.g. select *

from student

where ID = ‘12345’

can be executed by using the index to find the required record,

without looking at all records of student

Index Creation

60

CREATE TABLE student (
id
name
dept_name
tot_cred

varchar(5),
varchar(20) NOT NULL,
varchar(20),
numeric(3,0) DEFAULT 0,

PRIMARY KEY (id)
);

CREATE INDEX student_id_index ON student (id);

Authorization

61

Forms of authorization on parts of the database:

● Read -allows reading, but not modification of data.

● Insert -allows insertion of new data, but not modification of existing

data.

● Update -allows modification, but not deletion of data.

● Delete -allows deletion of data.

Forms of authorization to modify the database schema

● Index – allows creation and deletion of indices.

● Resources -allows creation of new relations.

● Alteration -allows addition or deletion of attributes in a relation.

● Drop -allows deletion of relations.

Authorization Specification in SQL

62

● The GRANT statement is used to conferauthorization

GRANT

ON TO

is:

○ a user-id

○ public, which allows all valid users the privilege granted

○ a role

● Granting a privilege on a view does not imply granting any privileges

on the underlying relations.

● The grantor of the privilege must already hold the privilege on the

specified item (or be the database administrator).

Privileges in SQL

63

● select: allows read access to relation,or the ability to query using

the view

○ Example: grant users U
1
, U

2
, and U

3
select authorization on the

instructor relation:

grant select on instructor to U
1
, U

2
, U

3

● insert: the ability to insert tuples

● update: the ability to update using the SQL update statement

● delete: the ability to delete tuples.

● all privileges: used as a short form for all the allowable privileges

Classwork Question: Poll

Which of the following is a system privilege?

A – Creating new users

B – Removing users

C – Removing tables

D – All of the above.

Revoking Authorization in SQL

65

● The revoke statement is used to revoke authorization.

revoke

on from

● Example:

revoke select on branch from U
1
, U

2
, U

3

may be all to revoke all privileges the revokee may

hold.

● If includes public, all users lose the privilege except

those granted it explicitly.

● If the same privilege was granted twice to the same user by different

grantees, the user may retain the privilege after the revocation.

● All privileges that depend on the privilege being revoked are also

revoked.

Roles

66

● create role instructor;

● grant instructor to Amit;

● Privileges can be granted to roles:

○ grant select on takes to instructor;

● Roles can be granted to users, as well as to other roles

○ create role teaching_assistant

○ grant teaching_assistant to instructor;

■ Instructor inherits all privileges of teaching_assistant

● Chain of roles

○ create role dean;

○ grant instructor to dean;

○ grant dean to Satoshi;

Authorization on Views

67

● Suppose that a geo_staff member issues

● What if

○ geo_staff does not have permissions on instructor?

○ creator of view did not have some permissions on instructor?

CREATE VIEW geo_instructor AS
SELECT *
FROM instructor
WHERE dept_name = ‘Geology’;

GRANT SELECT ON geo_instructor TO geo_staff;

SELECT *
FROM geo_instructor;

Other Authorization Features

68

● references privilege to create foreign key

○ grant reference (dept_name) on department to Mariano;

○ why is this required?

● transfer of privileges

○ grant select on department to Amit with grant option;

○ revoke select on department from Amit, Satoshi cascade;

○ revoke select on department from Amit, Satoshi restrict;

● Read Section 4.6 for more details

Acknowledgements

69

● Some content adapted or modified from the course assigned

textbook and its relevant resources by:

Silberschatz, A., Korth, H. F., and Sudarshan, S. (2019). Database

System Concepts, 7th Edition. McGraw-Hill.