Sie sind auf Seite 1von 26

Database Management Systems 1

COURSE 2
The Relational Model
Database Management Systems 1 2
Data Models
Hierarchical Model (1965)
Network Model (1965)
Relational Model (1NF) (1970s)
Nested Relational Model (1970s)
Complex Object (1980s)
Object Model (1980)
Object Relational Model (1990s)
New Trend: XML (DTD), XML Schema (1990s)
Database Management Systems 1 3
Hierarchical Model
a collection of records which are connected to one another
through links.
each record has
one parent and
many children
customers
accounts
Database Management Systems 1 4
Network Model
each record may have many parents and
many children





database = set of pairs of sets of records
Database Management Systems 1 5
Relational model - Ideas
Use a simple data structure: the Table
simple to understand
useful data structure (capture many situations)
leads to useful not too complex query lang.
Use mathematics to describe and represent
records and collections of records: the Relation
can be understood formally
leads to formal query languages
properties can be explained and proven

Database Management Systems 1 6
Relation
A domain is a set of scalar values (i.e. limited to
atomic types - integer, string(24), boolean, date,
blobs)
A relation or relation schema R is a list of
attribute names (fields) [A
1
, A
2
, , A
n
].
R also called the name of the relation
Corresponding to each attribute name A
i
is a
domain D
i
called the domain of A
i
, we write D
i
=
Dom(A
i
).
Database Management Systems 1 7
Relation (cont)
The number of attributes in a relation scheme is
called the degree or arity of the relation
A relation instance [R] is a subset of the
Cartesian product of the domains of the attributes
in the schema
An element of the relation instance, a record, is
called a tuple. All tuples of a relation are distinct
The number of rows of a relation is the
cardinality of the relation

Database Management Systems 1 8
Relation Example
Students(sid:integer; name:string;email:string; age:integer; gr:integer)









Cardinality = 4, degree = 5, all rows distinct

field name
field type
(domain)
sid name email age gr
2833 Jones jones@scs.ubbcluj.ro 19 231
2877 Smith smith@scs.ubbcluj.ro 20 232
2976 Jones jones@math.ubbcluj.ro 21 233
2765 Mary mary@math.ubbcluj.ro 22 233
relation
schema
relation
instance
relation
instance
relation
tuple
Database Management Systems 1 9
Warnings

Very often we will confuse
the relation, its schema, and its instance
the instance and the table
the attribute, the field and the column
the tuple and the row

Ask for precision if there is ambiguity!

Database Management Systems 1 10
Relational Database
A database is a set of relations

A database schema is the set of schemas of
the relations in the database

