Beruflich Dokumente
Kultur Dokumente
SQL Query
Exercise
CIT-4307
Database Management Systems
Abdullah Al Hasib
Department of Computer Science and Information Technology (CIT)
Islamic University of Technology (IUT)
Gazipur - 1704, Bangladesh
Email: hasib@iut-dhaka.edu
Abdullah Al Hasib
1 / 49
Constraints
SQL Query
Exercise
Outline
1
Constraints
Not Null
Primary Key
Unique
Foreign Key
Check
SQL Query
Aggregate functions
String operations
Set comparison
Exercise
SQL Constraints
SQL Query
Abdullah Al Hasib
2 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Outline
Constraints
SQL Query
Exercise
Abdullah Al Hasib
3 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Integrity Constraint
Abdullah Al Hasib
4 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
5 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
6 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
6 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
6 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
6 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
7 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
7 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
8 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
8 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Abdullah Al Hasib
9 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Unique Constraint
Unique specifications are verified in the same way as primary
key(also accept NULL values)
- [Constraint <constraint name>] Unique
- [Constraint <constraint name>] Unique (<column name>)
Create Table Movies (
mID int,
title varchar2(20),
director varchar2(10),
year int default 0,
Constraint movies mid pk Primary key (mID),
Constraint movies tdir un Unique (title, director));
Abdullah Al Hasib
10 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
11 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
12 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
14 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
Check Constraints
The most generic constraint type
It allows you to specify that the value of an attribute must
satisfy some condition
- [Constraint <constraint name>]
Check (<column name> <condition>)
15 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
16 / 49
Constraints
SQL Query
Exercise
Not Null
Primary Key
Unique
Foreign Key
Check
To enable/disable a Constraint
To enable a constraint
- Alter Table <table name>
Enable Constraint <constraint name>
To disable a constraint
- Alter Table <table name>
Disable Constraint <constraint name>
Abdullah Al Hasib
17 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Outline
Constraints
SQL Query
Exercise
Abdullah Al Hasib
18 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
19 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
20 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
title
Shining
Player
Chinatown
Repulsion
21 / 49
director
Kubrick
Altman
Polanski
Polanski
year
1980
1982
1974
1965
length
146
146
131
143
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
title
Shining
Player
Chinatown
Repulsion
22 / 49
director
Kubrick
Altman
Polanski
Polanski
noOfYears
30
28
36
34
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
23 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
23 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Join queries
Query: Find actors playing in movies directed by Lucas:
Abdullah Al Hasib
24 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Join queries
Query: Find actors playing in movies directed by Lucas:
Select A.aName
From Artists A, Roles R, Movies M
Where M.director = Lucas
Abdullah Al Hasib
24 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Join queries
Query: Find actors playing in movies directed by Lucas:
Select A.aName
From Artists A, Roles R, Movies M
Where M.director = Lucas And
R.mID = M.mID
Abdullah Al Hasib
24 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Join queries
Query: Find actors playing in movies directed by Lucas:
Select A.aName
From Artists A, Roles R, Movies M
Where M.director = Lucas And
R.mID = M.mID And
A.aID = R.aID
Evaluation strategy
[-] From clause produces Cartesian product of listed relations;
[-] Where clause:
Selection condition M.director = Lucas
eliminates irrelevant tuples;
Join conditions R.mID = M.mID And A.aID = R.aID
relates facts to each other;
[-] Select clause retains only the listed attributes.
Abdullah Al Hasib
24 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Nested subqueries
SQL provides a mechanism for the nesting of subqueries.
A subquery is a Select-From-Where expression that is
nested within another query.
In general, a Where clause could contain another query,
and test some relationship between an attribute and the
result of that query.
A common use of subqueries is to perform tests for
Set-valued subqueries
- <expression> [Not] In ( subquery )
- <expression> <comparison operator> In ( subquery )
Test for non-existence
- [Not] Exists ( subquery )
Abdullah Al Hasib
25 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
26 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Evaluation strategy:
The subquery is evaluated once to produce the
set of aIDs of actors who played in movies of
Lucas
Abdullah Al Hasib
27 / 49
aID
2
4
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Evaluation strategy:
The subquery is evaluated once to produce the
set of aIDs of actors who played in movies of
Lucas
Each tuple (as A) is tested against this set
Abdullah Al Hasib
27 / 49
aID
2
4
aName
Harrison Ford
Carrie Fisher
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Aggregate functions
These functions operate on the values of a column of a relation,
and return a value.
Avg: average value
Min: minimum value
Max: maximum value
Sum: sum of the values
Count: number of values
Stddev: standard deviation of the values
Var: variance of the values
Abdullah Al Hasib
28 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
title
Shining
Player
Chinatown
Repulsion
Star Wars IV
American Graffiti
Full Metal Jacket
director
Kubrick
Altman
Polanski
Polanski
Lucas
Lucas
Kubrick
year
1980
1982
1974
1965
1977
1973
1987
Abdullah Al Hasib
29 / 49
length
146
146
131
143
126
110
156
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
title
Shining
Player
Chinatown
Repulsion
Star Wars IV
American Graffiti
Full Metal Jacket
director
Kubrick
Altman
Polanski
Polanski
Lucas
Lucas
Kubrick
year
1980
1982
1974
1965
1977
1973
1987
Abdullah Al Hasib
29 / 49
length
146
146
131
143
126
110
156
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
title
Shining
Player
Chinatown
Repulsion
Star Wars IV
American Graffiti
Full Metal Jacket
director
Kubrick
Altman
Polanski
Polanski
Lucas
Lucas
Kubrick
year
1980
1982
1974
1965
1977
1973
1987
noTuples
7
29 / 49
length
146
146
131
143
126
110
156
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
30 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Result:
noDirectors
4
Abdullah Al Hasib
30 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
30 / 49
Result:
noDirectors
4
Result:
Avgl
118
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
title
director
year
length
Player
Shining
Full Metal Jacket
Chinatown
Repulsion
Star Wars IV
American Graffiti
Altman
Kubrick
Kubrick
Polanski
Polanski
Lucas
Lucas
1982
1980
1987
1974
1965
1977
1973
146
146
156
131
143
126
110
Abdullah Al Hasib
31 / 49
146
151
137
118
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
32 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
33 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
34 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
String operations
SQL includes a string-matching operator for comparisons on
character strings e.g Like operator)
Syntax: match expression [Not] Like pattern
Patterns are described using two special characters:
Wildcard
underscore ( )
percent (%)
Abdullah Al Hasib
Description
Any single character
Any string of zero or
more characters
35 / 49
Example
a b matches cacbc, aabba etc
%a%b matches ab, ccaccbc,
aaaabcbcbbd, aba, etc
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
36 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
UPPER(x)
Abdullah Al Hasib
Description
Appends y to x
Converts the initial letter of each word in x to
uppercase
Returns the number of characters in x
Converts the letters in x to lowercase
Searches x for str1 and replaces it with str2
Returns a substring of x that begins at the
position specified by start. You can supply
an optional length for the substring
Converts the letters in x to uppercase
37 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
38 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Set comparison
< value >< condition > ALL(< query >)
Is true if either
< Query > evaluates the empty set
for every < value1 > in the result of < query >,
< value >< condition >< value1 > is true.
39 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
40 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
41 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Abdullah Al Hasib
42 / 49
Constraints
SQL Query
Exercise
Aggregate functions
String operations
Set comparison
Set operations
Abdullah Al Hasib
43 / 49
Constraints
SQL Query
Exercise
SQL Constraints
SQL Query
Outline
Constraints
SQL Query
Exercise
Abdullah Al Hasib
44 / 49
Constraints
SQL Query
Exercise
SQL Constraints
SQL Query
Exercise
Create a client master table as described below
Column Name
client no
name
address
city
pincode
state
bal due
Data Type
char
varchar2
varchar2
varchar2
float
varchar2
number
Size
6
10
10
10
15
10,2
Attributes
Primary Key
Not Null, *
*
Cannot be negative
45 / 49
Constraints
SQL Query
Exercise
SQL Constraints
SQL Query
Exercise
Create a order details table as described below
Column Name
order no
order date
client no
Data Type
char
date
char
delivery date
date
Abdullah Al Hasib
46 / 49
Size
6
6
Attributes
Primary Key
Foreign Key references
client no of client master table;
if a row in master table is deleted
corresponding row in child is also
deleted
Cant be less than order date
Constraints
SQL Query
Exercise
SQL Constraints
SQL Query
Abdullah Al Hasib
47 / 49
Constraints
SQL Query
Exercise
SQL Constraints
SQL Query
Abdullah Al Hasib
48 / 49
Constraints
SQL Query
Exercise
SQL Constraints
SQL Query
10
Abdullah Al Hasib