Relational Data Model
Relational Data Model
1
Relational Data Model
• In relational model, tables are called relations
that store data for different columns. Each
table can have multiple columns where each
column name should be unique.
• It is important to note here that relations in a
database are not independent tables, but are
associated with each other.
3
Entity : An entity is something that exists and about
which we can store some information . It is an object
which can be distinctly identified . For example student
entity , employee entity , item entity , etc.
ATTRIBUTE: The columns of a relation are the attributes
which are also referred as fields. For example, GUID,
GName, GPhone and GAddress are attributes of relation
GUARDIAN.
TUPLE: Each row of data in a relation (table) is called a
tuple.
4
DOMAIN: It is a set of values from which an attribute
can take a value in each row. Usually, a data type is
used to specify domain for an attribute.
For example, in GURDIAN relation, the attribute GUID
takes integer values and hence its domain is a set of
integer values. Similarly, the set of character strings
constitutes the domain of the attribute GNAME.
DEGREE: The number of attributes in a relation is called the Degree
of the relation. For example, relation GUARDIAN with four
attributes is a relation of degree 4.
6
Important Properties of a Relation
• Property 1:Each attribute in a relation has a unique name.
Sequence of attributes in a relation is immaterial.
• Property 2 :Each tuple in a relation is distinct. Thus, each tuple of a
relation must be uniquely identified by its contents.
• Property 3: All data values in an attribute must be from the same
domain (same data type).
• Each data value associated with an attribute must be atomic
(Atomicity : cannot be further divisible into meaningful subparts).
• No attribute can have many data values in one tuple. For example,
Guardian cannot specify multiple contact numbers under GPhone
attribute. For multiple contact numbers we may have to define
multiple columns such as Gphone_1 ,Gphon_2 etc. .
• A special value “NULL” is used to represent values that are unknown
or non-applicable to certain attributes. For example, if a guardian
does not share his or her contact number with the school
authorities, then GPhone is set to NULL (data unknown).
7
Keys in a relational database
Candidate Key
A relation can have one or more attributes that takes
distinct values. Any of these attributes can be used to
uniquely identify the tuples in the relation. Such
attributes are called candidate keys as each of them are
candidates for the primary key.
The relation GUARDIAN has four attributes out of which
GUID and GPhone always take unique values. No two
guardians will have same phone number or same GUID.
Hence, these two attributes are the candidate keys as
they both are candidates for primary key.
8
Primary Key
Out of one or more candidate keys, the attribute
chosen by the database designer to uniquely
identify the tuples in a relation is called the
primary key of that relation. The remaining
attributes in the list of candidate keys are called
the alternate keys. In the relation GUARDIAN,
suppose GUID is chosen as primary key, then
GPhone will be called the alternate key.
9
Composite Primary Key
If no single attribute in a relation is able to uniquely distinguish the
tuples, then more than one attribute are taken together as primary
key. Such primary key consisting of more than one attribute is
called Composite Primary key.
Foreign Key
A foreign key is used to represent the relationship between two
relations. A foreign key is a non–key attribute whose value is
derived from the primary key of another relation.
10
A school has a rule that each student must
participate in a sports activity. So each one
should give only one preference for sports
activity. Suppose there are five students in a
class, each having a unique roll number. The
class representative has prepared a list of sports
preferences as shown below. Answer the
following:
11
• a) Roll no 24 may not be interested in sports.
Can a NULL value be assigned to that student’s
preference field?
• b) Roll no 17 has given two preferences
sports. Which property of relational DBMS is
violated here? Can we use any constraint or
key in the relational DBMS to check against
such violation, if any?
• c) Kabaddi was not chosen by any student. Is it
possible to have this tuple in the Sports
Preferences relation?
12
In another class having 2 sections, the two
respective class representatives have prepared 2
separate Sports Preferences tables, as shown
below: Sports preference of section 1 (arranged
on roll number column) .
13
• Sports preference of section 2 (arranged on
Sports name column, and column order is also
different)
14
Overview Of SQL And MySQL
• SQL (Structured Query Language) is a standard
language for accessing and manipulating
databases.
• SQL commands are used to create , transform
and retrieve information from Relational
Database Management System and also to create
an interface between the user and database.
• By using SQL commands , one can search any
data in the database and perform other functions
like create tables , add records , modify data
,remove rows ,etc.
MYSQL
• MYSQL is an open source and freely available
Relational Database Management System that
uses Structured Query Language.
• It provides excellent features for creating , storing
, maintaining and accessing data stored in the
form of databases and their respective tables.
• Being an open source software , it can be
downloaded freely and easily from
www.mysql.org.