You are on page 1of 20

Outline


Relational model
 Basic modelling concepts for schema specification
 Basic SQL commands
Relational Model -- Quick Example

 A relational schema = tables and constraints.


 Tables: customer, account
 Constraints:
Key constraints: Referential Integrity constraints: Null Constraint:
(foreign keys)
•ssno is the key for customer customer name cannot
table The custid attribute in account table take null values
takes values from ssno in customer
•both accountno and custid are 2
table
keys for account table
Relational data model
The relational database consists of a collection of
tables each of which has a unique name.
A row in a table represents a relationship among a set
of values.
It consists of headers which are called attributes.
For each attribute there is a set of permitted values for
that attribute which is called its domain.
Relational model
Relation and tuple can be used instead of table and
row.
The order in which tuples appear in a relation is
irrelevant.
For all relations r , the domains of all attributes of r,
should be atomic.
A domain is atomic if elements of the domain are
considered to be indivisible units.
Example relation

Definition Summary
Informal Terms Formal Terms
Table Relation
Column Header Attribute
All possible Column Domain
Values
Row Tuple

Table Definition Schema of a Relation

Populated Table State of the Relation


Relational Model

Database schema consists of
 a set of relation schema
 a set of constraints over the relation schema

Relational Schema = name(attributes). Graphically drawn
as table.

Example: employee(IDno, name, salary)

7
Attributes

With each attribute a domain is specified

In relational model attributes are atomic:
 the values are not divisible. That is, we cannot refer to
or directly see a subpart of the value.

an attribute can take a special null value

Null value represents either attributes whose value is not
known, or do not exist
Example of a Relation
Diagnosis: an example relation/table

Patient Disease

Jim Schizophrenic
Jane Obsessive-Comp
Jerry Manic
Joe null

null in this case may mean that diagnosis is not complete and
disease has not been identified. Notice possibility of confusion
that null means that the patient has no disease! This is one of
the reasons why using nulls is not always a great idea.
9
Constraints


What are they?
 represent the semantics of the domain being modeled.
 restrict the set of possible database states

Why do we want to specify the constraints?
 Useful information to application programmers. They can
write programs to prevent constraints violation

constraint -- acct balance should not fall below 0 dollars

programmer writing code for debit application should check
at the end if the account gets overdrawn!
 DBMS might enforce specified constraints directly
making task of application writer easier

If DBMS guarantees that account does not get overdrawn,
then debit application programmer need not worry about
checking for overdrawn account condition. 10
Specifying Constraints in Data Models

ER model
 domain and key constraints over entities
 participation and cardinality constraints over
relationships

Relational Model
 domain constraints, entity identity, key constraint,
functional dependencies -- generalization of key
constraints, referential integrity.
Domain Constraint


In the schema, every attribute is declared to have a type ---
integer, float, date, boolean, string, etc.

An insertion request can violate the domain constraint.

DBMS can check if insertion violates domain constraint
and reject the insertion.

12
Key Constraint


Each relation has a primary key.

Superkey:
 set of attributes such that if two tuples agree on those
attributes, then they agree on all the attributes of the
relation
 Note: the set of all the attributes of a relation is always
a superkey.

Candidate key:
 superkey no subset of which is a superkey

Primary key:
 one of the candidate keys

13
Disallowing Null Values

Some fields of a relation are too important to contain
null values.

Example:
 in sales(customer, salesman, date, amount, saleID)
we may not want ‘customer’ to contain a null value.

14
Entity Integrity Constraint

A primary key must not contain a null value.
 Else it may not be possible to identify some tuples.
 For Example, if more than one tuple has a null value in
its primary key, we may not be able to distinguish
them .
Foreign Key and Referential Integrity Constraint


Consider following 2 relation schemas:
 R1(A1, A2, …An) and R2(B1, B2, … Bm)

Let PK be subset of {A1, …,An} be primary key of
R1

A set of attributes FK is a foreign key of R2 if:
 attributes in FK have same domain as the attributes in PK
 For all tuples t2 in R2, there exists a tuple t1in R1 such
that t2[FK] = t1[PK].

A referential integrity constraint from attributes FK
of R2 to R1 means that FK is a foreign that refers
to the primary key of R1.

16
Example of Referential Integrity

student-grades
student C# Semester grade
Susan CS101 1-91 A
Jane CS101 1-91 B

LegalGrades
Grade
A we will have a referential integrity
B constraint saying that
C every value of student-grades.grade
D must also be a value of
F LegalGrades.grade,

17
Data Modification and Integrity Constraints

Modification, insertion and deletion requests can
lead to violations of integrity constraints.
 Key constraint, entity integrity, null value constraint,
referential integrity.

must check for violation at end of each operation
and suitably allow or disallow operation.

18
Example

Relations
 CourseOfferings(C#, semester, instructor)
 Takes(S#, C#, semester, grade)

Delete from courseOfferings where c# = “CS101” AND
Semester = “2-91”;

What should happen to tuples in Takes that refer to CS101
and semester 2-91??
Example (cont)

Takes S# C# Semester Grade


1001 CS101 2-91 ^
1002 CS101 2-91 ^
1003 CS101 1-91 A


Possible Solutions:
1) reject update (abort) - or -
2) delete tuples from ‘Takes’ that refer
to ‘CS101, 2-91’ (cascade)

You might also like