0% found this document useful (0 votes)
10 views30 pages

Unit2-Relational Model-Part1

Uploaded by

kadsjzzzz
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)
10 views30 pages

Unit2-Relational Model-Part1

Uploaded by

kadsjzzzz
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

Relational Model

UNIT-II
Part-1
Structure of Relational DB
, Database Schema, Keys
Structure of Relational DB

• A database that can be perceived as a set of tables and manipulated in


accordance with the relational model of data. Each database includes:a set
of system catalog tables that describe the logical and physical structure of
the data
• a configuration file containing the parameter values allocated for the
database
• a recovery log with ongoing transactions and archivable transactions

• Reference link:
https://www.ibm.com/docs/en/mfci/7.6.2?topic=design-relational-database-s
tructure
Relational DB
• Attribute data types
Each database record contains multiple attributes. Every attribute has an associated data type.
• Database views
A database view is a subset of a database and is based on a query that runs on one or more
database tables. Database views are saved in the database as named queries and can be used to
save frequently used, complex queries.
• Indexes
You can use indexes to optimize performance for fetching data. Indexes provide pointers to
locations of frequently accessed data. You can create an index on the columns in an object that
you frequently query.
• Primary keys
When you assign a primary key to an attribute, the key uniquely identifies the object that is
associated with that attribute. The value in the primary column determines which attributes are
used to create the primary key.
Example of a Relation
Basic Structure
• Formally, given sets D1, D2, …. Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2, …, an) where each ai  Di

• Example: If
customer_name = {Jones, Smith, Curry, Lindsay}
customer_street = {Main, North, Park}
customer_city = {Harrison, Rye, Pittsfield}
Then r = { (Jones, Main, Harrison),
(Smith, North, Rye),
(Curry, North, Rye),
(Lindsay, Park, Pittsfield) }
is a relation over
customer_name x customer_street x customer_city
Attribute Types
• Each attribute of a relation has a name
• 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
• Note: multivalued attribute values are not atomic
• Note: composite attribute values are not atomic
• The special value null is a member of every domain
• The null value causes complications in the definition of
many operations
• We shall ignore the effect of null values in our main
presentation and consider their effect later
Relation Schema
• A1, A2, …, An are attributes

• R = (A1, A2, …, An ) is a relation schema


Example:
Customer_schema = (customer_name, customer_street,
customer_city)

• r(R) is a relation on the relation schema R


Example:
customer (Customer_schema)
Relation Instance
• The current values (relation instance) of a
relation are specified by a table
• An element t of r is a tuple, represented by a row
in a table attributes
(or columns)
customer_name customer_street customer_city

Jones Main Harrison


Smith North Rye tuples
Curry North Rye (or rows)
Lindsay Park Pittsfield

customer
Relations are Unordered
Order of tuples is irrelevant (tuples may be stored in an arbitrary order)
Example: account relation with unordered tuples
The customer Relation
The depositor Relation
Relational database schema
• a Relational database schema is an arrangement of relation states in
such a manner that every relational database state fulfills the integrity
constraints set on a relational database schema.
• A database schema includes a relational schema for each of its tables
and a set of integrity constraints
Relational database schema
Keys in RDBMS
• A key refers to an attribute/a set of attributes that help us identify a
row (or tuple) uniquely in a table (or relation).
• A key is also used when we want to establish relationships between
the different columns and tables of a relational database.
Types of Keys in DBMS
• Super Key
• Candidate Key
• Primary Key
• Alternate Key
• Foreign Key
• Composite Key
• Unique Key
Super Key

• A super key refers to the set of all those keys that help us uniquely
identify all the rows present in a table.
• It means that all of these columns present in a table that can identify
the columns of that table uniquely act as the super keys.
• A super key is a candidate key’s superset (candidate key has been
explained next).
• We need to pick the primary key of any table from the super key’s set
so as to make it the table’s identity attribute.
Candidate Key

