Transcript Chapter-03

The Relational Model
Chapter 3
Why Study the Relational Model?
 Most widely used model.

Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
 “Legacy systems” in older models

E.G., IBM’s IMS
 Recent competitor: object-oriented model


ObjectStore, Versant, Ontos
A synthesis emerging: object-relational model
 Informix Universal Server, UniSQL, O2, Oracle, DB2
Relational Model
 The Relational Model is very simple and elegant: a database is
a collection of one or more relations, where each relation is a
table with rows and columns.
 The major advantages of the relational model over the older
data models are its simple data representation and the ease
with which even complex queries can be expressed.
Relational Model
 The DDL(Data Definition Language) features of SQL , the
standard language for creating, manipulating, and querying
data in a relational DBMS.
 An important component of a data model is the set of
constructs it provides for specifying conditions that must be
satisfied by the data.
 Such conditions called integrity constraints(ICs) enable
the DBMS to reject operations that might corrupt the data.
Relational Database
 Relational database: a set of relations
 Relation: made up of 2 parts:


Instance : a table, with rows and columns.
#Rows = cardinality, #fields = degree / arity.
Schema : specifies name of relation, plus name and type of each
column.
 E.G.
Students(sid: string, name: string, login: string, age: integer, gpa: real).
Relational Database
 A domain is referred to in a relation schema by the domain
name and has a set of associated values.
 In above example the field name sid has a domain named
string.
 The set of values associated with domain string is the set of
all character strings.
 An instance of a relation is a set of tuples , also called records
in which each tuple has the same number of fields as the
relation schema.
Relational Database
 The requirement of relational model is each relation is
defined to be a set of unique tuples or rows.
 A relation schema specifies the domain of each field or
column in the relation instance.
 These domain constraints in the schema specify an important
condition that we want each instance of the relation to satisfy.
 The values that appear in a column must be drawn from the
domain associated with that column.
Relational Database
 More formally let R(f1:D1….Fn:Dn) be a relation schema
and for each Fi , 1<=i<=n,
let Dom(i) be the set of values associated with the domain
named Di.
Domain constraints are so fundamental in the relational
model that we henceforth consider only relation instances
that satisfy them; therefore , relation instance means relation
instance that satisfies the domain constraints in the relation
schema.
Relational Database
 The degree also called arity, of a relation is the number of
fields.
 The cardinality of a relation instance is the number of tuples
in it.
sid
name
login
53666 Jones jones@cs
53688 Smith smith@eecs
53650 Smith smith@math
age
18
18
19
gpa
3.4
3.2
3.8
vCardinality = 3, degree = 5, all rows distinct
Relational Database
 A relational database is a collection of relations with distinct
relation names.
 The relational database schema is the collection of schemas
for the relations in the database.
 An instance of a relational database is a collection of relation
instances, one per relation schema in the database schema; of
course each relation instance must satisfy the domain
constraints in its schema.
Relational Query Languages
 A major strength of the relational model: supports simple,
powerful querying of data.
 Queries can be written intuitively, and the DBMS is
responsible for efficient evaluation.


The key: precise semantics for relational queries.
Allows the optimizer to extensively re-order operations, and
still ensure that the answer does not change.
The SQL Query Language
 To find all 18 year old students, we can write:
SELECT *
FROM Students S
WHERE S.age=18
sid
name
53666 Jones
login
jones@cs
age gpa
18
3.4
53688 Smith smith@ee 18
3.2
•To find just names and logins, replace the first line:
SELECT S.name, S.login
Querying Multiple Relations
 What does the following query compute?
SELECT S.name, E.cid
FROM Students S, Enrolled E
WHERE S.sid=E.sid AND E.grade=“A”
Given the following instance
of Enrolled (is this possible if
the DBMS ensures referential
integrity?):
we get:
sid
53831
53831
53650
53666
cid
grade
Carnatic101
C
Reggae203
B
Topology112
A
History105
B
S.name E.cid
Smith
Topology112
Creating Relations in SQL
 Creates
the Students relation.
Observe that the type (domain) of
each field is specified, and enforced
by the DBMS whenever tuples
are added or modified.
 As another example, the Enrolled
table holds information about
courses
that students take.
CREATE TABLE Students
(sid CHAR(20),
name CHAR(20),
login CHAR(10),
age INTEGER,
gpa REAL)
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2))
Destroying and Altering Relations
DROP TABLE Students
 Destroys the relation Students. The schema information and
the tuples are deleted.
ALTER TABLE Students
ADD COLUMN firstYear: integer
v
The schema of Students is altered by adding a
new field; every tuple in the current instance
is extended with a null value in the new field.
Adding and Deleting Tuples
 Can insert a single tuple using:
INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
v
Can delete all tuples satisfying some
condition (e.g., name = Smith):
DELETE
FROM Students S
WHERE S.name = ‘Smith’
* Powerful variants of these commands are available; more later!
Relational Model: Summary
 A tabular representation of data.
 Simple and intuitive, currently the most widely used.
 Integrity constraints can be specified by the DBA, based on
application semantics. DBMS checks for violations.


Two important ICs: primary and foreign keys
In addition, we always have domain constraints.
 Powerful and natural query languages exist.
 Rules to translate ER to relational model