Top Banner
SQL SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database
64

SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Dec 24, 2015

Download

Documents

Asher Patterson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

SQLSQL

Overview

Data Definition

Basic Queries

Set Operations

Null Values

Aggregate Functions

Nested Subqueries

Modification of the Database

Page 2: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

HistoryHistory

IBM Sequel language developed as part of System R project at the IBM San Jose Research Laboratory

Renamed Structured Query Language (SQL)

ANSI and ISO standard SQL:

SQL-86

SQL-89

SQL-92

SQL:1999 (language name became Y2K compliant!)

SQL:2003…., 2008, 2011.

Not all examples here may work on your particular system.

Page 3: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Domain Types in SQLDomain Types in SQL

char(n). varchar(n). int. smallint. numeric(p,d). real, double precision. float(n). Can define your own as well.

Page 4: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Creating TablesCreating Tables

Example:

create table instructor ( ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2))

insert into instructor values (‘10211’, ’Smith’, ’Biology’, 66000); insert into instructor values (‘10211’, null, ’Biology’, 66000);

Page 5: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Integrity Constraints in Create TableIntegrity Constraints in Create Table

Example:create table instructor (

ID char(5), name varchar(20) not null, dept_name varchar(20), salary numeric(8,2), primary key (ID), foreign key (dept_name) references department)

primary key declaration on an attribute automatically ensures not null

Department(dept_name, address,….)

Page 6: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Drop and Alter Table ConstructsDrop and Alter Table Constructs

drop table

alter table

alter table r add A D

where A is the name of the attribute to be added to relation r and D is the domain of A.

All tuples in the relation are assigned null as the value for the new attribute.

alter table r drop A

where A is the name of an attribute of relation r

Dropping of attributes not supported by many databases.

Page 7: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Basic QueryBasic Query

A typical SQL query has the form:

select A1, A2, ..., An

from r1, r2, ..., rm

where P

Ai represents an attribute

ri represents a relation

P is a predicate.

The result of an SQL query is a relation.

Case insensitive

What are the relational algebra equivalents?

Page 8: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

The select Clause (Cont.)The select Clause (Cont.)

SQL allows duplicates in relations as well as in query results.

To force the elimination of duplicates, insert the keyword distinct after select.

Find the names of all departments with instructor, and remove duplicates

select distinct dept_namefrom instructor

The keyword all specifies that duplicates not be removed.

select all dept_namefrom instructor

Page 9: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

The select Clause (Cont.)The select Clause (Cont.)

An asterisk in the select clause denotes “all attributes”

select *from instructor

Arithmetic expressions involving +, –, , and /, and operating on attributes.

The query:

select ID, name, salary/12 from instructor

Page 10: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

The where ClauseThe where Clause

To find all instructors in Comp. Sci. dept with salary > 80000

select namefrom instructorwhere dept_name = ‘Comp. Sci.' and salary > 80000

Can use logical connectives and, or, and not

Comparisons can be applied to results of arithmetic expressions.

Page 11: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

The from ClauseThe from Clause

Find the Cartesian product instructor X teaches

select from instructor, teaches

Page 12: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

JoinsJoins

For instructors who have taught courses, find their names and the course ID of the courses they taught.

select name, course_id from instructor, teaches where instructor.ID = teaches.ID

Find the course ID, semester, year and title of each course offered by the Comp. Sci. department

select section.course_id, semester, year, title from section, course where section.course_id = course.course_id and dept_name = ‘Comp. Sci.’

Instructor (ID, name, dept_name, salary)

Teaches (ID, course_id, section_id, semester, year)

Page 13: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Try Writing Some Queries in SQLTry Writing Some Queries in SQL

Find departments that are housed in the ‘Taylor’ building.

Department(dept_name, building, budget)

Find course sections taught in rooms that can hold at least 100 students.