A database instance is the set of instances
of the relations in the database
Database Management Systems 1 11
Graphical Representation of Relations
Students(sid:string, name:string, email:string, age:integer, gr:integer)
Courses(cid: string, cname: string, credits:integer)
Enrolled(sid:string, cid:string, grade:double)
Teachers(tid:integer; name: string; sal : integer)
Teaches(tid:integer; cid:string)
Database Management Systems 1 12
Integrity Constraints (ICs)
IC: condition that must be true for any instance of
the database; e.g., domain constraints.
ICs are specified when schema is defined.
ICs are checked when relations are modified.
A legal instance of a relation is one that satisfies all
specified ICs.
DBMS should not allow illegal instances.
If the DBMS checks ICs, stored data is more
faithful to real-world meaning.
Avoids data entry errors, too!
Database Management Systems 1 13
Primary Key Constraints
A set of fields is a key for a relation if :
1. No two distinct tuples can have same values in all key
fields, and
2. This is not true for any subset of the key.
Part 2 false? A superkey.
If theres >1 key for a relation, one of the keys is
chosen (by DBA) to be the primary key.
Possibly many candidate keys, one of which is
chosen as the primary key.
Database Management Systems 1 14
Foreign Keys, Referential Integrity
Foreign key : Set of fields in one relation that is
used to `refer to a tuple in another relation. (Must
correspond to primary key of the second relation.)
Like a `logical pointer.
E.g. sid is a foreign key referring to Students:
Enrolled (sid: string, cid: string, grade: double)
If all foreign key constraints are enforced,
referential integrity is achieved, i.e., no dangling
references.
e.g. of a data model w/o referential integrity:
Links in HTML!
Database Management Systems 1 15
Graphical Representation of ICs
Database Management Systems 1 16
Enforcing Referential Integrity
Consider Students and Enrolled; sid in Enrolled is
a foreign key that references Students.
What should be done if an Enrolled tuple with a
non-existent student id is inserted? (Reject it!)
What should be done if a Students tuple is deleted?
Also delete all Enrolled tuples that refer to it.
Disallow deletion of a Students tuple that is referred to.
Set sid in Enrolled tuples that refer to it to a default sid.
(In SQL, also: Set sid in Enrolled tuples that refer to it to
a special value null, denoting `unknown or `inapplicable.)
Similar if primary key of Students tuple is updated.
Database Management Systems 1 17
Where do ICs Come From?
ICs are based upon the semantics of the real-
world enterprise that is being described in the
database relations.
We can check a database instance to see if an IC is
violated, but we can NEVER infer that an IC is true
by looking at an instance.
An IC is a statement about all possible instances!
From example, we know name is not a key, but the
assertion that sid is a key is given to us.
Key and foreign key ICs are the most common;
more general ICs supported too.
Database Management Systems 1 18
Relational Query Languages
A relational database query is a question about the
data, and the answer consist of a new relation.
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.
Database Management Systems 1 19
Structured Query Language (SQL)
Developed by IBM (system R) in the 1970s
Need for a standard since it is used by many
vendors
Standards (ANSI):
SQL-86
SQL-89 (minor revision)
SQL-92 (major revision) - 1,120 pages
SQL-99 (major extensions) - 2,084 pages
SQL-2003 (SQL/XML new section) - 3,606 pages
SQL-2007 (expansion completed in 2006)
Database Management Systems 1 20
SQL Levels
Data-manipulation language (DML)
Allows users to pose queries
Insert /delete / modify (update) tuples.

Data-definition language (DDL):
Create / destroy / alter relations and views.
Define integrity constraints (ICs).

Access Control:
Can grant / revoke the right to access and manipulate
tables (relations / views).
Database Management Systems 1 21
DDL Commands Creating Relations
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(50),
email CHAR(30),
age INTEGER,
gr INTEGER)
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(5),
grade REAL)
Database Management Systems 1 22
DDL Destroying/Altering Relations
DROP TABLE Students

Destroys the relation Students. The schema
information and the tuples are deleted.

ALTER TABLE Students
ADD COLUMN firstYear INTEGER

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.
Database Management Systems 1 23
DDL Primary/Candidate Keys
Possibly many candidate keys (specified using
UNIQUE), one of which is chosen as the primary key.









! Used carelessly, an IC can prevent the storage of
database instances that arise in practice!
For a given student and
course, there is a single
grade.
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid))
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY(sid),
UNIQUE (cid, grade))
Students can take only one course,
and receive a single grade for that
course; further, no two students in
a course receive the same grade.
Database Management Systems 1 24
DDL Commands Foreign Keys
Only students listed in the Students relation should be
allowed to enroll for courses.
CREATE TABLE Enrolled
(sid CHAR(20), cid CHAR(20), grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid) REFERENCES Students )
sid cid grade
1234 Alg1 9
1235 Alg1 10
1234 DB1 10
1234 DB2 9
sid name email age gr
1234 John j@cs.ro 21 331
1235 Smith s@cs.ro 22 331
1236 Anne a@cs.ro 21 332
Students
Enrolled
Database Management Systems 1 25
DDL Commands Referential Integrity
SQL-99 and SQL-2003
support all 4 options on
deletes and updates.
Default is NO ACTION
(delete/update is rejected)
CASCADE (also delete
all tuples that refer to
deleted tuple)
SET NULL / SET
DEFAULT (sets foreign
key value of referencing
tuple)
CREATE TABLE Enrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid),
FOREIGN KEY (sid)
REFERENCES Students
ON DELETE CASCADE
ON UPDATE SET DEFAULT )
Database Management Systems 1 26
General Constraints
Useful when
more general ICs
than keys are
involved.
Can use queries
to express
constraint.
Constraints can
be named.

CREATE TABLE Students
(sid CHAR(20),
name CHAR(50),
email CHAR(30),
age INTEGER,
gr INTEGER,
PRIMARY KEY (sid),
CONSTRAINT ageInterv
CHECK (age >= 18
AND age<=70))

Das könnte Ihnen auch gefallen