Database Management System5

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Database Management

Systems

SUBJECT TEACHER: PRIYA SACHDEVA, ASSISTANT PROFESSOR (CSE)


Relational Model
What is Relational Model
•It 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.
•Relational Model was proposed by E.F. Codd.
•Popular RDBMS are:
IBM DB2 , Oracle, Microsoft SQL Server, MySQL, Microsoft Access, Oracle,
PostgreSQL, dBASE, SQLite, MariaDB, Microsoft SQL Server
Terminology
RELATIONAL DATA STRUCTURE OPERATIONS
• Attribute/Field/Column • Insert
• Tuple/Record/Row • Delete
• Relation/Table • Update
• Degree • Select
• Cardinality
• Domain
• NULL Value
• Relation key
Relational Keys
•Key is used for identifying unique rows from table.
•It also establishes relationship among tables.
•A Key can be a single attribute or a group of attributes.

Student
Types
Super Key
•The set of attributes which can uniquely identify a tuple is known as Super Key.
•Super Key is a superset of Candidate key.
•It has uniqueness property but not necessarily irreducibility property.

Candidate Key
•Candidate keys are defined as the minimal set of fields which can uniquely identify each record
in a table.
•It has uniqueness property and irreducibility property.
•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.
•There can be more than one candidate key.
•A candidate key can never be NULL or empty.
•A candidate key can be a combination of more than one columns(attributes).
Primary Key
•A Primary Key is a column or set of columns in a table that uniquely identifies tuples in that table.
•Primary Key must contain unique values, and cannot contain NULL values.
•A table can have only one Primary Key.
•It should not change or become NULL throughout life of an entity.

Alternate Key
•Out of all Candidate Keys, only one gets selected as Primary Key.
•Remaining Candidate Keys are known as Alternate or Secondary Keys.

Composite Key
•A key that consists of more than one attribute to uniquely identify rows in a table is called Composite
key.

