0% found this document useful (0 votes)
11 views

Chaptrer 3

Uploaded by

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

Chaptrer 3

Uploaded by

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

Chapter 3: Introduction

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

For the table Student we can make the student_id column as


the primary key
Unique Key
A unique key is a set of one or more
attribute that can be used to
uniquely identify the records in
table.
Composite Key
Key that consists of two or more
attributes that uniquely identify any
record in a table is called Composite
key. But the attributes which together
form the Composite key are not a key
independently or individually.
 Just like as candidate key
Natural Keys
A natural key is a key composed of columns
that actually have a logical relationship to
other columns within a table.
 For example, if we use Student_Id,
Student_Name and Father_Name columns
to form a key then it would be “Natural
Key” because there is definitely a
relationship between these columns and
other columns that exist in table.
 Natural keys are often called “Business
Key ” or “Domain Key”.
Surrogate Key
Surrogate key is an artificial key that is used
to uniquely identify the record in table.
 For example, in SQL Server contain an
artificial key that is known as “Identity”.
 Surrogate keys are just simple sequential
number. Surrogate keys are only used to act
as a primary key.
Example:
Branch_Id is a Surrogate Key in Branch_Info
table and Student_Id is a Surrogate key of
Student_Information table.
Foreign Keys
Foreign key is used to generate the
relationship between the tables.
 Foreign Key is a field in database table
that is Primary key in another table.
 A foreign key can accept null and
duplicate value.

Example:

Branch_Id is a Foreign Key in


Student_Information table that primary
key exist in Branch_Info (Branch_Id) table.
Secondary or Alternative key
The candidate key which are not selected as
primary key are known as secondary keys or
alternative keys.
Non-key Attributes
Non-key attributes are the attributes or
fields of a table, other than candidate
key attributes/fields in a table.
Non-prime Attributes
 Non-prime Attributes are attributes other
than Primary Key attribute
Conclusion of Keys
Database generally only contain
Primary Key, Foreign Key, Unique
Key and Surrogate key and other
remaining keys are just concept.
 A table must have a unique key.
So each table must have keys ,
because use of keys make data
highly reliable and provide several
types of content like unique data and
null values.
Schema Diagram for University Database
Relational Query Languages
Not turning-machine equivalent
consists of 6 basic operations
Project Operation – selection of
columns (Attributes)
Relation
r:


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

You might also like