• 검색 결과가 없습니다.

Create Table Construct

N/A
N/A
Protected

Academic year: 2022

Share "Create Table Construct"

Copied!
29
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

CHAPTER 3 CHAPTER 3 SQL

SQL

(2)

Chapter 3: SQL p Q

ƒ Data Definition

ƒ Basic Structure of SQL Queries

ƒ Set Operations

ƒ Set Operations

ƒ Aggregate Functions

ƒ Null Values

ƒ Nested Subqueriesq

ƒ Complex Queries

ƒ Views

ƒ Views

ƒ Modification of the Database

ƒ Joined Relations

(3)

Create Table Construct

ƒ An SQL relation is defined using the create table command:Q g create table r (A1 D1, A2 D2, ..., An Dn,

(integrity-constraint1), (integrity constraint1), ...,

(integrity-constraintk))

( g y k))

à r is the name of the relation

à each Ai is an attribute name in the schema of relation r

à Di is the data type of values in the domain of attribute Ai

ƒ Example:

create table branch

(branch-name char(15) not null,

( ( ) ,

branch-city char(30), assets integer)g

(4)

Schema Used in Examples p

(5)

Basic Structure of SQL Queries Q Q

ƒ SQL is based on set and relational operations with certain modifications and

h t

enhancements

ƒ A typical SQL query has the form:

select A1, A2, ..., An from r1, r2, ..., rm where P

à Ais represent attributes

à r s represent relationsris represent relations

à P is a predicate.

ƒ This query is equivalent to the relational algebra expression.

A1, A2, ..., An(σP (r1 x r2 x ... x rm))

ƒ The result of an SQL query is a relation.

(6)

Tuple Variables p

ƒ Defined in the from clause by use of as.y

ƒ Find the customer names and their loan numbers for all customers having a loan at some branch.

customers having a loan at some branch.

select customer-name, T.loan-number, S.amount from borrower as T loan as S

from borrower as T, loan as S

where T.loan-number = S.loan-number

ƒ Find the n mes of ll br n hes th t h e gre ter ssets th n

ƒ Find the names of all branches that have greater assets than some branch located in Brooklyn.

select distinct T.branch-name from branch as T branch as S from branch as T, branch as S

(7)

Ordering the Display of Tuples g p y p

ƒ List in alphabetic order the names of all customers having a loan in Perryridge branch

select distinct customer-name from borrower, loan

where borrower loan-number - loan.loan-number and branch name = ‘Perryridge’

branch-name = Perryridge order by customer-name

ƒ desc for descending order or asc for ascending order

ƒ desc for descending order or asc for ascending order (default)

à E.g. order by customer-name descg y

à E.g. order by customer-name desc, loan-number asc

(8)

Set Operations p

ƒ The set operations: union, intersect, except correspond to the p , , p p relational algebra operations ∪, ∩, −.

ƒ Each set operation automatically eliminates duplicatesEach set operation automatically eliminates duplicates

ƒ to retain all duplicates use multiset versions:

union all intersect all and except all 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 sm + 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 smax(0, m n) times in r except all s

(9)

Set Operations p (cont.) ( )

ƒ Find all customers who have a loan, an account, or both:

(select customer-name from depositor) union

union

(select customer-name from borrower)

ƒ Find all customers who have both a loan and an account

ƒ Find all customers who have both a loan and an account.

(select customer-name from depositor) intersect

intersect

(select customer-name from borrower)

Fi d ll h h b l

ƒ Find all customers who have an account but no loan.

(select customer-name from depositor) except

(select customer-name from borrower)

(10)

Aggregate Functions gg g

ƒ Operate on the multiset of values of a column of a relation, and p return a value

avg: average value avg: average value

min: minimum value max: maximum value sum: sum of values

count: number of values

(11)

Aggregate Functions – Examples

ƒ Find the names of all branches where the average account

gg g p

g balance is more than $1,200.

select branch-name, avg (balance) select branch name, avg (balance) from account

group by branch-name

g p y

having avg (balance) > 1200 Note:

di i h h i l li d f h f i f

predicates in the having clause are applied after the formation of groups whereas

predicates in the where clause are applied before forming groups

(12)

Null Values

ƒ Tuples may have a null value (null), for some of their attributesp y ( )

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

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

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

Find all loan numbers which appear in the loan relation with null values for amount.

select loan number select loan-number from loan

where amount is null (why not ‘amount=null’) where amount is null (why not amount=null )

ƒ The result of any arithmetic expression involving null is null

E 5 + ll ll

à E.g. 5 + null returns null

ƒ However, aggregate functions simply ignore nulls

à more on this shortly

(13)

Null Values and Three Valued Logic

ƒ Any comparison with null returns unknown

g

y p

à 5 < null or null <> null or null = null

ƒ Three-valued logic using the truth value unknown:g g

à 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 (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

(14)

Null Values and Aggregates gg g

ƒ Total all loan amounts

select sum (amount) from loan

from loan

à Above statement ignores null amounts

à result is null if there is no non-null amount

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

(15)

Nested Subqueries q

ƒ A subquery is a select-from-where expression that is nested q y p within another query.

ƒ Common use of subqueries:Common use of subqueries:

perform tests for set membership, set comparisons, and set cardinality

cardinality.

(16)

Example Query p Q y

ƒ Find all customers who have both an account and a loan at the bank.

select distinct customer-name select distinct customer name from borrower

where customer-name in (select customer-name( from depositor)

ƒ Find all customers who have a loan at the bank but do not haveFind all customers who have a loan at the bank but do not have an account at the bank

select distinct customer name select distinct customer-name from borrower

where customer-name not in (select customer-name where customer-name not in (select customer-name

(17)

Example Query p Q y

ƒ Find all customers who have both an account and a loan at the Perryridge branch

select distinct customer-name select distinct customer name from borrower, loan

where borrower.loan-number = loan.loan-number and branch-name = “Perryridge” and

(branch-name, customer-name) in

( )

(select branch-name, customer-name from depositor, account

where depositor.account-number = account.account-number)

Note: This query can be written in a much simpler manner.

(18)

Test for Empty Relations p y

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

ƒ exists r ⇔ r ≠ Øexists r ⇔ r ≠ Ø

ƒ not exists r ⇔ r = Ø

(19)

Example Query p Q y

ƒ Find all customers who have an account at all branches located in Brooklyn.

select distinct S.customer-name from depositor as S

h i (

where not exists (

(select branch-name from branch

where branch-city = ‘Brooklyn’) except

( l R b h

(select R.branch-name

from depositor as T, account as R

where T.account-number = R.account-number and S.customer-name = T.customer-name))

Note: X – Y = Ø ⇔ X ⊆ Y

(20)

Views

ƒ Consider a person who needs to know a customer’s name, loan p number and branch name, but has no need to see the loan

amount. This person should see a relation described, in SQL, by (select customer_name, borrower.loan_number, branch_name

( _ , _ , _

from borrower, loan

where borrower.loan_number = loan.loan_number ))

ƒ A view provides a mechanism to hide certain data from the view

ƒ A view provides a mechanism to hide certain data from the view of certain users.

A l i h i f h l d l b i d

ƒ Any relation that is not of the conceptual model but is made

(21)

View Definition

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

create view v as < query expression >

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.

ƒ When a view is created, the query expression is stored in the W v w , q y p

database; the expression is substituted into queries using the view.

(22)

Example Queries p Q

ƒ A view consisting of branches and their customersg

create view all_customer as

(select branch_name, customer_name from depositor, account

where depositor.account_number = b )

account.account_number ) union

(select branch name customer name (select branch_name, customer_name from borrower, loan

where borrower.loan_number = loan.loan_number )

ƒ Find all customers of the Perryridge branch

select customer name select customer_name

(23)

Views Defined Using Other Views g

ƒ One view may be used in the expression defining another view y p g

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

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 v or there is a path of dependencies from v to v

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.

(24)

View Expansion p

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

ƒ Let view vLet view v11 be defined by an expression ebe defined by an expression e11 that may itself containthat may itself contain uses of view relations.

ƒ View expansion of an expression repeats the following

ƒ View expansion of an expression repeats the following replacement step:

repeat repeat

Find any view relation vi in e1

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

A l h i d fi i i i hi l ill

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

(25)

Update of a View p

ƒ Create a view of all loan data in the loan relation, hiding the g amount attribute

create view loan branch as create view loan_branch as

select loan_number, branch_name from loan

ƒ Add a new tuple to branch_loan insert into b n h l n insert into branch_loan

values ('L-37‘, 'Perryridge‘)

Thi i i b d b h i i f h l

This insertion must be represented by the insertion of the tuple ('L-37', 'Perryridge', null )

into the loan relation

(26)

Updates Through Views (Cont.) p g ( )

ƒ Some updates through views are impossible to translate into p g p updates on the database relations

create view v as

select loan_number, branch_name, amount from loan

where branch name = ‘Perryridge’

where branch_name = Perryridge

insert into v values ( 'L-99','Downtown', '23')

ƒ Others cannot be translated uniquely

i i ll l ('P id ' 'J h ')

insert into all_customer values ('Perryridge', 'John')

‚ Have to choose loan or account, and create a new loan/account number!

M SQL i l i ll d l i l i

ƒ Most SQL implementations allow updates only on simple views

(27)

Materialized Views (section 14.5) ( )

ƒ A materialized view is a view whose contents are computed and p stored.

ƒ Consider the viewConsider the view

create view branch_total_loan(branch_name, total_loan) as select branch_name, sum(amount)_ , ( )

from loan

group by branch_name

g p y

ƒ Materializing the above view would be very useful if the total loan amount is required frequently

loan amount is required frequently

à Saves the effort of finding multiple tuples and adding up their amounts

(28)

Materialized View Maintenance

ƒ The task of keeping a materialized view up-to-date with the underlying data is known as materialized view maintenance

ƒ Materialized views can be maintained by recomputation on every update

ƒ A better option is to use incremental view maintenance

à Changes to database relations are used to compute changes to the materialized view, which is then updated

Vi i b d b

ƒ View maintenance can be done by

à Manually defining triggers on insert, delete, and update of each relation in the view definition

the view definition

à Manually written code to update the view whenever database relations are updated

à Periodic recomputation (e.g. nightly)

(29)

END OF CHAPTER 3

END OF CHAPTER 3

참조

관련 문서

이 밖에도 재난 관련 지원프로그램으로 긴급융자프로그램 (Emergency Loan Program), 재난상환유예프로그램 (Disaster Set-Aside Program),

Select: Skin surface (Target of intersection, white border on upper figure) Click: [GO] (in the lower right side of modeling window). Select: original and duplicated curves

Granger causality tests shaw that agriculture and transport storage industry are affected loan but the loan of restaurants and hotel industry and

select customer name borrower loan number as loan id amount select customer-name, borrower.loan-number as loan-id, amount from borrower, loan.. where borrower lo n number = lo n

™ The first nested query, which is correlated, retrieves the project numbers on which the employee works, which is different for each employee tuple because of the

select item-name, color, size, sum(number) select item name, color, size, sum(number) from sales. group by

Should a module be used with any of the absolute maximum ratings exceeded, the characteristics of the module may not be recovered, or in an extreme case, the module

v EMP 테이블에서 DEPTNO의 오름차순으로 정렬하고 같은 경우 JOB의 오름차순으로 JOB이 같은 경우에는 SAL의 내림차순으로 DEPTNO, JOB, SAL, EMPNO, ENAME, HIREDATE