Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic...
-
Upload
alberta-atkins -
Category
Documents
-
view
236 -
download
7
Transcript of Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic...
Chapter 3: SQLChapter 3: SQL Data Definition LanguageData Definition Language Basic Structure of SQLBasic Structure of SQL Set OperationsSet Operations Aggregate FunctionsAggregate Functions Null ValuesNull Values Nested SubqueriesNested Subqueries
Schema Used in ExamplesSchema Used in Examples
DDL and DMLDDL and DML
DDL: Data Definition LanguageDDL: Data Definition Language Changes data base schemaChanges data base schema Example: create table, drop table, alter table, Example: create table, drop table, alter table,
create indexcreate index DML: Data Manipulation LanguageDML: Data Manipulation Language
Read or change the content of the databaseRead or change the content of the database Example: insert, delete, select, updateExample: insert, delete, select, update
Data Definition LanguageData Definition Language
May change:May change: The schema for each relation.The schema for each relation. The domain of values associated with The domain of values associated with
each attribute.each attribute. Integrity constraintsIntegrity constraints The set of indices to be maintained for The set of indices to be maintained for
each relations.each relations. Security and authorization information Security and authorization information
for each relation.for each relation. The physical storage structure of each The physical storage structure of each
relation on disk.relation on disk.
Domain Types in SQLDomain Types in SQL char(n).char(n). Fixed length character string, with Fixed length character string, with
user-specified length user-specified length n.n. varchar(n). varchar(n). Variable length character strings, Variable length character strings,
with user-specified maximum length with user-specified maximum length n.n. int. int. Integer (a finite subset of the integers that Integer (a finite subset of the integers that
is machine-dependent).is machine-dependent). smallint.smallint. Small integer (a machine-dependent Small integer (a machine-dependent
subset of the integer domain type).subset of the integer domain type). numeric(p,d).numeric(p,d). Fixed point number, with user- Fixed point number, with user-
specified precision of specified precision of pp digits, with digits, with nn digits to the digits to the right of decimal point. right of decimal point.
Domain Types in SQLDomain Types in SQL real, double precision.real, double precision. Floating point and Floating point and
double-precision floating point numbers, with double-precision floating point numbers, with machine-dependent precision.machine-dependent precision.
float(n).float(n). Floating point number, with user- Floating point number, with user-specified precision of at least specified precision of at least nn digits. digits.
Null values are allowed in all the domain types. Null values are allowed in all the domain types. Declaring an attribute to be Declaring an attribute to be not nullnot null prohibits prohibits null values for that attribute.null values for that attribute.
create domaincreate domain construct in SQL-92 creates user- construct in SQL-92 creates user-defined domain typesdefined domain types
create domain create domain person-name person-name charchar(20) (20) not nullnot null
Date/Time Types in SQLDate/Time Types in SQL
date.date. Dates, containing a (4 digit) year, month and Dates, containing a (4 digit) year, month and datedate E.g. E.g. datedate ‘2001-7-27’ ‘2001-7-27’
time. time. Time of day, in hours, minutes and seconds. Time of day, in hours, minutes and seconds. E.g. E.g. time time ’09:00:30’ ’09:00:30’ time time ’09:00:30.75’ ’09:00:30.75’
timestamptimestamp: date plus time of day: date plus time of day E.g. E.g. timestamptimestamp ‘2001-7-27 09:00:30.75’ ‘2001-7-27 09:00:30.75’
IntervalInterval: period of time: period of time E.g. Interval ‘1’ dayE.g. Interval ‘1’ day Subtracting a date/time/timestamp value from another Subtracting a date/time/timestamp value from another
gives an interval valuegives an interval value Interval values can be added to date/time/timestamp valuesInterval values can be added to date/time/timestamp values
Create Table ConstructCreate Table Construct An SQL relation is defined using the An SQL relation is defined using the
create table create table command:command:create table create table r r ((AA11 DD11, , AA22 DD22, ..., , ..., AAnn D Dnn,,
(integrity-constraint(integrity-constraint11),),...,...,(integrity-constraint(integrity-constraintkk))))
rr is the name of the relation is the name of the relation each each AAii is an attribute name in the schema is an attribute name in the schema
of relation of relation rr DDii is the data type of values in the domain of is the data type of values in the domain of
attribute attribute AAii
Integrity Constraints in Create Integrity Constraints in Create TableTable
not nullnot null primary keyprimary key ( (AA11, ..., , ..., AAnn)) Foreign KeyForeign Key (A,…,An) (A,…,An) references references s(B1,…Bn)s(B1,…Bn) check check (P),(P), where where PP is a predicate is a predicate
Example: Declare branch-name as the primary key for branch and ensure that the values of assets are non-negative.
create table branch(branch-namechar(15),branch-city char(30)assets integer,primary key (branch-name),check (assets >= 0))
primary key declaration on an attribute automatically ensures not null in SQL-92 onwards, needs to be explicitly stated in SQL-89
Integrity Constraints in Create Integrity Constraints in Create TableTable
Foreign KeyForeign Key
Example: Declare branch-name in loan table referencing the branch- name for branch CREATE TABLE loan ( loan_number char(100), branch_name char(15) REFERENCES branch(branch_name), amount int, PRIMARY KEY(loan_number) );
Foreign key means: loan.branch_name branch.branch_name
Drop and Alter TableDrop and Alter Table The The drop table drop table command deletes all command deletes all
information about the dropped relation information about the dropped relation from the database.from the database.
The The alter tablealter table command is used to command is used to add attributes to an existing relation. add attributes to an existing relation.
alter table alter table r r add add A DA D
where where AA is the name of the attribute to is the name of the attribute to be added to relation be added to relation r r and and DD is the is the domain of domain of A.A. All tuples in the relation are assigned All tuples in the relation are assigned nullnull
as the value for the new attribute. as the value for the new attribute.
Drop and Alter TableDrop and Alter Table
The The alter tablealter table command can also be command can also be used to drop attributes of a relationused to drop attributes of a relation
alter table alter table rr drop drop A Awhere where AA is the name of an attribute of is the name of an attribute of relationrelation r r Dropping of attributes not supported by many Dropping of attributes not supported by many
databasesdatabases
DMLDML
Read or change the content of the Read or change the content of the databasedatabase
Basic Structure Basic Structure A typical SQL query has the form:A typical SQL query has the form:
select select AA11, , AA22, ..., , ..., AAnn
fromfrom rr11, , rr22, ..., , ..., rrmm
where where PP
The result of an SQL query is a relation.The result of an SQL query is a relation.
attributes
relations
predicates
The select ClauseThe select Clause E.g. find the names of all branches in the E.g. find the names of all branches in the
loanloan relation relationselect select branch_namebranch_namefrom from loan;loan;
NOTENOTE: SQL names are case insensitive, : SQL names are case insensitive, i.e. you can use capital or small letters. i.e. you can use capital or small letters.
The select ClauseThe select Clause
SQL allows duplicates in relations as well SQL allows duplicates in relations as well as in query results.as in query results.
To force the elimination of duplicates, To force the elimination of duplicates, insert the keyword insert the keyword distinct distinct after after select.select.
Find the names of all branches in the Find the names of all branches in the loanloan relations, and remove duplicatesrelations, and remove duplicates
select distinct select distinct branch_namebranch_namefrom from loan;loan;
The select ClauseThe select Clause
An asterisk in the select clause denotes An asterisk in the select clause denotes “all attributes”“all attributes”
select select **from from loanloan
The select ClauseThe select Clause
The The selectselect clause can contain arithmetic clause can contain arithmetic expressions involving the operation, +, –, expressions involving the operation, +, –, , and /, , and /, and operating on constants or attributes of and operating on constants or attributes of tuples.tuples.
The query: The query:
selectselect loan_number, branch_name, amount loan_number, branch_name, amount 100 100
from from loan;loan;
would return a relation which is the same as the would return a relation which is the same as the loan loan relations, except that the attribute relations, except that the attribute amount amount is is multiplied by 100.multiplied by 100.
The where ClauseThe where Clause
The The where where clause specifies conditions clause specifies conditions that the result must satisfythat the result must satisfy corresponds to the selection predicate of the corresponds to the selection predicate of the
relational algebra. relational algebra.
The where ClauseThe where Clause
To find all loan number for loans made at To find all loan number for loans made at the Perryridge branch with loan amounts the Perryridge branch with loan amounts greater than $1200.greater than $1200. select select loan_numberloan_number
from from loanloanwhere where branch_name = ‘branch_name = ‘PerryridgePerryridge’ ’ and and amount amount > 1200;> 1200;
Comparison results can be combined using Comparison results can be combined using the logical connectives the logical connectives and, or, and, or, and and not.not.
Comparisons can be applied to results of Comparisons can be applied to results of arithmetic expressions.arithmetic expressions.
The where ClauseThe where Clause
SQL includes a SQL includes a betweenbetween comparison operator comparison operator E.g. Find the loan number of those loans with E.g. Find the loan number of those loans with
loan amounts between $90,000 and $100,000 loan amounts between $90,000 and $100,000 (that is, (that is, $90,000 and $90,000 and $100,000)$100,000)
select loan_numberfrom loanwhere amount between 90000 and 100000;
The from ClauseThe from Clause
The The from from clause lists the relations involved clause lists the relations involved in the queryin the query corresponds to the Cartesian product operation corresponds to the Cartesian product operation
of the relational algebra.of the relational algebra. Find the Cartesian product Find the Cartesian product borrower x loanborrower x loan
select select from from borrower, loan;borrower, loan;
Find the name, loan number and loan amount of all customers having a loan at the Perryridge branch.
select customer_name, borrower.loan_number, amountfrom borrower, loanwhere borrower.loan_number = loan.loan_number and branch_name = ‘Perryridge’;
The Rename OperationThe Rename Operation
The SQL allows renaming relations and The SQL allows renaming relations and attributes using the attributes using the as as clause:clause:
old-name old-name asas new-name new-name Find the name, loan number and loan amount Find the name, loan number and loan amount
of all customers; rename the column name of all customers; rename the column name loan-number loan-number as as loan-id.loan-id.
select customer_name, borrower.loan_number as loan_id, amountfrom borrower, loanwhere borrower.loan_number = loan.loan_number;
Tuple VariablesTuple Variables
Tuple variables are defined in the Tuple variables are defined in the fromfrom clause via the use clause via the use of the of the as as clause.clause.
Find the customer names and their loan numbers for all 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.amountfrom borrower as T, loan as Swhere T.loan_number = S.loan_number;
select distinct T.branch_name from branch as T, branch as S where T.assets > S.assets and S.branch_city = ‘Brooklyn;’
Find the names of all branches that have greater assets than some branch located in Brooklyn.
String OperationsString Operations SQL includes a string-matching operator for SQL includes a string-matching operator for
comparisons on character strings. Patterns are comparisons on character strings. Patterns are described using two special characters:described using two special characters: percent (%). The % character matches any substring.percent (%). The % character matches any substring. underscore (_). The _ character matches any underscore (_). The _ character matches any
character.character. Find the names of all customers whose street Find the names of all customers whose street
includes the substring “Main”.includes the substring “Main”.
select select customer-namecustomer-namefrom from customercustomerwherewhere customer-street customer-street like like
‘‘%Main%%Main%’’ Match the name “Main%”Match the name “Main%”
likelike ‘‘Main\%Main\%’’ escape escape ‘‘\\’’
Ordering the Display of Ordering the Display of TuplesTuples
List in alphabetic order the names of all customers List in alphabetic order the names of all customers having a loan in Perryridge branchhaving a loan in Perryridge branchselect distinct select distinct customer-namecustomer-namefrom from borrower, loanborrower, loanwhere where borrower loan-number = loan.loan-number borrower loan-number = loan.loan-number
andand branch-name = branch-name = ‘‘PerryridgePerryridge’’ order by order by customer-namecustomer-name
We may specify We may specify descdesc for descending order or for descending order or ascasc for for ascending order, for each attribute; ascending order is ascending order, for each attribute; ascending order is the default.the default. E.g. E.g. order byorder by customer-namecustomer-name descdesc
Set OperationsSet Operations
The set operations The set operations union, intersect, union, intersect, and and except except operate on relations and correspond operate on relations and correspond to the relational algebra operations to the relational algebra operations
Each of the above operations automatically Each of the above operations automatically eliminates duplicates; to retain all duplicates eliminates duplicates; to retain all duplicates use the corresponding multiset versions use the corresponding multiset versions union all, intersect all union all, intersect all and and except all.except all.
Set OperationsSet Operations
Suppose a tuple occurs Suppose a tuple occurs mm times in times in rr and and n n times in times in s, s, then, it occurs:then, it occurs: m m + n + n times in times in r r union all union all ss min(min(m,n)m,n) times in times in rr intersect all intersect all ss max(0, max(0, m – n)m – n) times in times in rr except all except all ss
Set OperationsSet Operations
(select customer-name from depositor)except(select customer-name from borrower)
(select customer_name from depositor)intersect(select customer_name from borrower);
Find all customers who have an account but no loan.
(select customer-name from depositor)union(select customer-name from borrower)
Find all customers who have both a loan and an account.
Find all customers who have a loan, an account, or both:Find all customers who have a loan, an account, or both:
Aggregate FunctionsAggregate Functions
These functions operate on the multiset of These functions operate on the multiset of values of a column of a relation, and values of a column of a relation, and return a valuereturn a value
avg: avg: average valueaverage valuemin: min: minimum valueminimum valuemax: max: maximum valuemaximum valuesum: sum: sum of valuessum of valuescount: count: number of valuesnumber of values
Aggregate Functions Aggregate Functions (Cont.)(Cont.)
Find the number of depositors in the bank.
Find the number of tuples in the customer relation.
select avg (balance)from accountwhere branch-name = ‘Perryridge’
select count (*)from customer
select count (distinct customer-name)from depositor
Find the average account balance at the Perryridge Find the average account balance at the Perryridge branch.branch.
Aggregate Functions – Aggregate Functions – Group ByGroup By
Find the number of depositors for each branch.Find the number of depositors for each branch.
Note: Attributes in select clause outside of aggregate functions must appear in group by list
select branch-name, count (distinct customer-name)from depositor, accountwhere depositor.account-number = account.account-numbergroup by branch-name
Aggregate Functions – Aggregate Functions – Having ClauseHaving Clause
Find the names of all branches where the average account Find the names of all branches where the average account balance is more than $1,200.balance is more than $1,200.
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 branch-name, avg (balance)from accountgroup by branch-namehaving avg (balance) > 1200
Null ValuesNull Values The predicate The predicate is nullis null can be used to can be used to
check for null values.check for null values. E.g. Find all loan number which appear in the E.g. Find all loan number which appear in the
loanloan relation with null values for relation with null values for amount.amount.
selectselect loan-number loan-numberfromfrom loan loanwhere where amount amount is nullis null
The result of any arithmetic expression The result of any arithmetic expression involving involving nullnull is is nullnull E.g. 5 + null returns nullE.g. 5 + null returns null
However, aggregate functions simply However, aggregate functions simply ignore nullsignore nulls
Null ValuesNull Values
Any comparison with Any comparison with nullnull returns returns unknownunknown E.g. 5 < null or null <> null or null = E.g. 5 < null or null <> null or null =
nullnull Result of Result of where where clause predicate is clause predicate is
treated as treated as false false if it evaluates to if it evaluates to unknownunknown
Three Valued LogicThree Valued Logic
Three-valued logic using the truth value Three-valued logic using the truth value unknownunknown:: OR: (OR: (unknownunknown oror truetrue) = ) = truetrue, (, (unknownunknown oror
falsefalse) = ) = unknownunknown ( (unknown unknown oror unknown) = unknown unknown) = unknown
AND:AND: (true (true and and unknown) = unknown, (falseunknown) = unknown, (false and and unknown) = false,unknown) = false, (unknown (unknown andand unknown) = unknown unknown) = unknown
NOTNOT: (: (notnot unknown) = unknown unknown) = unknown ““PP is unknown” is unknown” evaluates to true if predicate evaluates to true if predicate
PP evaluates to evaluates to unknownunknown
Null Values and AggregatesNull Values and Aggregates
Total all loan amountsTotal all loan amounts
select sumselect sum ( (amount)amount)fromfrom loan loan
Above statement ignores null amountsAbove statement ignores null amounts result is null if there is no non-null amountresult is null if there is no non-null amount All aggregate operations except All aggregate operations except count(*)count(*)
ignore tuples with null values on the ignore tuples with null values on the aggregated attributes.aggregated attributes.
Nested SubqueriesNested Subqueries
SQL provides a mechanism for the nesting SQL provides a mechanism for the nesting of subqueries.of subqueries.
A subquery is a A subquery is a select-from-whereselect-from-where expression that is nested within another expression that is nested within another query.query.
A common use of subqueries is to perform A common use of subqueries is to perform tests for set membership, set tests for set membership, set comparisons, and set cardinality.comparisons, and set cardinality.
Example QueryExample Query
Find all customers who have a loan at the bank but do not have
an account at the bankselect distinct customer-name
from borrowerwhere customer-name not in (select customer-name
from depositor)
select distinct customer-namefrom borrowerwhere customer-name in (select customer-name
from depositor)
Find all customers who have both an account and a Find all customers who have both an account and a loan at the bank.loan at the bank.
Example QueryExample Query Find all customers who have both an account and a loan at Find all customers who have both an account and a loan at
the Perryridge branchthe Perryridge branch
Note: Above query can be written in a much simpler manner. The formulation above is simply to illustrate SQL features.
select distinct customer-namefrom borrower, loanwhere borrower.loan-number = loan.loan-number and
branch-name = “Perryridge” and (branch-name, customer-name) in
(select branch-name, customer-namefrom depositor, accountwhere depositor.account-number =
account.account-number)
Set ComparisonSet Comparison
Same query using > some clause
select branch-namefrom branchwhere assets > some (select assets from branch
where branch-city = ‘Brooklyn’)
select distinct T.branch-namefrom branch as T, branch as Swhere T.assets > S.assets and S.branch-city = ‘Brooklyn’
Find all branches that have greater assets than some Find all branches that have greater assets than some branch located in Brooklyn.branch located in Brooklyn.
Definition of Some ClauseDefinition of Some Clause F <comp> F <comp> some some r r t t rr s.t. ( s.t. (F <comp> F <comp> tt)) Where Where
<comp> can be: <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
Definition of all ClauseDefinition of all Clause F <comp> F <comp> all all r r t t rr (F <comp> (F <comp> t)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
Example QueryExample Query
Find the names of all branches that have greater Find the names of all branches that have greater assets than all branches located in Brooklyn.assets than all branches located in Brooklyn.
select branch-namefrom branchwhere assets > all
(select assetsfrom branchwhere branch-city = ‘Brooklyn’)
Test for Empty RelationsTest for Empty Relations
The The existsexists construct returns the value construct returns the value truetrue if the argument subquery is if the argument subquery is nonempty.nonempty.
exists exists r r r r ØØ not exists not exists r r r r = = ØØ
Example QueryExample Query Find all customers who have an account at all Find all customers who have an account at all
branches located in Brooklyn.branches located in Brooklyn.
select distinct S.customer-namefrom depositor as Swhere not exists (
(select branch-namefrom branchwhere branch-city = ‘Brooklyn’)
except(select R.branch-namefrom depositor as T, account as Rwhere T.account-number = R.account-number and
S.customer-name = T.customer-name))
(Note that X – Y = Ø X Y
Note: Cannot write this query using = all and its variants
Test for Absence of Test for Absence of Duplicate TuplesDuplicate Tuples
Find all customers who have at most one Find all customers who have at most one account at the Perryridge branch.account at the Perryridge branch.
select select T.customer-nameT.customer-name from from depositor depositor as as TT where unique where unique ((
select select R.customer-nameR.customer-name fromfrom account, depositor account, depositor as as RR wherewhere T.customer-name = R.customer- T.customer-name = R.customer-
name name
and and R.account-number = account.account-R.account-number = account.account-number number
and and account.branch-name = ‘account.branch-name = ‘Perryridge’)Perryridge’)