Professional Documents
Culture Documents
Overview
The Model
Two papers
Properties of a Relation
Notion of Keys
Structural part
relations, domains, etc.
Integrity part
entity, referential, domain/user defined
Manipulative part
operators & extensions
Structural Features
Relations
Mathematical entity to hold data in the relational model.
A set of n-tuple, perceived as a two dimensional table
where an intersection of a row and a column is a atomic value.
Base tables
A named and autonomous relation, one that actually holds
data.
Query tables
Relations that result from execution of queries, not named and
do not have persistent existance.
View tables
A named, virtual and derived relation, that is defined in terms
of other named relations.
Snapshot tables
A named, derived and real relation, represented in terms of
other relations and also by it’s own materialized data.
Attributes
Correspond to columns of the table, all attribute values are of
the same type and atomic in nature.
Domains
All possible values from which attribute values are chosen.
Primary key
A set of attributes, whose value is a minimal unique identifier
to a row of the table. A designated candidate key.
Foreign keys
A set of attributes, whose value is the the primary key of
another table
Integrity Features
Entity Integrity
No component of primary key of base relation is allowed
to be NULLS
Referential Integrity
The database must not contain any unmatched foreign key
values
Manipulative Features
•Restrict
•Project
•Cross product
•Union, Intersection, Difference
•Join
•Divide
•Extensions
Twelve Rules
Information Rule
All information be represented in one and only one way,
i.e values in column positions within rows of tables.
Integrity Independence
Integrity constraints specified seperately from application
programs, they are stored in catalog, it is possible to change
integrity constraints without affecting existing code.
Distributive Independence
Existing applications should operate sucessfully when
distributed version of DBMS is first introduced and when
existing data is redistributed around the system.
History
1970 - Dr.Codd proposed Relational model
1971-79 - SEQUEL implemented in System R
1980 - SEQUEL became SQL
1986 - SQL 86 (ANSI standard)
1989 - Follow on to SQL-86 - SQL-89
1992 - SQL-2
1995 - SQL-3
SQL components
Create Table S
( S# Char(5) Not Null,
SNAME Char(20) Not Null
With Default,
STATUS Smallint Not Null
With Default,
CITY Char(15) Not Null
With Default,
Primary Key (S#)
);
Table Modification
Alter Table S
Add DISCOUNT SmallInt ;
Table Deletion
Drop Table S ;
Index
Index Creation
Index Deletion
Drop Index X ;
1. Supplier S (S#,SNAME,CITY,STATUS)
2. Part P (P#,PNAME,COLOR,WEIGHT)
3. Supp-Part SP (S#,P#)
Simple retrieval
Select PNAME
From P
Select Distinct P#
From SP
Select P#,’Height’,HEIGHT*250
From P
Select *
From S
Qualified Retrieval
Select S#
From S
Where CITY = ‘Bombay’ and
STATUS > 20
Select S#,STATUS
From S
Where CITY = ‘Bombay’
Order By STATUS desc
Select P#,’Height’,’HEIGHT*250
From P
Order By 3,P#
Retrieval using Range of values
Normal way
Select P#,PNAME
From P
Where WEIGHT >= 16 and
WEIGHT <= 19
Using BETWEEN
Select P#,PNAME
From P
Where WEIGHT Between 16 and 19
Similarly
Normal way
Select P#,PNAME
From P
Where WEIGHT = 12 or
WEIGHT = 16 or
WEIGHT = 17
Using IN
Select P#,PNAME
From P
Where WEIGHT IN (12,16,17)
Similarly
Select S#
From S
Where STATUS Is Null
Similarly
Select S.*,P.*
From S,P
Equi Join
Select S.*,P.*
From S,P
Where S.CITY = P.CITY
Theta Join
Select S.*,P.*
From P,SP
Where P.P# = SP.P# And
P.RATE < SP.RATE
Natural Join
Select S.*,SP.*
From S,SP
Where S.S# = SP.S#
Select S.*,SP.*,P.*
From S,SP,P
Where S.S# = SP.S# And
P.P# = SP.P#
Select First.E#,First.ENAME,First.M#,
Second.ENAME
From E First, E Second
Where First.M# = Second.E#
Simple Subquery
Select SNAME
From S
Where S# In (‘S1’,’S2’,’S3’,’S4’)
Select S#
From SP
Where P# = ‘P2’
Select SNAME
From S
Where S# In ( Select S#
From SP
Where P# = ‘P2’)
Select SNAME
From S
Where S# In (List of suppliers supplying red part)
Select SNAME
From S
Where S# In ( Select S#
From SP
Where P# In (List of red parts))
Seleet SNAME
From S
Where S# In ( Select S#
From SP
Where P# In ( Select P#
From P
Where COLOR =
‘RED’))
Subquery & Outer query referring to same table
Select Distinct S#
From SP
Where P# In (List of parts supplied by supplier S2)
Select Distinct S#
From SP
Where P# In ( Select P#
From SP
Where S# = ‘S2’)
Select S#
From S
Where CITY = (City of supplier S2)
Select S#
From S
Where CITY = ( Select CITY
From S
Where S# = ‘S1’)
Query using EXISTS
Select SNAME
From S
Where Exists (
List of suppliers where S# is the same as that
of the outer query and P# is ‘P2’)
Select SNAME
From S
Where Exists ( Select *
From SP
Where S# = S.S# And
P# = ‘P2’)
Select SNAME
From S
Where Not Exists ( Select *
From SP
Where S# = S.S# And
P# = ‘P2’)
Quantified comparisons
Select HEIGHT
From P
Where COLOR = ‘Blue’
Select PNAME
From P
Where HEIGHT > ALL
(List of heights of blue parts)
Select PNAME
From P
Where HEIGHT > ALL
( Select HEIGHT
From P
Where COLOR = ‘Blue’)
Similarly
Examples
Select Count(*)
From S
Select Count(*)
From SP
Where P# = ‘P2’
Select Sum(QTY)
From SP
Where P# = ‘P2’
Select Sum(QTY)/Count(*)
From SP
Where P# = ‘P3’
Or
Select Avg(QTY)
From SP
Where P# = ‘P3’
Aggregate functions in subquery
Select S#
From S
Where STATUS < (Maximum status)
Select S#
From S
Where STATUS < ( Select Max(STATUS)
From S)
Select S#,STATUS,CITY
From S
Where STATUS >= (Average of their city)
Select S#,STATUS,CITY
From S SX
Where STATUS >= ( Select Avg(STATUS)
From S SY
Where SY.CITY = SX.CITY
)
Group By, Having
Group By statement
•rearranges rows into groups
•on the basis of Group By attributes
•such that each group has same value for Group By attributes
•expressions in Select should be single valued for the group
•such as Aggregate functions or Group By attributes
Example
Select P#,Avg(RATE)
From SP
Group By P#
Use of Where and Group By
Select P#,Sum(QTY),Max(QTY)
From SP
Where S# <> ‘S1’
Group By P#
Select P#,Avg(RATE)
From SP
Where TYPE <> ‘B’
Group BY P#
Having
The Having clause restricts output of rows that result from the
Group By clause, the restriction is based on a condition that
usually includes an aggregate function.
Get part numbers for all such parts that are supplied
by more than one supplier
Select P#
From SP
Group By P#
The results of this query are all parts that are supplied,
we need to choose from this list only those that are
supplied by more than one supplier, this information
is available as an aggregate function i.e.Count(*)
Select P#
From SP
Group By P#
Having Count(*) > 1
Union
Select P#
From P
Where WEIGHT > 16
Union
Select P#
From SP
Where S# = ‘S2’
Note:
Union eliminates redundant duplicates
Union All retains duplicates
Union
Select P#,’Weight’
From P
Where WEIGHT > 16
Union All
Select P#,’Supplied’
From SP
Where S# = ‘S2’
Order By 2,1
Intersection
Difference
Schema
SAILORS (sid, sname, rating)
RES (sid, bid, date)
BOATS (bid, bname, color)
9. Find names of sailors who have reserved red and green boats
10. Find names of sailors who have reserved boats reserved by Ravi
SQL Exercises
Schema
CUSTOMER (cno, cname, city, status, category)
PRODUCT (pno, pname, type)
SALES (cno, pno, date, qty, rate)
eg:
Singleton Select
If RANKIND = -1 Then
...<RANK has NULL value>
End-If
SQL examples - Not involving Cursors
INSERT
COLORIND = -1
CITYIND = -1
UPDATE
RANKIND = -1
DELETE
The FETCH ... INTO statement is used to retrieve rows from the
result table, one row at a time. The INTO clause specifies a list
of host variables that match the SELECT clause declaration of
the cursor.
Since there are multiple rows in the result table, Fetch is placed in
a loop. A Fetch would result in SQLCODE as +100 if no more
rows exist in the result table, this condition is used to terminate
the loop. The cursor is finally closed by CLOSE statement.
Cursor Declaration
Notes:
Since base tables are supposed to reflect reality, all Integrity rules
apply to base tables. Integrity rules are specific and general.
General Integrity rules are those that concern primary key and
foreign key. Specific Integrity rules are those concerning
domain & user defined integrity.
Notion of Primary key
Primary key
•is a unique identifier for a relation
•can be composite
•is a designated candidate key
•no component can be eliminated without destroying uniqueness
Notes
•Every relation has a primary key, moreso the base relations
•Reason for choosing primary key is outside the scope of model
•Important for primary key to be really significant
•There need not be an index on primary key
•Primary key is pre-requisite to foreign key support
•Provides tuple level addressing mechanism in relational system
Entity Integrity
Justification
Null value for primary key implies that entity in database has
no full or partial identity. Primary key is supposed to perform a
unique identification function.
Rule
In a relational model, we never record information about
something we cannot identify.
Foreign keys
The problem ensuring that the database does not include any
invalid foreign key values is known as Referential Integrity
problem.
There should exist a base relation with primary key such that
each NON NULL foreign key value has a corresponding primary
key value.
Notes
The database must not contain any unmatched foreign key values
Note
Create Table SP
( S# Char(5) Not Null,
P# Char(6) Not Null,
QTY Integer,
Primary Key (S#, P#)
);
Create Table SP
( S# Char(5) Not Null,
P# Char(6) Not Null,
QTY Integer,
Primary Key (S#, P#),
Foreign Key SKF (S#) References S
On Delete Cascade,
Foreign Key PKF (P#) References P
On Delete Restrict
);
Views
Introduction
An example:
Examples
The above views are created such that they are vertical or
column subsets of base tables, hence they are called
column-subset views.
View SCITY includes the primary key of the base table, whereas
view STATUS_CITY does not.
Views that include the primary key of the base tables are known
as Key preserving views. Column subset views are theoretically
updatable if they preserve the primary key of the base table.
Types of Views
Join Views
Statistical Summary
There are some views that are theoretically updatable but are
not updatable in SQL systems.
Check Option
Growth
Restructuring
An Example
A query that can use an index has more access path options,
so it has the capability of being a more efficient query than
the query that cannot use an index. DB2 optimizer can use
an index or indexes in a variety of ways to speed the retrieval
of data from DB2 tables.