0% found this document useful (0 votes)
5 views27 pages

relational model

concept of rdbms

Uploaded by

kunal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views27 pages

relational model

concept of rdbms

Uploaded by

kunal kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

The Relational Model

Unit-3

• Relational Data Model


• Relational Query Language (DDL + DML)
• Integrity Constraints (IC)
 Relational Data Model
 Relational Query Language
 Integrity Constraints

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.

 Schema : specifies name of relation, plus name and type of


each column.
• E.G.
• Students(sid: string, name: string, login: string,age: integer, gpa:
real).

 Think of a relation as a set of rows or tuples


i.e., all rows are distinct (not required by commercial database)
 Relational Data Model
 Relational Query Language
 Integrity Constraints

Example Instance of Students


 ER to Relational

Relation

sid name login age gpa


53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2
53650 Smith smith@math 19 3.8

 Cardinality = ?, degree = ?
 Cardinality = 3, degree = 5.
 All rows are distinct
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Attribute Types / Domain


 Each column of a relation has a name
 Set of allowed values for each column is called domain of column
 Domain specifies that values of the column must be drawn from the
domain associated with the column – domain constraint
 Column values are (normally) required to be atomic, i.e.,
indivisible
 The special value null is a member of every domain
 Null value causes complications in the definition of many
operations.
 used to represent values that are unknown or inapplicable to certain tuples.
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Relations are Unordered


 Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
 E.g., account relation with unordered tuples
 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

E.g.: account : stores information about accounts


depositor : stores information about which
customer
owns which account
customer : stores information about customers

 Storing all information as a single relation such as


bank(account-number, balance, customer-name, ..)
results in:
 repetition of information (e.g., two customers own an
account)
 need for null values (e.g., represent a customer without an
account)
 Relational Data Model
 Relational Query Language
 Integrity Constraints

Relational Query Languages


 ER to Relational

(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

DDL ---- Creating Relations


CREATE TABLE Students
 Creates Students (sid: CHAR(20),
relation. Observe that name: CHAR(20),
type (domain) of each login: CHAR(10),
field is specified, and age: INTEGER,
enforced by DBMS gpa: REAL)
whenever tuples are
added or modified.
CREATE TABLE Enrolled
 As another example, (sid: CHAR(20),
Enrolled table holds cid: CHAR(20),
information about courses grade:
that students take. CHAR(2))
 Relational Data Model
 Relational Query Language
 Integrity Constraints

DDL --- Destroying and


 ER to Relational

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.
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

DML --- Query single relation

 To find all 18 year old students, we can


write:
sid name login age gpa
SELECT *
FROM Students S 53666 Jones jones@cs 18 3.4
WHERE S.age=18 53688 Smith smith@ee 18 3.2

•To find just names and logins, replace the first line:

SELECT S.name, S.login

1
 Relational Data Model
 Relational Query Language

DML --- Querying Multiple


 Integrity Constraints
 ER to Relational

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”

Given the following sid cid grade


instances of Enrolled and 53831 Carnatic101 C
53831 Reggae203 B
Students:
53650 Topology112 A
sid name login age gpa 53666 History105 B
53666 Jones jones@cs 18 3.4
53688 Smith smith@eecs 18 3.2 we get:
53650 Smith smith@math 19 3.8 S.name E.cid
Smith Topology112

1
 Relational Data Model
 Relational Query Language
 Integrity Constraints

DML --- Adding and Deleting


 ER to Relational

Tuples
 Can insert a single tuple using:
INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)

 Can delete all tuples satisfying some


condition (e.g., name = Smith):
DELETE
FROM Students S
WHERE S.name = ‘Smith’

1
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Integrity Constraints (ICs)


 IC: condition that must be true for any
instance of the database
• 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.

1
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Integrity Constraints (ICs)


 IC include:

• 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

Primary and Candidate Keys


 ER to Relational

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) )

CREATE TABLE Enrolled


(sid CHAR(20)
cid CHAR(20),
grade CHAR(2),
PRIMARY KEY (sid),
UNIQUE (cid, grade) )

1
 Relational Data Model
 Relational Query Language
 Integrity Constraints

Primary and Candidate Keys


 ER to Relational

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) )

Used carelessly, an IC can prevent the


storage of database instances that arise
in practice! 1
 Relational Data Model
 Relational Query Language
 Integrity Constraints

Foreign Keys, Referential


 ER to Relational

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

Foreign Keys in SQL


 Only students listed in 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 )

 If all foreign key constraints are enforced, referential


integrity is achieved, i.e., no dangling references.

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

 Insertion: What if a new Student tuple is inserted?


 Insertion: What should be done if an Enrolled tuple
with a non-existent student id is inserted?
 Reject it
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 an Enrolled tuple is deleted?

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)

 Similar if primary key of Students tuple is updated.


2
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Referential Integrity in SQL


 SQL/92 and
SQL/99 support CREATE TABLE Enrolled
all 4 options on (sid CHAR(20),
deletes and cid CHAR(20),
grade CHAR(2),
updates: PRIMARY KEY (sid,cid),
 Default is NO ACTION FOREIGN KEY (sid)
(delete/update is REFERENCES Students
rejected) ON DELETE CASCADE
 CASCADE (also ON UPDATE SET DEFAULT )
delete all tuples that
refer to deleted
tuple)
 SET NULL / SET
DEFAULT (sets
foreign key value of
referencing tuple) 2
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Where do ICs Come From?


 ICs are based upon semantics of real-world
enterprise being described in 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;


but more general ICs supported too in some systems.

2
 Relational Data Model
 Relational Query Language
 Integrity Constraints
 ER to Relational

Relational Model Terminology


 Schema / Data Model
 Relation /Relation Schema
 Instance / Database

 Schema Definition Language


 Constraint Specification Language

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.

You might also like