Artificial Key
•It is created when there is no natural Primary key.
•In the table when the Primary Key is too big or complicated, we create Artificial Key.
Foreign Key
•Foreign keys are the columns of a table that points to the primary key of another table.
•They act as a cross-reference between tables.
•It permits only those values which appear in Primary Key table.
Relational Algebra
•Relational Algebra is a
procedural query language,
which takes instances of relations
as input and yields instances of
relations as output.
•It uses operators to perform
queries.
•Relational Algebra is performed
recursively on a relation and
intermediate results are also
considered relations.
Select
•The Select operation selects tuples that satisfy a given predicate.
•It is denoted by sigma (σ).
•Notation: σ p(r)
where r is relation, p is used as a propositional logic formula which may use
Connectors like and, or, not
Relational Operators like =, ≠, ≥, <, >, ≤
Project
•This operation shows the list of those attributes that we wish to appear in the
result. Rest of the attributes are eliminated from the table.
•It is denoted by ∏.
•Notation: ∏ A1, A2 (R)
where A1, A2 is used as an attribute name of relation r.
Rename Operation
•The Rename operation is used to rename the output relation.
•It is denoted by rho (ρ).
•Notation: ρ(Relation New, Relation Old)
•Example: We can use the Rename operator to rename STUDENT relation to
STUDENT1.
•ρ(STUDENT1, STUDENT)
Union
•Suppose there are two relations R and S.
•The Union operation contains all the tuples that are either in R or S or both in R
& S.
•It eliminates the duplicate tuples. It is denoted by ∪.
•Notation: R ∪ S
•A Union operation must hold the following condition: R and S must have the
attribute of the same name.
Intersection
•Suppose there are two relations R and S.
•The Set Intersection operation contains all tuples that are in both R & S.
•It is denoted by Intersection ∩
•Notation: R ∩ S
Set Difference
•Suppose there are two relations R and S.
•The Set Difference operation contains all tuples that are in R but not in S.
•It is denoted by minus (-)
•Notation: R - S
Cartesian Product
•The Cartesian Product is used to combine each row in one table with each row in
the other table.
•It is also known as a Cross Product.
•It is denoted by X.
•Notation: E X D
Division
•Division operator can be applied if and only if Attributes of Table2 is proper
subset of Attributes of Table1.
•The relation returned by Division operator will have attributes = (All attributes of
Table1 – All Attributes of Table2)
•The relation returned by Division operator will return those tuples from Table1
which are associated to every Table2’s tuple.
•Notation: Table1÷Table2 or Table 1/Table2
Joins
•A Join operation combines related
tuples from different relations, if and
only if a given join condition is
satisfied.
•Join operation is essentially a
cartesian product followed by a
selection criterion.
•It is denoted by ⋈
Natural Join
•A Natural Join is the set of tuples of all combinations in R and S that are equal on
their common attribute names.
•Equality condition hold on all attributes which have same name in relations R
and S (relations on which join operation is applied).
•Natural Join will also return the similar attributes only once as their value will be
same in resulting relation.
•It is denoted by ⋈.
•Notation: R⋈S
Equi Join
•Equijoin is a special case of conditional join where only equality condition holds
between a pair of attributes.
•As values of two attributes will be equal in result of equijoin, only one attribute
will be appeared in result.
•Notation: R⋈ R. column_name1=S. column_name2 S
•Natural Join is a special case of equijoin
in which equality condition hold on all
attributes which have same name in
relations R and S.
Left Outer Join
•When applying join on two relations R and S, some tuples of R or S does not
appear in result set which does not satisfy the join conditions.
•But Left Outer Joins gives all tuples of R in the result set.
•The tuples of R which do not satisfy join condition will have values as NULL for
attributes of S
•It is denoted by ⟕
•Notation: R⟕S
Right Outer Join
•When applying join on two relations R and S, some tuples of R or S does not
appear in result set which does not satisfy the join conditions.
•But Right Outer Joins gives all tuples of S in the result set.
•The tuples of S which do not satisfy join condition will have values as NULL for
attributes of R.
•It is denoted by ⟖
•Notation: R⟖S
Full Outer Join
•When applying join on two relations R and S, some tuples of R or S does not
appear in result set which does not satisfy the join conditions.
•Full Outer Joins gives all tuples of S and all tuples of R in the result set.
•The tuples of S which do not satisfy join condition will have values as NULL for
attributes of R and vice versa.
•It is union of Left outer join & Right outer join.
i.e. (R ⟕ S) ∪ (R ⟖ S).
•It is denoted by ⟗
•Notation: R ⟗ S
Relational Calculus
•Relational Calculus in non-procedural
query language and has no description
about how the query will work or the
data will be fetched.
•It only focusses on what to do, and not
on how to do it.
•Relational Calculus exists in two forms:
1. Tuple Relational Calculus (TRC)
2. Domain Relational Calculus
(DRC)
Tuple Relational Calculus (TRC)
•In Tuple Relational Calculus, filtering is done on the tuples based on the given
condition.
•A query is expressed as {t | P(t)}
where t = resulting tuples, P(t) is known as Predicate.
Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
•P(t) may have various conditions logically combined with OR (∨), AND (∧),
NOT(¬).
•It also uses quantifiers: ∃ t ∈ r (Q(t)) = ”there exists”
∀ t ∈ r (Q(t)) = Q(t) “for all”
•For example if our table is Student, we would put it as Student (T)
•Then comes the condition part, to specify a condition applicable for a
particular attribute(column), we can use the . dot variable with the tuple
variable.
•E.g. in table Student, if we want to get data for students with age greater than
17, then, we can write it as
T. age > 17
•If we want to use Tuple Relational Calculus to fetch names of students, from
table Student with age greater than 17, then,
•T.name | Student(T) and T. age > 17
Domain Relational Calculus (DRC)
•In Domain Relational Calculus, filtering is done based on the domain of the
attributes and not based on the tuple values.
•A query is expressed as { a1, a2, a3, ..., an | P(a1, a2, a3, ... ,an)}
where, a1, a2... an represents domain of attributes (columns) and P defines the
formula including the condition for fetching the data.
•E.g. in table Student, if we want to get data for students with age greater than 17,
then, we can write it as
{< name, age > | ∈ Student ∧ age > 17}
•The above query will return the names and ages of the students in the
table Student who are older than 17.

You might also like