• The candidate keys refer to those attributes that identify rows uniquely
in a table. In a table, we select the primary key from a candidate key.
Thus, a candidate key has similar properties as that of the primary
keys.
• In a table, there can be multiple candidate keys.
Primary Key

• The primary key refers to a column or a set of columns of a table that


helps us identify all the records uniquely present in that table.
• A table can consist of just one primary key.
• Also, this primary key cannot consist of the same values
reappearing/repeating for any of its rows.
• The PK (PRIMARY KEY) constraint that we put on a column/set of
columns won’t allow these to have a null value or a duplicate.
• Any table can consist of only a single primary key constraint.
• A foreign key (explained below) that refers to it can never change the
values present in the primary key.
Properties of PK
• It is a unique key.
• It can identify only one tuple (a record) at a time.
• It has no duplicate values.
• it has unique values.
• It cannot be NULL.
• Primary keys are not necessarily to be a single column; more than one
column can also be a primary key for a table.
Alternate Key Foreign Key
All those keys that did not become • We use a foreign key to establish
a primary key are known as relationships between two
alternate keys. available tables.
• The foreign key would require
every value present in a
column/set of columns to match
the referential table’s primary key.
• A foreign key helps us to maintain
data as well as referential
integrity.
Composite Key Unique Key
• The composite key refers to a set of • A unique key refers to a column/a set
multiple attributes that help us of columns that identify every record
uniquely identify every tuple present uniquely in a table.
in a table. • All the values in this key would have
• The attributes present in a set may not to be unique.
be unique whenever we consider them • Remember that a unique key is
separately. different from a primary key. Because
• Thus, when we take them all together, it is only capable of having one null
it will ensure total uniqueness. value.
• A primary key, on the other hand,
cannot have a null value.
Example: consider below set of Super keys
1.ABC -> ABC From all the super keys available, the candidate key is the one
whose proper subset is not a super key
2.AB -> C
3.AC -> B
4.BC -> A
5.A -> BC
6.B -> AC
7.C -> AB
Selection of candidate key
• But now, the given set of the super key is:
• ABC-> ABC
AB -> C
BC -> A
A -> BC
• Given ABC key, we’re saying if we find its proper subset, i.e., {A, B,
C, AB, AC, BC} and any of them can be a key, then ABC can’t
become a candidate key.
• AB, which comes from the proper subset of ABC, is also a key.
Therefore, ABC is not a candidate key.
Selection of candidate key
• Note: If x is a proper subset of y, then x must NOT have one element
that is included in y. Example,
• X = {1,2,3} Y = {1,2,3,4}
• Here, we can say X is a proper subset of Y since X doesn’t have 4 in it.
• Similarly, AB is also not a candidate key because we have got A in its
proper subset which, can be used as a key. Therefore, at last, we found
that A is the ONLY candidate key available for the above schema.
Selection of primary key
• The primary key is nothing but a candidate key which has given the
right to be called the primary key.
• All other candidate keys can also be used as a primary key, but the
database administrator (or you) can choose a single key out of those to
be a primary key. We can use the primary key to uniquely identify the
records of a table.
Selection of CK, primary key
• Now we have a set of the super key as,
1.ABC-> ABC
2.AB -> C
3.BC -> A
4.A -> BC
5.B -> AC
6.C -> AB
• Out of those, A, B, and C are valid candidate keys.
• From A, B, and C, we can choose either A, B, or C as primary key.
• One may choose A, other can choose B, and both are fine.
Types of keys in DBMS
NULL values
• We pay special attention to the NULL value in a database.
• Intuitively, NULL means that the value is not known or may not exist.
• For example, the value of an age attribute may be NULL if it's not
known, whereas the value of spouse could be either unknown or may
not exist.
• The important property to keep in mind about the NULL value is that
an equality test involving a NULL returns NULL.
• In fact, even NULL = NULL returns NULL.
• We can test explicitly for NULL with the predicate is NULL.

You might also like