Chaptrer 3
Chaptrer 3
to Relational Model
Outline
Relational Model
Types of Attributes
Database KEYS
Relational Query Language
6 basic operation
Relational Model
The relational model represents the
database as a collection of relations. A
relation is nothing but a table of values. Every
row in the table represents a collection of
related data values. These rows in the table
denote a real-world entity or relationship. ...
In the relational model, data are stored as
tables.
Example of a Relation
In relational model, the data and
relationships are represented by collection of
inter-related tables. Each table is a group of
column and rows, where column represents
attribute of an entity and rows represents
records or
tuples. Sample relationship Model:
Employees table with 4 columns/attributes
and 12 tuples /row/records.
Example of a Relation
attributes
(or columns or
fields)
tuples
(or rows or
records)
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.
An atomic value is a value that cannot
be divided.
The special value null is a member of
every domain. Indicated that the value is
“unknown”
The null value causes complications in the
definition of many operations
Relation Schema and Instance
The main difference between schema and
instance is that schema is a structural view
of the database, while the instance is the
data stored in a database at a particular
moment of time.
The current values (relation instance) of
a relation are specified by a table
An element of a tuple, represented by a
row in a table
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in an
arbitrary order) Arbitrary order is based on random choice or
personal whim, rather than any reason or system.
Example: instructor relation with unordered tuples
Introduction to Database Keys
Keys are very important part of Relational
database model. They are used to establish
and identify relationships between tables and
also to uniquely identify any record or row of
data inside a table.
A Key can be a single attribute or a group of
attributes, where the combination may act as
a key.
Why we need a Key?
In real world applications, number of tables
required for storing the data is huge, and the
different tables are related to each other as well.
Also, tables store a lot of data in them. Tables
generally extends to thousands of records stored in
them, unsorted and un-organized.
Now to fetch any particular record from such
dataset, you will have to apply some conditions, but
what if there is duplicate data present and every
time you try to fetch some data by applying certain
condition, you get the wrong data. How many trials
before you get the right data?
To avoid all this, Keys are defined to easily identify
any row of data in a table.
Example
Let's try to understand about all the keys using
a simple example
student_i name phone age
d
1 Pari 234566 23
7
2 Noor 345678 24
9
3 Momina 123456 25
7
4 Pari 877605 23
Let's take a simple Student table, with
4
fields( student_id, name, phone and age)
Super Key
Super Key is defined as a set of attributes
within a table that can uniquely identify each
record within a table. Super Key is a superset
of Candidate key.
student_id, (student_id, name), phone etc.
Confused? The first one is pretty simple
as student_id is unique for every row of data,
hence it can be used to identity each row
uniquely.
Next comes, (student_id, name), now name of
two students can be same, but
their student_id can't be same hence this
combination can also be a key.
Candidate Key
Candidate keys are defined as the minimal set
of fields which can uniquely identify each record
in a table. It is an attribute or a set of attributes
that can act as a Primary Key for a table to
uniquely identify each record in that table.
In our example, student_id and phone both are
candidate keys for table Student.
A candiate key can never be NULL or empty.
And its value should be unique.
There can be more than one candidate keys for
a table.
A candidate key can be a combination of more
than one columns(attributes).
Primary Key
Primary key is a candidate key that is most
appropriate to become the main key for any table.
It is a key that can uniquely identify each record
in a table.
One of the candidate keys is selected to be the primary
key.
student_id name phone age
Primary Key for this table
Example:
A,C (r)
Union of two relations
Relations r, s:
r s:
Set difference of two relations
Relations r, s:
r –
s:
Set intersection of two relations
Relation r, s:
r s
Note: r s = r – (r –
s)
joining two relations -- Cartesian-product
Relations r, s:
r x s:
Composition of Operations
Can build expressions using multiple operations
Example: A=C (r x s)
r x s
A=C (r x s)
Notes about Relational Languages
Each Query input is a table (or set of tables)
Each query output is a table.
All data in the output table appears in one of
the input tables
Relational Algebra is not Turning complete
Can we compute:
SUM
AVG
MAX
MIN