relational model
relational model
Unit-3
Relational Database:
ER to Relational
Definitions
Relational database: a set of relations
Relation: made up of 2 parts:
Instance : a table, with rows (tuples) and columns (attributes).
#Rows = cardinality, #attributes = degree / arity.
Relation
Cardinality = ?, degree = ?
Cardinality = 3, degree = 5.
All rows are distinct
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
Database
A database consists of multiple relations
Information about an enterprise is broken up into parts,
with each relation storing one part of the information
(SQL)
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.
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
Altering Relations
DROP TABLE Students
•To find just names and logins, replace the first line:
1
Relational Data Model
Relational Query Language
Relations
What does the
SELECT S.name, E.cid
following query FROM Students S, Enrolled E
compute? WHERE S.sid=E.sid AND E.grade=“A”
1
Relational Data Model
Relational Query Language
Integrity Constraints
Tuples
Can insert a single tuple using:
INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
1
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
1
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
• Fundamental constraints :
• Key
• Foreign Key,
• Domain Constraints
• General constraints:
• table constraints (single table)
• assertions (several tables)
1
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
Key Constraint
Two rules for Key constraints:
Two distinct tuples in a legal instance cannot
have identical values in all columns of keys
(unique)
No subset of the set of fields in a key is a
unique identifier for a tuple (maximal)
Example:
• “No two students can have the same student Id “
• “No two students can have the same student Id and
name”
CORE IDEA : Minimal subset of columns of the
relation that uniquely identify the tuple.
1
Relational Data Model
Relational Query Language
Integrity Constraints
Keys
ER to Relational
Let K R
Candidate key:
Minimum set of attributes which makes difference between any two
records of the table.
CK: { sid}
Ck constraint: No two record have same sid.
sid
K is a superkey of R if values for K are sufficient to
identify a unique tuple of relation r(R)
Example: {customer-name, customer-street} and
{customer-name}
are both superkeys of relation Customer.
NO two customers can possibly have the same name.
Set of all fields is a super key
K is a candidate key if K is minimal
Example: {customer-name} is a candidate key for
Customer.
- superkey
1
Relational Data Model
Relational Query Language
Integrity Constraints
in SQL
Possibly many candidate keys (specified using
UNIQUE), one of which is chosen as the primary
key. CREATE TABLE Enrolled
(sid CHAR(20)
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid,cid) )
1
Relational Data Model
Relational Query Language
Integrity Constraints
in SQL
CREATE TABLE Enrolled
“For a given student and course, (sid CHAR(20)
there is a single grade.” cid CHAR(20),
grade CHAR(2),
vs. PRIMARY KEY (sid,cid) )
“Students can take only one
CREATE TABLE Enrolled
course, and receive a single (sid CHAR(20)
grade for that course; further, cid CHAR(20),
no two students in a course grade CHAR(2),
PRIMARY KEY (sid),
receive the same grade.” UNIQUE (cid, grade) )
Integrity
Foreign key : Set of fields in one relation that is used
to "refer" to a tuple in another relation.
- Like a `logical pointer’.
Foreign key :
FK in referencing relation must match PK of referenced
relation.
Match = same number of columns, compatible data types
(column names can be different).
Enrolled (referencing relation)
sid cid grade Students (referenced relation)
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 Key 1
Foreign Key
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
2
Relational Data Model
Relational Query Language
Integrity Constraints
Enforcing Referential
ER to Relational
Integrity
Consider Students and Enrolled; sid in Enrolled is a
foreign key that references Students.
Enrolled (referencing relation)
sid cid grade Students (referenced relation)
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 Key
Foreign Key
Enforcing Referential
Relational Query Language
Integrity Constraints
ER to Relational
Integrity
Enrolled (referencing relation)
sid cid grade Students (referenced relation)
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 Key
Foreign Key
2
Relational Data Model
Enforcing Referential
Relational Query Language
Integrity Constraints
ER to Relational
Integrity
Enrolled (referencing relation)
sid cid grade Students (referenced relation)
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 Key
Foreign Key
Deletion: What if a Students tuple is deleted?
Cascading -- Also delete all Enrolled tuples that refer to it.
No Action -- Disallow deletion of a Students tuple that is referred to.
Set Default -- Set sid in Enrolled tuples that refer to it to a default
sid.
Set Null -- Set sid in Enrolled tuples that refer to it to a special value
null, denoting `unknown’ or `inapplicable’. (Not always applicable)
2
Relational Data Model
Relational Query Language
Integrity Constraints
ER to Relational
2
Relational Data Model
Relational Query Language
Integrity Constraints
Relational Model:
ER to Relational
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.