Section(course_id, sec_id, semester, year, building, room#, time_slot_id)

Classroom(building, room#, capacity)

Find all advisors of students in Biology.

Student(ID, name, dept_name, tot_cred)

Advisor(S_ID, I_ID)

Instructor(ID, name, dept_name, salary)

Page 14: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Natural JoinNatural Join

Natural join matches tuples with the same values for all common attributes, and retains only one copy of each common column

select *from instructor natural join teaches;

Instructor (ID, name, dept_name, salary)

Teaches (ID, course_id, section_id, semester, year)

Page 15: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Natural Join (Cont.)Natural Join (Cont.)

Beware of attributes with same name which get equated incorrectly

List names of instructors along with titles of courses that they teach

Instructor(ID, name, dept_name, salary)

Teaches(ID, course_id, sec_id, semester, year)

Course(course_id, title, dept_name,credits)

Incorrect version (equates course.dept_name with instructor.dept_name)

select name, titlefrom instructor natural join teaches natural join course;

Correct version

select name, titlefrom instructor natural join teaches, coursewhere teaches.course_id= course.course_id;

Another correct version

select name, titlefrom (instructor natural join teaches) join course using(course_id);

Page 16: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

The Rename OperationThe Rename Operation

select ID, name, salary/12 as monthly_salaryfrom instructor

Find the names of all instructors who have a higher salary than some instructor in ‘Comp. Sci’.

select distinct T. namefrom instructor as T, instructor as Swhere T.salary > S.salary and S.dept_name = ‘Comp. Sci.’

Keyword as is optional and may be omitted instructor as T ≡ instructor T

Page 17: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

String OperationsString Operations

percent (%). The % character matches any substring.

underscore (_). The _ character matches any character.

Find names of all instructors whose name includes the substring “dar”.

select namefrom instructorwhere name like '%dar%'

Match the string “100 %”like ‘100 \%' escape '\’

Page 18: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

String OperationsString Operations

SELECT CONCAT(name,dept_name) FROM Instructor

SELECT UPPER(dept_name) FROM Instructor

SELECT length(dept_name) FROM Instructor

– SELECT SUBSTR(dept_name,2,4)

– FROM Instructor

– WHERE dept_name= ’Biology';

» Answer = iolo

Page 19: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Ordering the Display of TuplesOrdering the Display of Tuples

List in alphabetic order the names of all instructors

select distinct namefrom instructororder by name

order by name desc

order by dept_name, name

Page 20: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Set OperationsSet Operations

Find courses that ran in Fall 2009 or in Spring 2010

Find courses that ran in Fall 2009 but not in Spring 2010

(select course_id from section where sem = ‘Fall’ and year = 2009) union(select course_id from section where sem = ‘Spring’ and year = 2010)

Find courses that ran in Fall 2009 and in Spring 2010

(select course_id from section where sem = ‘Fall’ and year = 2009) intersect(select course_id from section where sem = ‘Spring’ and year = 2010)

(select course_id from section where sem = ‘Fall’ and year = 2009) except(select course_id from section where sem = ‘Spring’ and year = 2010)

Page 21: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Set OperationsSet Operations

Set operations union, intersect, and except

Each of the above operations automatically eliminates duplicates

To retain all duplicates use the corresponding multiset versions union all, intersect all and except all.

Suppose a tuple occurs m times in r and n times in s, then, it occurs:

m + n times in r union all s

min(m,n) times in r intersect all s

max(0, m – n) times in r except all s

Page 22: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Null ValuesNull Values

It is possible for tuples to have a null value, denoted by null, for some of their attributes

null signifies an unknown value or that a value does not exist.

The result of any arithmetic expression involving null is null

Example: 5 + null returns null

The predicate is null can be used to check for null values.

Example: Find all instructors whose salary is null.

select namefrom instructorwhere salary is null

Page 23: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Null Values and Three Valued LogicNull Values and Three Valued Logic

Any comparison with null returns unknown

Example: 5 < null or null <> null or null = null

Three-valued logic using the truth value unknown:

OR: (unknown or true) = true, (unknown or false) = unknown (unknown or unknown) = unknown

AND: (true and unknown) = unknown, (false and unknown) = false, (unknown and unknown) = unknown

NOT: (not unknown) = unknown

“P is unknown” evaluates to true if predicate P evaluates to unknown

Result of where clause predicate is treated as false if it evaluates to unknown

Page 24: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Aggregate FunctionsAggregate Functions

These functions operate on the multiset of values of a column of a relation, and return a value

avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

Page 25: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Aggregate Functions (Cont.)Aggregate Functions (Cont.)

select avg (salary)from instructorwhere dept_name= ’Comp. Sci.’;

select count (distinct ID)from teacheswhere semester = ’Spring’ and year = 2010

select count (*)from course;

Instructor(ID, name, dept_name, salary)

Teaches (ID, course_id, section_id, semester, year)

Courses (course_id, dept_name)

Page 26: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Aggregate Functions – Group ByAggregate Functions – Group By

Find the average salary of instructors in each department

select dept_name, avg (salary)from instructorgroup by dept_name

avg_salary

Page 27: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Aggregation (Cont.)Aggregation (Cont.)

Attributes in select clause outside of aggregate functions must appear in group by list

/* erroneous query */select dept_name, ID, avg (salary)from instructorgroup by dept_name;

Page 28: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Aggregate Functions – Having ClauseAggregate Functions – Having Clause

Find the names and average salaries of all departments whose average salary is greater than 42000

Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups

select dept_name, avg (salary)from instructorgroup by dept_namehaving avg (salary) > 42000;

lect dept_name, avg (salary)from instructorwhere age > 35group by dept_namehaving avg (salary) > 42000;

Page 29: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Null Values and AggregatesNull Values and Aggregates

Total all salaries

select sum (salary )from instructor

Above statement ignores null amounts

Result is null if there is no non-null amount

All aggregate operations except count(*) ignore tuples with null values on the aggregated attributes

What if collection has only null values?

count returns 0

all other aggregates return null

Page 30: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Nested SubqueriesNested Subqueries

SQL provides a mechanism for the nesting of subqueries.

A subquery is a select-from-where expression that is nested within another query.

A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.

Page 31: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Example QueryExample Query

Find courses offered in Fall 2009 and in Spring 2010

Find courses offered in Fall 2009 but not in Spring 2010

select distinct course_idfrom sectionwhere semester = ’Fall’ and year= 2009 and course_id in (select course_id from section where semester = ’Spring’ and year= 2010);

select distinct course_idfrom sectionwhere semester = ’Fall’ and year= 2009 and course_id not in (select course_id from section where semester = ’Spring’ and year= 2010);

Page 32: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Example QueryExample Query

Find the total number of (distinct) students who have taken course sections taught by the instructor with ID 10101

Note: Above query can be written in a much simpler manner. The formulation above is simply to illustrate SQL features.

select count (distinct ID)from takeswhere (course_id, sec_id, semester, year) in (select course_id, sec_id, semester, year from teaches where teaches.ID= 10101);

Page 33: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Set ComparisonSet Comparison

Find names of instructors with salary greater than that of some (at least one) instructor in the Biology department.

Same query using > some clause

select namefrom instructorwhere salary > some (select salary

from instructorwhere dept name = ’Biology’);

select distinct T.namefrom instructor as T, instructor as Swhere T.salary > S.salary and S.dept name = ’Biology’;

Mysql: any, some, all should be possible

Page 34: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Definition of Some ClauseDefinition of Some Clause

F <comp> some r t r such that (F <comp> t )Where <comp> can be:

056

(5 < some ) = true

05

0

) = false

5

05(5 some ) = true (since 0 5)

(read: 5 < some tuple in the relation)

(5 < some

) = true(5 = some

(= some) inHowever, ( some) not in

Page 35: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Example QueryExample Query

Find the names of all instructors whose salary is greater than the salary of all instructors in the Biology department.

select namefrom instructorwhere salary > all (select salary from instructor where dept name = ’Biology’);

Page 36: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Definition of all ClauseDefinition of all Clause

F <comp> all r t r (F <comp> t)

056

(5 < all ) = false

610

4

) = true

5

46(5 all ) = true (since 5 4 and 5 6)

(5 < all

) = false(5 = all

( all) not inHowever, (= all) in

Page 37: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Test for Empty RelationsTest for Empty Relations

The exists construct returns the value true if the argument subquery is nonempty.

exists r r Ø

not exists r r = Ø

Page 38: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Correlation VariablesCorrelation Variables

Yet another way of specifying the query “Find all courses taught in both the Fall 2009 semester and in the Spring 2010 semester”

select course_id from section as S where semester = ’Fall’ and year= 2009 and exists (select * from section as T where semester = ’Spring’ and year= 2010 and S.course_id= T.course_id);

Correlated subquery

Correlation name or correlation variable

Page 39: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Not ExistsNot Exists

Find all students who have taken all courses offered in the Biology department.

select distinct S.ID, S.namefrom student as Swhere not exists ( select course_id from course where dept_name = ‘Biology’ AND

course_id NOT IN (select T.course_id

from takes as T where S.ID = T.ID));

Note that X – Y = Ø X Y

Note: Cannot write this query using = all and its variants

Nested query: (Biology courses – courses taken by student)

Page 40: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Test for Absence of Duplicate TuplesTest for Absence of Duplicate Tuples

The unique construct tests whether a subquery has any duplicate tuples in its result.

Find all courses that were offered at most once in 2009

select T.course_idfrom course as Twhere unique (select R.course_id from section as R where T.course_id= R.course_id and R.year = 2009);

Mysql: ??

Page 41: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Subqueries in from clauseSubqueries in from clause

Find the average instructors’ salaries of those departments where the average salary is greater than $42,000.”

select dept_name, avg_salaryfrom (select dept_name, avg (salary) as avg_salary from instructor group by dept_name)where avg_salary > 42000;

We already know how to do this with the having clause

select dept_name, avg_salaryfrom instructorgroup by dept_name)havingavg_salary > 42000;

Page 42: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Scalar SubqueryScalar Subquery

select dept_name, (select count(*) from instructor where department.dept_name = instructor.dept_name) as num_instructorsfrom department;

Scalar: a single value, i.e., a single row and single column. Count, average, …

Mysql: yes.

Page 43: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Outer JoinOuter 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.

Page 44: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Left Outer JoinLeft Outer Join

course natural left outer join prereq

Course(course_id, title, dept_name, credits)Prereq(course_id, prereq_id)

/* note in above that prereq_id refers to course_id in Course

Page 45: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Right Outer JoinRight Outer Join

course natural right outer join prereq

Course(course_id, title, dept_name, credits)Prereq(course_id, prereq_id)

/* note in above that prereq_id refers to course_id in Course

Page 46: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Full Outer JoinFull Outer Join

course natural full outer join prereq

Course(course_id, title, dept_name, credits)Prereq(course_id, prereq_id)

/* note in above that prereq_id refers to course_id in Course

Page 47: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Varieties of JoinVarieties of Join

R1 cross join R2: cross product

R1, R2: cross product

R1 Join R2: cross product (so you must have a Where clause to specify the join condition)

R1 Natural Join R2 (enforces equality on all identically named attributes). Also removes duplicate columns

R1 Natural Left Outer Join R2 (null values on R1 side as appropriate)

R1 Natural Right Outer Join R2 (null values on R2 side as appropriate)

R1 Inner Join R2 ON (R1.attx = R2.attM AND R1.att3 <> R2.att3)

R1 Inner Join R2 Using (att2) ….. Meaning you want equality on att2 values

R1 Left Join R2 ON (…) (null values on R1 sides as appropriate)

R1 Right Join R2 ON (….) (null values on R2 sides as appropriate)

-- apply somewhat specifically to mysql.

Page 48: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

ViewsViews

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 instructors name and department, but not the salary. This person should see a relation described, in SQL, by

select ID, name, dept_name from instructor

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 to a user as a “virtual relation” is called a view.

Instructor(ID, name, dept_name, salary)

Page 49: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

View DefinitionView Definition

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

create view v as < query expression >

where <query expression> 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.

Page 50: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Example ViewsExample Views

A view of instructors without their salary

create view faculty as select ID, name, dept_name from instructor

Find all instructors in the Biology department select name from faculty where dept_name = ‘Biology’

Create a view of department salary totals create view departments_total_salary(dept_name, total_salary) as select dept_name, sum (salary) from instructor group by dept_name;

Instructor(ID, name, dept_name, salary)

Page 51: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Views Defined Using Other ViewsViews Defined Using Other Views

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’;

Page 52: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

View ExpansionView Expansion

Expand use of a view in a query/another view

create view physics_fall_2009_watson as(select course_id, room_numberfrom (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’)where building= ’Watson’;

[create view physics_fall_2009_watson as select course_id, room_number from physics_fall_2009 where building= ’Watson’;]

Page 53: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Views Defined Using Other ViewsViews Defined Using Other Views

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

A view relation v1 is said to depend directly on a view relation v2 if v2 is used in the expression defining v1

A view relation v1 is said to depend on view relation v2 if either v1

depends directly to v2 or there is a path of dependencies from v1 to v2

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

Page 54: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

View ExpansionView Expansion

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

Let view v1 be defined by an expression e1 that may itself contain uses of view relations.

View expansion of an expression repeats the following replacement step:

repeatFind any view relation vi in e1

Replace the view relation vi by the expression defining vi until no more view relations are present in e1

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

Page 55: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Update of a ViewUpdate of a View

Add a new tuple to faculty view which we defined earlier

insert into faculty values (’30765’, ’Green’, ’Music’);

This insertion must be represented by the insertion of the tuple

(’30765’, ’Green’, ’Music’, null)

into the instructor relation.

Instructor(ID, Name, dept, salary)

create view faculty as select ID, name, dept_name from instructor

Instructor(ID, name, dept_name, salary)

Page 56: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Some Updates cannot be Translated UniquelySome Updates cannot be Translated Uniquely

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

which department, if multiple departments in Taylor?

what if no department is in Taylor?

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. max(salary)

Any attribute not listed in the select clause can be set to null

The query does not have a group by or having clause.

Page 57: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Modification of the Database – DeletionModification of the Database – Deletion

Delete all instructors

delete from instructor

Delete all instructors from the Finance department delete from instructor where dept_name= ’Finance’;

Delete all tuples in the instructor relation for those instructors associated with a department located in the Watson building.

delete from instructor where dept name in (select dept name from department where building = ’Watson’);

Page 58: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Example QueryExample Query

Delete all instructors whose salary is less than the average salary of instructors

delete from instructorwhere salary< (select avg (salary) from instructor);

Problem: as we delete tuples from deposit, the average salary changes

Solution used in SQL:

1. First, compute avg salary and find all tuples to delete

2. Next, delete all tuples found above (without

recomputing avg or retesting the tuples)

Page 59: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Modification of the Database – InsertionModification of the Database – Insertion

Add a new tuple to course

insert into course values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);

or equivalently

insert into course (course_id, title, dept_name, credits) values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);

Add a new tuple to student with tot_creds set to null

insert into student values (’3003’, ’Green’, ’Finance’, null);

Page 60: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Modification of the Database – InsertionModification of the Database – Insertion

Add all instructors to the student relation with tot_creds set to 0

insert into studentselect ID, name, dept_name, 0

from instructor

The select from where statement is evaluated fully before any of its results are inserted into the relation (otherwise queries like

insert into table1 select * from table1would cause problems)

Page 61: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Modification of the Database – UpdatesModification of the Database – Updates

Increase salaries of instructors whose salary is over $100,000 by 3%, and all others receive a 5% raise

Write two update statements:

update instructor set salary = salary * 1.03 where salary > 100000; update instructor set salary = salary * 1.05 where salary <= 100000;

The order is important

Can be done better using the case statement (next slide)

Page 62: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Case Statement for Conditional UpdatesCase Statement for Conditional Updates

Same query as before but with case statement

update instructor set salary = case when salary <= 100000 then salary * 1.05

when salary > 100000 AND

salary <= 200000 then salary * 1.02 else salary * 1.03 end

Page 63: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Updates with Scalar SubqueriesUpdates with Scalar Subqueries

Recompute and update tot_creds value for all students

update student S set tot_cred = ( select sum(credits) from takes natural join course where S.ID= takes.ID and takes.grade <> ’F ’ and takes.grade is not null);

Sets tot_creds to null for students who have not taken any course

Page 64: SQL Overview Data Definition Basic Queries Set Operations Null Values Aggregate Functions Nested Subqueries Modification of the Database.

Data Definition LanguageData Definition Language

The schema for each relation.

The domain of values associated with each attribute.

Integrity constraints

The set of indices to be maintained for each relations.

Security and authorization information for each relation.

The physical storage structure of each relation on disk.

Allows the specification of not only a set of relations but also information about each relation, including: