Date post: | 03-Jun-2018 |
Category: |
Documents |
Upload: | mariana-ungureanu |
View: | 222 times |
Download: | 0 times |
of 26
8/12/2019 Course02 - Relational Model
1/26
Database Management Systems 1
COURSE 2
The Relational Model
8/12/2019 Course02 - Relational Model
2/26
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)
8/12/2019 Course02 - Relational Model
3/26
Database Management Systems 1 3
Hierarchical Modela collection of recordswhich are connected to one anotherthrough links.
each record hasone parentandmany children
customers
accounts
8/12/2019 Course02 - Relational Model
4/26
Database Management Systems 1 4
Network Model
each record may have many parentsandmany children
database = set of pairs of sets of records
8/12/2019 Course02 - Relational Model
5/26
Database Management Systems 1 5
Relational model - Ideas
Use a simple data structure: the Tablesimple to understand
useful data structure (capture many situations)
leads to useful not too complex query lang.Use mathematics to describe and representrecords and collections of records: the Relation
can be understood formallyleads to formal query languages
properties can be explained and proven
8/12/2019 Course02 - Relational Model
6/26
Database Management Systems 1 6
Relation
A domain is a set of scalar values (i.e. limited toatomic types - integer, string(24), boolean, date,blobs)
A relationor relation schemaRis a list ofattribute names (fields)[A1, A2, , An].
Ralso called the name of the relation
Corresponding to each attribute name Aiis adomain Dicalled the domain of Ai, we write Di=Dom(Ai).
8/12/2019 Course02 - Relational Model
7/26
Database Management Systems 1 7
Relation (cont)
The number of attributes in a relation scheme iscalled the degree or arity of the relation
A relation instance [R] is a subset of the
Cartesian product of the domains of the attributesin the schema
An element of the relation instance, a record, iscalled a tuple. All tuples of a relation are distinct
The number of rows of a relation is thecardinalityof the relation
8/12/2019 Course02 - Relational Model
8/26
Database Management Systems 1 8
Relation ExampleStudents(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 [email protected] 19 231
2877 Smith [email protected] 20 232
2976 Jones [email protected] 21 233
2765 Mary [email protected] 22 233
relation
schema
relation
instance
relation
instance
relation
tuple
mailto:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]:[email protected]8/12/2019 Course02 - Relational Model
9/26
Database Management Systems 1 9
Warnings
Very often we will confuse
the relation, its schema, and its instance
the instance and the tablethe attribute, the field and the column
the tuple and the row
Ask for precision if there is ambiguity!
8/12/2019 Course02 - Relational Model
10/26
Database Management Systems 1 10
Relational Database
A database is a set of relations
A database schema is the set of schemas ofthe relations in the database
A database instance is the set of instancesof the relations in the database
8/12/2019 Course02 - Relational Model
11/26
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)
8/12/2019 Course02 - Relational Model
12/26
Database Management Systems 1 12
Integrity Constraints (ICs)
IC: condition that must be true for anyinstance ofthe database; e.g., domain constraints.
ICs are specified when schema is defined.
ICs are checked when relations are modified.A legalinstance of a relation is one that satisfies allspecified ICs.
DBMS should not allow illegal instances.
If the DBMS checks ICs, stored data is morefaithful to real-world meaning.
Avoids data entry errors, too!
8/12/2019 Course02 - Relational Model
13/26
Database Management Systems 1 13
Primary Key Constraints
A set of fields is a keyfor a relation if :
1. No two distinct tuples can have same values in all keyfields, 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 theprimary key.Possibly many candidate keys, one of which ischosen as theprimary key.
8/12/2019 Course02 - Relational Model
14/26
Database Management Systems 1 14
Foreign Keys, Referential Integrity
Foreign key: Set of fields in one relation that isused to `refer to a tuple in another relation. (Mustcorrespond to primary key of the second relation.)Like a `logical pointer.
E.g. sidis a foreign key referring to Students:Enrolled (sid: string, cid: string,grade: double)
If all foreign key constraints are enforced,
referential integrityis achieved, i.e., no danglingreferences.
e.g. of a data model w/o referential integrity:Links in HTML!
8/12/2019 Course02 - Relational Model
15/26
Database Management Systems 1 15
Graphical Representation of ICs
8/12/2019 Course02 - Relational Model
16/26
8/12/2019 Course02 - Relational Model
17/26
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 thedatabase relations.
We can check a database instance to see if an IC is
violated, but we can NEVERinfer that an IC is trueby looking at an instance.
An IC is a statement about all possible instances!
From example, we know nameis not a key, but theassertion that sidis a key is given to us.
Key and foreign key ICs are the most common;more general ICs supported too.
8/12/2019 Course02 - Relational Model
18/26
Database Management Systems 1 18
Relational Query Languages
A relational database query is a question about thedata, and the answer consist of a new relation.
A major strength of the relational model: supports
simple, powerful queryingof data.Queries can be written intuitively, and the DBMSis 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.
8/12/2019 Course02 - Relational Model
19/26
Database Management Systems 1 19
Structured Query Language (SQL)
Developed by IBM (system R) in the 1970sNeed for a standard since it is used by manyvendors
Standards (ANSI):SQL-86
SQL-89 (minor revision)
SQL-92 (major revision) - 1,120 pages
SQL-99 (major extensions) - 2,084 pagesSQL-2003 (SQL/XML new section) - 3,606 pages
SQL-2007 (expansion completed in 2006)
8/12/2019 Course02 - Relational Model
20/26
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 relationsand views.
Define integrity constraints(ICs).
Access Control:
Can grant / revoke the right to access and manipulatetables (relations / views).
8/12/2019 Course02 - Relational Model
21/26
Database Management Systems 1 21
DDL Commands Creating Relations
Creates the Studentsrelation. Observe that thetype (domain) of eachfield is specified, and
enforced by the DBMSwhenever tuples areadded or modified.
As another example, theEnrolledtable holdsinformation about coursesthat students take.
CREATE TABLEStudents(sid CHAR(20),
name CHAR(50),
email CHAR(30),
age INTEGER,
gr INTEGER)
CREATE TABLEEnrolled
(sid CHAR(20),
cid CHAR(5),
grade REAL)
8/12/2019 Course02 - Relational Model
22/26
Database Management Systems 1 22
DDLDestroying/Altering Relations
DROP TABLEStudents
Destroys the relation Students. The schemainformation andthe tuples are deleted.
ALTER TABLE Students
ADD COLUMNfirstYear INTEGER
The schema of Students is altered by adding anew field; every tuple in the current instance isextended with a nullvalue in the new field.
8/12/2019 Course02 - Relational Model
23/26
Database Management Systems 1 23
DDL Primary/Candidate KeysPossibly many candidate keys (specified usingUNIQUE), one of which is chosen as theprimary key.
!Used carelessly, an IC can prevent the storage of
database instances that arise in practice!
For a given student andcourse, there is a single
grade.
CREATE TABLEEnrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),PRIMARY KEY(sid,cid))
CREATE TABLEEnrolled
(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 thatcourse; further, no two students in
a course receive the same grade.
8/12/2019 Course02 - Relational Model
24/26
Database Management Systems 1 24
DDL Commands Foreign Keys
Only students listed in the Students relation should beallowed to enroll for courses.
CREATE TABLE Enrolled
(sid CHAR(20), cid CHAR(20), grade CHAR(2),
PRIMARY KEY (sid,cid),FOREIGN KEY(sid) REFERENCESStudents )
sid cid grade
1234 Alg1 91235 Alg1 10
1234 DB1 10
1234 DB2 9
sid name email age gr1234 John [email protected] 21 331
1235 Smith [email protected] 22 331
1236 Anne [email protected] 21 332
StudentsEnrolled
8/12/2019 Course02 - Relational Model
25/26
Database Management Systems 1 25
DDL Commands Referential Integrity
SQL-99 and SQL-2003support all 4 options ondeletes and updates.
Default is NO ACTION
(delete/update is rejected) CASCADE(also delete
all tuples that refer todeleted tuple)
SET NULL/SETDEFAULT (sets foreignkey value of referencingtuple)
CREATE TABLEEnrolled
(sid CHAR(20),
cid CHAR(20),
grade CHAR(2),PRIMARY KEY (sid,cid),
FOREIGN KEY(sid)
REFERENCESStudents
ON DELETE CASCADE
ON UPDATE SET DEFAULT)
8/12/2019 Course02 - Relational Model
26/26
Database Management Systems 1 26
General Constraints
Useful whenmore general ICsthan keys are
involved.Can use queriesto express
constraint.Constraints canbe named.
CREATE TABLE Students
(sid CHAR(20),
name CHAR(50),
email CHAR(30),
age INTEGER,
gr INTEGER,
PRIMARY KEY (sid),
CONSTRAINTageInterv
CHECK (age >= 18ANDage