Ch2 - Intro To Relational Model

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

1

Chapter 2: Intro to Relational


Model

Adapted from : Database System Concepts 6th edition


Silbetchatz, Korth and Sudarshan
2
Relational model
• Uses a collection of tables to describe the data and the relationships among the data
• Each of which has a unique name
• Describes data at logical and view levels

3
Attribute Types
• The set of allowed values for each attribute is called the domain of the
attribute
• Attribute values are (normally) required to be atomic; that is,
indivisible
• The special value null is a member of every domain
A relation is defined only over atomic attributes.

4
Relation Schema and Instance
Relation schema : logical design of the db
• Gives the list of attributes of the relation and their domains
Eg: instructor(ID,name,dept_name,salary)
relation instance: set of tuples(rows) contained in a relation at a particular point in
time

5
Relational schema is a collection of attributes 6
instructor
Keys
ID name dept_name salary
22222 Einstein Physics 95000
12121 Karl Chemistry 78000
13425 Einstein Physics 96000
15151 Mozart Music 94000
17834 Einstein Maths 83456
22345 Faraday Physics 95000

Which of the attributes that can be used to uniquely identify the tuples of relation (value of
the attribute (set of attibutes) will be different for each row)?
• {name}
• {name,dept_name}
• {ID}
• {ID,name}
• {ID,name,dept_name}
• {ID,name,dept_name,salary}
7
Keys
• Candidate key
• A Candidate key is a set of one or more attributes that can uniquely identify a row
in a given table
• There can be more than one candidate key in a table.

Customer_Details Table

8
• Assumptions
• • One customer can have only one account
• • An account can belong to only one customer
• Simple Candidate key:
• A candidate key comprising of one attribute only.
• Eg: Account_No, Cust_ID, Cust_Email
• Composite candidate key:
• A candidate key comprising two or more attributes.
• {Cust_Last_Name, Cust_First_Name}
• Invalid Candidate Key:
• A candidate key should be comprised of a set of attributes that can uniquely
identify a row. A subset of the attributes should not possess the unique
identification property.
Candidate Keys are identified during the design of the database

9
Primary Key:
• During the creation of the table (the implementation phase), the database designer
chooses one of the candidate key from amongst the several available, to uniquely
identify rows in the customer_details table. The candidate key so chosen is called
the primary key.
• A primary key that is a combination of more than one attribute is called a
composite primary key.

10
Entity integrity constraint:
• The primary key of a table is always not null and unique
• The attributes which constitute the primary key cannot have duplicate values in the
rows of the table.
• It is mandatory to provide input for the primary key attributes.
• This constraint is referred to as the entity integrity constraint.
• A null value is an unknown value.
• It is not a blank character or a zero value
It is preferable to select a candidate key with a minimal number of attributes to
function as a primary key

• Selection guidelines
– Give preference to numeric columns
– Give preference to single attribute keys
– Give preference to minimal composite keys
• If the candidate keys are {a1,a2} and {a3,a4,a5}; then chose {a1,a2}
– Based on business convenience

11
• Foreign key
– A Foreign Key is a set of the attribute (s) whose values are required to match
values of a Candidate key in the same or another table.
– It can have null or duplicate values.

12
• The problem of ensuring that the database does not include any invalid foreign key
values is known as referential integrity problem.
• The constraint where values of a given Foreign Key must match the values of the
corresponding Candidate Key is known as Referential constraint.
• The relation that contains the foreign key is the referencing relation (also called
the child table) and the relation that contains the corresponding candidate key is
the referenced relation (also called the parent table).
• Invalid foreign key:
– A value of 1053 in the Account_No attribute of Customer_Transaction table is
invalid because a value of 1053 is not present in the Account_No attribute of
the Customer_Details table.

13
• Foreign key constraint: Value in one relation must appear in
another relation.
• dept_name of instructor is a foreign key from instructor ,
referencing department since dept_name is the primary key
of department
• Every value for instructor.dept_name should be present in
department.dept_name
Referencing (child) relation Referenced (parent)relation

instructor department

ID dept_name
name building
dept_name
salary

14
Foreign key constraints
• Enforce referential integrity
– A new row cannot be added to referencing relation unless it has a
matching record in the referenced relation
– Prevent actions that would destroy links between tables
• Handle modifications and deletions in the parent table
• When deleting or updating a table referential integrity constraint is
violated.
• To maintain referential integrity, we attach referential triggered action to
the foreign key when creating the table.
• CREATE TABLE orders ( order_id integer PRIMARY KEY, order_date date,
customer integer REFERENCES customers ON DELETE CASCADE );
• CASCADE option deletes or updates the row from the parent table
(containing PRIMARY KEYs), and automatically delete or update the
matching rows in the child table (containing FOREIGN KEYs).
15
• Self- Referencing
table
• – A table that has a
Foreign Key
referring to its own
Candidate Key is
known as self
referencing.
• From the example
Prerequisite can
only have any
existing value from
the Course_ID
attribute

16
Super Key
• Any superset of a candidate Key is a super key.
• Super Key is defined as a set of attributes within a table that uniquely identifies
each record within a table
• Consider the following sets comprising of attributes from the Customer_Details
table:
• {Account_No}
• {Account_No,Account_Type}
• {Account_No,Account_Type,Bank_Branch}
• {Account_No} is a candidate key for the table. {Account_No,Account_Type}
is a superset of {Account_No} and hence super key for the table
• A Super Key may have unnecessary attributes

17
Non-Key Attributes
• The attributes other than the Candidate Key attributes in a table/relation are called
Non-Key attributes.
• OR
• The attributes which do not participate in any of the Candidate keys..
• Egs: Cust_Last_Name, Cust_Mid_Name,Cust_First_Name, Bank_Branch etc are
non-key attributes in the Customer_Details table

18
Points to remember
• A table can have only 1 primary key. It may be a simple or a composite key
• Primary key attribute cannot contain NULL values
• Values for the primary key in a relation should be unique
• A table can have any number of foreign keys (simple or composite)
• Values of foreign key attribute need not be unique
• Foreign key attribute in the referencing relation can have null values

19
Database
• A database consists of multiple relations
• Information about an enterprise is broken up into parts
instructor
student
advisor
• Bad design:
univ (instructor -ID, name, dept_name, salary, student_Id, ..)
results in
– repetition of information (e.g., two students have the same instructor)
– the need for null values (e.g., represent an student with no advisor)

• Normalization theory (Chapter 7) deals with how to design “good”


relational schemas

20
Terms
• Attribute - a property or description of an entity. A toy
department employee entity could have attributes
describing the employee’s name, salary, and years of
service.
• Domain is synonymous with data type. Attributes can be
thought of as columns in a table. Therefore, an attribute
domain refers to the data type associated with a column
• Entity - an object in the real world that is distinguishable
from other objects such as the green dragon toy.
• Relational Database: It is a collection of tables each of
which is assigned a unique name or entity.

21
Terms
• A relation schema can be thought of as the basic
information describing a table or relation. This includes a
set of column names, the data types associated with each
column, and the name associated with the entire table. For
example, a relation schema for the relation called
Students could be expressed using the following
representation:
• Students(sid: string, name: string, login: string, age:
integer, gpa: real)
• A relational database schema is a collection of relation
schemas, describing one or more relations
• A relation instance is a set of tuples (also known as rows
or records) that each conform to the schema of the
relation
22

You might also like