Relational_Model
Relational_Model
Data Models
• DBMS models real world
• Data Model is link
between user’s view of
the world and bits
stored in computer
Student(sid:Students(sid: string, name:
string, login: string, age: integer, gpa:real)
1010
1111
01
Why Study the Relational
Model?
• Most widely used model.
– Vendors: IBM, Microsoft, Oracle, Sybase, etc.
• “Legacy systems” in older models
– e.g., IBM’s IMS
• Object-oriented concepts have recently merged in
– object-relational model
• IBM DB2, Oracle 9i, IBM Informix
• UPDATE <name>
SET <field name> = <value>
WHERE <condition>
• SELECT <fields>
FROM <name>
WHERE <condition>
Creating Relations in SQL
• Creates the Students relation. CREATE TABLE Students
• Note: the type (domain) of (sid CHAR(20),
name CHAR(20),
each field is specified, and login CHAR(10),
enforced by the DBMS age INTEGER,
– whenever tuples are added or gpa FLOAT)
modified.
• Another example: the Enrolled
table holds information aboutCREATE TABLE Enrolled
courses students take. (sid CHAR(20),
cid CHAR(20),
grade CHAR(2))
Adding and Deleting Tuples
• Can insert a single tuple using:
DELETE
FROM Students S
WHERE S.name = ‘Smith’
Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Reggae203 B 53666 Jones jones@cs 18 3.4
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8
Primary Keys
Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Reggae203 B 53666 Jones jones@cs 18 3.4
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8
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: string)
– If all foreign key constraints are enforced,
referential integrity is achieved (i.e., no dangling
references.)
Foreign Keys in SQL
• 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 )
Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Reggae203 B 53666 Jones jones@cs 18 3.4
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8
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!
Where do ICs Come From?
• ICs are based upon the semantics of the real-
world 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.
Enrolled
sid cid grade Students
sid name login age gpa
53666 Carnatic101 C
53666 Reggae203 B 53666 Jones jones@cs 18 3.4
53650 Topology112 A 53688 Smith smith@eecs 18 3.2
53666 History105 B 53650 Smith smith@math 19 3.8
S.name E.cid
we get:
Smith Topology112
Semantics of a Query